Technology Sharing

Grain Mall Study Notes-23-Distributed Components-SpringCloud Alibaba-Nacos Configuration Center-Simple Example

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

We have already learned how to use Nacos as a registration center. In this section, we will learn another core function of Nacos:配置中心

1. Introduction to Nacos Configuration Center

Nacos is an easy-to-use platform for dynamic service discovery and configuration management. As a configuration center, Nacos provides the following core features and advantages:

  1. Dynamic Configuration Management:Nacos allows users to dynamically modify and push configuration information without restarting the service, achieving real-time update of configuration.

  2. Configure centralized storage:Nacos provides a centralized configuration storage, which makes configuration information easy to manage and maintain, avoiding the problem of configuration being scattered in different environments and systems.

  3. Configuration version control: Nacos supports configuration version control, which can track the configuration change history and facilitate rollback operations.

  4. Configuration Sharing: Nacos allows configuration to be shared across services, improving configuration reusability and reducing redundancy.

  5. Configuring Isolation: Through the concept of namespace, Nacos supports configuration isolation of different environments (such as development, testing, and production) to ensure that configurations between environments do not interfere with each other.

  6. Configure listening and push: Nacos provides a configuration monitoring function. When the configuration changes, it can actively push updates to the client to achieve hot updates of the configuration.

  7. safety: Nacos supports configuration access control and permission management to ensure that only authorized users can access or modify configurations.

  8. Multiple environment support: Nacos supports multiple deployment environments and can meet configuration management of different scales and requirements.

  9. Integration with Spring Cloud:Nacos is tightly integrated with Spring Cloud and provides components such as spring-cloud-starter-alibaba-nacos-config, which simplifies the process of using the Nacos configuration center in Spring Cloud applications.

  10. Ease of use:Nacos provides a friendly user interface, making configuration viewing, modification and management intuitive and simple.

As a configuration center, Nacos is suitable for microservice architecture, cloud-native applications, and distributed systems that require dynamic configuration management. By centrally managing configuration, Nacos helps enterprises improve the efficiency and security of configuration management, while also simplifying development and operation work.

2. Instructions for using Nacos Configuration Center

The instructions for use are detailed in the official documentation. Click to view the documentationhttps://nacos.io/zh-cn/docs/v2/ecology/use-nacos-with-spring-cloud.html

1. Declare Maven dependency on the configuration center

To use the configuration center, you must first add dependencies.

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
  • 1
  • 2
  • 3
  • 4

In the Grain Mall project, since all services need to use the Nacos configuration center, add this dependency in the common module

2. Configure the address and application name of the Nacos server in bootstrap.properties

In the resource directory of the project or module, declare the address and service name of the Nacos server in bootstrap.properties.

spring.cloud.nacos.config.server-addr=127.0.0.1:8848
spring.application.name=gulimall-coupon
  • 1
  • 2

3. Create a configuration file in the Nacos configuration list interface

insert image description here

After clicking Create Configuration, the following page will appear.

insert image description here

  • ① Data ID is equivalent to the file name and must include the service name, such as gulimall-coupon.properties
  • ② Configuration file type, supports multiple types. For microservices, it is usually yaml or properties
  • ③ The black box contains the configuration information to be filled

exist Nacos Spring Cloud middle,Data Id The full format is as follows:

${prefix}-${spring.profiles.active}.${file-extension}
  • 1
  • prefix The default isspring.application.name The value can also be configured throughspring.cloud.nacos.config.prefixto configure.
  • spring.profiles.active That is, the current environment corresponds toprofileFor details, please refer to the Spring Boot documentation. spring.profiles.active When it is empty, the corresponding connector - will not exist, and the concatenation format of dataId will become${prefix}.${file-extension}
  • file-exetension To configure the data format of the content, you can use the configuration itemspring.cloud.nacos.config.file-extension To configure. Currently only supportsproperties andyaml type.

3. Test

1. Put the configuration information in the project

We first put the information in the project configuration file and configure the following information in the coupon module configuration file application.properties:

coupon.user.name=lcy
coupon.age=22
  • 1
  • 2

insert image description here

2. Write test code

Add a method in the Controller to use the configuration information in the configuration file.

	@Value("${coupon.user.name}")
    private String userName;
    @Value("${coupon.user.age}")
    private String userAge;

    @RequestMapping("test")
    public R test(){
        return R.ok().put("name", userName).put("age", userAge);
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

Note that in the above code, we inject the configuration in the configuration file into the class variable through the @Value annotation and then reference it in the method.

3. Testing

After starting the service, enter the following address in your browser.

http://localhost:7000/coupon/coupon/test
  • 1

The output is as follows, indicating that the information in the configuration file has been read.
insert image description here

4. Configure the configuration information to the configuration center

Take the coupon service as an example for configuration.
insert image description here

Click the Edit button on the right side of the configuration file list to enter the editing interface.

insert image description here

Note that in order to clarify the difference and connection between the configuration center configuration file and the local configuration file, we did not delete the local configuration in the previous step.

Currently, only one configuration is configured in the configuration center. This configuration also exists in the local configuration, but the value is different. The local configuration iscoupon.user.name=lcy, the configuration center iscoupon.user.name=lcy2

After restarting the service and refreshing the browser, the results are as follows.

insert image description here
Obviously, the value of name is taken from the configuration center, not the local configuration file, indicatingThe value in the configuration center will override the value in the local configuration file

Error Logging

Error log of GuLi Mall Configuration Center