Nacos of Spring Cloud Alibaba entry is used as the configuration center

Nacos as a configuration center

  In addition to being a registration center, Nacos can also be used as a configuration center. Below we demonstrate how Nacos is used as a configuration center.

1. Add configuration items in Nacos Configuration Center

  When using the configuration center earlier, you can see that in addition to service management, there is also a configuration management menu. This is where the configuration center manages.

  Add a configuration item for testing. Enter the editing interface by clicking the "+" button (new) in the upper right corner of the list, and then add a test configuration.

  among them,

  • Data ID, corresponding to the client's configuration spring.cloud.nacos.config.prefix+spring.cloud.nacos.config.file-extension, where the default value of the former is ${spring.application.name} which represents the service name, which can also be passed The parameter customizes a name instead of the service name; the latter defaults to properties, which is used to indicate the extension of the configuration file, or you can customize an extension through parameters.

  • Group, corresponding to the spring.cloud.nacos.config.group parameter of the client, default DEFAULT_GROUP, used to indicate the grouping of configuration files.

  • Configuration format refers to the format of the configuration file, generally properties or yaml is used.

  • The configuration content, the parameters that need to be configured, and the corresponding parameters are configured according to the selected configuration format. The configuration method is the same as that in the SpringBoot reference. The properties format is selected here, and the content is shown above.

  After the configuration is complete, click the "Publish" button. At this time, the configuration file of the configuration center is ready, let's start using the configuration items in this configuration file in the project.

2. Use nacos-service to verify the configuration of the configuration center

1. Add the dependencies required to use the configuration center as follows:

<!--引入Nacos的配置中心依赖-->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>

The dependency and the dependency required for registration discovery can be used together or separately.

2. Modify the configuration file bootstrap.properties

  It should be noted here that the relevant configuration of the configuration center needs to be placed in the bootstrap.properties configuration file. This involves the loading order of the configuration file in Spring Boot. It will not be expanded here. For details, please refer to "Configuration File in Spring Boot Detailed explanation of loading order issues. For convenience, all the configurations are directly placed in the bootstrap.properties file, as follows:

spring.application.name=qriver-nacos-server
server.port=8001
#注册中心配置
spring.cloud.nacos.discovery.server-addr=121.36.73.183:8848
#配置中心配置
spring.cloud.nacos.config.server-addr=121.36.73.183:8848

3. Modify the test class ServiceController

  A few things to note here:

  1>, @RefreshScope annotation,

  The @RefreshScope annotation needs to be added to the class, otherwise the application will not automatically refresh when the file in the configuration center is modified.

  2>, use @Value annotation to get parameters

  Through @Value annotation, use ${xxx} to get the parameters in the configuration.

  3>, modify the test method

  The service method is mainly modified here for testing.

@RestController
@RefreshScope
public class ServiceController {

    @Value("${config.testValue}")
    private String testValue;

    @GetMapping("/service")
    public String service(@RequestParam(required = false) String name) {
        if(StringUtils.isEmpty(name)){
            return "hello, " + testValue;
        }
        return "hello, " + name;
    }
}

4. Start the program, and then use postman to access: http://127.0.0.1:8001/service, be careful not to take parameters here. At this time, the parameters of the configuration items in the configuration center will appear as follows: After

modifying the value of the configuration item, republish it and visit again, the returned result will also change, and the result will not be posted here.

  At this point, the use of nacos to realize the function of the configuration center has also been demonstrated. Complete source code: Portal .

Guess you like

Origin blog.csdn.net/hou_ge/article/details/111500510