• nacos配置中心使用教程


    nacos配置中心 项目

    yml配置切换url地址

    #第一步:创建配置4个yml

    第一个application.yml
    spring:
      application:
        name: myconf01
      profiles:
        active: test
    server:
      port: 9001
     
    第二个application-dev.yml
      url:
        http://localhost:9999/dev
    第三个application-prov.yml
      url:
        http://localhost:9997/prov
    第四个application-test.yml 
      url:
        http://localhost:9998/test
      
    ##其中  profiles:
        active: test
    activc指定下面使用那个url地址

    第二步配置service

    @Service
    public class MyService {
        @Value("${url}")
        private String url;
    ​
        public void displayUrl(){
            System.out.println(url);
        }
    }

    第三步测试类测试

    @SpringBootTest
    class NcaosspringbootApplicationTests {
        @Resource
        private MyService myService;
        @Test
        void contextLoads() {
            myService.displayUrl();
        }
    }

    点击运行!!!

     

    Nacos**配置中心使用**

     

     

     

     

    #第一步:配置pom 导入依赖

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

    #第二步:配置yml

    #创建application.yml 
    #注意name: nacosconf 要和nacos浏览器里配置的名一致
    spring:
      application:
        name: nacosconf
      cloud:
        nacos:
          discovery:
            server-addr: 192.168.64.200:8848
            username: nacos
            password: nacos
            namespace: public
      profiles:
        active: dev
    server:
      port: 12003
    ​
    ​
    ​
    #创建bootstrap.yml
    spring:
      cloud:
        nacos:
          config:
            server-addr: 192.168.64.200:8848
            username: nacos
            password: nacos
            namespace: public

    #第三步:配置服务类

    package com.kgc.mynacos.myconfig.services;
    ​
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.cloud.context.config.annotation.RefreshScope;
    import org.springframework.stereotype.Service;
    ​
    @Service
    //同步刷新 修改nacos里的配置 这边能自动更新
    @RefreshScope
    public class ReadConfService {
        @Value("${user.name}")
        private String name;
        @Value("${user.age}")
        private String age;
    ​
        public String getInfo(){
            return name+"======="+age;
        }
    }
    ​

     

     

    #第四步:编写测试类

    package com.kgc.mynacos.myconfig;
    ​
    import com.kgc.mynacos.myconfig.services.ReadConfService;
    import org.junit.jupiter.api.Test;
    import org.springframework.boot.test.context.SpringBootTest;
    ​
    import javax.annotation.Resource;
    ​
    @SpringBootTest
    public class MyTest {
        @Resource
        private ReadConfService rcs;
    ​
        @Test
        public void test01(){
            while(true){
                System.out.println(rcs.getInfo());
    ​
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
    ​
        }
    }
    ​
    #注意:Test导包有2个 选api这个
    #@springBootTest后面单词别拼错
    #测试类使用 不要使用private!!!!!不然报错no tests were found

    注意点

    #bootstrap.yml配置
    spring:
      cloud:
        nacos:
          config:
            server-addr: 192.168.64.200:8848
            username: nacos
            password: nacos
            namespace: public
            file-extension: yaml
            #自定义 file-extension: yaml
            #配合浏览器nacos修改的yaml
            group: yf01
            #浏览器里的分组名  group: yf01

    nacos配置开启权限

    vim /opt/soft/nacos8848/conf/application.properties
    ​
    jps
    ​
    shutdown.sh
    ​
    kill -9 (jps出来的数字)
    ​
    startup.sh

     

     

    自定义扩展

    第一步配置yml

    #application.yml
    spring:
      application:
        name: nacosconf
      cloud:
        nacos:
          discovery:
            server-addr: 192.168.64.200:8848
            username: test
            password: test
            namespace: 66f8bc4c-bb18-41c7-8b6f-e5a84dde11b9
    server:
      port: 12003
    ​
    ​
    #bootstrap.yml
    spring:
      cloud:
        nacos:
          config:
            server-addr: 192.168.64.200:8848
            username: test
            password: test
            namespace: 66f8bc4c-bb18-41c7-8b6f-e5a84dde11b9
            shared-configs:
              - data-id: myconf.properties
                group: yf01
                refresh: true
              - data-id: myconf1.peoperties
                group: yf01
                refresh: true
              - data-id: myconf3.properties
                group: yf01
                refresh: true
      #extensionConfigs 和sharedConfigs 功能相同
      #通过自定义扩展的 Data Id 配置,既可以解决多个应用间配置共享的问题,又可以支持一个应用有多个配置文件。

    第二步nacos创建角色 添加权限

     

     

     

    第三步克隆服务列表

     

    第四步点击测试 启动成功

     

  • 相关阅读:
    HZOJ-838:2020年数据结构41题
    纯前端 excel 导出
    Android的六大布局详解
    Windows Server 2022 安全功能重大更新
    华为云服务器价格表_云耀/HECS/S7/S3/C7配置报价表
    Sealos CLI快速部署部署K8s集群
    [Approaching any Machine Learning] Supervised vs unsupervised learning - Note
    shell之常用小工具(sort、uniq、tr、cut)
    [源码解析] TensorFlow 分布式之 ParameterServerStrategy V2
    springboot基于微信小程序的在线办公系统+java+uinapp+Mysql+计算机毕业设计
  • 原文地址:https://blog.csdn.net/just_learing/article/details/125546703