1. 程式人生 > 其它 >解決: spring-boot 有了eurake怎麼再新增 nacos? 【spring-boot配置多註冊中心,同時生效~,不是切換】

解決: spring-boot 有了eurake怎麼再新增 nacos? 【spring-boot配置多註冊中心,同時生效~,不是切換】

spring-boot有了eureka 怎麼再新增一個nacos呢?使用eureka本身就可以實現配置中心,又為何要再用nacos作為配置中心呢?

  

一般實現都是使用單個的註冊中心(包含服務註冊+配置中心),上面的情況很少使用。考慮到eureka的配置中心比較繁瑣,需要git+github,沒有nacos簡單,所以直接安裝一個nacos就可以了,它有自己的內建資料庫。

 

下面講解一下如何配置nacos(在spring-boot+eureka的基礎上)。假設已經搭建好了spring-boot+eureka,下面來說明如何再引入nacos。

 

1、安裝nacos+建立bootstrop.yml 


  1)在nacos中(localhost:8848/nacos/index.html)中建立nacos-config-dev.yml 

  

 

 

 

   2)在bootstrop.yml新增內容:

  

server:
  port: 9001
spring:
  application:
    ## 指定服務名稱,在nacos中的名字
    name: nacos-provider
  cloud:
    nacos:
      discovery:
        # nacos的服務地址,nacos-server中IP地址:埠號
        server-addr: 127.0.0.1:8848
management:
  endpoints:
    web:
      exposure:
        ## yml檔案中存在特殊字元,必須用單引號包含,否則啟動報錯
        include: 
'*'

2、修改pom.xml

  引入nacos的依賴,在pom.xml中新增

  

  <!-- nacos -->
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        <version>2.2.1.RELEASE</version>
    </dependency
> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <version>2.2.1.RELEASE</version> </dependency>

注意:我的spring-boot是2.3.4 ,nacos客戶端是1.4.0

3、修改bootstrop.yml

  一般配置好pom.xml後,啟動後會報錯,以下為我遇到的報錯:

  1)Field registration in org.springframework.cloud.client.serviceregistry.ServiceRegistryAutoConfiguration$ServiceRegistryEndpointConfiguration required a single bean, but 2 were found:這個是註冊中心多了,spring不知道要注入哪個。解決方法:

  在bootstrap.yml中新增:

  

spring:
    autoconfigure:
        exclude: org/springframework/cloud/consul/serviceregistry/ConsulAutoServiceRegistrationAutoConfiguration

 

 

  2)無法找到類:org.springframework.cloud.client.discovery.ReactiveDiscoveryClient。這個是jar衝突導致的。找到衝突的jar包,在pom.xml中刪除,我的情況是spring-boot-commons的版本不對,一個是2.0.0,一個是2.2.2,導致啟動的時候總是找缺失類的那個jar包,因此我刪除了2.0.0,同時引入了2.2.2.如:

    

  <!-- nacos -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-commons</artifactId>
        <version>2.2.2.RELEASE</version>
    </dependency>

    <dependency>
        <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            </dependency>
        <version>2.0.0.RELEASE</version>
     <exclusions>
        <exclusion> 
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-commons</artifactId>
        </exclusion>
      </exclusions> 
    </dependency>

 

  3)報錯ClassNotFoundException:org.springframework.cloud.client.loadbalancer.LoadBalancerClientsProperties

  4)Your project setup is incompatible with our requirements due to following reasons:- Spring Boot [2.1.0.RELEASE] is not compatible with this Spring Cloud release train

    關閉版本相容性檢測,bootstrop.yml中新增:

  

spring:
    cloud:
       compatibility-verifier:
           enabled: false            

 

完成以上更改,啟動專案即可。

 

 

 

參考:

https://didispace-wx.blog.csdn.net/article/details/89702127?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1.pc_relevant_default&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1.pc_relevant_default&utm_relevant_index=2

https://www.cnblogs.com/chengbaiyi/p/11943878.html#3autoserviceregistrationconfiguration