Spring cloud Alibaba 4. Using Nacos as the distributed configuration center

Keywords: Spring Maven Apache SpringBoot

1, Description of Nacos distributed configuration center

1. What are the mainstream configuration centers?

1. Spring cloud config. It comes with spring cloud. It is not friendly. There is no background management. You can directly use git/svn to manage files
2. Apollo: too heavy, difficult to deploy, suitable for large-scale projects, generally too expensive to use
3. Nacos: lightweight, supports both configuration center and registration center, simple deployment, disadvantages: if the configuration center fails, the registration center will hang together

I recommend adding Nacos. The background interface is good-looking, simple to deploy, fast to update, and can be clustered. It's not so easy to hang

2. What's the use of configuration center?

When the configuration file in a system changes, we need to restart the service to make the new configuration file effective,
spring cloud config can realize the unified management of configuration files of all systems in microservices,
In addition, when the configuration file changes, the system will automatically update to get new configuration.

2, Background configuration

1. Publish configuration

After the publishing is configured, springboot only supports yaml and properties
data Id = filename,
Configuration rule: Service Name +. yaml | service name +. properties | service name + environment +. yaml | service name + environment. properties
Current rule: Service Name +. Yaml, Alibaba Nacos config.yaml


Check whether it is published successfully

3, Project environment construction

1,pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <artifactId>alibaba-nacos-config-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>alibaba-nacos-config-demo</name>
    <description>Demo project for Spring Boot</description>
    <dependencies>
    
        <!--  springboot integration web assembly-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        
        <!-- nacos Registry Center -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>0.2.2.RELEASE</version>
        </dependency>
        
        <!-- nacos Distributed configuration center -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
            <version>0.2.2.RELEASE</version>
        </dependency>
    </dependencies>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

2,bootstrap.yml

Be careful:
When connecting to the nacos distributed configuration center, it must be loaded in the form of bootstrap first or an error may be reported.
The configuration center connection must be placed in the bootstrap.yml file, and others can be placed in the application.yml file

Explain:
bootstrap.yml is used for the boot phase of the application context.
application.yml is loaded by the parent Spring ApplicationContext.

server:
  port: 8080
spring:
#  profiles:
    ### Switch configuration environment according to different environments
#      active: dev
  application:
    ### Name of the service
    name: alibaba-nacos-config
  cloud:
    nacos:
      discovery:
        ### Registered address of nacos
        server-addr: 192.168.177.128:8848
        enabled: true
      config:
        ### Configuration center connection address
        server-addr: 192.168.177.128:8848
        ### Grouping
        group: DEFAULT_GROUP
        ### file type
        file-extension: yaml

3. Add test interface

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * TODO   @RefreshScope The configuration file data is refreshed in real time, that is to say, when the nacos management platform modifies the configuration, it will be acquired synchronously in real time (whether to add or not can be configured according to the actual situation)
 * @return
 */
@RestController
@RefreshScope
public class UserServiceImpl  {

    @Value("${user.name}")
    private String name;

    @GetMapping("/getName")
    public String test() {
        return "name="+name;
    }
}

4. Startup class (in fact, nothing needs to be configured)

@SpringBootApplication
class AlibabaClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(AlibabaClientApplication.class, args);
    }
}

Four, test

1. Visit

http://127.0.0.1:8080/getName

2, modify

Edit, modify, publish

3. Access after modification

http://127.0.0.1:8080/getName
The configuration is also updated to the latest configuration information

5, Multi environment configuration description

1. Naming Nacos dataId

nacos configuration rules
dataId = service name + environment +. yaml | service name + environment. properties interface
Such as:

alibaba-nacos-config-dev.yaml
alibaba-nacos-config-pre.yaml
alibaba-nacos-config-pro.yaml
alibaba-nacos-config-test.yaml

2. bootstrap.yml add configuration

spring:
   profiles:
      ### Switch the configuration environment according to different environments, - dev, - pre, - pro, - test
      active: dev
184 original articles published, praised 23, visited 60000+
Private letter follow

Posted by mrodrigues on Sat, 18 Jan 2020 05:31:38 -0800