一.简介

使用nacos实现配置中心demo. nacos config配置中心入门.

特别说明: demo只为演示功能, 与具体业务无关. 切勿强行对号入座.

源代码下载链接: https://files.cnblogs.com/files/forest-xs/nacos-config-demo.zip

二. demo结构

nacos-config-demo为父级模块, order-service和user-service分别为俩子模块, 俩子模块结构是一样的, 文中只展示user-service模块的代码.

三.demo详细代码

父模块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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.forest.xs.sfg</groupId>
    <artifactId>nacos-config-demo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>user-service</module>
        <module>order-service</module>
    </modules>
    <packaging>pom</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.1.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
            <version>0.2.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <properties>
        <maven.compiler.source>15</maven.compiler.source>
        <maven.compiler.target>15</maven.compiler.target>
    </properties>

</project>
user-service子模块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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>nacos-config-demo</artifactId>
        <groupId>com.forest.xs.sfg</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>user-service</artifactId>

    <properties>
        <maven.compiler.source>15</maven.compiler.source>
        <maven.compiler.target>15</maven.compiler.target>
    </properties>

</project>
user-service子模块application.yml代码
server:
  port: 8080
user-service子模块bootstrap.properties代码
# nacos配置中心地址
spring.cloud.nacos.config.server-addr=148.70.65.31:8848
# nacos配置中心命名空间
spring.cloud.nacos.config.namespace=forest-nacos-demo-zhoujl
# nacos配置中心分组
spring.cloud.nacos.config.group=NACOS-CONFIG-DEMO-ZHOUJL
# 当前应用的应用名称
spring.application.name=user-service
spring.profiles.active=dev
spring.cloud.nacos.config.file-extension=yaml

其中, spring.application.name、spring.profiles.active和spring.cloud.nacos.config.file-extension会组成nacos config的dataId. 见官网原文说明:


In Nacos Spring Cloud, the format of dataId is as follows:

${prefix}-${spring.profiles.active}.${file-extension}
  • The value of prefix is the value of spring.application.name by default. You can also configure this value in spring.cloud.nacos.config.prefix.
  • spring.profiles.active is the profile of the current environment. For more details, refer to Spring Boot Document. Note: When the value of spring.profiles.active is empty, the corresponding hyphen - will be deleted, and the format of dataId becomes: \({prefix}.\){file-extension}
  • file-exetension is the data format of the configuration content, and can be configured in spring.cloud.nacos.config.file-extension . Currently only the properties and yaml type is supported.

此处顺便贴一个官网使用spring cloud nacos的演示链接:

https://nacos.io/en-us/docs/quick-start-spring-cloud.html

UserServiceApplication.java代码(启动类)
package com.forest.xs.sfg;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @Author:周建林
 * @Time:2021/3/10 23:48
 * @Description
 */
@SpringBootApplication
public class UserServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(UserServiceApplication.class, args);
    }
}

UserAPI.java代码(访问入口类)
package com.forest.xs.sfg.resources;

import com.forest.xs.sfg.configuration.NacosConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author:周建林
 * @Time:2021/3/10 23:49
 * @Description
 */
@RestController
@RequestMapping("/user")
public class UserAPI {
    @Autowired
    private NacosConfiguration cfg;

    @GetMapping("/info/{id}")
    public String getInfoById(@PathVariable String id) {
        String info = String.format("Get info which id is %s from redis which host is %s and port is %s", id, cfg.getRedisHost(), cfg.getRedisPort());
        return info;
    }
}

NacosConfiguration.java代码(配置类)
package com.forest.xs.sfg.configuration;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Configuration;

/**
 * @Author:周建林
 * @Time:2021/3/10 23:50
 * @Description
 */
@Configuration
// Add the native @RefreshScope annotation of Spring Cloud to enable autorefresh of configuration updates
@RefreshScope
public class NacosConfiguration {
    @Value("${nacos-config-demo.redisHost}")
    private String redisHost;

    @Value("${nacos-config-demo.redisPort}")
    private String redisPort;

    public String getRedisHost() {
        return redisHost;
    }

    public String getRedisPort() {
        return redisPort;
    }
}

四.nacos配置中心配置信息

五.启动user-service服务并从浏览器访问查看效果