Implementation of service configuration center with spring cloud + Nacos (Hoxton version)

Platanus acerifolia 2021-04-08 11:20:26
implementation service configuration center spring

About Nacos Spring Cloud Please refer to :Nacos Config and Nacos Discovery.

adopt Nacos Server and spring-cloud-starter-alibaba-nacos-config Realize the dynamic change of configuration .

adopt Nacos Server and spring-cloud-starter-alibaba-nacos-discovery Implement service registration and discovery .


First, download and install a nacos server, Then start nacos server. Download and install process view
The new configuration
There are two ways to create a new configuration .
Mode one : visit Nacos Server The page is created manually .
Log in first Nacos Server, The address is generally
Then go to the configuration list , Click the new configuration button , Enter the new configuration page .





  Be careful :

Here I create Data Id by Configuration file for , among Group Is the default DEFAULT_GROUP, The format of the configuration file is also selected accordingly Properties, Add configuration to configuration content nacos.config=hello_nacos, As shown in the figure .



  Mode two : By calling Nacos Open API towards Nacos Server Release configuration :dataId by, The content is nacos.config=hello_nacos

curl -X POST ""


Create service applications  

(1) Use SpringBoot, Create a file called nacos-config The sub project of .

(2)pom.xml Add the following dependencies to the file .

<relativePath/> <!-- lookup parent from repository -->
<!-- Statement Spring cloud The version is Hoxton.SR3-->
<!-- Statement Spring cloud alibaba Version is 2.1.0.RELEASE -->
<!-- Statement nacos-discovery rely on -->
<!-- Statement nacos-config rely on -->

remarks : If you don't need to register the service , It can also be removed nacos-discovery rely on .

Be careful : edition 2.1.x.RELEASE  The corresponding is Spring Boot 2.1.x edition . edition  2.0.x.RELEASE  The corresponding is Spring Boot 2.0.x edition , edition  1.5.x.RELEASE  The corresponding is Spring Boot 1.5.x edition .

Refer to for more version correspondence : Version Description Wiki

(3) modify Add related configuration


(4) create profile , The file named And in Middle configuration Nacos server Address and application name of .

Be careful : The file name is, instead of application Or other . because Nacos Same as Spring Cloud Config equally , At project initialization , Make sure to pull the configuration from the configuration center first , After pulling the configuration , To ensure the normal start of the project .SpringBoot There is a priority order for the loading of configuration files in ,bootstrap Priority over application.

The profile type here can be selected according to personal habits , When I use it here properties type , The configuration is as follows :

# The service name
# Specify the address of the registry . If you don't need to sign up for the service , You can also remove this , And delete discovery rely on .
# Specify the address of the configuration center .
# Specify the format of the configuration file in the configuration center .

Why configuration is needed, It's because it's made up of Nacos Configuration Management dataId Part of the field . 

stay Nacos-Server Configuration Management , among Data ID It's defined by :  

prefix: The default is Value , You can also configure items through To configure the . That is, the current environment corresponds to profile, For details, please refer to  Spring Boot file . You can use the configuration item To configure the . When It's empty time , Corresponding connector - There will be no ,dataId The splicing format of becomes ${prefix}.${file-extension}.

file-exetension: To configure the data format of the content , You can use the configuration item To configure the . Currently only supported properties and yaml type .

(5) Create external interfaces ConfigController, from nacos Middle read configuration .

public class ConfigController {
private String configValue;
public String get() {
return configValue;

@RefreshScope:Spring Cloud The original annotation of , The configuration under the current class can support dynamic update .

@Value: adopt @Value annotation , Read out key by nacos.config The value of the configuration , And pass /config/get Interface to return .

The work for this code part has been completed .


Start the service for testing

(1) Guarantee Nacos-Server Has been launched .

(2) Check Nacos-Server Whether the corresponding configuration file has been added to the configuration list in .

(3) start-up nacos-config project .

(4) After the successful startup, in Nacos You can see our registered services in the console .

(5) Call the interface to test, You can see the return result .


  This indicates that the configuration has been read successfully .

(6) Below I will Nacos-Server The configuration on is modified to hello_lars, See if you can dynamically update . modify Nacos-Server After configuration on , Refresh page , The result is updated to a new configuration .


You can see me through Nacos-server The console for configuration changes , Client services nacos-config Heat update accordingly .

本文为[Platanus acerifolia]所创,转载请带上原文链接,感谢

  1. Java 学生成绩管理系统课程设计,附源码!
  2. Java arbitrary audio to MP3
  3. DNS of docker
  4. Docker - build log monitoring system
  5. SSM + MySQL + Maven + Shiro WMS
  6. Top 10 reasons to fall in love with java!
  7. 一本关于HTTP的恋爱日记
  8. 【RocketMQ源码分析】深入消息存储(3)
  9. SpringCloud+Nacos实现服务配置中心(Hoxton版本)
  10. SCIP:构造数据抽象--数据结构中队列与树的解释
  11. SCIP:构造过程抽象--面向对象的解释
  12. 使用 docker 进行 ElasticSearch + Kibana 集群搭建
  13. Spring IOC 特性有哪些,不会读不懂源码!
  14. [DB Bao 41] use of PMM -- monitoring mysql, PG, mongodb, proxysql, etc
  15. Spring Cloud 升级之路 - 2020.0.x - 3. Undertow 的 accesslog 配置
  16. [DB Bao 42] MySQL high availability architecture MHA + proxysql realizes read-write separation and load balancing
  17. [DataGuard] recovery of physical DG in case of losing archive files in main database (7)
  18. MyBatis(3)Map和模糊查询拓展
  19. 【TTS】AIX-&gt;Linux--基于RMAN(真实环境)
  20. 【TTS】传输表空间AIX-&gt;linux基于rman
  21. 【TTS】传输表空间AIX asm -&gt; linux asm
  22. 【TTS】传输表空间Linux asm -&gt; AIX asm
  23. 【DB宝40】MySQL高可用管理工具Orchestrator简介及测试
  24. 【TTS】传输表空间Linux -&gt;AIX 基于rman
  25. 一本关于HTTP的恋爱日记
  26. 【RocketMQ源码分析】深入消息存储(3)
  27. SpringCloud+Nacos实现服务配置中心(Hoxton版本)
  28. SICP:构造过程抽象--面向对象的解释
  29. 3w 字长文爆肝 Java 基础面试题!太顶了!!!
  30. Spring Cloud 升级之路 - 2020.0.x - 3. Undertow 的 accesslog 配置
  31. win10卸载mysql5.7
  32. MySQL 批量插入,如何不插入重复数据?
  33. k8s cronjob应用示例
  34. 非常规方法,轻松应对Oracle数据库危急异常
  35. Oracle hang 之sqlplus -prelim使用方法
  36. 如何全文搜索oracle官方文档
  37. Java student achievement management system course design, with source code!
  38. win10安装mysql8.0
  39. 手把手教你写一个spring IOC容器
  40. JAVA 中的异常(1)- 基本概念
  41. A love diary about http
  42. navicat连接win10 mysql8.0 报错2059
  43. [rocketmq source code analysis] in depth message storage (3)
  44. Implementation of service configuration center with spring cloud + Nacos (Hoxton version)
  45. SCIP: constructing data abstraction -- Explanation of queue and tree in data structure
  46. SCIP: abstraction of construction process -- object oriented explanation
  47. Using docker to build elasticsearch + kibana cluster
  48. What are the spring IOC features? I can't understand the source code!
  49. Spring cloud upgrade road - 2020.0. X - 3. Accesslog configuration of undertow
  50. 导致Oracle性能抖动的参数提醒
  51. 风险提醒之Oracle RAC高可用失效
  52. 小机上运行Oracle需要注意的进程调度bug
  53. Oracle内存过度消耗风险提醒
  54. Oracle SQL monitor
  55. 使用Bifrost实现Mysql的数据同步
  56. 揭秘Oracle数据库truncate原理
  57. 看了此文,Oracle SQL优化文章不必再看!
  58. Mybatis (3) map and fuzzy query expansion
  59. Kafka性能篇:为何这么“快”?
  60. 两个高频设计类面试题:如何设计HashMap和线程池