赞
踩
新系统微服务体系使用 SpringCloudAibaba,版本为目前最新的2021.1;由于公司旧项目使用的技术栈为基于Dubbo+Zookeeper,新项目使用Nacos,且Dubbo支持同时注册多个注册中心,故而采用双注册的技术选型。
网上查询相关资料比较少,SpringCloudAibaba会出现各种依赖导致的问题,使用经反复测试及冲突解决、目前采用版本如下面模块所示,在这基础上进行调整可支持多个版本的Spring Cloud。
- <properties>
- <jdk.version>1.8</jdk.version>
- <java.version>1.8</java.version>
- <maven.compiler.source>8</maven.compiler.source>
- <maven.compiler.target>8</maven.compiler.target>
-
- <spring.boot.version>2.5.5</spring.boot.version>
- <spring.cloud.version>2020.0.4</spring.cloud.version>
- <spring.cloud.bootstrap.version>3.0.4</spring.cloud.bootstrap.version>
- <alibaba.cloud.version>2021.1</alibaba.cloud.version>
- <stream.cloud.version>3.1.4</stream.cloud.version>
-
- <dubbo.version>2.6.6</dubbo.version>
- <dubbo.registry.nacos.version>2.6.7</dubbo.registry.nacos.version>
- <nacos.client.version>1.1.3</nacos.client.version>
- <zookeeper.version>3.4.13</zookeeper.version>
- <netty.all.version>4.1.68.Final</netty.all.version>
- <curator.framework.version>4.0.1</curator.framework.version>
- <curator.recipes.version>2.8.0</curator.recipes.version>
- <zkclient.version>0.10</zkclient.version>
- </properties>
-
- <!-- 依赖版本号管理 -->
- <dependencyManagement>
- <dependencies>
- <!--https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-dependencies/2.3.3.RELEASE-->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-dependencies</artifactId>
- <version>${spring.boot.version}</version>
- <type>pom</type>
- <scope>import</scope>
- </dependency>
-
- <!--https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-dependencies/Hoxton.SR8-->
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-dependencies</artifactId>
- <version>${spring.cloud.version}</version>
- <type>pom</type>
- <scope>import</scope>
- </dependency>
-
- <!--https://mvnrepository.com/artifact/com.alibaba.cloud/spring-cloud-alibaba-dependencies/2021.1-->
- <dependency>
- <groupId>com.alibaba.cloud</groupId>
- <artifactId>spring-cloud-alibaba-dependencies</artifactId>
- <version>${alibaba.cloud.version}</version>
- <type>pom</type>
- <scope>import</scope>
- </dependency>
-
-
- <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-stream-dependencies -->
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-stream-dependencies</artifactId>
- <version>${stream.cloud.version}</version>
- <type>pom</type>
- </dependency>
-
- <!-- https://mvnrepository.com/artifact/com.alibaba.cloud/spring-cloud-starter-alibaba-nacos-config -->
- <dependency>
- <groupId>com.alibaba.cloud</groupId>
- <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
- <version>${alibaba.cloud.version}</version>
- </dependency>
- <!-- https://mvnrepository.com/artifact/com.alibaba.cloud/spring-cloud-starter-alibaba-nacos-discovery -->
- <dependency>
- <groupId>com.alibaba.cloud</groupId>
- <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
- <version>${alibaba.cloud.version}</version>
- </dependency>
- <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-bootstrap -->
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-bootstrap</artifactId>
- <version>${spring.cloud.bootstrap.version}</version>
- </dependency>
-
- <!--[开始]dubbo相关-->
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>dubbo</artifactId>
- <version>${dubbo.version}</version>
- <exclusions>
- <exclusion>
- <groupId>org.jboss.netty</groupId>
- <artifactId>netty</artifactId>
- </exclusion>
- <exclusion>
- <artifactId>spring-context</artifactId>
- <groupId>org.springframework</groupId>
- </exclusion>
- </exclusions>
- </dependency>
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>dubbo-registry-nacos</artifactId>
- <version>${dubbo.registry.nacos.version}</version>
- </dependency>
- <dependency>
- <groupId>com.alibaba.nacos</groupId>
- <artifactId>nacos-client</artifactId>
- <version>${nacos.client.version}</version>
- </dependency>
- <dependency>
- <groupId>io.netty</groupId>
- <artifactId>netty-all</artifactId>
- <version>${netty.all.version}</version>
- </dependency>
- <dependency>
- <groupId>org.apache.curator</groupId>
- <artifactId>curator-framework</artifactId>
- <version>${curator.framework.version}</version>
- </dependency>
- <dependency>
- <groupId>org.apache.curator</groupId>
- <artifactId>curator-recipes</artifactId>
- <version>${curator.recipes.version}</version>
- </dependency>
- <dependency>
- <groupId>org.apache.zookeeper</groupId>
- <artifactId>zookeeper</artifactId>
- <version>${zookeeper.version}</version>
- <type>pom</type>
- <exclusions>
- <exclusion>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-log4j12</artifactId>
- </exclusion>
- </exclusions>
- </dependency>
- <dependency>
- <groupId>com.101tec</groupId>
- <artifactId>zkclient</artifactId>
- <version>${zkclient.version}</version>
- </dependency>
- <!--[结束]dubbo-->
- </dependencies>
- </dependencyManagement>
- <dependencies>
- <!-- Spring Cloud版本升级后,bootstrap.yml失效,需要引入该依赖 -->
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-bootstrap</artifactId>
- </dependency>
- <dependency>
- <groupId>com.alibaba.cloud</groupId>
- <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
- </dependency>
- <!-- dubbo -->
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>dubbo</artifactId>
- <exclusions>
- <exclusion>
- <groupId>org.jboss.netty</groupId>
- <artifactId>netty</artifactId>
- </exclusion>
- <exclusion>
- <artifactId>spring-context</artifactId>
- <groupId>org.springframework</groupId>
- </exclusion>
- </exclusions>
- </dependency>
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>dubbo-registry-nacos</artifactId>
- </dependency>
- <dependency>
- <groupId>com.alibaba.nacos</groupId>
- <artifactId>nacos-client</artifactId>
- </dependency>
- <dependency>
- <groupId>io.netty</groupId>
- <artifactId>netty-all</artifactId>
- </dependency>
- <dependency>
- <groupId>org.apache.curator</groupId>
- <artifactId>curator-framework</artifactId>
- </dependency>
- <dependency>
- <groupId>org.apache.curator</groupId>
- <artifactId>curator-recipes</artifactId>
- </dependency>
- <dependency>
- <groupId>com.101tec</groupId>
- <artifactId>zkclient</artifactId>
- </dependency>
- </dependencies>
4.主要配置文件
- ########## nacos注册 ###########
- #向Nacos注册中心注册
- spring.cloud.nacos.discovery.register-enabled = true
- #Nacos注册地址
- spring.cloud.nacos.discovery.server-addr = 127.0.0.1:8848,127.0.0.2:8848
-
- ########## Dubbo ###########
- #对应常见的传输协议:Dubbo、RMI、Hessain、WebService、Http
- dubbo.protocol.name = dubbo
- #设置端口为 -1 表示 dubbo 自动扫描并使用可用端口(从20880开始递增),避免了端口冲突的问题
- dubbo.protocol.port = -1
- #注册地址
- 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
- #dubbo提供者名称
- dubbo.application.name = dubbo-demo
5.启动类
- package cn.com;
-
- import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
-
- /**
- * @author jiangyf
- * @description: 网关服务启动类
- * @date 2021/11/30
- */
- @EnableDubbo(scanBasePackages = "cn.com.bluemoon.aad")
- @SpringBootApplication
- public class DubboDemoApplication {
-
- public static void main(String[] args) {
- SpringApplication.run(DubboDemoApplication.class, args);
- }
-
- }
ps:看到这里,如果能解决你的问题,麻烦点个关注支持一下,谢谢~
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。