nacos-config入門-使用nacos實現配置中心


一.簡介

使用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服務並從瀏覽器訪問查看效果


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM