SpringCloud (Alibaba version) Nacos Configuration Center

A, Nacos Config Introduction

What is?

  Nacos is an easy to build dynamic cloud-native application service discovery, configuration management and service management platform. Use Spring Cloud Alibaba Nacos Config configurable management features Spring Cloud-based programming model for quick access Nacos.

  • Nacos:Dynamic Naming and Configuration Service。

  • Nacos: in fact, a combination of Eureka + Config service registry service configuration center.

 

Second, the basic configuration

1) build.gradle project dependencies

Creating gradle module config-client-nacos and add the web, actuator Monitoring, alibaba-nacos-discovery and alibaba-nacos-config-dependent

dependencies {
   compile group: 'org.springframework.boot', name: 'spring-boot-starter-web'

   compile group: 'org.springframework.boot', name: 'spring-boot-starter-actuator'

   compile group: 'com.alibaba.cloud', name: 'spring-cloud-starter-alibaba-nacos-config', version: '2.1.0.RELEASE'

   compile group: 'com.alibaba.cloud', name: 'spring-cloud-starter-alibaba-nacos-discovery', version: '2.1.0.RELEASE'
}

2) Why do you want to configure two profiles?

  Nacos with Spring Cloud Config when initializing the project, to ensure that the distribution center start to configure pull after pull configuration, in order to ensure the normal start of the project. Spring Boot loading configuration files is the presence priority order, bootstrap higher priority application.

bootstrap.yaml:

server:
  port: 7071
spring:
  application:
    name: config-client-nacos
  cloud:
    nacos:
      discovery:
        addr-Server: 127.0.0.1:8848 # Nacos designated service center address registration
      config:
        addr-Server: 127.0.0.1:8848 # Nacos designated service center address configuration
        file-extension: yaml # specify the configuration yaml format (the original configuration file on GitHub)
View Code

application.yaml:

spring:
  profiles:
    active: dev # indicates active development environment
View Code

3) Start class ConfigClientNacosApplication.java

package org.wesson.cloudalibaba.nacos;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@EnableDiscoveryClient
@SpringBootApplication
public class ConfigClientNacosApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigClientNacosApplication.class, args);
    }

}
View Code

4)Controller

Spring Cloud native annotations by @RefreshScopeauto-refresh functions for configuration:

package org.wesson.cloudalibaba.nacos.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RefreshScope // support Nacos dynamic refresh 
public  class ConfigNacosController {

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

    @GetMapping("/getInfo")
    public String getConfigInfo() {
        return configInfo;
    }

}
View Code

5) adding the configuration information Nacos

Theory, Nacos matching rules

Nacos format consisting of dataid and matching rules and Spring Boot configuration file. The official document links: https://nacos.io/zh-cn/docs/quick-start-spring-cloud.html

 

official:

${spring.application.name}-${spring.profile.active}.${spring.cloud.nacos.config.file-extension}

example:

config-client-nacos-dev.yaml

Note: in strict accordance with the official website of formula, do not let spring.profile.activeempty, otherwise baffling problem occurs.

Practical operation, the new configuration

After starting Nacos service, find the configuration list under configuration management, click the plus sign to the right:

 

Corresponding to the above formula, set the Data ID, add the following configuration content information, then click Publish:

 

Small summary

More intuitive views are as follows, so that, dataid format official website to see it clearly! ! !

 

6) Test

Before starting yaml need to have a corresponding profile in Nacos client configuration list menu

Step1: Start Nacos server

Step2: Run config-client-nacos startup class, port 7071

Step3: Call Interface view configuration information, visit HTTP: // localhost: 7071 / getInfo , results are as follows:

 

Nacos client configuration successfully acquired content list config-client-nacos-dev.yaml configuration file.

 

Third, comes the dynamic refresh configuration

  No need to add any relevant Bus message bus depend, directly edit the configuration file to modify yaml version Nacos client configuration list can be Published:

 

Click release, there will be a current contrast value and the original value, and then confirm that the release:

You do not need to restart any service and POST requests previously sent. Called again to check the interface configuration, the configuration is successful you will find the dynamic refresh function:

I have to say, Ali Baba Nacos technology is still very mature, completely replace the equivalent of Eureka, Config and Bus three components.

Guess you like

Origin www.cnblogs.com/wessonshin/p/12634449.html
Recommended