In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains how to configure Spring Cloud Config client. Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn "How to configure Spring Cloud Config client"!
Service configuration center
In the previous articles about Spring Cloud Config, we configure config-server address in config-client directly when the address is written dead, this way is obviously not flexible enough, some small partners may have thought of, here we can combine eureka registry, and then directly use the service name when configuring, OK, then we slightly modify the previous project.
config-server modification
The transformation here is very simple, the server transformation and client transformation are divided into three steps: 1. Add dependencies;2. Add comments;3. Modify application.properties.
First we add the following dependencies to config-server:
org.springframework.cloud spring-cloud-starter-eureka
Then add the @EnableDiscoveryClient annotation to the config-server entry class, indicating that this is an Eureka client, as follows:
@SpringBootApplication@EnableConfigServer@EnableDiscoveryClientpublic class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); }}
Finally, configure the eureka registry address in application.properties:
eureka.client.service-url.defaultZone=http://localhost:1111/eureka/
At this point, our config-server configuration is successful.
config-client modification
The first step of config-client transformation is to add dependencies first, as follows:
org.springframework.cloud spring-cloud-starter-eureka
Then add the @EnableDiscoveryClient annotation to the entry class, as follows:
@SpringBootApplication@EnableDiscoveryClientpublic class ConfigClientApplication { public static void main(String[] args) { SpringApplication.run(ConfigClientApplication.class, args); }}
Finally modify the configuration file as follows:
spring.application.name =app# dev Modify spring.cloud.config.profile=devspring.cloud.config.label= masterureka.client.service-url.defaultZone=http://localhost:1111/eureka/spring.cloud.config.discovery.enabled=truespring.cloud. config.discovery.service-id=config-serverserver.port=2008
I say three things about this profile:
1.eureka.client.service-url.defaultZone Set the address of the registry, register config-client in the eureka registry
2.spring.cloud.config.discovery.enabled means to access config-server by service name
3.spring.cloud.config.discovery.service-id=config-server indicates the service name of config-server
test
OK, after the above transformation, at this time we start the eureka service registry, config-server, config-client, and then visit http://localhost:1111, you can see that the two applications have been successfully registered:
Then continue testing config-client's/sang interface, and the result is as follows:
No problem.
All right, the service configuration center was successfully built.
Rapid response to failure
Without any additional configuration, the failure response is a bit slow. For a simple example, turn off config-server and start config-client directly. At this time, the startup will report an error, but the error time is late. When the error is reported, the system has printed many startup logs. If we want to respond quickly when the startup fails, the way is very simple. Add the following configuration to config-client:
spring.cloud.config.fail-fast=true
At this point do not start config-server directly start config-client will still report error, but we see that the error time is earlier, the system did not print a few startup logs.
retry mechanism
If config-client fails to access config-server at startup due to network jitter and other reasons, it is obviously not cost-effective. In this case, we hope config-client can retry several times. The retry mechanism is also supported here. The way to add retry mechanism is very simple. Introduce the following two dependencies:
org.springframework.retry spring-retry org.springframework.boot spring-boot-starter-aop
The introduction of dependencies is OK, without any additional configuration (of course, make sure that the failure fast response is enabled), at this point we try to start config-client directly without starting config-server, and the startup log is as follows:
We see that config-client tried to access config-server six times and failed six times before throwing an exception.
There are four configurations associated with retry mechanisms:
#Configure retry times, default 6spring.cloud.config.retry.max-attempts=6#interval multiplier, default 1.1spring.cloud.config.retry. multiplier =1.1#initial retry interval, default 1000msspring.cloud.config.retry.initial-interval=1000#maximum interval, default 2000msspring.cloud.config.retry.max-interval=2000 Dynamic refresh configuration
Sometimes, I dynamically update the configuration file in the Git repository, so how do I make my config-client aware of it in time? The method is very simple. First, add the following dependencies to config-client:
org.springframework.boot spring-boot-starter-actuator
This dependency contains an implementation of the/refresh endpoint that we will use to refresh configuration information. Then you need to configure Ignore Permission Interception in application.properties:
management.security.enabled=false
OK, after configuration, start eureka registry, config-server and config-client, visit http://localhost:2008/sang, the result is as follows:
At this point, I use the Git client tool to modify the contents of app-dev.properties. After the modification is successful, I first use POST request to access http://localhost:2008/refresh address. The result is as follows:
Then visit http://localhost:2008/sang again, and the result is as follows:
We see that the profile has been updated.
At this point, I believe everyone has a deeper understanding of "Spring Cloud Config client how to configure," may wish to actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!
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.