当前位置:网站首页>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
边栏推荐
- Logback logger and root
- Preview CSV file
- tcp_diag 内核相关实现 1 调用层次
- mysql查询库大小
- MySQL数据库讲解(十)
- x509证书cer格式转pem格式
- Tongxin UOS php7 2.3 upgrade to php7.0 two point two four
- mysql 5.1升级到5.66
- Notes on Visio drawing topology
- A table splitting implementation scheme of MySQL and InnoDB, MyISAM and MRG_ Introduction to MyISAM and other engine application scenarios
猜你喜欢
API Gateway/API 网关(三) - Kong的使用 - 限流rate limiting(redis)
剑指offer刷题(1)--面向华为
字节面试编程题:最小的K个数
ThreadGroup ThreadGroup implémente l'interface threadfactory en utilisant la classe Introduction + Custom thread Factory
krpano全景之vtour文件夹和tour
TLS/SSL 协议详解 (28) TLS 1.0、TLS 1.1、TLS 1.2之间的区别
A table splitting implementation scheme of MySQL and InnoDB, MyISAM and MRG_ Introduction to MyISAM and other engine application scenarios
Visio画拓扑图随记
统信UOS PHP7.2.3升级至PHP7.2.24
使用开源调研工具Prophet是一种什么体验?
随机推荐
第四届“传智杯”全国大学生IT技能大赛(决赛B组) 题解
gif转为静态图片处理
Operation instructions of star boundary text automatic translator
LLVM - 生成 if-else 以及 PH
rsync+inotify远程同步
爬虫练习题(一)
dp-[NOIP2000]方格取数
mysql 5.1升级到5.68
openstack理论知识
TUN 设备原理
flannel 原理 之 子网划分
redis数据库讲解(三)redis数据类型
squid代理
Redis cluster 原理
How does void * exist?
统信UOS PHP7.2.3升级至PHP7.2.24
百度笔试2022.4.12+编程题目:简单整数问题
获取线程返回值Future接口与FutureTask类使用介绍
利用json-server在本地创建服务器请求
剑指offer刷题(1)--面向华为