1.版本選擇
當前版本:
Spring Cloud 版本 Dalston.RELEASE
Spring Boot 版本 1.5.12.RELEASE
因為我們使用的Spring Cloud 版本太低曼玩,所以可能出現(xiàn)版本依賴的問題
Nacos 屬于Spring Cloud Alibaba 體系鳞骤,我們參考Spring Cloud 官網(wǎng)的版本對應(yīng)關(guān)系
Spring Cloud的E版本最低也要求1.5的版本,所以我這里只能選在nacos 的版本也為1.5
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>1.5.0.RELEASE</version>
</dependency>
但是當我啟動項目的時候發(fā)現(xiàn)報錯
就是我們在創(chuàng)建nacosAutoServiceRegistration
對象時候報錯黍判。我們進入該類
NacosAutoServiceRegistration
繼承自AbstractAutoServiceRegistration
,在構(gòu)造函數(shù)中調(diào)用了父類的構(gòu)造函數(shù)super(serviceRegistry, autoServiceRegistrationProperties);
,然后我們點去看到父類中此時父類的構(gòu)造函數(shù)只有一個參數(shù)豫尽。
NacosAutoServiceRegistration
來自于 spring-cloud-alibaba-nacos-discovery-1.5.0.RELEASE.jar
AbstractAutoServiceRegistration
來自于 spring-cloud-commons-1.2.0.RELEASE.jar.jar
Spring Cloud D版本默認依賴的是 spring-cloud-commons-1.2.0.RELEASE.jar
我們提高一下spring-cloud-commons 的版本再看一下AbstractAutoServiceRegistration
的構(gòu)造函數(shù)
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-commons</artifactId>
<version>1.3.5.RELEASE</version>
</dependency>
很明顯在更新的版本中增加了一個兩個參數(shù)的構(gòu)造函數(shù)。
2.增加配置
去掉所有Eureka 的配置
spring.cloud.nacos.discovery. server-addr: 192.168.1.10:8905
3.去掉所有Eureka依賴
如果不整體重構(gòu)我發(fā)現(xiàn)我們的項目Eureka 依賴很難去除干凈所以我在需要的項目中加入了如下的兩個依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<scope>test</scope>
</dependency>
設(shè)置Scope 為test 這樣子正式打包的時候就排除掉了Eureka 的依賴顷帖,很取巧但是不可取美旧。