In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article shows you how to use spring cloud to integrate nacos configuration center, the content is concise and easy to understand, can definitely brighten your eyes, through the detailed introduction of this article, I hope you can get something.
Spring cloud integrated nacos configuration center 1, add dependency Finchley.RELEASE 0.9.0.RELEASE org.springframework.cloud spring-cloud-starter-alibaba-nacos-config ${nacos.version} 2, add bootstrap.yml configuration file
(* application.yml cannot be read by nacos configuration center)
Spring: profiles: active: dev cloud: nacos: config: server-addr: 127.0.0.1 dev cloud 8848 # configuration Center-specify namespace namespace: babc3933-84ef-43c8-8171-af2ce2fbf6e4 # specify configuration file suffix file-extension: yaml # configuration group group: DEFAULT_ group three, add remote configuration
1. Use an existing namespace or add a namespace
2. Namespace: ID of the namespace
3. The new configuration DateId rule is as follows:
${prefix}-${spring.profile.active}. ${file-extension}
* pay attention to the file suffix
4. Configure format Select yaml
5. Write the configuration in the configuration content
Nacos as the configuration center of SpringCloud I. background introduction
In the dynamic configuration of distributed system, repeated restart of service and dynamic change of service parameters can be avoided. One word is very important. That's what another article said, . As a distributed scheduling system recommended by Spring, Nacos also has the function of configuration center, and we can also use it as the configuration center. The client side initiates the synchronization mechanism with the configuration center actively and regularly to realize the update of dynamic configuration.
Environmental dependence:
Name value: JDK1.8Consulting 1.5.2SpringCloudGreenwich.SR1Nacos1.1.0II, project practice
1) pom dependency (primary)
1.8 Greenwich.SR1 0.9.0.RELEASE org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} Pom import org.springframework.cloud spring-cloud-alibaba-dependencies ${spring-cloud-alibaba.version} pom Import org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-consul-discovery org.springframework.cloud spring-cloud-starter-alibaba-nacos-config
2) configuration file
Application.properties#0 indicates the server random port server.port=8090# the default value of the kv in this demonstration (the boss will give you 0 yuan by default) company.pay.money=0
Bootstrap.properties
# Service name spring.application.name=waiter-service#consul address spring.cloud.consul.host=localhost#consul port spring.cloud.consul.port=8500spring.cloud.consul.discovery.prefer-ip-address=true#nacos config server address spring.cloud.nacos.config.server-addr=127.0.0.1:8848#nacos configuration central function, default truespring.cloud.nacos.config.enabled=true#nacos config server dynamic configuration file format spring.cloud.nacos.config.file-extension=yaml
3) dynamic parameter receiving class
@ ConfigurationProperties ("company.pay") @ RefreshScope@Data@Componentpublic class PayMoneyProperties {/ / key ending, separated by the decimal point Integer money;}
Note:
ConfigurationProperties indicates that this class is associated with dynamic configuration, and "company.pay" represents the prefix part of the key.
@ RefreshScope means to dynamically refresh the config server value
@ Component means to load the class into the IOC container
In the actual combat, we try to obtain the dynamic by the way of @ Value. We can only obtain the dynamic config server value after the service restart.
4) external interface (for visual verification)
Method 1:
@ RestController@RequestMapping ("nacos) public class NacosConfigController {@ Autowired private PayMoneyProperties payMoneyProperties; @ RequestMapping (" / pay/money ") public Object getConfig (HttpRequest request) {String money =" the project was launched smoothly and the boss began to give out bonuses: "; return money + payMoneyProperties.getMoney ();}}
Method 2:
@ RestController@RequestMapping ("nacos") / / enable dynamic configuration refresh @ RefreshScopepublic class NacosConfigController {/ / get the configuration value @ Value ("${company.pay.money}") private String moneyConfig; @ RequestMapping ("/ pay/money") public Object getZkConfig (HttpRequest request) {String money = "the project is launched smoothly and the boss begins to pay bonuses:"; return money + moneyConfig;}}
5) start the project
The above figure can see the connection information of config server through the log. By default, pull the file whose project name. Yaml is Data ID on nacos.
6) demonstration when nacos config server has not set the corresponding node value (the local profile value is obtained)
Note: Spring boot is loading configuration order: local configuration file-- > Config Server-- > application.
7) create a data node in nacos
Request address: http://localhost:8848/nacos
Create data: Data ID:waiter-service.yaml
Note: in YAML data, the data hierarchy is indicated by spaces and ":". Before setting this value, you can verify the validity of YAML content on the Internet.
8) verify that the dynamic configuration has been received in the project
As shown in the following figure, the value of the project update has been notified
Request the corresponding interface in the verification interface, and find that the value is the same as the value dynamically set in nacos config server.
Summary
1) compared with consul and zookeeper, nacos as a registry has a more friendly web page, which supports functions such as listening node information query, configuration history version query, modification and comparison, etc.
2) client pulls the nacos config server value regularly and compares it with the local value
The above is how to use spring cloud to integrate nacos configuration center. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.