Use and configuration of SpringCloud Alibaba Nacos registration center and configuration center

Use and configuration of SpringCloud Alibaba Nacos registration center and configuration center

What is Nacos?

Nacos is committed to helping you discover, configure, and manage microservices. Nacos provides a set of easy-to-use features to help you quickly realize dynamic service discovery, service configuration, service metadata and traffic management.
Nacos helps you build, deliver and manage microservice platforms more agile and easily. Nacos is a service infrastructure for building modern application architectures (such as microservice paradigm, cloud native paradigm) centered on "services".

Registration center configuration: (service registration and discovery)

Official document: https://github.com/alibaba/spring-cloud-alibaba/blob/master/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/readme.md

1. Introduce pom dependency:

 <dependency>
     <groupId>com.alibaba.cloud</groupId>
     <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
 </dependency>

2. Download Nacos-server-1.1.3 client

Download link:
https://download.csdn.net/download/nxw_tsp/12720732

#官网下载地址
https://github.com/alibaba/nacos/releases/download/1.1.3/nacos-server-1.1.3.zip

3. Start Nacos client

After downloading the client, enter the bin directory, run startup.cmd, and then open the link: http://localhost:8848/nacos/ to enter the registration center management page

4. yml configure Nacos port

# 注册中心配置
spring
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
  application:
    name: member  #在配置中心的名字

5. Annotate the startup category:

//开启Nacos注册中心客户端
@EnableDiscoveryClient  

Nacos Configuration Center:

Realize the effect of dynamically modifying the configuration file.
Official document:
https://github.com/alibaba/spring-cloud-alibaba/blob/master/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/readme.md

1. Import dependencies

<!-- Nacos配置中心来做配置管理 -->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>

2. Create the bootstrap.properties file

spring.application.name=coupon
spring.cloud.nacos.config.server-addr=127.0.0.1:8848

3. Add configuration on Nacos configuration management page

Add a configuration file on the Nacos configuration management page, the file name is the module name.properties. For example, if my module is coupon, the file name is coupon.properties.

Add two attributes in the configuration file, for example:

coupon.user.name = Silence
coupon.user.age = 20

4. Get configuration dynamically

For example: I get the content of the configuration file in couponController, then I add @RefreshScope annotation to the header of this class and modify coupon.properties through Nacos to get the latest data.
Insert picture description here

@Value("${coupon.user.name}")
private String name;
@Value("${coupon.user.age}")
private Integer age;
@RequestMapping("/test")
public R testNacosConfig(){
    
    
    return R.ok().put("name",name).put("age",age);
}

Priority of configuration center properties and other configuration files currently applied:
configuration center properties>other configuration files

Detailed description of configuration center:

1. Namespace: environmental isolation (dev, uat, prd)

The default namespace is public (reserved space); by default, all new configurations are in the public space.
We can find the namespace option in the Nacos management panel and add new namespaces. For example, we can add namespaces in different environments.
Insert picture description here
After adding namespaces, create a coupon.properties file in the corresponding namespace.

Insert picture description here
Then after adding it, we need to configure which namespace file we need to call in the bootstrap.properties configuration file.

#namespace为命名空间的ID
spring.cloud.nacos.config.namespace=c3ac50b1-cd7c-4df8-bf83-c011939b8771

Similarly, if we have more microservices, we can also create a corresponding namespace based on each microservice to manage configuration files.

2, arrangement collection

A collection of related or unrelated configuration items. In the system, a configuration file is a configuration set.

3, placement collection ID

It is the Data Id (configuration file name) we configured in Nacos.

4. Configure grouping

By default, all configuration sets belong to DEFAULT_GROUP.
For example, if we configure different configuration groups, then we need to specify which configuration group we use in bootstrap.properties .

#my_group为你自己配置的组名
spring.cloud.nacos.config.group=my_group

Load multiple configuration sets at the same time:

In actual development, our configuration file needs to do a lot of configuration, so many configurations are put into one configuration file for management, it is easy to be confused, so we can distribute these configurations to different configuration files for configuration. For example: the configuration related to mybatis is placed in mybatis.properties, and the configuration related to spring is placed in spring.properties.

Step 1: Add a configuration file in Nacos

Insert picture description here

Step 2: Explain in the configuration file

After the configuration file is created, you need to specify the files that need to be loaded in bootstrap.properties .

spring.cloud.nacos.config.ext-config[0].data-id=mybatis.yml
spring.cloud.nacos.config.ext-config[0].group=dev
spring.cloud.nacos.config.ext-config[0].refresh=true

spring.cloud.nacos.config.ext-config[1].data-id=spring.yml
spring.cloud.nacos.config.ext-config[1].group=dev
spring.cloud.nacos.config.ext-config[1].refresh=true

If the configuration is correct, we will print out which configuration files have been loaded when starting the service.

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.8.RELEASE)

2020-08-18 16:58:53.766  INFO 29532 --- [           main] c.a.c.n.c.NacosPropertySourceBuilder     : Loading nacos data, dataId: 'mybatis.yml', group: 'dev'
2020-08-18 16:58:53.779  INFO 29532 --- [           main] c.a.c.n.c.NacosPropertySourceBuilder     : Loading nacos data, dataId: 'spring.yml', group: 'dev'
2020-08-18 16:58:53.825  INFO 29532 --- [           main] c.a.c.n.c.NacosPropertySourceBuilder     : Loading nacos data, dataId: 'coupon.properties', group: 'dev'

Nacos persistence operation:

Regarding the Nacos persistence operation is relatively simple, we can also operate according to the steps of the official document: https://nacos.io/zh-cn/docs/deployment.html
According to Nacos official introduction, currently only supports Mysql For persistence.

1. First, we need to create a new database in the mysql database for Nacos data storage, and create a new table required by Nacos in the library (in the Nacos conf directory, there is a nacos-mysql.sql file, run directly in mysql Will generate all required tables).
Insert picture description here
2. After the database and table are created, we need to go to the application.properties file in the Nacos conf directory to configure the database connection.

#*************** Config Module Related Configurations ***************#
### If use MySQL as datasource:
spring.datasource.platform=mysql

### Count of DB:
db.num=1

### Connect URL of DB:
db.url.0=jdbc:mysql://112.126.74.111:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC
db.user=root
db.password=123456

3. Restart Nacos. To
restart Nacos, we can create a new namespace to test whether it is persisted in the database.
Insert picture description here
Insert picture description here
In the tenant_info table, we can see that the newly added namespace has been saved.

Guess you like

Origin blog.csdn.net/nxw_tsp/article/details/108080424
Recommended