In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
SpringCloud-Ribbon configuration is how, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.
1. Disable Eureka
When we add the @ LoadBalanced annotation to RestTemplate, we can call the interface with the service name, and when there are multiple services, we can also do load balancing.
This is because the service information in Eureka has been pulled locally to the client, and if we do not want to integrate with Eureka, we can disable it through the following configuration method.
# disable Eureka
Ribbon.eureka.enabled=false
When we disable Eureka, we can't use the service name to invoke the interface, we have to specify the service address.
two。 Configure interface address list
We mentioned above that you can disable Eureka. After disabling it, you need to manually configure the calling service address as follows:
# manually configure the service address after disabling Eureka
Ribbon-config-demo.ribbon.listOfServers=localhost:8081,localhost:8083
This configuration is for a specific service, and the prefix is the service name. After configuration, you can use the service name to invoke the interface as before.
3. Configure load balancing policy
The default policy for Ribbon is polling, and as you can see from the output of the example we explained earlier, there are a lot of policies available in Ribbon, which will be explained later. Through configuration, we can specify which policy the service uses for load operations.
4. Timeout time
There are two time-related settings in Ribbon: the timeout for request connection and the timeout for request processing. The setting rules are as follows:
# request connection timeout ribbon.ConnectTimeout=2000# request processing timeout ribbon.ReadTimeout=5000 can also set a different timeout for each Ribbon client, specified by the service name: ribbon-config-demo.ribbon.ConnectTimeout=2000ribbon-config-demo.ribbon.ReadTimeout=50005. Concurrent parameter # maximum connections ribbon.MaxTotalConnections=500# maximum connections per host ribbon.MaxConnectionsPerHost=500 code configuration Ribbon
The easiest way to configure Ribbon is through a configuration file. Of course, we can also configure it through code.
To configure the previously customized load policy through code, you first need to create a configuration class and initialize the custom policy, as shown below.
@ Configurationpublic class BeanConfiguration {@ Beanpublic MyRule rule () {return new MyRule ();}}
Create a configuration class for the Ribbon client, associate BeanConfiguration, and use name to specify the name of the service to be invoked, as shown below.
@ RibbonClient (name = "ribbon-config-demo", configuration = BeanConfiguration.class) public class RibbonClientConfig {}
You can remove the policy configuration from the previous configuration file, then restart the service, and the access interface will see the same effect as before.
Configure Ribbon by configuration file
In addition to using code to configure Ribbon, we can also specify the corresponding configuration for Ribbon through the configuration file:
.ribbon.NFLoadBalancerClassName: Should implement ILoadBalancer (load balancer API)
.ribbon.NFLoadBalancerRuleClassName: Should implement IRule (load balancing algorithm)
.ribbon.NFLoadBalancerPingClassName: Should implement IPing (Service availability check)
.ribbon.NIWSServerListClassName: Should implement ServerList (Service list acquisition)
.ribbon.NIWSServerListFilterClassName: Should implement ServerList Filter (filtering of service lists)
Retry mechanism
In a cluster environment, if multiple nodes are used to provide services, it is inevitable that a node will fail. When using Nginx for load balancer, if your application is stateless and can be published scrolling, that is, you need to restart the application one by one, the impact on users is actually relatively small, because Nginx will re-forward the request to another instance after it fails to forward the request.
Because Eureka is built on the principle of AP, at the expense of data consistency, each Eureka service will save the registered service information. When the heartbeat between the registered client and the Eureka cannot be maintained, it may be due to the network or the service may be down.
In this case, the registration information will also be saved in Eureka for a period of time. At this time, the client may get the service information that has been hung up, so Ribbon may get the service information that has expired, which will lead to a failed request.
This kind of problem can be avoided by using the retry mechanism. The retry mechanism is that when Ribbon finds that the requested service is unreachable, it requests another service again.
1. RetryRule retry
The easiest way to solve the above problem is to use the retry policy included in Ribbon to retry. In this case, you only need to specify the load policy of a service as the retry policy:
Ribbon-config-demo.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RetryRule2. Spring Retry retry
In addition to using the retry strategy that comes with Ribbon, we can also integrate Spring Retry for retry operations.
Add the dependency of Spring Retry to pom.xml, as shown below.
Org.springframework.retryspring-retry
Configure information such as the number of retries:
# the number of retries of the current instance, ribbon.maxAutoRetries=1#, the number of retries of switching instances, ribbon.maxAutoRetriesNextServer=3# retries all operation requests, ribbon.okToRetryOnAllOperations=true# retries Http response codes, ribbon.retryableStatusCodes=500404502: is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
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.