当前位置:网站首页>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
边栏推荐
猜你喜欢
关于UDP接收icmp端口不可达(port unreachable)
x509证书cer格式转pem格式
统信UOS卸载php7.2.24,安装php7.4.27 ;卸载再安装为PHP 7.2.34
KVM学习资源
線程組ThreadGroup使用介紹+自定義線程工廠類實現ThreadFactory接口
Storage path of mod subscribed by starbound Creative Workshop at Star boundary
Operation instructions of star boundary text automatic translator
常见存储类型和FTP主被动模式解析
倒计时1天~2022云容灾产品线上发布会即将开始
About the configuration and use of json5 in nodejs
随机推荐
mysql 5.1升级到5.69
JS key value judgment
在电视屏幕上进行debug调试
百度笔试2022.4.12+编程题目:简单整数问题
API Gateway/API 网关(二) - Kong的使用 - 负载均衡Loadbalance
错误:无法远程查找到密钥 “428F7ECC7117F726“
一些小小小小记录~
Logback logger and root
kprobe 的 3 种使用
Tongxin UOS php7 2.3 upgrade to php7.0 two point two four
dp-能量项链
LLVM - 生成加法
利用json-server在本地创建服务器请求
Golang 对分片 append 是否会共享数据
VMware installation 64 bit XP Chinese tutorial
Introduction to the use of semaphore for inter thread control
微信小程序将原生请求通过es6的promise来进行优化
Gartner预测云迁移规模大幅增长;云迁移的优势是什么?
mysql 5.1升级到5.68
ThreadGroup ThreadGroup implémente l'interface threadfactory en utilisant la classe Introduction + Custom thread Factory