springcloud Alibaba 笔记12 nacos当配置中心

本文档详细介绍了如何将Nacos配置为中心,包括在pom.xml中引入依赖,配置bootstrap.yaml和application.yaml,创建主配置类,编写Controller以读取配置,并在Nacos UI中进行配置。此外,还探讨了Nacos的配置分类方案,如DataID、Group和Namespace,以及如何针对不同环境加载相应配置。
摘要由CSDN通过智能技术生成

nacos 配置中心

1. 配置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.pyh.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloudalibaba-config-nacos-client3377</artifactId>

    <dependencies>
        <!--        nacos config-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
        <!--     SpringCloud Alibaba nacos discovery -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>

        <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>com.pyh.springcloud</groupId>
            <artifactId>cloud-api-common</artifactId>
            <version>${project.version}</version>
        </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>
    </dependencies>

</project>
2. 配置文件
2.1 bootstrap.yaml
server:
  port: 3377
spring:
  application:
    name: nacos-config-client
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.226.128:8848   # nacos 服务中心地址
      config:
        server-addr: 192.168.226.128:8848   # nacos 配置中心地址
        file-extension: yaml          # 指定 yaml 格式的配置

# ${prefix}-${spring.profiles.active}.${file-extension}
# ${spring.application.name}-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
# 根据官方定义,配置文件格式如上,所以得出配置文件名称为: nacos-config-client-dev.yaml
2.2 application.yaml
spring:
  profiles:
    active: dev
3. 主配置类
package com.pyh.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class NacosConfigClientMain3377 {
    public static void main(String[] args) {
        SpringApplication.run(NacosConfigClientMain3377.class,args);
    }
}
4. controller
package com.pyh.springcloud.controller;

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;

@RestController
@RefreshScope   // 支持 nacos 动态刷新功能
public class ConfigClientController {

    @Value("${config.info}")
    private String configInfo;

    @GetMapping("/config/info")
    public String getConfigInfo(){
        return configInfo;
    }
}

5. nacos UI.

官网:

https://nacos.io/zh-cn/docs/quick-start-spring-cloud.html

${prefix}-${spring.profiles.active}.${file-extension}

在这里插入图片描述

5.1 配置nacos

打开nacos主页 : http://192.168.226.128:8848/nacos
添加新配置文件 nacos-config-client-dev.yaml

5.2 配置dataId

在这里插入图片描述

6. 测试

在这里插入图片描述

7. nacos 分类配置

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

7.1 DataID 方案

application.yaml

spring:
  profiles:
    active: test  # 测试环境,配置是什么就加载什么
#    active: dev  # 开发环境

nacos 添加配置:
在这里插入图片描述

7.2 Group 方案

application.yaml

spring:
  profiles:
    active: info

bootstrap.yaml 添加参数

spring:
  cloud:
    nacos:
      config:
        group: TEST_GROUP         # 默认 DEFAULT_GROUP # DEV_GROUP

在这里插入图片描述

7.3 namespace 命名空间方案

新建两个命名空间 test 和 dev
在这里插入图片描述
以 dev namespace 为例,添加三个不同group的 配置文件
在这里插入图片描述
yaml文件添加以下配置

spring:
  cloud:
    nacos:
      config:
        group: TEST_GROUP         # 默认 DEFAULT_GROUP  # DEV_GROUP
        namespace: d6b14de0-82a2-4404-a7ad-e9bfc3f58128  # 不填写则默认default, 这个在nacos的配置中找
spring:
  profiles:
#    active: info
    active: test  # 测试环境,配置是什么就加载什么
#    active: dev  # 开发环境

配置好对应的 namespace / group / filename 之后,
从上面 application.yaml 和 bootstrap.yaml 可以得到, 会获取一个属性如下的文件
namespace : d6b14de0-82a2-4404-a7ad-e9bfc3f58128
group: TEST_GROUP
dataID(文件名称): nacos-config-client-test.yaml

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值