亲宝软件园·资讯

展开

springcloud eureka切换nacos的配置方法

lsh2366254 人气:0

springcloud eureka切换nacos

配置中心地址: http://10.166.9.7:8848/nacos/

bootstrap.yml文件修改

springcloudconfig配置

spring:
    profiles: dev
    cloud:
        config:
            uri: http://10.166.9.7:3301/
        inetutils:
            preferred-networks:
                - 192.168

替换为nacos配置【以gateway-server为例子】

spring:
    application:
        name: gateway-server
    main:
        allow-bean-definition-overriding: true
    cloud:
        # spring cloud config配置
        #        config:
        #            uri: http://10.166.9.7:3301/
        # 默认网卡获取配置示例 spring.cloud.inetutils.preferred-networks=192.168.*
        #        inetutils:
        #            preferredNetworks:
        #            - 192.168.*
        #nacos的配置文件(DataID)默认的加载规则是: ${prefix}- ${spring.profiles.active}- ${file-extension}
        #本次配置未使用spring.profiles.active
        nacos:
            discovery:
                #ip: 192.168
                server-addr: nacos-${spring.profiles}.rxcx.com:8848
                namespace: ${spring.profiles}
                group: rxcx
                #配置中心登录名称
                username: nacos
                #配置中心密码
                password: Zwrx0803!
            config:
                server-addr: nacos-${spring.profiles}.rxcx.com:8848   #nacos配置中心地址
                file-extension: yml              #文件后缀
                prefix: gateway-server            #文件前缀
                namespace: ${spring.profiles}                  #配置文件命名空间
                #配置分组
                group: rxcx
                #配置中心登录名称
                username: nacos
                #配置中心密码
                password: Zwrx0803!
                #支持动态生效
                refresh-enabled: true
                #扩展配置文件
                extension-configs[0]:
                    #配置文件名称
                    data-id: common.yml
                    #配置文件分组
                    group: rxcx
                    #支持动态生效
                    refresh: true
management.endpoints.web.exposure.include: '*'
---
spring:
    profiles: dev

---
spring:
    profiles: testwlhy
#    cloud:
#        config:
#            uri: http://wlhy.config.com:3301/
---
spring:
    profiles: test3A
#    cloud:
#        config:
#            uri: http://172.30.0.181:3301/
#        inetutils:
#            preferred-networks: 192.168.210.*

---
spring:
    profiles: product
#    cloud:
#        config:
#            uri: http://172.16.1.29:3301/

pom.xml依赖修改

删除依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka</artifactId>
    <version>1.4.3.RELEASE</version>
</dependency>

新增依赖

#注意与springboot的版本对应
<!-- 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>
          <exclusions>
              <exclusion>
                  <groupId>com.netflix.ribbon</groupId>
                  <artifactId>ribbon</artifactId>
              </exclusion>
              <exclusion>
                  <groupId>org.springframework.cloud</groupId>
                  <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
              </exclusion>
          </exclusions>
      </dependency>
    
      <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-loadbalancer</artifactId>
      </dependency>

启动文件修改【删除eureka配置】

删除@EnableEurekaClient即可

@SpringBootApplication(scanBasePackages = "com.chinaunicom.rxcx.gateway",exclude={ DataSourceAutoConfiguration.class , MongoAutoConfiguration.class, MongoDataAutoConfiguration.class})
@EnableDiscoveryClient
@EnableEurekaClient
@EnableFeignClients(basePackages="com.chinaunicom.rxcx")
@ComponentScan(basePackages="com.chinaunicom.rxcx")
public class GatewayServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(GatewayServerApplication.class, args);
        System.out.println("网关启动成功-v9");
    }
}

替换为

@SpringBootApplication(scanBasePackages = "com.chinaunicom.rxcx.gateway",exclude={ DataSourceAutoConfiguration.class , MongoAutoConfiguration.class, MongoDataAutoConfiguration.class})
@EnableDiscoveryClient
@EnableFeignClients(basePackages="com.chinaunicom.rxcx")
@ComponentScan(basePackages="com.chinaunicom.rxcx")
public class GatewayServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(GatewayServerApplication.class, args);
        System.out.println("网关启动成功-v9");
    }
}

学习文档参考

https://nacos.io/zh-cn/docs/v2/quickstart/quick-start.html

开发环境,目前已改造gateway,其余服务通过同步工具同步至nacos,保障服务正常

http://10.166.9.7:8083/

错误

No Feign Client for loadBalancing defined.

spring-cloud-starter-alibaba-nacos-discovery依赖排除

com.netflix.ribbon ribbon

加载全部内容

相关教程
猜你喜欢
用户评论