In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you how to use nacos as a registry and configuration center in springcloud. The content is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
A configuration center
Add dependencies:
Org.springframework.cloud spring-cloud-starter-alibaba-nacos-config ${latest.version}
Note: version 0.2.x.RELEASE corresponds to Spring Boot 2.x, and version 0.1.x.RELEASE corresponds to Spring Boot 1.x.
More version correspondence reference: version Notes Wiki
Configure the address and application name of Nacos server in bootstrap.properties
Spring.cloud.nacos.config.server-addr=127.0.0.1:8848spring.application.name=example
Description: you need to configure spring.application.name because it forms part of the Nacos configuration management dataId field.
In Nacos Spring Cloud, the full format of dataId is as follows:
${prefix}-${spring.profile.active}. ${file-extension}
Prefix defaults to the value of spring.application.name, which can also be configured through the configuration item spring.cloud.nacos.config.prefix.
Spring.profile.active is the profile corresponding to the current environment. For more information, please see the Spring Boot documentation. Note: when spring.profile.active is empty, the corresponding connector-will not exist, and the splicing format of dataId will become ${prefix}. ${file-extension}
File-exetension is the data format of the configuration content, which can be configured through the configuration item spring.cloud.nacos.config.file-extension. Currently, only properties and yaml types are supported.
Automatically update the configuration through the Spring Cloud native annotation @ RefreshScope:
@ RestController@RequestMapping ("/ config") @ RefreshScopepublic class ConfigController {@ Value ("${useLocalCache:false}") private boolean useLocalCache; @ RequestMapping ("/ get") public boolean get () {return useLocalCache;}}
First, publish the configuration to Nacos Server by calling Nacos Open API: dataId is example.properties, content is useLocalCache=true
Curl-X POST "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=example.properties&group=DEFAULT_GROUP&content=useLocalCache=true"
Run NacosConfigApplication, call curl http://localhost:8080/config/get, and return true.
Call Nacos Open API to publish the configuration to Nacos server again: dataId is example.properties, content is useLocalCache=false
Curl-X POST "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=example.properties&group=DEFAULT_GROUP&content=useLocalCache=false"
When you visit http://localhost:8080/config/get again, the return content is false, indicating that the useLocalCachevalue in the program has been dynamically updated.
Note: where @ Value annotations are used, @ RefreshScope must be used to implement configuration automatic updates. Automatic updates can be achieved with @ ConfigurationProperties.
Second registration center
Add dependencies:
Org.springframework.cloud spring-cloud-starter-alibaba-nacos-discovery ${latest.version}
Note: version 0.2.x.RELEASE corresponds to Spring Boot 2.x, and version 0.1.x.RELEASE corresponds to Spring Boot 1.x.
More version correspondence reference: version Notes Wiki
Configure the service provider so that the service provider can register its services with the Nacos server through Nacos's service registration discovery feature.
i. Configure the address of Nacos server in application.properties:
Server.port=8070spring.application.name=service-providerspring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
ii. Enable the service registration discovery feature through Spring Cloud native annotation @ EnableDiscoveryClient:
@ SpringBootApplication@EnableDiscoveryClientpublic class NacosProviderApplication {public static void main (String [] args) {SpringApplication.run (NacosProviderApplication.class, args);} @ RestController class EchoController {@ RequestMapping (value = "/ echo/ {string}", method = RequestMethod.GET) public String echo (@ PathVariable String string) {return "Hello Nacos Discovery" + string }}}
Configure the service consumer so that the service consumer can obtain the service it wants to invoke from the Nacos server through Nacos's service registration discovery feature.
i. Configure the address of Nacos server in application.properties:
Server.port=8080spring.application.name=service-consumerspring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
ii. Enable the service registration discovery feature through the Spring Cloud native annotation @ EnableDiscoveryClient. Add @ LoadBalanced annotation to the RestTemplate instance to enable the integration of @ LoadBalanced and Ribbon:
@ SpringBootApplication@EnableDiscoveryClientpublic class NacosConsumerApplication {@ LoadBalanced @ Bean public RestTemplate restTemplate () {return new RestTemplate ();} public static void main (String [] args) {SpringApplication.run (NacosConsumerApplication.class, args);} @ RestController public class TestController {private final RestTemplate restTemplate; @ Autowired public TestController (RestTemplate restTemplate) {this.restTemplate = restTemplate } @ RequestMapping (value = "/ echo/ {str}", method = RequestMethod.GET) public String echo (@ PathVariable String str) {return restTemplate.getForObject ("http://service-provider/echo/" + str, String.class);}
Start ProviderApplication and ConsumerApplication, call http://localhost:8080/echo/2018, and return Hello Nacos Discovery 2018.
The above is how to use nacos as a registry and configuration center in springcloud. 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.