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 .

 

Prerequisite
First, download and install a nacos server, Then start nacos server. Download and install process view https://www.cnblogs.com/ying-z/p/14630738.html
 
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 http://127.0.0.1:8848/nacos/index.html.
Then go to the configuration list , Click the new configuration button , Enter the new configuration page .

 

 

 

 

  Be careful :

Here I create Data Id by nacos-config.properties 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 nacos-config.properties, The content is nacos.config=hello_nacos

curl -X POST "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=nacos-config.properties&group=DEFAULT_GROUP&content=nacos.config=hello_nacos"

 

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 .

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencyManagement>
<dependencies>
<!-- Statement Spring cloud The version is Hoxton.SR3-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- Statement Spring cloud alibaba Version is 2.1.0.RELEASE -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.1.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- Statement nacos-discovery rely on -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- Statement nacos-config rely on -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
</dependencies>

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 application.properties Add related configuration

server.port=9002
spring.application.name=nacos-config
spring.application.cloud.nacos.discovery.server-addr=127.0.0.1:8848

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

Be careful : The file name is bootstrap.xxx, 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
spring.application.name=nacos-config
# 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 .
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
# Specify the address of the configuration center .
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
# Specify the format of the configuration file in the configuration center .
#spring.cloud.nacos.config.file-extension=properties

Why configuration is needed spring.application.name, 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}-${spring.profile.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.profile.active: That is, the current environment corresponds to profile, For details, please refer to  Spring Boot file . You can use the configuration item spring.profile.active To configure the . When spring.profile.active 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 spring.cloud.nacos.config.file-extension To configure the . Currently only supported properties and yaml type .

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

@RefreshScope
@RestController
public class ConfigController {
@Value("${nacos.config}")
private String configValue;
@RequestMapping("/config/get")
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 http://127.0.0.1:9002/config/get, 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 http://127.0.0.1:9002/config/get 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]所创,转载请带上原文链接,感谢
https://javamana.com/2021/04/20210408104918535Q.html

  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和线程池