Dubbo+Zookeeper+SpringBoot整合
Dubbo+Zookeeper+SpringBoot整合
Dubbo作为一款非常高性能轻便的RPC框架,往往搭配着Zookeeper作为服务注册发现中心来使用
SpringBoot在Java开发中也越来越主流,开发非常便利
如何整合Dubbo+Zookeeper+SpringBoot进行分布式开发呢?
整合步骤
1. 创建项目
新建一个maven工程dubbo_demo,然后在工程下, 创建三个模块,provider,consumer,common(通用接口jar)

在父pom中导入需要的包
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
| <?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.ycj</groupId> <artifactId>DubboDemo</artifactId> <packaging>pom</packaging> <version>1.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.4.RELEASE</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <modules> <module>provider</module> <module>common</module> <module>consumer</module> </modules>
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.47</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>0.1.0</version> </dependency> <dependency> <groupId>io.netty</groupId> <artifactId>netty-all</artifactId> <version>4.1.42.Final</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.7</version> </dependency> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.10</version> </dependency> </dependencies>
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
|
common的pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| <?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"> <parent> <groupId>com.ycj</groupId> <artifactId>DubboDemo</artifactId> <version>1.1-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <groupId>com.ycj.dubboDemo</groupId> <artifactId>common</artifactId> <packaging>jar</packaging> </project>
|
provider和consumer只需导入common即可
1 2 3 4 5
| <dependency> <groupId>com.ycj.dubboDemo</groupId> <artifactId>common</artifactId> <version>1.1-SNAPSHOT</version> </dependency>
|
2. 编写接口

1 2 3
| public interface DemoService { public String getDemoString(); }
|
3. 编写提供者

1 2 3 4 5 6 7 8 9 10
|
@Service public class ProviderService implements DemoService {
@Override public String getDemoString() { return "ProviderService"; } }
|
1 2 3 4 5 6 7 8 9 10 11
| @SpringBootApplication @ImportResource(locations = {"classpath:provider.xml"}) @EnableDubbo public class ProviderApplication {
public static void main(String[] args) { SpringApplication.run(ProviderApplication.class, args); }
}
|
dubbo提供者配置,注意资源地址http://code.alibabatech.com/
,Apache的我试了一下不可用(当前pom配置情况下)
1 2 3 4 5 6 7 8 9 10 11 12 13
| <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.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <dubbo:application name="demo-provider"/> <dubbo:registry address="zookeeper://127.0.0.1:2181"/> <dubbo:protocol name="dubbo" port="20890"/> <bean id="demoService" class="com.ycj.dubboDemo.provider.service.ProviderService"/> <dubbo:service interface="com.ycj.dubboDemo.common.service.DemoService" ref="demoService"/> </beans>
|
4. 编写消费者

1 2 3 4 5 6 7 8 9 10 11 12
| @Controller public class consumer { @Autowired public DemoService demoService;
@RequestMapping("/test") @ResponseBody public String test(){ return demoService.getDemoString(); } }
|
1 2 3 4 5 6 7 8 9
| @SpringBootApplication @ImportResource(locations = {"classpath:consumer.xml"}) @EnableDubbo public class ConsumerApplication { public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class, args); } }
|
dubbo消费者配置
1 2 3 4 5 6 7 8 9 10 11
| <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.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <dubbo:application name="demo-consumer"/> <dubbo:registry address="zookeeper://127.0.0.1:2181"/> <dubbo:reference id="demoService" check="false" interface="com.ycj.dubboDemo.common.service.DemoService"/> </beans>
|
5. 启动Zookeeper

6. 启动提供者

7. 启动消费者

8. 访问成功

Anything can go right will go right