0


一个基于Zookeeper+Dubbo3+SpringBoot3的完整微服务调用程序示例代码

一、关于 Dubbo3 的一些优化改进介绍

  1. Dubbo3 的官方文档地址: https://cn.dubbo.apache.org/zh-cn/overview/what/overview/ 其针对一些问题进行了优化和改变。个人整理3个小的方面:
  2. 1. 在服务注册方面使用 @DubboService 注解,不再使用 @Service 这个关键词,以区别于 Spring @Service 注解。即 @Service 注解从 3.0 版本开始就已经废弃了。
  3. 2. 在服务发现方面,使用 @DubboReference 注解,而 @Reference 注解从 3.0 版开始废弃,以区别于 Spring @Reference 注解。
  4. 3. 在包引入方面,针对之前需要引入多个包,如 Dubbo-spring-boot-starterZkclientCurate-frameworkCurate-recipeszookeeper 以及其 zookeeper log4j之间的日志冲突问题等,甚是麻烦。又远离了 springboot 的简单配置的初衷,于是 Dubbo3 里只需要引入两个包 dubbo-spring-boot-starter dubbo-zookeeper-spring-boot-starter

二、搭建基本环境 zookeeper 以及可视化客户端

  1. zookeeper 的下载就不多介绍了,去官网下载后在本地编辑 zoo.cfg 文件 windows 上的话运行 zkServer.cmd 文件即可启动。
  2. 关于 zookeeper 以及可视化客户端,可以使用 dubbo-admin,不过这个并不是核心功能的东西,我还是选择使用一些现成的工具吧,如 ZooInspector prettyZoo。在这里提一下 ZooInspector的界面比较丑,不如 prettyZoo 好用。不过两者都有不足之处,就是显示出来的JSON内容为什么就不能加个格式化的按钮美观地展示出来!prettyZoo 界面截图如下:

三、 Dubbo3 + SpringBoot3的微服务调用程序代码示例

A. 服务提供方的代码

1. 服务提供方的程序结构截图

2. application.yml 配置文件内容

  1. spring:
  2. application:
  3. name: hisroty-provider
  4. server:
  5. port: 8082
  6. dubbo:
  7. application:
  8. name: history-server
  9. registry:
  10. address: zookeeper://127.0.0.1:2181
  11. scan:
  12. base-packages: cn.history.service

3. 文件 HistoryService.java 代码

  1. package cn.fangha.service;
  2. public interface HistoryService {
  3. String getHistory();
  4. }

4. 文件 cn/fangha/service/HistoryServiceImpl.java 代码

  1. package cn.fangha.service;
  2. import org.apache.dubbo.config.annotation.DubboService;
  3. import org.springframework.stereotype.Component;
  4. @Component
  5. @DubboService
  6. public class HistoryServiceImpl implements HistoryService {
  7. @Override
  8. public String getHistory() {
  9. return "微服务返回的历史数据内容.";
  10. }
  11. }

5. 项目启动程序需要加上注解

Dubbo3 的话需要加上 @EnableDubbo 注解,这个在2.X版本的时候是不需要的。

  1. package cn.fangha;
  2. import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. @SpringBootApplication
  6. @EnableDubbo
  7. public class HisrotyProviderApplication {
  8. public static void main(String[] args) {
  9. SpringApplication.run(HisrotyProviderApplication.class, args);
  10. }
  11. }

6. 依赖的包配置 pom.xml 文件

  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>3.3.5</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>cn.fangha</groupId>
  12. <artifactId>hisroty-provider</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>hisroty-provider</name>
  15. <description>hisroty-provider</description>
  16. <url/>
  17. <licenses>
  18. <license/>
  19. </licenses>
  20. <developers>
  21. <developer/>
  22. </developers>
  23. <scm>
  24. <connection/>
  25. <developerConnection/>
  26. <tag/>
  27. <url/>
  28. </scm>
  29. <properties>
  30. <java.version>17</java.version>
  31. </properties>
  32. <dependencies>
  33. <dependency>
  34. <groupId>org.springframework.boot</groupId>
  35. <artifactId>spring-boot-starter-web</artifactId>
  36. </dependency>
  37. <dependency>
  38. <groupId>org.springframework.boot</groupId>
  39. <artifactId>spring-boot-starter-test</artifactId>
  40. <scope>test</scope>
  41. </dependency>
  42. <!--dubbo相关的依赖-->
  43. <dependency>
  44. <groupId>org.apache.dubbo</groupId>
  45. <artifactId>dubbo-spring-boot-starter</artifactId>
  46. <version>3.3.0</version>
  47. </dependency>
  48. <dependency>
  49. <groupId>org.apache.dubbo</groupId>
  50. <artifactId>dubbo-zookeeper-spring-boot-starter</artifactId>
  51. <version>3.2.14</version>
  52. </dependency>
  53. </dependencies>
  54. <build>
  55. <plugins>
  56. <plugin>
  57. <groupId>org.springframework.boot</groupId>
  58. <artifactId>spring-boot-maven-plugin</artifactId>
  59. </plugin>
  60. </plugins>
  61. </build>
  62. </project>

B. 服务消费方的代码

1. 服务消费方的程序结构截图

2. application.yml 配置文件内容

  1. spring:
  2. application:
  3. name: history-serv
  4. datasource:
  5. driver-class-name: com.mysql.cj.jdbc.Driver
  6. url: jdbc:mysql://localhost:3306/myblog?useUnicode=True&characterEncoding=utf-8&userSSL=true=utf-8
  7. username: root
  8. password: 123456
  9. server:
  10. port: 8081
  11. dubbo:
  12. application:
  13. name: test-service
  14. registry:
  15. address: zookeeper://127.0.0.1:2181

3. 接口 cn/fangha/service/HistoryService.java 程序

  1. 其和服务提供方是一样的。
  1. package cn.fangha.service;
  2. public interface HistoryService {
  3. String getHistory();
  4. }

4. 控制器的程序代码:

  1. 控制器中,直接定义一个服务类,类的类型必须是在本项目中定义(保持和微服务方一样),然后在使用的时候直接用 @DubboReference 注解自动将其注入为 Dubbo 服务代理实例,如此即可发起远程服务调用。
  1. package cn.fangha.controller;
  2. import cn.fangha.service.HistoryService;
  3. import org.apache.dubbo.config.annotation.DubboReference;
  4. import org.springframework.web.bind.annotation.GetMapping;
  5. import org.springframework.web.bind.annotation.ResponseBody;
  6. import org.springframework.web.bind.annotation.RestController;
  7. @RestController
  8. public class TestController {
  9. @DubboReference
  10. private HistoryService historyService;
  11. @GetMapping("/get_service")
  12. @ResponseBody
  13. public String test() {
  14. String result = historyService.getHistory();
  15. String outResult = "Receive from remote service ======> " + result;
  16. return outResult;
  17. }
  18. }

5. 项目启动程序,同服务方一样需要加上注解

  1. package cn.fangha;
  2. import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. @SpringBootApplication
  6. @EnableDubbo
  7. public class HistoryServApplication {
  8. public static void main(String[] args) {
  9. SpringApplication.run(HistoryServApplication.class, args);
  10. }
  11. }

6. 消费方的 pom.xml 配置文件

  1. 服务方大同小异,只是我这里引入了其它的 mysql 之类的包,未作删除。
  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>3.3.5</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>cn.fangha</groupId>
  12. <artifactId>history-serv</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>history-serv</name>
  15. <description>history-serv</description>
  16. <url/>
  17. <licenses>
  18. <license/>
  19. </licenses>
  20. <developers>
  21. <developer/>
  22. </developers>
  23. <scm>
  24. <connection/>
  25. <developerConnection/>
  26. <tag/>
  27. <url/>
  28. </scm>
  29. <properties>
  30. <java.version>17</java.version>
  31. </properties>
  32. <dependencies>
  33. <dependency>
  34. <groupId>org.springframework.boot</groupId>
  35. <artifactId>spring-boot-starter-data-redis</artifactId>
  36. </dependency>
  37. <dependency>
  38. <groupId>org.springframework.boot</groupId>
  39. <artifactId>spring-boot-starter-jdbc</artifactId>
  40. </dependency>
  41. <dependency>
  42. <groupId>org.springframework.boot</groupId>
  43. <artifactId>spring-boot-starter-thymeleaf</artifactId>
  44. </dependency>
  45. <dependency>
  46. <groupId>org.springframework.boot</groupId>
  47. <artifactId>spring-boot-starter-web</artifactId>
  48. </dependency>
  49. <dependency>
  50. <groupId>org.mybatis.spring.boot</groupId>
  51. <artifactId>mybatis-spring-boot-starter</artifactId>
  52. <version>3.0.3</version>
  53. </dependency>
  54. <dependency>
  55. <groupId>com.mysql</groupId>
  56. <artifactId>mysql-connector-j</artifactId>
  57. <scope>runtime</scope>
  58. </dependency>
  59. <dependency>
  60. <groupId>org.projectlombok</groupId>
  61. <artifactId>lombok</artifactId>
  62. <optional>true</optional>
  63. </dependency>
  64. <dependency>
  65. <groupId>org.springframework.boot</groupId>
  66. <artifactId>spring-boot-starter-test</artifactId>
  67. <scope>test</scope>
  68. </dependency>
  69. <dependency>
  70. <groupId>org.mybatis.spring.boot</groupId>
  71. <artifactId>mybatis-spring-boot-starter-test</artifactId>
  72. <version>3.0.3</version>
  73. <scope>test</scope>
  74. </dependency>
  75. <!--dubbo相关的依赖-->
  76. <dependency>
  77. <groupId>org.apache.dubbo</groupId>
  78. <artifactId>dubbo-spring-boot-starter</artifactId>
  79. <version>3.3.0</version>
  80. </dependency>
  81. <dependency>
  82. <groupId>org.apache.dubbo</groupId>
  83. <artifactId>dubbo-zookeeper-spring-boot-starter</artifactId>
  84. <version>3.2.14</version>
  85. </dependency>
  86. </dependencies>
  87. <build>
  88. <plugins>
  89. <plugin>
  90. <groupId>org.springframework.boot</groupId>
  91. <artifactId>spring-boot-maven-plugin</artifactId>
  92. <configuration>
  93. <excludes>
  94. <exclude>
  95. <groupId>org.projectlombok</groupId>
  96. <artifactId>lombok</artifactId>
  97. </exclude>
  98. </excludes>
  99. </configuration>
  100. </plugin>
  101. </plugins>
  102. </build>
  103. </project>

C. 微服务调用执行

  1. 运行起 zookeeper 之后,将服务方运行起来,通过 zookeeper 的可视化工具,可以方便地检查服务是否正常运行并注册到 zk 中。

  2. 运行起 消费方,当然我这里都是 springboot ,直接在页面API上进行了调用显示,也可以直接在单元测试程序中调用检验。

  3. 访问消费方的 localhost:8081/get_service ,可以看到其能正常调用到另一个项目服务的内容。

四、其它小记

  1. 在使用最新版之前,我也使用了早前的的版本进行测试,即会引入如 Dubbo-spring-boot-starterZkclientCurate-frameworkCurate-recipeszookeeper 这些依赖,碰到不少报错也解决不少问题,也成功进行了服务注册发现,但确实是非常麻烦。
  2. 比如在老版本里启动生产端,遇到报错:java.lang.NoSuchMethodError: 'org.apache.curator.framework.api.ProtectACLCreateModePathAndBytesable org.apache.curator.framework.api.CreateBuilder.creatingParentContainersIfNeeded()'

Caused by: java.lang.NoSuchMethodError: 'org.apache.curator.framework.api.ProtectACLCreateModePathAndBytesable org.apache.curator.framework.api.CreateBuilder.creatingParentContainersIfNeeded()'
at org.apache.curator.x.discovery.details.ServiceDiscoveryImpl.internalRegisterService(ServiceDiscoveryImpl.java:222)

  1. 还有报错: NoClassDefFoundError: org/apache/curator/RetryPolicy 缺少包:

2024-11-02T11:50:56.212+08:00 WARN 19264 --- [history-serv] [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: java.lang.NoClassDefFoundError: org/apache/curator/RetryPolicy
2024-11-02T11:50:56.219+08:00 INFO 19264 --- [history-serv] [ main] .s.b.a.l.ConditionEvaluationReportLogger :

  1. 问题就是需要导入版本正确的包:curator-framework ,我这里开始用的 Curator Framework » 4.0.1 curator-framework 是一个 High-level API that greatly simplifies using ZooKeeper. 基于zk的高性能的API调用工具)后来改成了2.X的版本,这些问题就没报了。
  1. <dependency>
  2. <groupId>org.apache.curator</groupId>
  3. <artifactId>curator-framework</artifactId>
  4. <version>4.0.1</version>
  5. </dependency>
  1. 还有报错如:[bboShutdownHook] o.apache.dubbo.config.DubboShutdownHook : [DUBBO] Run shutdown hook now., dubbo version: 3.3.0, current host: 192.168.2.28

Exception in thread "main" java.lang.IllegalStateException: java.lang.NoSuchMethodError: 'org.apache.curator.framework.CuratorFrameworkFactory$Builder org.apache.curator.framework.CuratorFrameworkFactory$Builder.ensembleTracker(boolean)'

INFO 19368 --- [history-serv] [bboShutdownHook] o.apache.dubbo.config.DubboShutdownHook : [DUBBO] Run shutdown hook now., dubbo version: 3.3.0, current host: 192.168.2.28
Exception in thread "main" java.lang.IllegalStateException: java.lang.NoSuchMethodError: 'org.apache.curator.framework.CuratorFrameworkFactory$Builder org.apache.curator.framework.CuratorFrameworkFactory$Builder.ensembleTracker(boolean)'

  1. Exception in thread "main" java.lang.IllegalStateException: java.lang.NoSuchMethodError: 'org.apache.curator.framework.CuratorFrameworkFactory$Builder org.apache.curator.framework.CuratorFrameworkFactory$Builder.ensembleTracker(boolean)'
  2. 真的是非常麻烦。建议碰到这些问题后,不要使用新的版本,SpringBoot的初衷就是不希望大家浪费大量的时间精力在这些各个jar包的冲突问题上,Dubbo 随着它慢慢发展完善,也一定会在版本问题上有更好的解决办法,如 Dubbo3 的出现就是如此。
标签: 微服务 rpc dubbo

本文转载自: https://blog.csdn.net/weixin_47792780/article/details/143448642
版权归原作者 林戈的IT生涯 所有, 如有侵权,请联系我们删除。

“一个基于Zookeeper+Dubbo3+SpringBoot3的完整微服务调用程序示例代码”的评论:

还没有评论