当前位置:   article > 正文

SpringBoot入门,Dubbo+Zookeeper简单入门案例_java中dubbo和zk使用的简单案例基于springboot

java中dubbo和zk使用的简单案例基于springboot

简介:

1、Dubbo:一款高性能,轻量级的开源Java RPC框架,它提供了三大核心能力:面向接口的远程方法调用,智能容错和负载均衡,以及服务自动注册和发现。架构如下图所示(摘自官网

 

节点及说明
节点说明
Consumer调用远程服务的服务消费方
Provider暴露服务的服务提供方
Container服务运行容器
Registry服务注册与发现的注册中心
Monitor统计服务的调用次数和调用时间的监控中心

 

调用关系说明
0服务容器负责启动,加载,运行服务提供者。
1服务提供者在启动时,向注册中心注册自己提供的服务。
2服务消费者在启动时,向注册中心订阅自己所需的服务。
3注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。
4服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。
5服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。

 

2.Zookeeper:Apacahe Hadoop 的子项目,是一个树型的目录服务,支持变更推送,适合作为 Dubbo 服务的注册中心,工业强度较高,可用于生产环境,Dubbo官方推荐使用。执行流程如下图所示(摘自网站

/user-guide/images/zookeeper.jpg

 

流程说明
  • 服务提供者启动时: 向 /dubbo/com.foo.BarService/providers 目录下写入自己的 URL 地址
  • 服务消费者启动时: 订阅 /dubbo/com.foo.BarService/providers 目录下的提供者 URL 地址。并向 /dubbo/com.foo.BarService/consumers 目录下写入自己的 URL 地址
  • 监控中心启动时: 订阅 /dubbo/com.foo.BarService 目录下的所有提供者和消费者 URL 地址。

 

Dubbo与Zookeeper就简单介绍到这,详细请参考官方文档,下面开始进入实战练习。

1.安装Zookeeper

  • 修改配置文件,进入conf文件夹下,将zoo_sample.cfg重命名为zoo.cfg,zookeeper启动会默认加载这个配置文件,之后修改配置文件内容,设置数据及日志存储路径,并修改启动端口,避免与其他冲突,完整配置文件如下,主要配置的是dataDir数据存储路径dataLogDir日志存储路径以及admin.serverPort服务启动端口(避免与其他端口冲突)
  1. # The number of milliseconds of each tick
  2. tickTime=2000
  3. # The number of ticks that the initial
  4. # synchronization phase can take
  5. initLimit=10
  6. # The number of ticks that can pass between
  7. # sending a request and getting an acknowledgement
  8. syncLimit=5
  9. # the directory where the snapshot is stored.
  10. # do not use /tmp for storage, /tmp here is just
  11. # example sakes.
  12. dataDir=D:\\Zookeeper-3.5.5\\data
  13. dataLogDir=D:\\Zookeeper-3.5.5\\logs
  14. # the port at which the clients will connect
  15. clientPort=2181
  16. admin.serverPort=8082
  17. # the maximum number of client connections.
  18. # increase this if you need to handle more clients
  19. #maxClientCnxns=60
  20. #
  21. # Be sure to read the maintenance section of the
  22. # administrator guide before turning on autopurge.
  23. #
  24. # http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
  25. #
  26. # The number of snapshots to retain in dataDir
  27. #autopurge.snapRetainCount=3
  28. # Purge task interval in hours
  29. # Set to "0" to disable auto purge feature
  30. #autopurge.purgeInterval=1
  • 启动zookeeper,修改完配置文件后,进入到bin文件夹下,由于我是在window下,所以双击zkServer.cmd启动,启动成功如下图所示

启动后不要关闭窗口,最小化就行。

2.项目准备

新建一个SpringBoot项目dubbo-demo,包含三个子模块,分别为dubbo-demo-apidemo-demo-consumerdubbo-demo-provider,目录结构如下:

  

其中dubbo-demo-consumer和dubbo-demo-provider都引入dubbo-demo-api,依赖关如下:

dubbo-demo-api公共api接口
dubbo-demo-consumer服务消费者
dubbo-demo-provier服务提供者

(1)dubbo-demo-api

  • pom文件内容如下:就是新建一个springboot模块,没有引入其他依赖的默认状态

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.2.1.RELEASE</version>
  9. <relativePath/>
  10. </parent>
  11. <groupId>dubbo.demo.api</groupId>
  12. <artifactId>dubbo-demo-api</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>dubbo-demo-api</name>
  15. <description>Demo project for Spring Boot</description>
  16. <properties>
  17. <java.version>1.8</java.version>
  18. <project.bulid.sourceEncoding>UTF-8</project.bulid.sourceEncoding>
  19. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  20. </properties>
  21. <dependencies>
  22. <dependency>
  23. <groupId>org.springframework.boot</groupId>
  24. <artifactId>spring-boot-starter</artifactId>
  25. </dependency>
  26. <dependency>
  27. <groupId>org.springframework.boot</groupId>
  28. <artifactId>spring-boot-starter-test</artifactId>
  29. <scope>test</scope>
  30. <exclusions>
  31. <exclusion>
  32. <groupId>org.junit.vintage</groupId>
  33. <artifactId>junit-vintage-engine</artifactId>
  34. </exclusion>
  35. </exclusions>
  36. </dependency>
  37. </dependencies>
  38. <build>
  39. <plugins>
  40. <plugin>
  41. <groupId>org.springframework.boot</groupId>
  42. <artifactId>spring-boot-maven-plugin</artifactId>
  43. </plugin>
  44. </plugins>
  45. </build>
  46. </project>
  • IDeviceService内容如下:简单定义了一个借口,包含一个sayHello()的方法

  1. package dubbo.demo.api;
  2. import java.io.Serializable;
  3. public interface IDemoService {
  4. Serializable sayHello();
  5. }

(2)dubbo-demo-consumer

  • pom文件内容如下:引入dubbo-demo-api,dubbo,zookeeper依赖,顺便复习了下整合swagger,参考我的上一篇博客

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.2.1.RELEASE</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>dubbo.demo.consumer</groupId>
  12. <artifactId>dubbo-demo-consumer</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>dubbo-demo-consumer</name>
  15. <description>Demo project for Spring Boot</description>
  16. <properties>
  17. <java.version>1.8</java.version>
  18. <project.bulid.sourceEncoding>UTF-8</project.bulid.sourceEncoding>
  19. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  20. </properties>
  21. <dependencies>
  22. <dependency>
  23. <groupId>org.springframework.boot</groupId>
  24. <artifactId>spring-boot-starter-web</artifactId>
  25. </dependency>
  26. <!-- 引入dubbo-demo-api依赖 -->
  27. <dependency>
  28. <groupId>dubbo.demo.api</groupId>
  29. <artifactId>dubbo-demo-api</artifactId>
  30. <version>0.0.1-SNAPSHOT</version>
  31. </dependency>
  32. <!-- 引入swagger2依赖 -->
  33. <dependency>
  34. <groupId>io.springfox</groupId>
  35. <artifactId>springfox-swagger2</artifactId>
  36. <version>2.9.2</version>
  37. </dependency>
  38. <dependency>
  39. <groupId>io.springfox</groupId>
  40. <artifactId>springfox-swagger-ui</artifactId>
  41. <version>2.9.2</version>
  42. </dependency>
  43. <!-- 引入dubbo zookeeper依赖-->
  44. <dependency>
  45. <groupId>com.alibaba.boot</groupId>
  46. <artifactId>dubbo-spring-boot-starter</artifactId>
  47. <version>0.2.0</version>
  48. </dependency>
  49. <dependency>
  50. <groupId>org.apache.zookeeper</groupId>
  51. <artifactId>zookeeper</artifactId>
  52. <version>3.5.5</version>
  53. </dependency>
  54. <dependency>
  55. <groupId>org.springframework.boot</groupId>
  56. <artifactId>spring-boot-starter-test</artifactId>
  57. <scope>test</scope>
  58. <exclusions>
  59. <exclusion>
  60. <groupId>org.junit.vintage</groupId>
  61. <artifactId>junit-vintage-engine</artifactId>
  62. </exclusion>
  63. </exclusions>
  64. </dependency>
  65. </dependencies>
  66. <build>
  67. <plugins>
  68. <plugin>
  69. <groupId>org.springframework.boot</groupId>
  70. <artifactId>spring-boot-maven-plugin</artifactId>
  71. </plugin>
  72. </plugins>
  73. </build>
  74. </project>
  • Swagger2Config内容如下:

  1. package dubbo.demo.consumer.config;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;
  4. import springfox.documentation.builders.ApiInfoBuilder;
  5. import springfox.documentation.builders.PathSelectors;
  6. import springfox.documentation.builders.RequestHandlerSelectors;
  7. import springfox.documentation.service.ApiInfo;
  8. import springfox.documentation.service.Contact;
  9. import springfox.documentation.spi.DocumentationType;
  10. import springfox.documentation.spring.web.plugins.Docket;
  11. import springfox.documentation.swagger2.annotations.EnableSwagger2;
  12. @EnableSwagger2
  13. @Configuration
  14. public class Swagger2Config {
  15. @Bean
  16. public Docket swaggerPluginConfig(){
  17. return new Docket(DocumentationType.SWAGGER_2)
  18. .apiInfo(apiInfo()) //Api文档描述
  19. .select() //选择哪些路径和Api会生成文档
  20. .apis(RequestHandlerSelectors.basePackage("dubbo.demo.consumer.controller")) //对指定路径下Api进行监控
  21. .paths(PathSelectors.any()) //对所有路径进行监控
  22. .build();
  23. }
  24. private ApiInfo apiInfo(){
  25. return new ApiInfoBuilder()
  26. .title("Dubbo Demo接口文档")
  27. .description("API 接口文档")
  28. .termsOfServiceUrl("http://localhost")
  29. .version("1.0.0")
  30. .contact(new Contact("takano","","xxxxxx@qq.com"))
  31. .build();
  32. }
  33. }
  • DemoController内容如下:简单定义了一个请求方法sayHello(),通过@Reference来标记IDemoSerice接口的成员变量 demoService 是一个 dubbo 服务的引用,在sayHello()方法内的通过该接口向远端的服务提供者发起调用,客户端并没有实现IDemoService接口。其中参数version指定了服务的版本号,要与提供者提供的服务版本号一致。除此之外,@Reference还有许多其他的参数,请参考文档

  1. package dubbo.demo.consumer.controller;
  2. import com.alibaba.dubbo.config.annotation.Reference;
  3. import dubbo.demo.api.IDemoService;
  4. import io.swagger.annotations.Api;
  5. import io.swagger.annotations.ApiOperation;
  6. import org.springframework.web.bind.annotation.GetMapping;
  7. import org.springframework.web.bind.annotation.RestController;
  8. import java.io.Serializable;
  9. @Api(value = "DemoController",tags = "dubbo-demo控制器")
  10. @RestController
  11. public class DemoController {
  12. @Reference(version = "1.0.0")
  13. private IDemoService demoService;
  14. @ApiOperation(value = "测试方法",tags = "dubbo-demo控制器")
  15. @GetMapping("/sayHello")
  16. public Serializable sayHello(){
  17. return demoService.sayHello();
  18. }
  19. }
  • 启动文件内容如下:使用@EnableDubbo注解启用dubbo,它是@EnableDubboConfig@DubboComponentScan的组合形式,用来扫描服务消费者,即用@Reference注解标注,对其进行组装初始化操作,最终完成服务引用工作。

  1. package dubbo.demo.consumer;
  2. import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. @EnableDubbo
  6. @SpringBootApplication
  7. public class DubboDemoConsumerApplication {
  8. public static void main(String[] args) {
  9. SpringApplication.run(DubboDemoConsumerApplication.class, args);
  10. }
  11. }
  • 配置文件内容如下:

  1. #服务启动端口
  2. server.port=9001
  3. #dubbo
  4. #当前服务名
  5. dubbo.application.name=dubbo-demo-consumer
  6. #注册中心的协议和地址
  7. dubbo.registry.protocol=zookeeper
  8. dubbo.registry.address=localhost:2181
  9. #连接监控中心
  10. dubbo.monitor.protocol=registry

(3)dubbo-demo-provider

  • pom文件内容如下:引入dubbo-demo-api,dubbo,zookeeper依赖。

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.2.1.RELEASE</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>dubbo.demo.provider</groupId>
  12. <artifactId>dubbo-demo-provider</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>dubbo-demo-provider</name>
  15. <description>Demo project for Spring Boot</description>
  16. <properties>
  17. <java.version>1.8</java.version>
  18. <project.bulid.sourceEncoding>UTF-8</project.bulid.sourceEncoding>
  19. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  20. </properties>
  21. <dependencies>
  22. <dependency>
  23. <groupId>org.springframework.boot</groupId>
  24. <artifactId>spring-boot-starter-web</artifactId>
  25. </dependency>
  26. <!-- 引入dubbo-demo-api依赖 -->
  27. <dependency>
  28. <groupId>dubbo.demo.api</groupId>
  29. <artifactId>dubbo-demo-api</artifactId>
  30. <version>0.0.1-SNAPSHOT</version>
  31. </dependency>
  32. <!-- 引入dubbo zookeeper依赖 -->
  33. <dependency>
  34. <groupId>com.alibaba.boot</groupId>
  35. <artifactId>dubbo-spring-boot-starter</artifactId>
  36. <version>0.2.0</version>
  37. </dependency>
  38. <dependency>
  39. <groupId>org.apache.zookeeper</groupId>
  40. <artifactId>zookeeper</artifactId>
  41. <version>3.5.5</version>
  42. </dependency>
  43. <dependency>
  44. <groupId>org.springframework.boot</groupId>
  45. <artifactId>spring-boot-starter-test</artifactId>
  46. <scope>test</scope>
  47. <exclusions>
  48. <exclusion>
  49. <groupId>org.junit.vintage</groupId>
  50. <artifactId>junit-vintage-engine</artifactId>
  51. </exclusion>
  52. </exclusions>
  53. </dependency>
  54. </dependencies>
  55. <build>
  56. <plugins>
  57. <plugin>
  58. <groupId>org.springframework.boot</groupId>
  59. <artifactId>spring-boot-maven-plugin</artifactId>
  60. </plugin>
  61. </plugins>
  62. </build>
  63. </project>
  • DemoService内容如下:实现了demo-service-api的IDemoService接口,并实现了具体方法,使用“dubbo的@Service”注解配置服务提供者,暴露该服务。version指定了服务的版本号,interfaceClass指定了服务提供者实现的接口类,其他相关参数请查看文档。@Component声明该类是一个组件。

  1. package dubbo.demo.provider.service;
  2. import com.alibaba.dubbo.config.annotation.Service;
  3. import dubbo.demo.api.IDemoService;
  4. import org.springframework.stereotype.Component;
  5. import java.io.Serializable;
  6. @Service(version = "1.0.0",interfaceClass = IDemoService.class)
  7. @Component
  8. public class DemoService implements IDemoService {
  9. @Override
  10. public Serializable sayHello() {
  11. return "Hello World";
  12. }
  13. }
  • 启动文件内容如下:同样使用@EnableDubbo注解启动dubbo。

  1. package dubbo.demo.provider;
  2. import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. @EnableDubbo
  6. @SpringBootApplication
  7. public class DubboDemoProviderApplication {
  8. public static void main(String[] args) {
  9. SpringApplication.run(DubboDemoProviderApplication.class, args);
  10. }
  11. }
  • 配置文件内容如下:

  1. #服务启动端口
  2. server.port=9002
  3. #dubbo
  4. #当前服务名
  5. dubbo.application.name=dubbo-demo-provider
  6. #注册中心的协议和地址
  7. dubbo.registry.protocol=zookeeper
  8. dubbo.registry.address=localhost:2181
  9. #通信规则
  10. dubbo.protocol.name=dubbo
  11. dubbo.protocol.port=8365
  12. #连接监控中心
  13. dubbo.monitor.protocol=registry

3.测试

  • 先启动服务提者dubbo-demo-provider,启动成功后,可以在zookeeper窗口下看到如下信息,说明在zookeeper中注册成功。

  • 启动服务消费者dubbo-demo-consumer:同样,可以再zookeeper窗口下看到一条输出信息,说明在zookeeper中订阅成功。

可以看到我们成功的远程调用了服务提供者,并返回结果。至此一个简单的dubbo+zookeeper入门案例就完成了。

如有错误请批评指正,我会及时修改,欢迎大佬指点,让我更快进步。

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/人工智能uu/article/detail/851684
推荐阅读
相关标签
  

闽ICP备14008679号