The most complete explanation of springcloudalibaba in the whole network 9 ️⃣ Nacos config

Time:2022-5-26

11: Service configuration center: Nacos config

11.1 introduction to service configuration center

First, let’s take a look at some problems about configuration files under the micro Service Architecture:

The configuration files are relatively scattered. In a microservice architecture, configuration files will become more and more with the increase of microservices, and are scattered in various microservices, which is difficult to be uniformly configured and managed.

The configuration file cannot distinguish between environments. Microservice projects may have multiple environments, such as test environment, pre release environment and production environment. The configuration used in each environment is theoretically different. Once it needs to be modified, we need to manually maintain it under each microservice, which is more difficult.

The configuration file cannot be updated in real time. After we modify the configuration file, we must restart the microservice to make the configuration take effect, which is very unfriendly for a running project. Based on the above problems, we need to join the configuration center to solve these problems.

The idea of the configuration center is:

Firstly, all configurations in the project are placed in a centralized place for unified management, and a set of standard interfaces are provided.

When each service needs to obtain configuration, it comes to the interface of the configuration center to pull its own configuration.

When various parameters in the configuration center are updated, it can also notify each service to synchronize the latest information in real time to make it dynamically updated.

After joining the service configuration center, our system architecture diagram will become as follows:

The most complete explanation of springcloudalibaba in the whole network 9 ️⃣ Nacos config

There are several common service configuration centers in the industry:

Apollo Apollo is a distributed configuration center opened by Ctrip. There are many features. For example, after the configuration is updated, it can take effect in real time, support gray publishing function, carry out version management, operation audit and other functions for all configurations, and provide open platform API. And the information is also written in detail.

Disconf disconf is a distributed configuration center open source by Baidu. It is based on zookeeper to realize real-time notification and effectiveness after configuration change.

Springcloud config this is the configuration center component in spring cloud. It integrates seamlessly with spring, which is very convenient to use, and its configuration storage supports GIT. However, it has no visual operation interface, and the effectiveness of the configuration is not real-time. It needs to be restarted or refreshed.

This is a component in the spingcloud Alibaba technology stack. We have used it as a service registry earlier. In fact, it also integrates the function of service configuration. We can directly use it as the service configuration center.

11.2 introduction to Nacos config

Using Nacos as the configuration center is actually to treat Nacos as a server and each micro service as a client. We store the configuration files of each micro service on Nacos, and then pull the configuration of each micro service from Nacos.

Introducing dependency in commodity microservices

com. alibaba. Cloudspring cloud starter Alibaba Nacos config copy code

Add the configuration of Nacos config in the micro service

You cannot use the original application Instead of using YML as a configuration file, create a bootstrap YML as configuration file

Priority of configuration file (from high to low): bootstrap properties -> bootstrap. yml -> application. properties -> application. yml

Comment out the original YML configuration file first, leaving only unique configurations, such as port and service name.

Spring: application: Name: product servicecloud: Nacos: config: server addr: 127.0.0.1: 8848#nacos central address file extension: yaml# configuration file format profiles: active: dev# environment ID copy code

Add the configuration in Nacos, and then put the order micro service application Copy the YML configuration to the configuration content

The most complete explanation of springcloudalibaba in the whole network 9 ️⃣ Nacos config

The most complete explanation of springcloudalibaba in the whole network 9 ️⃣ Nacos config

It is a local application Yam content, start the program to test

11.3 dynamic refresh of Nacos config configuration

We have realized the remote storage of the configuration, but if the configuration is modified at this time, our program cannot read it. Therefore, we need to turn on the dynamic refresh function of the configuration.

This configuration is in the order-service-dev.yaml configuration item in Nacos(It must be on the remote configuration file, not local)

Config: appName: order # doesn’t mean anything. It’s just to test the dynamic refresh. When changing parameters, see if the copied code can be refreshed dynamically

Add nacosconfigcontroller in order micro service

@ [email protected] //Annotation for dynamic refresh publicclassnacosconfigcontroller {@ value (“${AppConfig. Name}”) privatestring [email protected] Requestmapping (“/ nacosconfig1”) publicstring nacosconfig() {return “remote information:” + appconfigname; Copy code

11.4 Nacos config configuration sharing

When there are more and more configurations, we will find that many configurations are repeated. At this time, we need to consider extracting the public 1 configuration file and sharing the configuration file. Whoever needs to import it directly.

11.4.1 shared configuration between different environments of the same microservice

If you want to realize configuration sharing between different environments of the same microservice, you only need to extract one to spring application. Name (spring. Service name), and then put the public configuration of all its environments in it.

Create a new order service Yaml configuration file

This configuration file is used to store public configuration files.

The maximum concurrency value of server: Port: 8080tomcat: threads: Max: 10 #tomcat is modified to 10, spring: Zipkin: base URL: http://127.0.0.1:9411/#zipkin The server’s request address discoveryclientenabled: false # let Nacos treat it as a URL instead of a service name Sleuth: Sampler: Probability: 1.0 # percentage of sampling application: Name: order servicedatasource: driver class name: com mysql. jdbc. Driverurl:jdbc: mysql:///shop-product?serverTimezone=UTC&useUnicode=true&characterEncoding=utf -8 & usessl = trueusername: rootpassword: 1101121833cloud: Nacos: discovery: server addr: localhost: 8848sentinel: Transport: Port: 9999 # the port to communicate with the console. You can specify an unused port at will. Dashboard: localhost: 8080 # specify the address of the console Service Web context unify: false JPA: properties: Hibernate: hbm2ddl: Auto: updatedialect: org hibernate. dialect. Mysql5innodbdialectproduct service: # name of the calling provider ribbon: nfloadbalancerruleclassname: com netflix. loadbalancer. Randomrulefeign: client: config: default: connecttimeout: 3000readtimeout: 3000 copy code

Create a new order service test Yaml configuration file

This configuration file is used to store the configuration of the test environment.

AppConfig: Name: order serviceenv: devserver: application: Name: order servicespring: application: Name: order service # service name cloud: Nacos: config: server addr: 127.0.0.1: 8848#nacos central address file extension: yaml# configuration file format copy code

Add test method in controller

@ [email protected] {@Value(“${appConfig.name}”)privateString appConfigName;@ Value(“${env}”)privateString env;@ Requestmapping (“/ nacosconfig1”) publicstring nacosconfig() {return “remote information:” + appconfigname; } @ requestmapping (“/ nacosconfig2”) publicstring nacosconfig2() {return “public configuration:” + appconfigname + “, environment configuration information:” + env “; Copy code

11.4.2. Shared configuration among different microservices

The principle of realizing configuration sharing between different services is similar to file introduction, that is, defining a public configuration and then introducing it in the current configuration.

Define a global config. In Nacos Yaml configuration file

This configuration file is used to store the configuration shared by all microservices. For the convenience of testing, write an attribute.

Globalconfig: Global copy code

Modify bootstrap yaml

Spring: application: Name: order service # service name cloud: Nacos: config: server addr: 127.0.0.1: 8848 #nacos central address file extension: yaml# configuration file format shared configurations: – data ID: global config Yaml# configure the configuration to be imported refresh: trueprofiles: active: Test # environment ID copy code

Modify the side method of 1controller

@ [email protected] {@Value(“${appConfig.name}”)privateString appConfigName;@ Value(“${env}”)privateString env;@ Value(“${globalConfig}”)privateString globalConfig;@ Requestmapping (“/ nacosconfig1”) publicstring nacosconfig() {return “remote information:” + appconfigname; } @ requestmapping (“/ nacosconfig2”) publicstring nacosconfig2() {return “public configuration:” + appconfigname + “, environment configuration information:” + env “; } @ requestmapping (“/ nacosconfig3”) publicstring nacosconfig3() {return “global configuration:” + globalconfig + “, public configuration:” + appconfigname + “, environment configuration information:” + env “; Copy code

test

11.5 several concepts of Nacos

The namespace namespace can be used for configuration isolation of different environments. Generally, an environment is divided into a namespace.

Group configuration grouping is used to classify different services into the same group. Generally, the configuration of a project is divided into a group.

Configuration set (data ID) in a system, a configuration file is usually a configuration set. Generally, the configuration of microservices is a configuration set.

The most complete explanation of springcloudalibaba in the whole network 9 ️⃣ Nacos config

Recommended Today

Lagrange interpolation — Python

Data analysis Data cleaning: missing value processing, 1 deleting records2 data interpolation3 no treatment Data inhttps://book.tipdm.org/jc/219Data and code in resource package in Chapter4 \ demo \ data \ cataling_ sale. xls Common interpolation methods Interpolation Lagrange interpolation According to mathematical knowledge, for n known points on the plane (no two points), n-1 polynomial can be […]