Nacos
What is a Configuration Center
What is Configuration
Applications often need to read some configuration information when they start and run. Configuration is basically accompanied by the entire life cycle of the application, such as database connection parameters, startup parameters, and so on.
Characteristic
- Configuration is a program-independent read-only variable
Configuration is read-only for programs, programs change their behavior by reading configuration, but programs should not change configuration
- Configure the entire lifecycle of the accompanying application
Configuration runs through the entire lifecycle of an application, which is initialized at startup by reading the configuration and adjusts its behavior at run time based on the configuration. For example, the port number of the service needs to be read at startup, the system needs to read the timer policy to perform the timer task during operation, and so on.
- Configuration can be loaded in a variety of ways
Common ones are program internal hard code, configuration files, environment variables, startup parameters, database-based, etc.
- Configuration requires governance
The same program often needs different configurations in different environments (development, test production), different clusters (such as different data centers), so it needs a perfect environment, cluster configuration
What is a Configuration Center
In a microservice architecture, when a system is split from a single application into service nodes on a distributed system, the configuration files must also be migrated (split) so that the configuration is dispersed and, in addition, there is redundancy in the dispersal, as shown in the following figure:
The following figure shows the capabilities of the Configuration Center, which separates configurations from applications and manages them in a unified way.) Applications do not need to manage configurations themselves.
Introduction to Nacos
Nacos is an open source product of Alibaba. It is a comprehensive solution for service discovery, configuration management and service governance in the micro service architecture.
Characteristic
-
Service Discovery and Service Health Check
Nacos makes it easier to register services and discover other services through DNS or HTTP interfaces. Nacos also provides real-time health checks for services to prevent requests from unhealthy hosts or service instances.
-
Dynamic Configuration Management
Dynamic configuration services allow you to manage the configuration of all services in a centralized and dynamic manner in all environments. Nacos eliminates the need to redeploy applications when updating configurations, which makes configuration changes more efficient and flexible.
-
Dynamic DNS Service
Nacos provides DNS protocol-based service discovery capabilities designed to support service discovery in heterogeneous languages, and to enable services registered on Nacos to expose endpoints in the form of domain names for easy access and discovery by three parties.
-
Service and Metadata Management
Nacos allows you to manage all the services and metadata of a data center from a micro-service platform perspective, including the description, lifecycle, static dependency analysis of services, health status of services, traffic management of services, routing and security policies.
quick get start
Windows runs directly
Default port 8848
OPEN API Configuration Management Test
After successfully starting nacos, you can verify that the Nacos service is functioning properly through the http api provided by nacos.
Below we test nacos's open api with the curl tool:
curl is a common command line tool in development and can be used as an HTTP protocol test.
This tutorial downloads the Windows version of curl: curl-7.66.0 _2-win64-mingw, download address: htts://curl.haxx.se/windows/
Publish Configuration
curl -X POST "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=nacos.cfg.dataId&group=test&content=HelloWorld"
- 1
true response succeeded
Get Configuration
curl -X GET "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=nacos.cfg.dataId&group=test"
- 1
Get what we just published HelloWord
External MySQL database support
1.3. 2 Direct support 8.0+
Getting started with configuration
1. Publish Configuration
Open the nacos console and click Menu Configuration Management ~> Configuration List:
Add the following configuration to Nacos:
Data ID:nacos-simple-demo.yaml
Group:DEFAULT_GROUP
Configuration:
common:
config1: something
When publishing is complete, we'll see it
Didn't I use persistence to the database? Is he in that table?
2.nacos client acquisition configuration
<dependencies> <dependency> <groupId>com.alibaba.nacos</groupId> <artifactId>nacos-client</artifactId> <version>1.3.2</version> </dependency> </dependencies>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
package com.wqh.nacos;
import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;
import java.util.Properties;
/**
-
@author Wang Qinghua
-
@version 1.0
-
@date 2021/4/27 22:18
-
@Description TODO
-
@pojectname nacos
*/
public class SimpleDemo {
public static void main(String[] args) throws NacosException {
//Use nacos client to remotely obtain configuration information on nacos services
//nacos service address
String serverAddr = "127.0.0.1:8848";
//dataId
String dataId = "nacos-simple-demo.yaml";
//Group
String group = "DEFAULT_GROUP";Properties properties <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Properties</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> properties<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">"serverAddr"</span><span class="token punctuation">,</span>serverAddr<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//Specify address </span> <span class="token comment">//Get Configuration </span> ConfigService configService <span class="token operator">=</span> NacosFactory<span class="token punctuation">.</span><span class="token function">createConfigService</span><span class="token punctuation">(</span>properties<span class="token punctuation">)</span><span class="token punctuation">;</span> String config <span class="token operator">=</span> configService<span class="token punctuation">.</span><span class="token function">getConfig</span><span class="token punctuation">(</span>dataId<span class="token punctuation">,</span> group<span class="token punctuation">,</span> <span class="token number">5000</span><span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>config<span class="token punctuation">)</span><span class="token punctuation">;</span>
}
}
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
common:
config1: something
So we can quickly get our published configuration
Basic Applications
Nacos Configuration Management Model
For Nacos configuration management, a configuration set can be located through Namespace, group, and Data ID.
Configuration Set (Data ID)
In a system, a configuration file is usually a configuration set, which can contain various configuration information of the system, for example, a configuration set may contain configuration items such as data source, thread pool, log level, etc. Each configuration set can define a meaningful name, which is the ID of the configuration set, or Data ID.
Configuration Items
That's what we configure
One of the configurations contained in a configuration set is a configuration item. It represents a specific configurable parameter and its range, usually in the form of key=value. For example, the log output level (logLevel=INFO |WARN|ERROR) that we often configure for our system is a configuration item.
Configuration Group ing
Configuration grouping is grouping configuration sets, represented by a meaningful string such as Buy or Trade
You can have the same configuration set (DataID) under different configuration groups. When you create a configuration on Nacos, if the name of the configuration group is not filled in, the name of the configuration group defaults to DEFAULT. GROUP. Common scenarios for configuring grouping: Can be used to distinguish between different projects or applications, for example: student management system assignment
Set can define a group as: STUDENT GROUP.
Namespace
Namespaces can be used for configuration isolation of different environments.
For example, you can isolate development, test, and production environments because they may be configured differently or to isolate different users, and different developers manage their configurations using the same nacos, which can be isolated by namespace.
Configuration groupings (groups) or sets with the same name can exist under different namespaces.
practice
The Nacos Abstract defines the concepts of Namespace, Group, Data ID. What these concepts represent depends on what we think of them. Here's a recommended usage, as shown below:
Namespace: Represents different environments, such as development, testing, and production environments.
Group: Represents a project, such as XX Medical Project, XX E-commerce Project
Datald: There are often several projects under each project, and each configuration set (Datald) is the main configuration file for a project
Code to get a configuration set: needs to be specified
1.nacos service address, must be specified
2.namespace, do not specify public as default
3.group, do not specify DEFAULT_as default GROUP
4.dateId, must be specified
Namespace management
Isolation Design
Nacosspace is designed to be multi-environment based and multi-tenant (multiple users use Nacos together) data (configuration and services) isolated by nacos.
From a tenant (user) perspective, if there are multiple sets of different environments, then different namespce s can be created based on the specified environment to achieve multi-environment isolation. For example, if you have three different environments for development, testing, and production, you can build three different namespace s using a set of nacos clusters. As shown in the following figure:
From the perspective of multiple tenants (users), each tenant (user) may have its own namespace, and each tenant (user) configuration data and registered service data will be attributed to its own namespace to achieve data isolation between multiple tenants. Super administrators, for example, assign three tenants: Zhang San, Li Si, and Wang Wu. Once allocated, each tenant logs in with their own account name and password and creates their own namespace. As shown in the following figure:
Namespace management
New Namespace
There's a pit here, and I didn't respond to creating a new namespace at first, closing our nacos restart and OK
If you are not aware of this parameter's input when writing a program to get a configuration set, nacos unification uses a default namespace as input, nacos config initializes with an empty string as the default parameter, and the corresponding interface is the public namespace.
We tested that we had a nacos-simple-demo under public before. Yaml, we are now creating a new test namespace under
/** * @author Wang Qinghua * @version 1.0 * @date 2021/4/27 22:18 * @Description TODO * @pojectname Interview related */ public class SimpleDemo { public static void main(String[] args) throws NacosException { //Use nacos client to remotely obtain configuration information on nacos services //Address of nacos service String serverAddr = "127.0.0.1:8848";<span class="token comment">//dataId</span> String dataId <span class="token operator">=</span> <span class="token string">"nacos-simple-demo.yaml"</span><span class="token punctuation">;</span> <span class="token comment">//Group</span> String group <span class="token operator">=</span> <span class="token string">"DEFAULT_GROUP"</span><span class="token punctuation">;</span> <span class="token comment">//Specify namespace</span> String namespace <span class="token operator">=</span> <span class="token string">"404b3c96-0446-4821-815f-aecc0980da0a"</span><span class="token punctuation">;</span> Properties properties <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Properties</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> properties<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">"serverAddr"</span><span class="token punctuation">,</span>serverAddr<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//Specify address </span> properties<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">"namespace"</span><span class="token punctuation">,</span>namespace<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//Get Configuration </span> ConfigService configService <span class="token operator">=</span> NacosFactory<span class="token punctuation">.</span><span class="token function">createConfigService</span><span class="token punctuation">(</span>properties<span class="token punctuation">)</span><span class="token punctuation">;</span> String config <span class="token operator">=</span> configService<span class="token punctuation">.</span><span class="token function">getConfig</span><span class="token punctuation">(</span>dataId<span class="token punctuation">,</span> group<span class="token punctuation">,</span> <span class="token number">5000</span><span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>config<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span>
}
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
common:
config1:somethingaaa
configuration management
Configuration List
The main thing is to configure the pages and operations of the list, where the modified operations are with a comparison interface
Configuration Set Export
For example, many of the configurations in our test environment are the same as the development environment, so instead of publishing configurations one by one, we can export the configurations and load them in batches
Clone
Clone the configuration of the namespace on our side to another namespace
Historical Version
You can view the history of changes or roll back to previous versions
Listen for queries
Nacos provides the ability to configure subscribers, that is, listener queries, as well as MD5 checks currently configured by clients to help users better check whether configuration changes are pushed to the Client side.
package com.wqh.nacos;
import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.Listener;
import com.alibaba.nacos.api.exception.NacosException;
import java.util.Properties;
import java.util.concurrent.Executor;
/**
-
@author Wang Qinghua
-
@version 1.0
-
@date 2021/4/27 22:18
-
@Description TODO
-
@pojectname Interview Related
*/
public class SimpleDemo {
public static void main(String[] args) throws NacosException {
//Use nacos client to remotely obtain configuration information on nacos services
//nacos service address
String serverAddr = "127.0.0.1:8848";<span class="token comment">//dataId</span> String dataId <span class="token operator">=</span> <span class="token string">"nacos-simple-demo.yaml"</span><span class="token punctuation">;</span> <span class="token comment">//Group</span> String group <span class="token operator">=</span> <span class="token string">"DEFAULT_GROUP"</span><span class="token punctuation">;</span> <span class="token comment">//Specify namespace</span> String namespace <span class="token operator">=</span> <span class="token string">"404b3c96-0446-4821-815f-aecc0980da0a"</span><span class="token punctuation">;</span> Properties properties <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Properties</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> properties<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">"serverAddr"</span><span class="token punctuation">,</span>serverAddr<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//Specify address </span> properties<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">"namespace"</span><span class="token punctuation">,</span>namespace<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//Get Configuration </span> ConfigService configService <span class="token operator">=</span> NacosFactory<span class="token punctuation">.</span><span class="token function">createConfigService</span><span class="token punctuation">(</span>properties<span class="token punctuation">)</span><span class="token punctuation">;</span> String config <span class="token operator">=</span> configService<span class="token punctuation">.</span><span class="token function">getConfig</span><span class="token punctuation">(</span>dataId<span class="token punctuation">,</span> group<span class="token punctuation">,</span> <span class="token number">5000</span><span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>config<span class="token punctuation">)</span><span class="token punctuation">;</span> configService<span class="token punctuation">.</span><span class="token function">addListener</span><span class="token punctuation">(</span>dataId<span class="token punctuation">,</span> group<span class="token punctuation">,</span> <span class="token keyword">new</span> <span class="token class-name">Listener</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span> <span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> Executor <span class="token function">getExecutor</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span> <span class="token keyword">return</span> null<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment">//Get notifications </span>when configuration changes <span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">receiveConfigInfo</span><span class="token punctuation">(</span>String s<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span> <span class="token keyword">try</span> <span class="token punctuation">{<!-- --></span> Thread<span class="token punctuation">.</span><span class="token function">sleep</span><span class="token punctuation">(</span><span class="token number">2000</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">InterruptedException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span> e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span>
}
}
Listening for configuration files in our test environment
Let's go listen for queries.
Now that we've started listening, let's modify this configuration
See the client IDEA results again
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". SLF4J: Defaulting to no-operation (NOP) logger implementation SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details. common: config1:somethingaaa common: config1:somethingFFFF
Nacos Configuration Management for Distributed Systems
From Single Architecture to Micro Services
Single Architecture
In the early days of Web application development, most web engineers packaged all the functional modules together and ran them in a web container. All the functional modules used the same database. At the same time, it also provided API s or accessed web modules.
Although it is also modular logic, it will eventually be packaged and deployed as a single application, a system that deploys all the functionality to run in a web container called a single architecture (also known as a giant stone application).
The single architecture has many advantages:
Efficient development, easy testing, easy deployment
shortcoming
Complexity increases, maintainability decreases, and version iteration slows down
** Unable to scale on demand: ** Horizontal scaling is achieved by redundant deployment of full applications, not scalable on demand for a business.
Microservices
A microservice generally performs a specific function, such as order service, user service, and so on. Each microservice is a complete application with its own business logic and database. Some microservices also publish AP s for use by other microservices and application clients.
Each business module is accomplished using a separate service, and this micro-service architecture pattern also affects the relationship between applications and databases, unlike traditional multiple business modules
Shared - databases, micro-service architecture Each service has its own database.
benefit
Divide and govern, with a single responsibility; Easy to develop, understand and maintain, easy to team up and manage
Scalable; Ability to independently scale specified services
Easy to modify, replace and deploy locally for continuous integration and fast iteration
Not limited to any technology stack
Distributed Application Configuration Management
Users centrally manage the configuration of multiple services through the console of Nacos Server.
Services uniformly obtain their configurations from Nacos Server and listen for configuration changes.
We test in an environment where the namespace is dev
Publish Configuration
First, we published the configuration in nacos. We planned two services, service1 and service2, and wanted to centrally maintain the configuration of these two services.
Add the following configuration to Nacos:
Create Parent Project
Specification Dependency
<packaging>pom</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.RELEASE</version>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependenices</artifactId>
<version>2.1.3.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>dependencies</span><span class="token punctuation">></span></span>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Micro Service Service 1/2 Configuration
What is Spring Cloud:
Spring Cloud is an ordered collection of frameworks. It cleverly simplifies the development of distributed system infrastructure using the convenience of Spring Boot development, such as service discovery registration, configuration center I message bus, load balancing, circuit breakers, data monitoring, and so on, all of which can be started and deployed with the Spring Boot development style. Spring Cloud does not make wheels over and over again. Instead, it combines mature, proven service frameworks developed by companies today. Netflix is the company that integrates the most components, using the Spring Boot style.
Re-encapsulation masks out complex configuration and implementation principles and ultimately leaves developers with a set of distributed system development toolkits that are easy to understand, deploy, and maintain.
What is Spring Cloud Alibaba
Spring Cloud Alibaba Nacos Discovery is a subproject of spring cloud alibaba, an open-source collection of spring cloud alibaba's suite of microservices, which is dedicated to providing a one-stop solution for microservice development, which can be understood as a set of standards for microservice development. spring cloud alibaba and spring cloud Netflix are implementations. With the spring cloud alibaba scenario, developers can apply Spring Cloud with just a few comments and a few configurations
Connect to Ali Distributed Application Solution and quickly set up distributed application system through Ali Middleware.
Since Nacos is Ali's middleware, it would be wise to use Spring Cloud Alibaba NacosConfig to integrate Nacos's configuration management capabilities if developing Spring cloud micro-service applications.
New project service1
First add a new project called service1 and add the group ID as com. alibaba.cloud and artifact ID are the starters of spring-cloud-starter-alibaba-nacos-config.
<dependencies> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> <version>2.1.0.RELEASE</version> </dependency><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>dependency</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>groupId</span><span class="token punctuation">></span></span>org.springframework.boot<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>groupId</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>artifactId</span><span class="token punctuation">></span></span>spring-boot-starter-web<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>artifactId</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>version</span><span class="token punctuation">></span></span>2.1.3.RELEASE<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>version</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>dependency</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>dependencies</span><span class="token punctuation">></span></span>
Configuration File Writing
Generally speaking, the configuration of spring boot will be in application. Written in the YML (or application.properties) file, the original application must be used because the external configuration center is used. YML was renamed bootstrap. Yml, bootstrap. YM is as follows:
Why not write in our application. What about in yml?
Because our bootstarp load order takes precedence over our application
server: port: 56010 #Start port number command line injection
spring:
application:
name: service1
cloud:
nacos:
config:
server-addr: 127.0.0.1:8848 #Configuration Center
file-extension: yaml #profile suffix dataId = application.name + file-extension
namespace: ea8fdc12-3509-472b-b8a9-5145f7e5ce85 #development environment
group: TEST_GROUP #Test Group
Write startup client
@SpringBootApplication public class Service1Bootstrap { public static void main(String[] args) { SpringApplication.run(Service1Bootstrap.class,args); } }
Service 2 works the same way, but with a different name, server 2. Just change the prot port number
test
@SpringBootApplication @RestController public class Service1Bootstrap { public static void main(String[] args) { SpringApplication.run(Service1Bootstrap.class,args); }<span class="token annotation punctuation">@Value</span><span class="token punctuation">(</span><span class="token string">"${common.name}"</span><span class="token punctuation">)</span> <span class="token keyword">private</span> String config1<span class="token punctuation">;</span> <span class="token annotation punctuation">@GetMapping</span><span class="token punctuation">(</span><span class="token string">"/configs"</span><span class="token punctuation">)</span> <span class="token keyword">public</span> String <span class="token function">getConfig</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span> <span class="token comment">//Read configuration information </span> <span class="token keyword">return</span> config1<span class="token punctuation">;</span> <span class="token punctuation">}</span>
}
Start accessing our localhost:56010/configs to get the configuration we want
Notice that after nacos changes the configuration, we can't get the latest configuration value by refreshing without notice?
No, actually this notification has been produced, but our value annotation does not assign the latest configuration. How can we update it dynamically?
Support dynamic updates of configurations
Use Spring's context mechanism to dynamically update
@SpringBootApplication @RestController public class Service1Bootstrap { public static void main(String[] args) { SpringApplication.run(Service1Bootstrap.class,args); }<span class="token annotation punctuation">@Autowired</span> ConfigurableApplicationContext applicationContext<span class="token punctuation">;</span> <span class="token annotation punctuation">@GetMapping</span><span class="token punctuation">(</span><span class="token string">"/configs"</span><span class="token punctuation">)</span> <span class="token keyword">public</span> String <span class="token function">getConfig</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span> <span class="token comment">//Read configuration information </span> <span class="token keyword">return</span> applicationContext<span class="token punctuation">.</span><span class="token function">getEnvironment</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getProperty</span><span class="token punctuation">(</span><span class="token string">"common.name"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span>
}
In this way, once the nacos configuration center has changed its configuration, we can update it dynamically by refreshing the request directly
Customize namespace and group
Without explicitly specifying the ${spring.cloud.nacos.config.namespace} configuration, Public on Nacos is the namespace used by default. If you need to use a custom namespace, you can do so with the following configuration:
Custom Extended DataId Configuration
If we have a service that uses some public configuration, how do we fix it more than once
Spring Cloud Alibaba Nacos Config supports custom Data ld configurations. A complete configuration case is as follows:
server: port: 56010 #Start port number command line injection
spring:
application:
name: service1
cloud:
nacos:
config:
server-addr: 127.0.0.1:8848 #Configuration Center
file-extension: yaml #profile suffix dataId = application.name + file-extension
namespace: ea8fdc12-3509-472b-b8a9-5145f7e5ce85 #development environment
group: TEST_GROUP #Test Group
<span class="token comment">#Extend dataId</span> <span class="token comment">#1.DataId in default group DEFAULT_ Dynamic refresh of configuration is not supported under GROUP </span> ext<span class="token punctuation">-</span>config<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">:</span> <span class="token key atrule">data-id</span><span class="token punctuation">:</span> ext<span class="token punctuation">-</span>config<span class="token punctuation">-</span>common01.properties <span class="token comment">#dataId is not in the default group and does not support dynamic refresh </span> ext<span class="token punctuation">-</span>config<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">:</span> <span class="token key atrule">data-id</span><span class="token punctuation">:</span> ext<span class="token punctuation">-</span>config<span class="token punctuation">-</span>common02.properties <span class="token key atrule">group</span><span class="token punctuation">:</span> GLOBALE_GROUP <span class="token comment">#dataId is neither a default group nor supports dynamic refresh </span> ext<span class="token punctuation">-</span>config<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">:</span> <span class="token key atrule">data-id</span><span class="token punctuation">:</span> ext<span class="token punctuation">-</span>config<span class="token punctuation">-</span>common03.properties <span class="token key atrule">group</span><span class="token punctuation">:</span> REFREH_GROUP <span class="token key atrule">refresh</span><span class="token punctuation">:</span> <span class="token boolean important">true</span>
Configuration 1
Configuration 2
Configuration 3
test
@SpringBootApplication @RestController public class Service1Bootstrap { public static void main(String[] args) { SpringApplication.run(Service1Bootstrap.class,args); }<span class="token annotation punctuation">@Autowired</span> ConfigurableApplicationContext applicationContext<span class="token punctuation">;</span> <span class="token annotation punctuation">@GetMapping</span><span class="token punctuation">(</span><span class="token string">"/configs"</span><span class="token punctuation">)</span> <span class="token keyword">public</span> String <span class="token function">getConfig</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span> <span class="token comment">//Read configuration information </span> <span class="token keyword">return</span> applicationContext<span class="token punctuation">.</span><span class="token function">getEnvironment</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getProperty</span><span class="token punctuation">(</span><span class="token string">"common.name"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token annotation punctuation">@GetMapping</span><span class="token punctuation">(</span><span class="token string">"/configs2"</span><span class="token punctuation">)</span> <span class="token keyword">public</span> String <span class="token function">getConfig2</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span> String name <span class="token operator">=</span> applicationContext<span class="token punctuation">.</span><span class="token function">getEnvironment</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getProperty</span><span class="token punctuation">(</span><span class="token string">"common.name"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> String age <span class="token operator">=</span> applicationContext<span class="token punctuation">.</span><span class="token function">getEnvironment</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getProperty</span><span class="token punctuation">(</span><span class="token string">"common.age"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> String address <span class="token operator">=</span> applicationContext<span class="token punctuation">.</span><span class="token function">getEnvironment</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getProperty</span><span class="token punctuation">(</span><span class="token string">"common.address"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> String birthday <span class="token operator">=</span> applicationContext<span class="token punctuation">.</span><span class="token function">getEnvironment</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getProperty</span><span class="token punctuation">(</span><span class="token string">"common.birthday"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> String fullname <span class="token operator">=</span> applicationContext<span class="token punctuation">.</span><span class="token function">getEnvironment</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getProperty</span><span class="token punctuation">(</span><span class="token string">"common.fullname"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> name <span class="token operator">+</span> <span class="token string">"+"</span> <span class="token operator">+</span> age <span class="token operator">+</span> <span class="token string">"+"</span> <span class="token operator">+</span> address <span class="token operator">+</span> <span class="token string">"+"</span> <span class="token operator">+</span> birthday <span class="token operator">+</span> <span class="token string">"+"</span> <span class="token operator">+</span> fullname<span class="token punctuation">;</span> <span class="token punctuation">}</span>
}
Can I read it? Not in a group
Is readable, then we'll modify and modify each of these three configurations separately, then refresh our request to see if we can refresh dynamically
As we can see, only our third profile can be dynamically refreshed
Custom Shared DataId Configuration
To make it clearer to configure shared Data Id s across multiple applications, you can configure them in the following ways:
spring: cloud: nacos: config: shared-dataids: ext-config-common01. properties,ext-config-common02.properties refreshable-dataids: ext-config-common01.properties #Refresh a profile
You can see:
Separated by commas, configurations of multiple shared data LDs are supported through (spring.cloud. nacos. config. shared-dataids).
Through spring. Cloud. Nacos. Config. Refreshable-dataids to support which shared configurations of DataLD can be dynamically refreshed in applications when configuration changes, aware of the latest configuration values, and separated by commas between multiple Data Id s. Dynamic refresh is not supported by default for all shared configuration DataLDs without explicit configuration.
server: port: 56010 #Start port number command line injection
spring:
application:
name: service1
cloud:
nacos:
config:
server-addr: 127.0.0.1:8848 #Configuration Center
file-extension: yaml #profile suffix dataId = application.name + file-extension
namespace: ea8fdc12-3509-472b-b8a9-5145f7e5ce85 #development environment
group: TEST_GROUP #Test Group
<span class="token key atrule">shared-dataids</span><span class="token punctuation">:</span> ext<span class="token punctuation">-</span>config<span class="token punctuation">-</span>common01.properties<span class="token punctuation">,</span>ext<span class="token punctuation">-</span>config<span class="token punctuation">-</span>common02.properties<span class="token punctuation">,</span>ext<span class="token punctuation">-</span>config<span class="token punctuation">-</span>common03.properties <span class="token key atrule">refreshable-dataids</span><span class="token punctuation">:</span> ext<span class="token punctuation">-</span>config<span class="token punctuation">-</span>common01.properties
Why doesn't my second and third profile work?
Because this shared id only recognizes our DEFAULT_GROUP Something else Not Recognized
Configuration Priority
Enjoy whether the configured DataLD can be refreshed dynamically in the application as configuration changes, aware of the latest configuration values, and separated by commas between multiple Data Id s. Dynamic refresh is not supported by default for all shared configuration DataLDs without explicit configuration.
server: port: 56010 #Start port number command line injection
spring:
application:
name: service1
cloud:
nacos:
config:
server-addr: 127.0.0.1:8848 #Configuration Center
file-extension: yaml #profile suffix dataId = application.name + file-extension
namespace: ea8fdc12-3509-472b-b8a9-5145f7e5ce85 #development environment
group: TEST_GROUP #Test Group
<span class="token key atrule">shared-dataids</span><span class="token punctuation">:</span> ext<span class="token punctuation">-</span>config<span class="token punctuation">-</span>common01.properties<span class="token punctuation">,</span>ext<span class="token punctuation">-</span>config<span class="token punctuation">-</span>common02.properties<span class="token punctuation">,</span>ext<span class="token punctuation">-</span>config<span class="token punctuation">-</span>common03.properties <span class="token key atrule">refreshable-dataids</span><span class="token punctuation">:</span> ext<span class="token punctuation">-</span>config<span class="token punctuation">-</span>common01.properties
Why doesn't my second and third profile work?
Because this shared id only recognizes our DEFAULT_GROUP Something else Not Recognized
Configuration Priority
Spring Cloud Alibaba Nacos Config currently offers three configuration capabilities to pull related configurations from Nacos.
A: via spring. Cloud. Nacos. Config. Shared-dataids supports configuration of multiple shared Data Id s
B: via spring. Cloud. Nacos. Config. Ext-config[n]. The data-id approach supports the configuration of multiple extended data lds, and when multiple data Ids are configured at the same time, their priority relationship is'spring'. Cloud. Nacos. Config. Ext-config[n]. The higher the value of N in data-id, the higher the priority.
C: Automatically generate related Data ld configurations through internal related rules (application name, extension)
When the three approaches are used together, one of their priority relationships is C>B>A
Close configuration completely
By setting spring.cloud.nacos.config.enabled = false to completely close Spring Cloud Nacos Config
Nacos Cluster Deployment
Cluster Deployment
3 or more Nacos nodes to form a cluster
(1) Install more than three Nacos
We can copy the previously unzipped Nacos folders, named nacos. nacos1, nacos2
(2) Configure cluster profiles
In the conf directory of all nacos directories, there is the file cluster. Conf. Example, named cluster. Conf, and configure each line as ip:port. (Please configure 3 or more nodes)
(3) cluster mode startup
Execute startup in the bin directory of the nacos directory:
startup -m cluster
Client Configuration
All clients, specifying several nodes in the nacos cluster:
spring: application: name: xXxx cloud: nacos: config: server-addr: 127.0.0.1:8848,127.0.0.1: 8849,127.0.0.1: 8850