springBoot+dubbo服务提供者的使用
相关连接:
基于idea的lombok安装和使用
zookeeper安装
dubbo后台安装-war版或者
dubbo后台安装-jar版
-
创建服务接口项目(maven或java项目即可)
-
pom.xml文件(这里使用了lombok插件,有兴趣的可以点击lombok阅读安装使用)
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.dubbo</groupId> <artifactId>service</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.8</version> <optional>true</optional> </dependency> </dependencies> </project>
-
创建数据模型
import lombok.Getter; import lombok.Setter; import lombok.ToString; import java.io.Serializable; @Getter @Setter @ToString public class ResultModel implements Serializable { private String name; private Integer age; }
-
创建接口类
import com.dubbo.model.ResultModel; public interface DubboTestService { public ResultModel getTestResult(); }
-
使用mvn clean install命令打包,创建完成,完整结构如图
-
-
创建服务实现项目(springBoot),基于注解方式
-
pom.xml文件
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.8.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.dubbo</groupId> <artifactId>service-impl</artifactId> <version>0.0.1-SNAPSHOT</version> <name>service-impl</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--自定义接口类--> <dependency> <groupId>com.dubbo</groupId> <artifactId>service</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <!--引入dubbo--> <dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>0.2.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
-
springBoot启动类增加@EnableDubbo注解
-
application.properties配置文件增加以下dubbo配置
#提供方应用信息,用于计算依赖关系
dubbo.application.name=dubboTest
#使用zookeeper注册中心
dubbo.registry.address=zookeeper://127.0.0.1:2181
#用dubbo协议在20880端口暴露服务
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
-
添加服务提供类,注意:
1、@Service注解是dubbo提供的不是spring提供的
2、DubboTestService接口是第一个项目编写的,由服务提供者实现
3、@Service注解增加interfaceClass参数是防止这个类使用了其他AOP使dubbo无法正常解析接口类
4、group参数用于区分环境
-
启动项目后可以在dubbo-admin后台看到
-
-
基于XML方式(使用注解方式的pom.xml文件)
-
创建XML配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 提供方应用信息,用于计算依赖关系 --> <dubbo:application name="dubboTest"/> <!-- 使用zookeeper注册中心 --> <dubbo:registry address="zookeeper://127.0.0.1:2181"/> <!-- 用dubbo协议在20880端口暴露服务 --> <dubbo:protocol name="dubbo" port="20880"/> <!--创建bean--> <bean id="DubboTestServiceImpl" class="com.dubbo.service.impl.DubboTestServiceImpl" /> <!-- 将该接口暴露到dubbo中 --> <dubbo:service interface="com.dubbo.service.DubboTestService" ref="DubboTestServiceImpl" group="test" version="1.0.0"/> </beans>
-
创建启动类
import org.springframework.context.support.ClassPathXmlApplicationContext; import java.io.IOException; public class ServiceImplApplication { public static void main(String[] args) throws IOException { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("serviceImpl.xml"); context.start(); System.out.println("----------------服务已启动,按任意键结束···········--------------------"); System.in.read(); } }
-