当前位置:网站首页>Nacos作为配置中心(四) 使用Demo
Nacos作为配置中心(四) 使用Demo
2022-04-23 14:11:00 【anron】
一、前言
Nacos除了可以作为服务的注册中心,它还可以作为服务的配置中心使用,类似于Spring Cloud Config。
Nacos配置中心是支持热加载的,如果需要对Bean进行动态刷新,给类添加@RefreshScope或@ConfigurationProperties注解即可,当Nacos监听到服务端配置发生变化时会实时触发org.springframework.cloud.context.refresh.ContextRefresher的refresh方法,将新的配置实时推送到客户端。
二、Nacos的安装
Nacos的安装参考该篇文章《Nacos作为注册中心(一) 使用Spring Cloud开发消费者对提供者的调用》
三、概念
3.1 Profile
Java项目一般都会有多个Profile配置,用于区分开发环境,测试环境,准生产环境,生成环境等,每个环境对应一个properties文件(或是yml/yaml文件),然后通过设置 spring.profiles.active 的值来决定使用哪个配置文件。Nacos Config的作用就把这些文件的内容都移到一个统一的配置中心,即方便维护又支持实时修改后动态刷新应用。
3.2 Data ID
当使用Nacos Config后,Profile的配置就存储到Data ID下,即一个Profile对应一个Data ID
Data ID的拼接格式:${prefix} - ${spring.profiles.active} . ${file-extension}
prefix 默认为 spring.application.name 的值,也可以通过配置项 spring.cloud.nacos.config.prefix 来配置
spring.profiles.active 取 spring.profiles.active 的值,即为当前环境对应的 profile
file-extension 为配置内容的数据格式,可以通过配置项 spring.cloud.nacos.config.file-extension 来配置
3.3 Group
Group 默认为 DEFAULT_GROUP,可以通过 spring.cloud.nacos.config.group 来配置,当配置项太多或者有重名时,可以通过分组来方便管理
四、配置中心的配置
4.1 创建2个Data ID
打开Nacos配置界面,依次创建2个Data ID
- nacos-provider-dev.yaml 开发环境的配置
- nacos-provider-test.yaml 测试环境的配置
4.1.1 开发环境的配置
4.1.2 测试环境的配置
五、创建IDEA项目
5.1 创建工程
5.2 创建bootstrap.yml
spring:
application:
name: nacos-provider
profiles:
active: dev
cloud:
nacos:
config:
server-addr: 192.168.1.17:8848
prefix: nacos-provider
group: DEFAULT_GROUP
file-extension: yaml
5.3 NacosConfigApplication.java
package com.anron.nacosconfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class NacosConfigApplication {
public static void main(String[] args) {
SpringApplication.run(NacosConfigApplication.class, args);
}
}
5.4 NacosConfigApplication.java
package com.anron.nacosconfig.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Author: Anron
* @Date: 2020/9/15 17:31
*/
@RestController
@RefreshScope
@RequestMapping("/api")
public class ConfigController {
Logger log = LoggerFactory.getLogger(this.getClass());
@Value("${db.host:127.0.0.1}")
private String dbhost;
@Value("${db.port:80}")
private String dbport;
@RequestMapping("/getConfig")
public String getConfig() {
log.info("call db.host = [{}], db.port=[{}]", dbhost, dbport);
return dbhost + ":" + dbport;
}
}
六、测试
调用URL进行测试
http://localhost:8080/api/getConfig
然后修改Nacos Config中的配置数据
再次调用URL进行测试,可以看到配置数据已更新
七、配置的隔离
在实际的应用中,存在着以下几种环境隔离的要求:
1、开发环境、测试环境、准生产环境和生产环境需要隔离
2、不同项目需要隔离
3、同一项目,不同的模块需要隔离
可以通过Nacos的服务器、namespace命名空间、group分组来进行隔离
- Nacos的服务器 spring.cloud.nacos.config.server-addr
- Nacos的命名空间 spring.cloud.nacos.config.namespace,是命名空间的ID不是名称
- Nacos的分组 spring.cloud.nacos.config.group
在bootstrap.yml文件中可以通过配置Nacos的server-addr、namespace和group来区分不同的配置信息。
版权声明
本文为[anron]所创,转载请带上原文链接,感谢
https://blog.csdn.net/anron/article/details/108582729
边栏推荐
- STD:: map and STD:: vector memory free
- Homebrew是什么?以及使用
- 倒计时1天~2022云容灾产品线上发布会即将开始
- MySQL数据库讲解(九)
- 编译Openssl
- win10自带Groove音乐不能播放CUE和APE文件的一种曲线救国办法,自己创建aimppack插件包,AIMP安装DSP插件
- 云容灾是什么意思?云容灾和传统容灾的区别?
- Introduction to loan market quotation interest rate (LPR) and loan benchmark interest rate
- HyperMotion云迁移完成阿里云专有云产品生态集成认证
- 预览CSV文件
猜你喜欢
Use the executors class to quickly create a thread pool
RecyclerView高级使用(一)-侧滑删除的简单实现
Visio画拓扑图随记
统信UOS PHP7.2.3升级至PHP7.2.24
星界边境文本自动翻译机(高级版)使用说明
处理 mkdir:无法创建目录“aaa“:只读文件系统
Operation instructions of star boundary automatic text translator (advanced version)
Win10 comes with groove music, which can't play cue and ape files. It's a curvilinear way to save the country. It creates its own aimpack plug-in package, and aimp installs DSP plug-in
OpenStack如何跨版本升级
Recyclerview advanced use (I) - simple implementation of sideslip deletion
随机推荐
x509证书cer格式转pem格式
困扰多年的系统调研问题有自动化采集工具了,还是开源免费的
如何轻松做好一个项目
Operation instructions of star boundary text automatic translator
ActiveMq基础知识
OpenStack如何跨版本升级
DP - [noip2000] grid access
VMWare安装64位XP中文教程
容灾有疑问?点这里
Five ways of using synchronized to remove clouds and fog are introduced
ansible及常用模块的使用
MySQL数据库讲解(八)
About the configuration and use of json5 in nodejs
Jira截取全图
贷款市场报价利率(LPR)与贷款基准利率介绍
GFS分布式文件系统(理论)
线程间控制之CountDownLatch和CyclicBarrier使用介绍
差分隐私(背景介绍)
js 递归(1)
mysql 5.1升级到5.66