Using Nacos to implement service registration and Discovery on Spring Cloud Alibaba We believe that we have a simple grasp of how to use Nacos to set up a registry and realize the registration and discovery of services. In addition, in Alibaba Spring Cloud, we can also use Ribbon and Feign to consume services. Next, let’s take a look at configuration management of Nacos to see the beauty of it
Introduction to Nacos Configuration Management
According to official documentation, Nacos not only has service discovery and service health monitoring, but also implements registered discovery of services, as well as dynamic configuration of services. The Nacos Dynamic configuration service allows you to centrally, externalize, and dynamically manage application and service configurations for all environments. We can store all configurations throughout the architecture in Nacos. This principle is the same as Spring Cloud Config, with the following advantages:
- Dynamic configuration eliminates the need to redeploy applications and services when configuration changes, making configuration management more efficient and agile
- Centralized configuration management makes it easier to implement stateless services and make it easier for services to scale flexibly on demand
Nacos provides key/value stores for storing configuration and other metadata, providing server-side and client-side support for externalizing configuration in distributed systems. Using Spring Cloud Alibaba Nacos Config, you can centrally manage the external property configuration of your Spring Cloud application in Nacos Server. Spring Cloud Alibaba Nacos Config is an alternative to Config Server and Client. Concepts on the client and server have consistent abstractions with Spring Environment and PropertySource, and the configuration is loaded into the Spring Environment during a special Bootstrap phase. As the application moves through the deployment pipeline from development to test to production, you can manage the configuration between these environments and ensure that the application has everything it needs to run at migration time
Nacos configuration actual combat
Here’s how to create configuration content in Nacos and how to get configuration information for Nacos in Spring Cloud applications:
Nacos: 1.0.1 Spring Boot: 2.1.6.RELEASE Spring Cloud: Greenwich
Create the Nacos configuration
Open your browser and enter localhost:8848. Enter your account and password to go to the Nacos control home page
PS:
- Data ID: enter alibaba-nacos-config-client.properties(why? Watch slowly below)
- Group: use the default value DEFAULT_GROUP
- Configuration format: Support six formats I choose Properties here
- Gongzhonghao =lihaodong_blog Gongzhonghao =lihaodong_blog
${prefix}-${spring.profile.active}.${file-extension}
- Prefix for spring. The default application. The name of the value, but can be by spring configuration items. The cloud. Nacos. Config. The prefix to configuration
- Spring.profile. active is the profile corresponding to the current environment. For details, refer to the Spring Boot documentation. Note: When spring.profile.active is null, the corresponding hyphen – will also not exist and the dataId concatenation format will become
${prefix}.${file-extension}
- File – exetension as the configuration of content data format, can be configured a spring. Cloud. Nacos. Config. The file – the extension to the configuration. Currently, only properties and YAML types are supported.
After the configuration is complete, it looks like this
Parameter configuration is completed, certainly need procedures to obtain, arrange!
Create a configuration client
The pose used here is the same as in the previous article
Pom. The XML file
<? xml version="1.0" encoding="UTF-8"? > <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.xd</groupId> <artifactId>SpringCloudAlibabaLearn</artifactId> < version > 1.0 - the SNAPSHOT < / version > < relativePath >). / < / relativePath > < / parent > < artifactId > alibaba - nacos - config - client < / artifactId > < version > 0.0.1 - the SNAPSHOT < / version > <name> Alibaba-nacos-config-client </name> <description>Nacos serves as a configuration center </description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <! - nacos config dependence - > < the dependency > < groupId > org. Springframework. Cloud < / groupId > <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>Copy the code
As a sub-module, it integrates the parent POM file and adds nacos-config dependencies
bootstrap.properties
server.port=8084
The configuration Data Id created in Nacos matches the configuration Data Id (except.properties or.yaml suffix).
spring.application.name=alibaba-nacos-config-client
Register service to NACOSSpring. Cloud. Nacos. Discovery. Server - addr = 127.0.0.1:8848Configure the center addressSpring. Cloud. Nacos. Config. Server - addr = 127.0.0.1:8848Copy the code
Main configuration registry and configuration center Note: Bootstrap.properties must be used here. Also, the spring.application.name value must match the configuration Data Id created in the previous Nacos phase (except for the.properties or.yaml suffix)
Create the main application class and implement an interface:
package com.xd.alibabanacosconfigclient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RefreshScope //主要用来让这个类下的配置内容支持动态刷新
@SpringBootApplication
public class AlibabaNacosConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(AlibabaNacosConfigClientApplication.class, args);
}
// nacos配置的参数
@Value("${gongzhonghao}")
private String gongzhonghao;
@GetMapping("/test")
public String getGongzhonghao() {
returngongzhonghao; }}Copy the code
@springBootApplication: Define a SpringBoot application @restController: The @restController annotation is equivalent to the @controller and @responseBody annotation combined to return json data without the @responseBody annotation in front of the method: A configuration with key gongzhonghao (empty string by default) is injected. This configuration is returned through the /test interface, which we will use later to verify that the configuration in Nacos is loaded. RefreshScope: used to enable dynamic RefreshScope for the configuration of this class, that is, when we change the configuration of Nacos after the application is launched
Start the program
test
Open a browser, type http://localhost:8084/test content is as follows:
http://localhost:8084/test
Iii. Reference Materials:
- Nacos official documentation: nacos. IO /zh-cn/
- Nacos blog: blog.didispace.com/spring-clou…
Four. Code examples
- Making: github.com/LiHaodong88…
- A code cloud: gitee.com/li_haodong/…
If you are interested in these, welcome to star, follow, favorites, forward to give support!