当前位置:网站首页>Nacos配置中心实战,盘古微服务开发标配组件
Nacos配置中心实战,盘古微服务开发标配组件
2022-06-09 10:34:00 【InfoQ】
配置中心介绍
- 快速 QA:前文中 Nacos 用于服务注册,为什么配置中心也是它?
- Nacos 是构建以“服务”为中心的现代应用架构 (例如微服务范式、云原生范式) 的服务基础设施。致力于发现、配置和管理微服务,完美的整合了配置中心和服务注册中心。因此,Nacos 不仅是服务注册中心也是功能完善的分布式配置中心。
- 快速 QA:单体分层架构的开发模式也可以使用配置中心进行配置吗?
- 配置中心是分布式微服务架构开发环境下强烈建议的必选标配组件。但如果你是基于单体分层架构开发,配置中心也是一样可以使用的。对于这些基础能力,无论是微服务还是单体,盘古框架都做了完美适配,只需要依赖
pangu-spring-boot-starter就可以实现开箱即用。
相关名词解释
命名空间
配置管理
配置项
配置集
配置集ID
配置快照
本地配置与配置中心对比
- 本地配置(配置文件)
- 配置分散、与应用耦合、静态配置
- 无环境隔离无版本支持,容易引发生产事故
- 无安全审计
- 配置中心
- 配置集中、外部化、动态化实时生效
- 多环境隔离多版本支持,较安全
- 配置权限控制、操作变更审计
配置中心实战
安装相关盘古模块
- 盘古 Parent
<parent>
<groupId>com.gitee.pulanos.pangu</groupId>
<artifactId>pangu-parent</artifactId>
<version>latest.version.xxx</version>
<relativePath/>
</parent>
- 基础模块
<dependency>
<groupId>com.gitee.pulanos.pangu</groupId>
<artifactId>pangu-spring-boot-starter</artifactId>
</dependency>
本地配置
spring.application.name=pangu-examples-config-remote-nacos
spring.profiles.active=${spring.profiles.active:dev}
nacos.config.bootstrap.enable=true
nacos.config.bootstrap.log-enable=true
nacos.config.auto-refresh=true
nacos.config.namespace=${nacos.namespace:pangu-dev}
nacos.config.server-addr=${nacos.server-addr:127.0.0.1:8848}
#nacos.config.type=yaml
nacos.config.type=properties
nacos.config.data-id=${spring.application.name}
关键配置项说明
- nacos.config.auto-refresh
- 配置中心的配置变更后自动刷新到配置客户端
- nacos.config.namespace
- 命名空间(注意是对应命名空间的 ID 值),建议使用命名空间来区分部署环境
- nacos.config.server-addr
- 配置中心通信地址
- nacos.config.type
- 配置中心使用的配置格式( properties、yaml 等)
- nacos.config.data-id
- 配置中心某配置集的唯一标识
配置中心配置
- 快速 QA:客户端是如何与 Nacos 配置中心进行数据同步的?
- 客户端通过 http 长轮询机制拉取 nacos server 端的配置数据,并会在本地创建配置快照(容灾)。在长轮询的超时等待过程中,nacos server 端如果配置发生改变,会主动将配置写入 response 并返回,模拟了“推送”效果。所以客户端能实时感知配置中心的配置变化。
创建命名空间
pangu-dev
创建配置集
pangu-examples-config-remote-nacos- 配置集列表

- 新增/编辑配置集

启动入口
@SpringBootApplication
public class NacosConfigurationApplication {
public static void main(String[] args) {
PanGuApplicationBuilder.init(NacosConfigurationApplication.class).run(args);
}
}
使用配置
@NacosValue/**
* 开启 autoRefreshed配置项, 可以实现参数的动态刷新
*/
@NacosValue(value = "${demo.app.id}")
private String appId;
@NacosValue(value = "${demo.app.name}", autoRefreshed = true)
private String appName;
@NacosValue(value = "${demo.app.author}", autoRefreshed = true)
private String appAuthor;
- 快速 QA:范例只演示了几个简单的变量参数,数据库等中间件的连接配置信息可以放配置中心吗?
- 当然是可以的,干就完了。
彩蛋:基于配置中心的日志级别热切换
#日志
logging.level.root=INFO
logging.level.com.gitee.pulanos.pangu=INFO
本文相关范例源码
- pangu-examples-config-remote-nacos:配置中心参考范例
- pangu-examples-log-dynamic:基于配置中心的日志级别热切换参考范例
彩蛋
- 盘古开源主页
- 盘古开发文档
边栏推荐
- MySQL 学习笔记-第五篇-数据备份与恢复、MySQL 日志
- Learning fuzzy from SQL injection to bypass the latest safe dog WAF
- 【 tgcalls】 suivi et débogage des gestionnaires d'appels 2
- DM platform management - NETCORE
- [go]实验包exp简介
- 从“无人问津”到全面竞争,复盘国内工控安全的第一个10年
- 单体模式
- 面试题快速排序 递归和非递归实现
- Go zero micro Service Practice Series (II. Service splitting)
- 常用函数式接口的学习
猜你喜欢

計網 | OSI模型中各層單比特

基于C语言实现的网络嗅探器设计课程设计

6% equity transfer of Fujian tulougou Cultural Tourism Development Co., Ltd., shared by tamigou

Thirty eight JS tried fractal graphics on canvas (II) tried mountain building, painted mountains and the basis of angular geometry

三维数字沙盘展示具备哪些应用优势

Thirty seven - JS tried fractal graphics on canvas (I) drew an ordinary box graph

塔米狗知识|2022年新的国有产权非公开协议转让新规解读来了!

excel条件格式使用详细步骤

What are the preparations for building your own website

Enterprise distributed batch processing scheme based on task scheduling
随机推荐
啥是腾讯PBD ,看我给讲讲
Matlab related function knowledge points (III) -floor function + dot division operator + matrix index rules
太神奇的 SQL 查询经历,group by 慢查询优化!
Comptage du réseau | unités de chaque couche du Modèle OSI
计网 | OSI模型中各层单位
中金财富开户安全吗
Float float simulates double precision computation on CPU and GPU
One question per day -1200 Minimum absolute difference
中银证券靠谱吗?开证券账户安全吗?
Leetcode 2048. 下一个更大的数值平衡数(有点意思,已解决)
每日一题-1200. 最小绝对差
环糊精金属有机骨架(β-CD-MOF)装载二巯丁二酸(β-CD-MOF/DMSA)β-环糊精金属有机骨架的载药机制
MySQL 学习笔记-第五篇-数据备份与恢复、MySQL 日志
Recursive and non recursive implementation of quick sorting of interview questions
Is Huatai Securities safe? I want to open an account
How much do you know, deep analysis, worth collecting
自己建设网站需要做哪些准备
Web development exchange, web development example tutorial
10 个派上用场的 Flutter 小部件
flutter setState() called after dispose()