当前位置:   article > 正文

Springboot整合dubbo加zookeeper注册中心简单案例_springboot dubbo zk注册

springboot dubbo zk注册

        案例采用环境

  •         zookeeper 3.7.0
  •         dobbo-admin
  •         springboot:2.3.2.RELEASE

一、本地zookeeper注册中心

        采用本地的原因是通过docker pull下来的zookeeper个人不会使用,通过idea连接一直是没办法连接上的,通过docker logs查看日志信息,发现很多拒绝连接的情况,但是通过本地使用ZooInspector却能连接上,但是用idea本地连接没办法顺利连接,目前没有排错出来,后续如果有解决会放出解决方法,也看看有没有大神遇到同样的问题请教一下。

        步骤:

                1.首先启动本地zookeeper服务中心,文件夹路径的bin启动zkServer.cmd

二、linux下使用的dubbo-admin

  •  问题:一开始我是使用docker运行zookeeper与dubbo-admin,然后dubbo-admin一直连接不上zookeeper,通过日志查看发现dubbo-admin一直在连接本地127.0.0.1:2181,然而每一个容器其实就是一个独立的虚拟机,查看本机自己的2181端口是没办法发现zookeeper,我采取的方案是直接输入参数进行ip配置
  • 说明:admin.registry.address、admin.config-center、admin.metadata 参数通过github查看dubbo-admin-server下的application.yml里面可以看见这三个配置参数

        步骤:

                1.打开虚拟机进行docker操作(一定要先启动zookeeper后才使用dubbo-admin)

  1. #拉取镜像
  2. docker pull apache/dubbo-admin
  3. #运行镜像
  4. #{zookeeper_ip}:${zookeeper_port}输入自己服务所在的ip与端口 如:192.168.1.1:2181
  5. #映射到虚拟机的9000端口
  6. docker run -p 9000:8080 -d --name dubbo-admin \
  7. -e "admin.registry.address=zookeeper://{zookeeper_ip}:{zookeeper_port}" \
  8. -e "admin.config-center=zookeeper://{zookeeper_ip}:{zookeeper_port}" \
  9. -e "admin.metadata-report.address=zookeeper://{zookeeper_ip}:{zookeeper_port}" apache/dubbo-admin
  10. #查看运行状况
  11. docker ps

        结果:访问虚拟机ip:9000

 三、使用idea编写程序

     1、文件结构

说明
common放置domain、util等共同工具
dubbo-api需要暴露的服务接口
dubbo-donsumer消费者,测试远程服务接口
dubbo-provider服务提供者

         2、pom依赖说明

                父pom

  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. <packaging>pom</packaging>
  6. <modules>
  7. <module>service</module>
  8. <module>dubbo-api</module>
  9. <module>common</module>
  10. </modules>
  11. <groupId>com.fine</groupId>
  12. <artifactId>Spring-boot-dubbo-zookeeper-test02</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>Spring-boot-dubbo-zookeeper-test02</name>
  15. <description>springboot整合dubbo与zookeeper方案测试</description>
  16. <properties>
  17. <java.version>1.8</java.version>
  18. <spring-boot.version>2.3.2.RELEASE</spring-boot.version>
  19. <dubbo.version>2.7.8</dubbo.version>
  20. <dubbo-zookeeper.version>2.7.8</dubbo-zookeeper.version>
  21. </properties>
  22. <dependencyManagement>
  23. <dependencies>
  24. <!-- springboot的依赖管理 -->
  25. <dependency>
  26. <groupId>org.springframework.boot</groupId>
  27. <artifactId>spring-boot-dependencies</artifactId>
  28. <version>${spring-boot.version}</version>
  29. <type>pom</type>
  30. <scope>import</scope>
  31. </dependency>
  32. <!-- dubbo的依赖管理 -->
  33. <dependency>
  34. <groupId>org.apache.dubbo</groupId>
  35. <artifactId>dubbo-dependencies-bom</artifactId>
  36. <version>${dubbo.version}</version>
  37. <type>pom</type>
  38. <scope>import</scope>
  39. </dependency>
  40. <!-- dubbo的zookeeper的依赖管理 -->
  41. <dependency>
  42. <groupId>org.apache.dubbo</groupId>
  43. <artifactId>dubbo-dependencies-zookeeper</artifactId>
  44. <version>${dubbo-zookeeper.version}</version>
  45. <type>pom</type>
  46. </dependency>
  47. <!--dubbo依赖-->
  48. <dependency>
  49. <groupId>org.apache.dubbo</groupId>
  50. <artifactId>dubbo</artifactId>
  51. <version>${dubbo.version}</version>
  52. <exclusions>
  53. <exclusion>
  54. <groupId>org.springframework</groupId>
  55. <artifactId>spring</artifactId>
  56. </exclusion>
  57. <exclusion>
  58. <groupId>javax.servlet</groupId>
  59. <artifactId>servlet-api</artifactId>
  60. </exclusion>
  61. <exclusion>
  62. <groupId>log4j</groupId>
  63. <artifactId>log4j</artifactId>
  64. </exclusion>
  65. </exclusions>
  66. </dependency>
  67. </dependencies>
  68. </dependencyManagement>
  69. <build>
  70. <plugins>
  71. <plugin>
  72. <groupId>org.springframework.boot</groupId>
  73. <artifactId>spring-boot-maven-plugin</artifactId>
  74. </plugin>
  75. </plugins>
  76. </build>
  77. <!--如果报错使用此镜像(官方说明) -->
  78. <repositories>
  79. <repository>
  80. <id>apache.snapshots.https</id>
  81. <name>Apache Development Snapshot Repository</name>
  82. <url>https://repository.apache.org/content/repositories/snapshots</url>
  83. <releases>
  84. <enabled>false</enabled>
  85. </releases>
  86. <snapshots>
  87. <enabled>true</enabled>
  88. </snapshots>
  89. </repository>
  90. </repositories>
  91. </project>

         service模块的pom(对于服务进行统一管理)

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <parent>
  6. <artifactId>Spring-boot-dubbo-zookeeper-test02</artifactId>
  7. <groupId>com.fine</groupId>
  8. <version>0.0.1-SNAPSHOT</version>
  9. </parent>
  10. <modelVersion>4.0.0</modelVersion>
  11. <artifactId>service</artifactId>
  12. <packaging>pom</packaging>
  13. <modules>
  14. <module>dubbo-provider</module>
  15. <module>dubbo-consumer</module>
  16. </modules>
  17. <properties>
  18. <maven.compiler.source>8</maven.compiler.source>
  19. <maven.compiler.target>8</maven.compiler.target>
  20. </properties>
  21. <dependencies>
  22. <dependency>
  23. <groupId>org.springframework.boot</groupId>
  24. <artifactId>spring-boot-starter-test</artifactId>
  25. <scope>test</scope>
  26. </dependency>
  27. <!-- web项目-->
  28. <dependency>
  29. <groupId>org.springframework.boot</groupId>
  30. <artifactId>spring-boot-starter-web</artifactId>
  31. </dependency>
  32. <!-- dubbo-快速启动springboot -->
  33. <dependency>
  34. <groupId>org.apache.dubbo</groupId>
  35. <artifactId>dubbo-spring-boot-starter</artifactId>
  36. <version>2.7.8</version>
  37. </dependency>
  38. <!-- 配置zookeeper-->
  39. <dependency>
  40. <groupId>org.apache.dubbo</groupId>
  41. <artifactId>dubbo-dependencies-zookeeper</artifactId>
  42. <type>pom</type>
  43. </dependency>
  44. <dependency>
  45. <groupId>com.fine</groupId>
  46. <artifactId>common</artifactId>
  47. <version>0.0.1-SNAPSHOT</version>
  48. </dependency>
  49. <!-- 自己配置需要暴露的api接口-->
  50. <dependency>
  51. <groupId>com.fine</groupId>
  52. <artifactId>dubbo-api</artifactId>
  53. <version>0.0.1-SNAPSHOT</version>
  54. </dependency>
  55. </dependencies>
  56. </project>

        3、配置

        provider配置(application.yml)

  1. spring:
  2. application:
  3. name: dubbo-provider
  4. dubbo:
  5. #服务所在包
  6. scan:
  7. base-packages: com.fine.dubbo.api
  8. #dubbo协议
  9. protocol:
  10. name: dubbo
  11. port: -1
  12. #dubbo注册中心
  13. registry:
  14. # client: curator
  15. address: 127.0.0.1:2181
  16. protocol: zookeeper
  17. application:
  18. name: dubbo-provider
  19. server:
  20. port: 11000

        consumer配置

  1. spring:
  2. application:
  3. name: dubbo-consumer
  4. server:
  5. port: 12000
  6. dubbo:
  7. registry:
  8. protocol: zookeeper
  9. address: 127.0.0.1:2181

        4、代码实现

        api模块   

         provider模块

         comsuner模块

  • controller
  • service

         4、启动服务

        查看dubbo-admin的监控信息

        测试访问 

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

闽ICP备14008679号