A, Nacos Config Introduction
What is?
-
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
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.
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)
spring:
profiles:
active: dev # indicates active development environment
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); } }
4)Controller
Spring Cloud native annotations by @RefreshScope
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; } }
5) adding the configuration information Nacos
Theory, Nacos matching rules
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.active
empty, otherwise baffling problem occurs.
Practical operation, the new configuration
Small summary
6) Test
Before starting yaml need to have a corresponding profile in Nacos client configuration list menu
Step1:
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.