You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
spring-cloud-alibaba/spring-cloud-alibaba-examples/nacos-example/nacos-config-2.4.x-example
Freeman Lau b671376f76 Use lombok simplify class 3 years ago
..
src/main Use lombok simplify class 3 years ago
pom.xml Use lombok simplify class 3 years ago
readme-zh.md Update Nacos config docs 3 years ago
readme.md Update Nacos config docs 3 years ago

readme.md

Nacos Config 2.4.x Example

Project Instruction

Spring Boot version 2.4.0 does not start the bootstrap container by default. This project demonstrates how to use nacos when Spring boot >= 2.4.0 version does not enable the bootstrap container.

Applicable to Spring boot >= 2.4.0 and import the configuration using the import method, the configuration will no longer be pulled by default, and the dataId needs to be configured manually.

Demo

How to use

  1. First, modify the pom.xml file and introduce Nacos Config Starter
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
  1. Configure Nacos Config metadata in the application's src/main/resources/application.yml configuration file
server:
  port: 8888
spring:
  application:
    name: nacos-config-import-example
  cloud:
    nacos:
      config:
        group: DEFAULT_GROUP
        server-addr: 127.0.0.1:8848
  config:
    import:
      - optional:nacos:test.yml
      - optional:nacos:test01.yml?group=group_02
      - optional:nacos:test02.yml?group=group_03&refreshEnabled=false
  1. Create test.yml in nacos
configdata:
  user:
    age: 21
    name: freeman
    map:
      hobbies:
        - art
        - programming
      intro: Hello, I'm freeman
    users:
      - name: dad
        age: 20
      - name: mom
        age: 18
  1. After completing the above operations, the application will obtain the corresponding configuration from Nacos Config and add it to the PropertySources of Spring Environment
// controller
@RestController
public class UserController {

    @Autowired
    private UserConfig userConfig;

    @GetMapping
    public String get() throws JsonProcessingException {
        return new ObjectMapper().writeValueAsString(userConfig);
    }
    
}

// ConfigurationProperties
@ConfigurationProperties(prefix = "configdata.user")
public class UserConfig {
    private String name;
    private Integer age;
    private Map<String, Object> map;
    private List<User> users;
    // getters and setters ...
    
    public static class User {
        private String name;
        private Integer age;
        // getters and setters ...
    }
}

Verify dynamic refresh access http://localhost:8888
Then modify the configuration from nacos, and visit again to verify that the dynamic configuration takes effect.