nacos as the configuration center of the project

Posted by nickdd on Sun, 20 Oct 2019 07:00:45 +0200

  1. This project is built by using spring cloud. You need to pay attention to the version when integrating with nacos. Otherwise, you may refresh the configuration when you restart the project, and it cannot be hot updated. The following is the version map:

  2. First of all, Download Nacos server, and don't attach the project address of github here, because it's too long for me to download from github. So give Baidu network disk link. nacos-server-1.1.3 Download

  3. You can use bin/startup.cmd to start the server, and then create a spring cloud project. The version must correspond to. Start the project after writing bootstrap.yaml.

    	spring:
    	  cloud:
    		nacos:
    		  config:
    			prefix: nacos-config
    			server-addr: 127.0.0.1:8848
    			file-extension: yaml
    	  profiles:
    		active: dev
    
  4. Enter the Nacos console, the default is 127.0.0.1:8848/nacos, and the account secret is Nacos. Add a configuration in the configuration list. Here is my configuration. It needs to correspond to the one in bootstrap.yaml.

  5. Attach the configuration of controller

  6. Specific dataId configuration and demo reference Official website

  7. Test. Modify the value of the configuration in nacos, publish it, and then call 127.0.0.1:8080/config/get to see if you have got the latest configuration.

  8. In fact, its general working principle is: start the client to see if the local configuration is up-to-date; if not, pull it from the server to refresh the local cache. The server notifies the client to update after modification.

  9. Cache address: the server cache is in Nacos server / data. If the client does not make any changes, it is saved to the user directory of C disk. You can see from the source code:

  10. Modify the address of the client's profile cache, mainly the startup parameters.

  11. Post my pom file

<?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 http://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.1.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.carter</groupId>
    <artifactId>demo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
        <spring-cloud-alibaba.version>0.2.1.RELEASE</spring-cloud-alibaba.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <!--SpringCloud-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!--SpringCloud Alibaba-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${spring-cloud-alibaba.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

Topics: Spring Maven Apache github