当前位置:网站首页>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
边栏推荐
- redis数据库讲解(三)redis数据类型
- Thread group ThreadGroup uses introduction + custom thread factory class to implement threadfactory interface
- 逻辑卷创建与扩容
- VMware installation 64 bit XP Chinese tutorial
- MySQL lock database lock
- Introduction to the use of countdownlatch and cyclicbarrier for inter thread control
- MySQL基础知识
- 線程組ThreadGroup使用介紹+自定義線程工廠類實現ThreadFactory接口
- krpano全景之vtour文件夹和tour
- OpenStack命令操作
猜你喜欢
微信小程序将原生请求通过es6的promise来进行优化
krpano全景之vtour文件夹和tour
sar命令详解
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
使用开源调研工具Prophet是一种什么体验?
VMware installation 64 bit XP Chinese tutorial
squid代理
TUN 设备原理
Notes on Visio drawing topology
字节面试编程题:最小的K个数
随机推荐
flannel 原理 之 TUN模式
使用开源调研工具Prophet是一种什么体验?
After entering the new company, the operation and maintenance engineer can understand the deployment of the system from the following items
dp-[NOIP2000]方格取数
KVM学习资源
解决ssh配置文件优化以及连接慢的问题
在电视屏幕上进行debug调试
字节面试编程题:最小的K个数
js 键值判断
MySQL同步Could not find first log file name in binary log index file错误
Nacos作为配置中心(四) 使用Demo
MySQL基础知识
Introduction to the use of countdownlatch and cyclicbarrier for inter thread control
mysql 5.1升级到5.610
困扰多年的系统调研问题有自动化采集工具了,还是开源免费的
Wechat applet rotation map swiper
翻牌效果
flannel 原理 之 子网划分
source insight via samba
GFS分布式文件系统(理论)