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--
How to use Nacos as the configuration center, I believe that many inexperienced people do not know what to do. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
We have learned how to use Nacos to register and discover services. At the same time, it also introduces several different coding styles of service consumption that we can use in Spring Cloud. Next, let's take a look at another important capability of Nacos: configuration management.
Brief introduction
Nacos not only implements the registration discovery of services, but also integrates the configuration center functions. Through the configuration management function of Nacos, we can centralize and store all the configurations in the entire architecture in Nacos. The benefits of this are also mentioned in the introduction to Spring Cloud Config in previous tutorials, mainly on the following points:
Separate multi-environment configuration, more flexible management rights and higher security
The packaging of the application is more pure, so that it can be packaged once and run in multiple places.
Nacos's configuration management model is similar to Taobao's open source configuration center Diamond. At the basic level, configuration content is located through DataId and Group. In addition, many other management functions are added.
Getting started
Let's use a simple example to show how to create configuration content in Nacos and how to load Nacos configuration information in a Spring Cloud application.
Create configuration
Step 1: go to the control page of Nacos. On the configuration list function page, click the "+" button in the upper right corner to enter the "New configuration" page, as shown in the following figure:
Where:
Data ID: enter alibaba-nacos-config-client.properties
Group: do not modify, use the default value DEFAULT_GROUP
Configuration format: select Properties
Configuration content: apply the configuration content to be loaded. Here is just an example to make a simple configuration, such as didispace.title=spring-cloud-alibaba-learning
Create an application
Step 1: create a Spring Boot application that can be named: alibaba-nacos-config-client.
Step 2: edit the pom.xml and add the necessary dependency configuration, such as:
Org.springframework.boot spring-boot-starter-parent 2.0.5.RELEASE org.springframework.cloud spring-cloud-dependencies Finchley.SR1 pom import org.springframework.cloud spring-cloud-alibaba-dependencies 0.2.2.RELEASE pom Import org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-alibaba-nacos-config org.projectlombok lombok 1.18.2 true
The above contents are mainly divided into three parts:
Parent: defines the version of spring boot
The version of dependencyManagement:spring cloud and the version of spring cloud alibaba. Since spring cloud alibaba has not been included in the main version management of spring cloud, you need to join it yourself.
Dependencies: dependent content to be used by the current application. Here is a new configuration client module of Nacos: spring-cloud-starter-alibaba-nacos-config. Since a version has been introduced in dependencyManagement, there is no need to specify a specific version here.
As you can see, there is no nacos service discovery module in this example, so the two contents can be used independently.
Step 3: create the application main class and implement a HTTP interface:
@ SpringBootApplicationpublic class TestApplication {public static void main (String [] args) {SpringApplication.run (TestApplication.class, args); @ Slf4j @ RestController @ RefreshScope static class TestController {@ Value ("${didispace.title:}") private String title; @ GetMapping ("/ test") public String hello () {return title;}
The content is very simple. @ SpringBootApplication is defined as a SpringBoot application, and a Controller is also defined, in which the configuration with key as didispace.title (empty string by default) is injected through the @ Value annotation. This configuration will be returned through the / test interface. Later, we will use this API to verify the load configured in Nacos. In addition, there is an important note @ RefreshScope, which is mainly used to enable the configuration content under this class to support dynamic refresh, that is, when our application is launched and the configuration content in Nacos is modified, it will take effect immediately.
Step 4: create a profile bootstrap.properties and configure the service name and Nacos address
Spring.application.name=alibaba-nacos-config-clientserver.port=8001spring.cloud.nacos.config.server-addr=127.0.0.1:8848
Note: bootstrap.properties must be used here. At the same time, the spring.application.name value must match the configuration Data Id created in the previous stage Nacos (except for the .properties or .yaml suffixes).
Step 5: start the application created above.
At startup, we can see the log information similar to the above, which will output the dataId and group for the application to get the configuration from the Nacos. If, after startup, you find that the configuration information is not available, you can start here to see if the configuration is loaded correctly.
Step 6: verify configuration acquisition and verify dynamic refresh
Use tools such as curl or postman to access the API: localhost:8001/test. If everything is normal, you will return the spring-cloud-alibaba-learning configured in Nacos. Then, through the Nacos page, modify the content, click publish, and then visit the interface, you can see that the returned result has changed.
After reading the above, have you mastered how to use Nacos as the configuration center? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
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.