SpringCloud Alibaba —— Nacos 配置中心

前文

SpringCloud 简介

SpringCloud 版本选型

SpringCloud 工程构建

SpringCloud —— Eureka 注册中心

SpringCloud —— Eureka 集群

SpringCloud —— 服务注册进 Eureka 集群

SpringCloud —— Eureka 自我保护

SpringCloud —— SpringCloud Consul 实现服务注册中心

SpringCloud —— 三个注册中心的异同点

SpringCloud —— Ribbon

SpringCloud —— Ribbon 负载均衡算法

SpringCloud —— OpenFeign

SpringCloud —— Hystrix 简介

SpringCloud —— Hystrix 断路器

SpringCloud —— HystrixDashboard 服务监控

SpringCloud —— Gateway 网关

SpringCloud —— Config 配置中心

SpringCloud —— Sleuth 分布式请求链路跟踪

SpringCloud Alibaba —— Nacos 服务注册


    前面使用 SpringCloud Config 实现配置中心 的时候是比较麻烦的,各种配置和依赖很复杂的

    SpringCloud Alibaba 退出的 Nacos 除了可以实现注册中心以外,还可以实现配置中心,而且实现非常简单,下面就用 Nacos 实现配置中心

基础配置

新建一个模块

目录结构如下:
在这里插入图片描述

POM

老规矩,不需添加的依赖可以不加

<dependencies>

    <!-- Nacos Config -->
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
    </dependency>

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

    <!-- SpringBoot整合Web组件 -->
    <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>
    <!--日常通用jar包配置-->
    <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>
    <dependency>
        <groupId>cn.hutool</groupId>
        <artifactId>hutool-all</artifactId>
        <version>RELEASE</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>cn.hutool</groupId>
        <artifactId>hutool-all</artifactId>
        <version>RELEASE</version>
        <scope>test</scope>
    </dependency>
</dependencies>

YML

这里需要配置两个 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 格式的配置(类似去 Github 读取文件)

# ${spring.application.name}-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}

application.yml

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

主启动类

package com.java.springcloud.alibaba;

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

/**
 * @author Woo_home
 * @create 2020/5/20 16:27
 */

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

业务类

package com.java.springcloud.alibaba.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;

/**
 * @author Woo_home
 * @create 2020/5/20 16:27
 */

@RestController
@RefreshScope   // 支持 Nacos 的动态刷新功能
public class ConfigController {

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

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

在 Nacos 中添加配置信息

Nacos 中的匹配规则

理论:

Nacos 中的 Data Id 的组成格式及与 SpringBoot 配置文件中的匹配规则

在这里插入图片描述
看下 Nacos 是怎么介绍的,官网地址 https://nacos.io/zh-cn/docs/quick-start-spring-cloud.html
在这里插入图片描述

  • prefix 默认为 spring.application.name 的值,也可以通过配置项 spring.cloud.nacos.config.prefix来配置。
  • spring.profile.active 即为当前环境对应的 profile,详情可以参考 Spring Boot文档。 注意:当 spring.profile.active 为空时,对应的连接符 - 也将不存在,dataId 的拼接格式变成 p r e f i x . {prefix}. prefix.{file-extension}
  • file-exetension 为配置内容的数据格式,可以通过配置项 spring.cloud.nacos.config.file-extension 来配置。目前只支持 properties 和 yaml 类型

公式:

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

公式图解:
在这里插入图片描述


实操:

Nacos 的该界面 点击加号,添加配置
在这里插入图片描述
按照官网的说明配置一下
在这里插入图片描述
配置完后点击发布按钮
在这里插入图片描述
在这里插入图片描述
此时回到主页面,已经将刚才的配置添加进来了
在这里插入图片描述
点击详情选项发现内容和刚刚配置的是一致的
在这里插入图片描述
点击编辑选项还可以对刚刚的配置内容进行修改
在这里插入图片描述

测试

  • 启动前需要在 Nacos 客户端 - 配置管理 - 配置管理栏目下有对应的 yaml 配置文件
  • 运行 cloud-config-nacos-client3377 的主启动类
  • 调用接口查看配置信息 http://localhost:3377/config/info

启动主启动类的时候报错了,说无法解析这个 config.info
在这里插入图片描述

原因是因为这个文件名不是 yaml 后缀,这里有点小 bug(但是有些人的是没问题的,可能是 Nacos 及时修复了,据说 1.2.0 以后已经解决了这个问题,我这里使用的是 1.1.4 版本的
在这里插入图片描述
删除该配置文件重新新建一个配置文件,如下:

在这里插入图片描述
修改之后再次启动主启动类,可以发现,控制台打印了 dataid 的信息
在这里插入图片描述

然后我们访问 http://localhost:3377/config/info ,可以发现,我们在 Nacos 配置中心的内容已经拿过来了
在这里插入图片描述

自带刷新功能

OK ,现在已经可以从 Nacos 配置中心将内容拿过来了,那么想修改一下配置的内容怎么办呢?非常简单,点击编辑该配置文件修改配置内容就可以了
在这里插入图片描述
这里讲 version = 1 改为 version = 1.0,然后保存发布一下
在这里插入图片描述
再次刷新该页面 http://localhost:3377/config/info
在这里插入图片描述
这样就实现了一个动态刷新的功能


完整代码已上传至码云 完整代码地址,感兴趣的朋友可以下载运行下

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值