In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
Today, I would like to share with you the relevant knowledge points about how to use SpringCloud Commons public abstract methods. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look.
Spring Cloud Commons public abstraction
Spring Cloud encapsulates common models such as service discovery, load balancing and circuit breakers in a common abstraction that can be used by all Spring Cloud clients without relying on specific implementations (for example, service discovery has different implementations such as Eureka and Consul). These common abstractions are located in the Spring Cloud Commons project.
@ EnableDiscoveryClient
Commons provides @ EnableDiscoveryClient annotations. This is done through META-INF/spring.factories to find the implementation of the DiscoveryClient interface. The implementation of DiscoveryClient will add a configuration class to the spring.factories under the org.springframework.cloud.client.discovery.EnableDiscoveryClient key. Examples of DiscoveryClient implementations are Spring Cloud Netflix Eureka,Spring Cloud Consul discovery and Spring Cloud Zookeeper discovery.
By default, the implementation of DiscoveryClient automatically registers the local Spring Boot server with the remote discovery server. You can disable this feature by setting autoRegister=false in @ EnableDiscoveryClient.
Service Registration ServiceRegistry
Commons now provides a ServiceRegistry interface that provides methods such as register (Registration) and deregister (Registration) that allow you to provide custom registration services. Registration is a tagging interface.
@ Configuration@EnableDiscoveryClient (autoRegister=false) public class MyConfiguration {private ServiceRegistry registry; public MyConfiguration (ServiceRegistry registry) {this.registry = registry;} / / called via some external process, such as an event or a custom actuator endpoint public void register () {Registration registration = constructRegistration (); this.registry.register (registration);}}
Each ServiceRegistry implementation has its own Registry implementation.
Load balancing of RestTemplate
When creating a RestTemplate instance, you can use the @ LoadBalanced annotation to automatically configure RestTemplate to use load balancer. @ LoadBalanced will use Ribbon to execute the load balancing policy for RestTemplate.
The RestTemplate for creating a load balancer can no longer be created through automatic configuration, but must be created through the configuration class. The specific example is as follows:
@ Configurationpublic class MyConfiguration {@ LoadBalanced@BeanRestTemplate restTemplate () {return new RestTemplate ():}} public class MyApplication {@ Autowiredprivate RestTemplate restTemplate; public string getMyApplicationName () {/ / use restTemplate to access the / name interface of the my- Application microservice string name = restTemplate.getFor0bject ("http://my-application/name",string.class); return name;}})
URI needs to use the service name to specify that the application service needs to be accessed. The Ribbon client will obtain the specific service address from the service discovery application through the service name to create a complete network address to realize the network call.
Failed retry of RestTemplate
The RestTemplate of load balancer can add a failure retry mechanism. By default, the failed retry mechanism is turned off and enabled by adding Spring Retry to the classpath of the application. You can also set the
Spring.cloud.loadbalancer.retry.enabled=false prohibits the retry logic of Spring retry in the classpath.
If you want to add one or more RetryListener to the retry request, you can create a Bean of type LoadBalancedRetryListenerFactory that returns a list of RetryListener that will be used for the retry mechanism, as shown in the following code:
@ Configurationpublic class RryListenerConfiguration {@ BeanLoadBalancedRetryListenerFactory retryListenerFactory ({return new LoadBalancedRetryListenerFactoryO {@ overridepublic RetryListener [] createRetryListeners (String service) return new RetryListener [] {new RetryListener ({@ Override// work before retry) {return true;} / / work after retry @ Overridepublic void close (RetryContext context,RetryCallbackcallback,Throwable throwable) {} / / work after retry error @ OverridepublicT,E extends Throwable > void onError (RetryContext context,RetryCal1backcallback,Throwable throwable) {}};};}
The @ Bean method to generate the LoadBalancedRetryListenerFactory instance is defined in the custom configuration class, and the createRetryListeners method of the factory class generates an RetryListener instance for retry of the network request.
These are all the contents of the article "how to use SpringCloud Commons Public Abstract methods". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to 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.