The article directories

  • preface
  • Nacos Configuration center
  • Using the step
  • conclusion

preface

The last article introduced the use of Nacos as a service registry. This article will introduce the use of Nacos as a configuration center. \


Nacos Configuration center

After entering Spring Cloud Alibaba, I found that NACOS is indeed very powerful, simple and easy to use. Besides serving as a service registry, nacOS can also function as a configuration center, similar to Spring Cloud Config. Nacos can save configuration files directly, making it easier to use. Nacos also supports hot loading of configuration files, which is a pleasure to use.

Using the step

The case in this chapter is modified on the basis of the previous cases. The general process is: 1. Add the configuration file in NACOS; 2. Load the configuration file by provider_Service; 3. First log in to nacOS and add the configuration in the configuration list: where Data ID is divided into three parts: Profile application environment, including dev, Pro, and test. 3. Suffix name, including port and JDBC attributes, is filled in using yamL configuration





Click Publish to view the configuration in the configuration list. The next step is to load the configuration in provider-service





Add a dependency to provider-service

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
Copy the code

${prefix}-${profiles. Active}.${file-extension} ${prefix}-${profiles.

spring:
  cloud:
    nacos:
      config:
        server-addr: 192.1687.188.:8848# config center address file-extension: yaml # config file suffix prefix: provider-service # config file prefix: active: dev # Profile usedCopy the code

application.yml

spring:
  application:
    name: provider-service
  cloud:
    nacos:
      discovery:
        server-addr: 192.1687.188.:8848  The default port is 8848
Copy the code

Controller for testing

@RestController
@RefreshScope
public class NacosConfigController {

    @Value("${server.port}")
    private String port;

    @Value("${spring.datasource.driver-class-name}")
    private String driver;

    @Value("${spring.datasource.url}")
    private String url;

    @Value("${spring.datasource.username}")
    private String username;

    @Value("${spring.datasource.password}")
    private String password;

    @RequestMapping("/config")
    public String config(a){
        return "{port='" + port + '\' ' +
                ", driver='" + driver + '\' ' +
                ", url='" + url + '\' ' +
                ", username='" + username + '\' ' +
                ", password='" + password + '\' ' +
                '} '; }}Copy the code

Start the service and test it directly in the browser



Change the password in the background of NacOS, then refresh the browser, and you can see that NacOS applies the mechanism of configuring hot loading


conclusion

Today I introduced the use of NACOS as a configuration center, and more alibaba components will be introduced later. If this article is helpful, please give it a like