当前位置:   article > 正文

【Spring Cloud Alibaba】1.新版SpringCloudAlibaba整合Dubbo,配置多注册中心——Nacos+Zookeeper_基于dubbo zookeeper nacos

基于dubbo zookeeper nacos

1.背景介绍

新系统微服务体系使用 SpringCloudAibaba,版本为目前最新的2021.1;由于公司旧项目使用的技术栈为基于Dubbo+Zookeeper,新项目使用Nacos,且Dubbo支持同时注册多个注册中心,故而采用双注册的技术选型。

网上查询相关资料比较少,SpringCloudAibaba会出现各种依赖导致的问题,使用经反复测试及冲突解决、目前采用版本如下面模块所示,在这基础上进行调整可支持多个版本的Spring Cloud。

2.父模块pom.xml

  1. <properties>
  2. <jdk.version>1.8</jdk.version>
  3. <java.version>1.8</java.version>
  4. <maven.compiler.source>8</maven.compiler.source>
  5. <maven.compiler.target>8</maven.compiler.target>
  6. <spring.boot.version>2.5.5</spring.boot.version>
  7. <spring.cloud.version>2020.0.4</spring.cloud.version>
  8. <spring.cloud.bootstrap.version>3.0.4</spring.cloud.bootstrap.version>
  9. <alibaba.cloud.version>2021.1</alibaba.cloud.version>
  10. <stream.cloud.version>3.1.4</stream.cloud.version>
  11. <dubbo.version>2.6.6</dubbo.version>
  12. <dubbo.registry.nacos.version>2.6.7</dubbo.registry.nacos.version>
  13. <nacos.client.version>1.1.3</nacos.client.version>
  14. <zookeeper.version>3.4.13</zookeeper.version>
  15. <netty.all.version>4.1.68.Final</netty.all.version>
  16. <curator.framework.version>4.0.1</curator.framework.version>
  17. <curator.recipes.version>2.8.0</curator.recipes.version>
  18. <zkclient.version>0.10</zkclient.version>
  19. </properties>
  20. <!-- 依赖版本号管理 -->
  21. <dependencyManagement>
  22. <dependencies>
  23. <!--https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-dependencies/2.3.3.RELEASE-->
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-dependencies</artifactId>
  27. <version>${spring.boot.version}</version>
  28. <type>pom</type>
  29. <scope>import</scope>
  30. </dependency>
  31. <!--https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-dependencies/Hoxton.SR8-->
  32. <dependency>
  33. <groupId>org.springframework.cloud</groupId>
  34. <artifactId>spring-cloud-dependencies</artifactId>
  35. <version>${spring.cloud.version}</version>
  36. <type>pom</type>
  37. <scope>import</scope>
  38. </dependency>
  39. <!--https://mvnrepository.com/artifact/com.alibaba.cloud/spring-cloud-alibaba-dependencies/2021.1-->
  40. <dependency>
  41. <groupId>com.alibaba.cloud</groupId>
  42. <artifactId>spring-cloud-alibaba-dependencies</artifactId>
  43. <version>${alibaba.cloud.version}</version>
  44. <type>pom</type>
  45. <scope>import</scope>
  46. </dependency>
  47. <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-stream-dependencies -->
  48. <dependency>
  49. <groupId>org.springframework.cloud</groupId>
  50. <artifactId>spring-cloud-stream-dependencies</artifactId>
  51. <version>${stream.cloud.version}</version>
  52. <type>pom</type>
  53. </dependency>
  54. <!-- https://mvnrepository.com/artifact/com.alibaba.cloud/spring-cloud-starter-alibaba-nacos-config -->
  55. <dependency>
  56. <groupId>com.alibaba.cloud</groupId>
  57. <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
  58. <version>${alibaba.cloud.version}</version>
  59. </dependency>
  60. <!-- https://mvnrepository.com/artifact/com.alibaba.cloud/spring-cloud-starter-alibaba-nacos-discovery -->
  61. <dependency>
  62. <groupId>com.alibaba.cloud</groupId>
  63. <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
  64. <version>${alibaba.cloud.version}</version>
  65. </dependency>
  66. <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-bootstrap -->
  67. <dependency>
  68. <groupId>org.springframework.cloud</groupId>
  69. <artifactId>spring-cloud-starter-bootstrap</artifactId>
  70. <version>${spring.cloud.bootstrap.version}</version>
  71. </dependency>
  72. <!--[开始]dubbo相关-->
  73. <dependency>
  74. <groupId>com.alibaba</groupId>
  75. <artifactId>dubbo</artifactId>
  76. <version>${dubbo.version}</version>
  77. <exclusions>
  78. <exclusion>
  79. <groupId>org.jboss.netty</groupId>
  80. <artifactId>netty</artifactId>
  81. </exclusion>
  82. <exclusion>
  83. <artifactId>spring-context</artifactId>
  84. <groupId>org.springframework</groupId>
  85. </exclusion>
  86. </exclusions>
  87. </dependency>
  88. <dependency>
  89. <groupId>com.alibaba</groupId>
  90. <artifactId>dubbo-registry-nacos</artifactId>
  91. <version>${dubbo.registry.nacos.version}</version>
  92. </dependency>
  93. <dependency>
  94. <groupId>com.alibaba.nacos</groupId>
  95. <artifactId>nacos-client</artifactId>
  96. <version>${nacos.client.version}</version>
  97. </dependency>
  98. <dependency>
  99. <groupId>io.netty</groupId>
  100. <artifactId>netty-all</artifactId>
  101. <version>${netty.all.version}</version>
  102. </dependency>
  103. <dependency>
  104. <groupId>org.apache.curator</groupId>
  105. <artifactId>curator-framework</artifactId>
  106. <version>${curator.framework.version}</version>
  107. </dependency>
  108. <dependency>
  109. <groupId>org.apache.curator</groupId>
  110. <artifactId>curator-recipes</artifactId>
  111. <version>${curator.recipes.version}</version>
  112. </dependency>
  113. <dependency>
  114. <groupId>org.apache.zookeeper</groupId>
  115. <artifactId>zookeeper</artifactId>
  116. <version>${zookeeper.version}</version>
  117. <type>pom</type>
  118. <exclusions>
  119. <exclusion>
  120. <groupId>org.slf4j</groupId>
  121. <artifactId>slf4j-log4j12</artifactId>
  122. </exclusion>
  123. </exclusions>
  124. </dependency>
  125. <dependency>
  126. <groupId>com.101tec</groupId>
  127. <artifactId>zkclient</artifactId>
  128. <version>${zkclient.version}</version>
  129. </dependency>
  130. <!--[结束]dubbo-->
  131. </dependencies>
  132. </dependencyManagement>

3.子模块pom.xml(Dubbo)

  1. <dependencies>
  2. <!-- Spring Cloud版本升级后,bootstrap.yml失效,需要引入该依赖 -->
  3. <dependency>
  4. <groupId>org.springframework.cloud</groupId>
  5. <artifactId>spring-cloud-starter-bootstrap</artifactId>
  6. </dependency>
  7. <dependency>
  8. <groupId>com.alibaba.cloud</groupId>
  9. <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
  10. </dependency>
  11. <!-- dubbo -->
  12. <dependency>
  13. <groupId>com.alibaba</groupId>
  14. <artifactId>dubbo</artifactId>
  15. <exclusions>
  16. <exclusion>
  17. <groupId>org.jboss.netty</groupId>
  18. <artifactId>netty</artifactId>
  19. </exclusion>
  20. <exclusion>
  21. <artifactId>spring-context</artifactId>
  22. <groupId>org.springframework</groupId>
  23. </exclusion>
  24. </exclusions>
  25. </dependency>
  26. <dependency>
  27. <groupId>com.alibaba</groupId>
  28. <artifactId>dubbo-registry-nacos</artifactId>
  29. </dependency>
  30. <dependency>
  31. <groupId>com.alibaba.nacos</groupId>
  32. <artifactId>nacos-client</artifactId>
  33. </dependency>
  34. <dependency>
  35. <groupId>io.netty</groupId>
  36. <artifactId>netty-all</artifactId>
  37. </dependency>
  38. <dependency>
  39. <groupId>org.apache.curator</groupId>
  40. <artifactId>curator-framework</artifactId>
  41. </dependency>
  42. <dependency>
  43. <groupId>org.apache.curator</groupId>
  44. <artifactId>curator-recipes</artifactId>
  45. </dependency>
  46. <dependency>
  47. <groupId>com.101tec</groupId>
  48. <artifactId>zkclient</artifactId>
  49. </dependency>
  50. </dependencies>

4.主要配置文件

  1. ########## nacos注册 ###########
  2. #向Nacos注册中心注册
  3. spring.cloud.nacos.discovery.register-enabled = true
  4. #Nacos注册地址
  5. spring.cloud.nacos.discovery.server-addr = 127.0.0.1:8848,127.0.0.2:8848
  6. ########## Dubbo ###########
  7. #对应常见的传输协议:Dubbo、RMI、Hessain、WebService、Http
  8. dubbo.protocol.name = dubbo
  9. #设置端口为 -1 表示 dubbo 自动扫描并使用可用端口(从20880开始递增),避免了端口冲突的问题
  10. dubbo.protocol.port = -1
  11. #注册地址
  12. dubbo.registry.address = nacos://127.0.0.1:8848;nacos://127.0.0.2|zookeeper://127.0.0.1:2181?backup=127.0.0.2:2182,127.0.0.3:2183
  13. #dubbo提供者名称
  14. dubbo.application.name = dubbo-demo

5.启动类

  1. package cn.com;
  2. import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. /**
  6. * @author jiangyf
  7. * @description: 网关服务启动类
  8. * @date 2021/11/30
  9. */
  10. @EnableDubbo(scanBasePackages = "cn.com.bluemoon.aad")
  11. @SpringBootApplication
  12. public class DubboDemoApplication {
  13. public static void main(String[] args) {
  14. SpringApplication.run(DubboDemoApplication.class, args);
  15. }
  16. }

ps:看到这里,如果能解决你的问题,麻烦点个关注支持一下,谢谢~

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

闽ICP备14008679号