Nacos Service Configuration Center Demo

New technology, replacing spring config center & bus

Nacos as the configuration center-basic configuration

⑴ module
cloudalibaba-config-nacos-client3377

(2) pom

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

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

        <!--web + actuator-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <!--一般基础配置-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

    </dependencies>

(3) yaml

  • application.yaml
# environment

spring:
  profiles:
    # active: dev
    # active: test
    active: info # 测试分组

  • bootstrap.yaml
# config center --> server

server:
  port: 3377

spring:
  application:
    name: nacos-config-client
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848 #服务注册中心地址
      config:
        server-addr: localhost:8848 #配置中心地址
        file-extension: yaml #指定yaml格式的配置
        # group: DEV_GROUP # 生产 分组
        group: TEST_GROUP # 测试 分组
        namespace: ac4eb07f-e257-44eb-80fb-178447a304f7 # 配置namespace 为流水号, 下面可以有多个分组group, 每个分组可以有多个实例dataId

# 配置规则如下.

# https://nacos.io/zh-cn/docs/quick-start-spring-cloud.html
# ${prefix}-${spring.profiles.active}.${file-extension}
# {spring.application.name}-${profile}.${spring.cloud.nacos.config.file-extension}
# so dataID ---> nacos-config-client-dev.yaml

(4) Main start

@EnableDiscoveryClient
@SpringBootApplication
public class NacosConfigClientMain3377 {
    
    

    public static void main(String[] args) {
    
    

        // boot
        SpringApplication.run(NacosConfigClientMain3377.class, args);

    }

}


(5) Business interface, configuration refresh

@RefreshScope // auto refresh config
@RestController
public class ConfigClientController {
    
    

    @Value("${config.info}")
    private String configInfo;

    @GetMapping("/config/info")
    public String getConfigInfo() {
    
    

        return "config info from nacos center ---> \n" + configInfo;
    }

}

(6) The core, the configuration information is stored in the nacos web interface


Interface configuration
Insert picture description here

(7) After the configuration is completed, start the service and access the interface to obtain the configuration information.

Nacos as the configuration center-classification configuration

The namespace, group, dataId in nacos are similar to packages, classes, and methods in Java.

  • dataId, specify spring.profile.active and the DataID of the configuration file to read different configurations in different environments. Just switch the environment in application.yaml.
  • In the group scheme, just create a new group, and distinguish the environment by group.
  • The namespace scheme distinguishes the environment.

Nacos persists the configuration to mysql

The default persistence to the built-in database derby
https://nacos.io/zh-cn/docs/deployment.html

Guess you like

Origin blog.csdn.net/qq_44783283/article/details/111590459