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:配置中心
。
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:
Dynamic Configuration Management:Nacos allows users to dynamically modify and push configuration information without restarting the service, achieving real-time update of configuration.
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.
Configuration version control: Nacos supports configuration version control, which can track the configuration change history and facilitate rollback operations.
Configuration Sharing: Nacos allows configuration to be shared across services, improving configuration reusability and reducing redundancy.
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.
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.
safety: Nacos supports configuration access control and permission management to ensure that only authorized users can access or modify configurations.
Multiple environment support: Nacos supports multiple deployment environments and can meet configuration management of different scales and requirements.
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.
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.
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。
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>
In the Grain Mall project, since all services need to use the Nacos configuration center, add this dependency in the common module
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
After clicking Create Configuration, the following page will appear.
exist Nacos Spring Cloud
middle,Data Id
The full format is as follows:
${prefix}-${spring.profiles.active}.${file-extension}
prefix
The default isspring.application.name
The value can also be configured throughspring.cloud.nacos.config.prefix
to configure.spring.profiles.active
That is, the current environment corresponds toprofile
For 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.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
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);
}
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.
After starting the service, enter the following address in your browser.
http://localhost:7000/coupon/coupon/test
The output is as follows, indicating that the information in the configuration file has been read.
Take the coupon service as an example for configuration.
Click the Edit button on the right side of the configuration file list to enter the editing interface.
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.
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。