开发者

springcloud eureka切换nacos的配置方法

开发者 https://www.devze.com 2023-01-14 10:23 出处:网络 作者: lsh2366254
目录springcloud eureka切换nacosbootstrap.yml文件修改pom.XML依赖修改启动文件修改【删除eureka配置】学习文档参考springcloud eureka切换nacos
目录
  • springcloud eureka切换nacos
    • bootstrap.yml文件修改
    • pom.XML依赖修改
    • 启动文件修改【删除eureka配置】
  • 学习文档参考

    springcloud eureka切换nacos

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

    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
             js       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.c编程客栈loud</groupId>
              <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
              <version>2.2.1.RELEASE</version>
          </dependency>
    
    
      <dependency>
              <groupId>cphpom.alibaba.cloud</groupId>
              <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifahttp://www.devze.comctId>
              <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) {
            SpringA开发者_Go入门pplication.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/#/serviceSync

    错误

    No Feign Client for loadBalancing defined.

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

    com.netflix.ribbon ribbon

    到此这篇关于springcloud eureka切换nacos的文章就介绍到这了,更多相关springcloud eureka切换nacos内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

    0

    精彩评论

    暂无评论...
    验证码 换一张
    取 消

    关注公众号