In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you how Spring Cloud Alibaba uses the nacos registry, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Spring Cloud Alibaba uses nacos registry background
In the first introduction to nacos, it was mentioned that nacos is both a registry and a configuration center. In the previous article, we introduced the use of Spring Cloud Alibaba's nacos registry. We talked about the use of configuration centers.
Nacos configuration Center
The previous one described the configuration of the configuration file. This time we just need to add the maven nacos configuration center dependency to the previous code, which is configured into the spring cloud alibaba tutorial total pom version control pom file. This still takes advantage of maven's own jar package dependencies. Discovery-server and cloud-discovery-client-common modules automatically introduce the dependency of nacos configuration center.
Com.alibaba.cloud spring-cloud-starter-alibaba-nacos-config
Modify the corresponding yml file to add
Server side yaml configuration file
Spring: cloud: config: server-addr: 47.99.209.72:8848 file-extension: yaml
Finally, the results are presented respectively.
Server: port: 9012spring: profiles: active: dev application: name: cloud-discovery-server cloud: nacos: config: server-addr: 47.99.209.72 file-extension 8848 # specify the file suffix file-extension: yaml discovery: server-addr: 47.99.209.72
Modify http interface
Package com.xian.cloud.controller;import lombok.extern.slf4j.Slf4j;import org.springframework.beans.factory.annotation.Value;import org.springframework.cloud.context.config.annotation.RefreshScope;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController / * * @ Author: xlr * @ Date: Created in 2:57 PM 2019-10-27 * / @ RestController@RequestMapping ("server") @ Slf4j# provides distributed configuration dynamic refresh @ RefreshScopepublic class DiscoverCotroller {@ Value ("${nacos.yaml.age}") private String age / * * external service HTTP interface * @ param name * @ return * / @ GetMapping ("/ hello") public String hello (@ RequestParam String name) {log.info ("invoked name =" + name+ "age =" + age); return "hello" + name+ "age =" + age;}}
Then click to log in to the nacos configuration Center to create a profile in the configuration Center.
Create cloud-discovery-server-dev.yaml configuration nacos.yaml.age= 30
Start the service to access curl http://localhost:9012/server/hello?name=tom
Log printing
It means that the nacos configuration center has come into effect, and then we modify the cloud-discovery-server-dev.yaml configuration file age to reissue it with 20 parameters.
You will see the background log print.
2019-10-27 19 org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type 53 INFO 08.884 INFO 44618-[.99.209.72 _ 8848] trationDelegate$BeanPostProcessorChecker: Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$87d25f89] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) 2019-10-27 1953 org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type 09.091 INFO 44618-[.99.209.72 _ 8848] c.a.c.n.c.NacosPropertySourceBuilder: Loading nacos data DataId: 'cloud-discovery-server-dev.yaml', group:' DEFAULT_GROUP'2019-10-27 19 INFO 53 INFO 44618-[.99.209.72 _ 8848] b.c.PropertySourceBootstrapConfiguration: Located property source: CompositePropertySource {name='NACOS', propertySources= [NacosPropertySource {name='cloud-discovery-server-dev.yaml'} NacosPropertySource {name='cloud-discovery-server.yaml'}]} 2019-10-27 19INFO 53 INFO 44618-[.99.209.72 _ 8848] o.s.boot.SpringApplication: The following profiles are active: dev2019-10-27 19INFO 53 The following profiles are active 09.103 INFO 44618-[.99.209.72 _ 8848] o.s.boot.SpringApplication: Started application in 0.277 seconds (JVM running for 883. 2) 2019-10-27 19 Refresh keys changed 53 INFO 44618-[.99.209.72 _ 8848] o.s.c.e.event.RefreshEventListener: Refresh keys changed: [nacos.yaml.age]
Do not restart the service, request again. Age has sent changes
This is the configuration of the configuration center of nacos.
Then let's talk about the matching rule dataID configured by nacos.
In Nacos Config Starter, the stitching format of dataId (that is, cloud-discovery-server-dev.yaml above) is as follows
${prefix}-${spring.profiles.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.profiles.active is the profile corresponding to the current environment. For more information, please see Spring Boot documentation.
Note that when activeprofile is empty, the corresponding connector-will not exist, and the splicing format of dataId will become ${prefix}. ${file-extension}
File-extension 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 types are supported.
Group
Group defaults to DEFAULT_GROUP and can be configured through spring.cloud.nacos.config.group.
Automatic injection
Nacos Config Starter implements the org.springframework.cloud.bootstrap.config.PropertySourceLocator interface and sets the priority to the highest.
During the startup phase of the Spring Cloud application, the corresponding data is actively obtained from the Nacos Server side, and the obtained data is converted into PropertySource and injected into the PropertySources attribute of Environment, so the @ Value annotation can also be used to directly obtain the configuration content of the Nacos Server side.
Dynamic refresh
By default, Nacos Config Starter adds a listening feature for all configuration items of Nacos that have successfully obtained data. When listening for a change in server configuration, the refresh method of org.springframework.cloud.context.refresh.ContextRefresher will be triggered in real time.
If you need to dynamically refresh the Bean, refer to the Spring and Spring Cloud specifications. It is recommended to add @ RefreshScope or @ ConfigurationProperties annotations to the class
These are all the contents of the nacos configuration center.
Supplementary namespace
Both the registry of nacos and the configuration center of nacos have a namespace attribute. This attribute is for our nacos console namespace.
In particular, the concept of namespaces because configuration centers and registries are common concepts. So put it in the configuration center and explain it here.
We create a namespace for lms in the console
There will be a namespace ID e071c3ab-b280-4ae7-a081-044fff5613ad. We put this ID in the configuration file corresponding to the namespace attribute configuration modification. If you do not modify the default public space,
Server: port: 9013spring: profiles: active: dev application: name: cloud-discovery-server cloud: nacos: config: server-addr: 47.99.209.72 file-extension 8848 file-extension: yaml namespace: e071c3ab-b280-4ae7-a081-044fff5613ad discovery: server-addr: 47.99.209.72 file-extension 8848 namespace: e071c3ab-b280-4ae7-a081-044fff5613ad
If you restart, you will find that the service is registered with the namespace of lms.
Introspection
DataID and group are used in combination. ${prefix}-${spring.profiles.active}. ${file-extension}. Among them, the change of active parameters can help us to achieve environmental isolation. The change of group. It can also help us to distinguish between project team and version. In this way, the configuration will not randomly match the changes of the two parameters to achieve the difference between the dynamic changes we want. In fact, the design of this piece meets the needs and support of most of our scenarios.
Add the concept of namespaces. We have one more situation to change and choose. However, there are so many that when our cluster environment is big enough, many students will not be able to find a clue. This requires us to make an agreement in advance.
The above is all the contents of the article "how Spring Cloud Alibaba uses the nacos Registry". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, 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.