nacos + springboot multi-method environment

Here is achieved by namespace method, the other did not succeed.

  1. Add dependent

    <dependency>
          <groupId>com.alibaba.boot</groupId>
          <artifactId>nacos-config-spring-boot-starter</artifactId>
    </dependency>
  2. Add Namespace

    pic

  3. Create several configuration files, such as application-dev.yamland application-test.yam, which specify a different namespace

    server:
      port: 8081
    nacos:
      config:
        server-addr: 127.0.0.1:8848
        namespace: 5c3638e7-ca2c-46af-b47b-67b009c14fa1
  4. Start time of the specified active profile.

    pic

  5. The code will be able to call

    @SpringBootApplication
    @RestController
    //@NacosPropertySource(dataId = "lou-nacos-demo-config",autoRefreshed = true)
    //这里不用指定上面source
    public class LouNacosDemo1App {
        public static void main(String[] args) {
            SpringApplication.run(LouNacosDemo1App.class,args);
        }
        @NacosValue("${userName:aaa}")
        private String userName;
    
        @GetMapping("hello")
        public String hello(){
            return "hello world";
        }
    
        @GetMapping("userName")
        public String getUserName(){
            return userName;
        }
    }

Guess you like

Origin www.cnblogs.com/sheldon-lou/p/11459519.html