当前位置:网站首页>Nacos uses demo as configuration center (IV)
Nacos uses demo as configuration center (IV)
2022-04-23 14:20:00 【anron】
One 、 Preface
Nacos In addition to being a registry for services , It can also be used as a configuration center for services , Be similar to Spring Cloud Config.
Nacos The configuration center supports hot loading , If necessary Bean Dynamic refresh , Add to class @RefreshScope or @ConfigurationProperties Annotations can be , When Nacos It will be triggered in real time when the server configuration changes org.springframework.cloud.context.refresh.ContextRefresher Of refresh Method , Push the new configuration to the client in real time .
Two 、Nacos Installation
Nacos Refer to this article for the installation of 《Nacos As a registry ( One ) Use Spring Cloud Develop consumer calls to providers 》
3、 ... and 、 Concept
3.1 Profile
Java There are usually multiple projects Profile To configure , Used to differentiate development environments , Test environment , Quasi production environment , Generation environment, etc , One for each environment properties file ( or yml/yaml file ), And then by setting spring.profiles.active To determine which profile to use .Nacos Config The function of this is to move the contents of these files to a unified configuration center , It is convenient for maintenance and supports dynamic refresh of application after real-time modification .
3.2 Data ID
When using Nacos Config after ,Profile The configuration of is stored in Data ID Next , That is, a Profile Corresponding to one Data ID
Data ID The mosaic format of :${prefix} - ${spring.profiles.active} . ${file-extension}
prefix The default is spring.application.name Value , You can also configure items through spring.cloud.nacos.config.prefix To configure the
spring.profiles.active take spring.profiles.active Value , That is, the current environment corresponds to profile
file-extension To configure the data format of the content , You can use the configuration item spring.cloud.nacos.config.file-extension To configure the
3.3 Group
Group The default is DEFAULT_GROUP, Can pass spring.cloud.nacos.config.group To configure the , When there are too many configuration items or there are duplicate names , It can be easily managed by grouping
Four 、 Configuration center configuration
4.1 establish 2 individual Data ID
open Nacos Configuration interface , Create... In turn 2 individual Data ID
- nacos-provider-dev.yaml Configuration of development environment
- nacos-provider-test.yaml Configuration of test environment
4.1.1 Configuration of development environment
4.1.2 Configuration of test environment
5、 ... and 、 establish IDEA project
5.1 Create a project
5.2 establish 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;
}
}
6、 ... and 、 test
call URL To test
http://localhost:8080/api/getConfig
And then modify Nacos Config Configuration data in
Call again URL To test , You can see that the configuration data has been updated
7、 ... and 、 Isolation of configuration
In practical applications , There are several requirements for environmental isolation :
1、 development environment 、 Test environment 、 The quasi production environment and the production environment need to be isolated
2、 Different projects need to be segregated
3、 Same project , Different modules need to be isolated
Can pass Nacos Server for 、namespace Namespace 、group Group to isolate
- Nacos Server for spring.cloud.nacos.config.server-addr
- Nacos The namespace of spring.cloud.nacos.config.namespace, Is namespace ID It's not the name
- Nacos The grouping spring.cloud.nacos.config.group
stay bootstrap.yml File can be configured by Nacos Of server-addr、namespace and group To distinguish between different configuration information .
版权声明
本文为[anron]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231409380545.html
边栏推荐
- rsync+inotify远程同步
- Redis cluster 原理
- man man随记和crontab的@reboot用法
- 时间复杂度计算举例
- MYSQL一种分表实现方案及InnoDB、MyISAM、MRG_MYISAM等各种引擎应用场景介绍
- Visio画拓扑图随记
- 在电视屏幕上进行debug调试
- API gateway / API gateway (IV) - use of Kong - Integrated JWT and fuse plug-in
- 文字组合,不重复,做搜索或查询关键字匹配
- 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
猜你喜欢
uni-app消息推送
微信小程序将原生请求通过es6的promise来进行优化
openstack理论知识
处理 mkdir:无法创建目录“aaa“:只读文件系统
进入新公司,运维工程师从下面这几项了解系统的部署
TLS/SSL 协议详解 (30) SSL中的RSA、DHE、ECDHE、ECDH流程与区别
使用Executors类快速创建线程池
Thread group ThreadGroup uses introduction + custom thread factory class to implement threadfactory interface
正则表达式
Nacos作为配置中心(四) 使用Demo
随机推荐
字节面试编程题:最小的K个数
API Gateway/API 网关(三) - Kong的使用 - 限流rate limiting(redis)
js 格式化时间
KVM learning resources
错误:无法远程查找到密钥 “428F7ECC7117F726“
Docker篇 (五) MySQL的安装
MySQL数据库讲解(九)
ActiveMQ Basics
处理 mkdir:无法创建目录“aaa“:只读文件系统
flannel 原理 之 TUN模式
void*是怎样的存在?
setcontext getcontext makecontext swapcontext
JS progress bar, displaying the loading progress
常见存储类型和FTP主被动模式解析
如何轻松做好一个项目
redis数据库讲解(三)redis数据类型
xx项目架构随记
Storage path of mod subscribed by starbound Creative Workshop at Star boundary
进入新公司,运维工程师从下面这几项了解系统的部署
Get the thread return value. Introduction to the use of future interface and futuretask class