详解Nacos配置中心的实现

2022-11-13 配置 中心 详解

基础配置

新建module:cloudalibaba-config-nacos-client3377

pom文件

版本号已经由父工程控制

<?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>cloud2020</artifactId>
        <groupId>com.atguigu.SpringCloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>cloudalibaba-config-nacos-client3377</artifactId>
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>
        <!--nacos-config-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
        <!--nacos-discovery-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <!--WEB + actuator-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!--一般基础配置-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

YML文件

俩个yml文件:

Nacos同sprinGCloud-config一样,在项目初始化时,要保证先从配置中心进行配置拉取,

拉取配置之后,才能保证项目的正常启动。

SpringBoot中配置文件的加载是存在优先级顺序的,bootstrap优先级高于application

bootstrap.yml文件

# nacos配置
server:
  port: 3377
spring:
  application:
    name: nacos-config-client
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848 #Nacos服务注册中心地址
      config:
        server-addr: localhost:8848 #Nacos作为配置中心地址
        file-extension: yaml #指定yaml格式的配置

application.yml文件

spring:
  profiles:
    active: dev # 表示开发环境

Nacos界面配置对应

公式:${spring.application.name}-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}

prefix 默认为 spring.application.name 的值

spring.profile.active 即为当前环境对应的 profile,可以通过配置项 spring.profile.active 来配置。

file-exetension 为配置内容的数据格式,可以通过配置项 spring.cloud.nacos.config.file-extension 来配置

具备回滚功能

主启动类

@EnableDiscoveryClient
@SpringBootApplication
public class NacosConfigClientMain3377
{
    public static void main(String[] args) {
            SpringApplication.run(NacosConfigClientMain3377.class, args);
    }
}

Controller类

@RestController
@RefreshScope
public class ConfigClientController {
    @Value("${config.info}")
    private  String configInfo;
    @GetMapping("/config/info")
    public String getConfigInfo() {
        return configInfo;
    }
}

配置配置中心文件

打开nacos ,登录到http://localhost:8848/nacos

填写配置内容

config:
 info: "dev config version=1"

填写以后发布

测试1

打开浏览器访问:localhost:3377/config/info

修改配置文件:

config:
 info: "dev config version=2"

测试1

打开浏览器访问:localhost:3377/config/info

Nacos中的匹配规则

Nacos作为配置中心-分类配置

问题

多环境多项目管理

问题1:

实际开发中,通常一个系统会准备

dev开发环境

test测试环境

prod生产环境。

如何保证指定环境启动时服务能正确读取到Nacos上相应环境的配置文件呢?

问题2:

一个大型分布式微服务系统会有很多微服务子项目,

每个微服务项目又都会有相应的开发环境、测试环境、预发环境、正式环境......

那怎么对这些微服务配置进行管理呢?

Nacos的图形化管理界面

配置管理

命名空间

三种方案加载配置

DataID方案

指定spring.profile.active和配置文件的DataID来使不同环境下读取不同的配置

测试2

重新启动3377

打开浏览器访问:http://localhost:3377/config/info

Group方案

通过Group实现环境区分

在config下增加一条group的配置即可。可配置为DEV_GROUP或TEST_GROUP

Namespace方案

bootstrap

# nacos注册中心
server:
  port: 3377
spring:
  application:
    name: nacos-order
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848 #Nacos服务注册中心地址
      config:
        server-addr: localhost:8848 #Nacos作为配置中心地址
        file-extension: yaml #这里我们获取的yaml格式的配置
        namespace: 5da1dccc-ee26-49e0-b8e5-7d9559b95ab0
        #group: DEV_GROUP
        group: TEST_GROUP

application

# Nacos注册配置,application.yml
spring:
  profiles:
    #active: test
    active: dev
    #active: info

到此这篇关于详解Nacos配置中心的实现的文章就介绍到这了,更多相关Nacos配置中心内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

相关文章