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 > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article shows you how SpringCloud uses Zuul to achieve fault-tolerant fallback function, the content is concise and easy to understand, absolutely can make your eyes bright, through the detailed introduction of this article, I hope you can get something.
The main function of Zuul is forwarding. In the forwarding process, we cannot guarantee that the forwarded service is available. At this time, we need fault-tolerant mechanism and fallback mechanism.
Fault-tolerant mechanism
Fault tolerance, in short, means that when a service is not available, you can switch to other available services, that is, you need a retry mechanism. Turning on the retry mechanism in Zuul depends on spring-retry.
First add the dependency of spring-retry to pom.xml, as shown below.
Org.springframework.retryspring-retry
Enable the retry mechanism in the properties file and configure the number of retries:
Zuul.retryable=trueribbon.connectTimeout=500ribbon.readTimeout=5000ribbon.maxAutoRetries=1ribbon.maxAutoRetriesNextServer=3ribbon.okToRetryOnAllOperations=trueribbon.retryableStatusCodes=500404502
Where:
Zuul.retryable: enable retry.
Ribbon.connectTimeout: the timeout (ms) of the request connection.
Ribbon.readTimeout: the timeout for request processing (ms).
Ribbon.maxAutoRetries: the number of retries on the current instance.
Ribbon.maxAutoRetriesNextServer: the maximum number of retries for switching instances.
Ribbon.okToRetryOnAllOperations: all operation requests are retried.
Ribbon.retryableStatusCodes: retries the specified Http response code.
Two hystrix-feign-demo services can be started, and the default Ribbon forwarding rule is polling, and then we stop a hystrix-feign-demo service. Before the trial mechanism is aggravated, when you request an interface, it must be forwarded to the service that has been stopped, and the abnormal message is returned.
When we add the retry mechanism, you can cycle through the request API. No exception will be returned at this time, because Ribbon will retry according to the retry configuration. When the request fails, it will reforward the request to the available service.
Fallback mechanism
In Spring Cloud, Zuul integrates Hystrix by default. When an exception occurs in the backend service, you can add a fallback feature for Zuul and return the default data to the client.
To implement the fallback mechanism, you need to implement the ZuulFallbackProvider interface, as shown below.
@ Componentpublic class ServiceConsumerFallbackProvider implements ZuulFallbackProvider {private Logger log = LoggerFactory.getLogger (ServiceConsumerFallbackProvider.class); @ Overridepublic String getRoute () {return "*";} @ Overridepublic ClientHttpResponse fallbackResponse (String route, Throwable cause) {return new ClientHttpResponse () {@ Overridepublic HttpStatus getStatusCode () throws IOException {return HttpStatus.OK;} @ Overridepublic int getRawStatusCode () throws IOException {return this.getStatusCode (). Value ();} @ Overridepublic String getStatusText () throws IOException {return this.getStatusCode (). GetReasonPhrase () } @ Overridepublic void close () {} @ Overridepublic InputStream getBody () throws IOException {if (cause! = null) {log.error (", cause.getCause ());} RequestContext ctx = RequestContext.getCurrentContext (); ResponseData data = ResponseData.fail (" server internal error ", ResponseCode.SERVER_ERROR_CODE.getCode ()) Return new ByteArrayInputStream (JsonUtils.toJson (data). GetBytes ()); @ Overridepublic HttpHeaders getHeaders () {HttpHeaders headers = new HttpHeaders (); MediaType mt = new MediaType ("application", "json", Charset.forName ("UTF-8")); headers.setContentType (mt); return headers;}};}}
The return * in the getRoute method indicates a fallback operation for all services. If you only want to fallback to a service, return the name of the service that needs to be fallback, which must be the name registered in the Eureka.
The content of fallback is constructed through ClientHttpResponse. The status code of the response is returned through getStatusCode. The text corresponding to the response status code is returned through getStatusText. The fallback content is returned through getBody. The request header information of the response is returned through getHeaders.
Access the hystrix-feign-demo service through the API gateway, stop the hystrix-feign-demo service, and then access it again, and you can see the fallback content, as shown in figure 1.
Zuul High availability
We register all business-related services in Eureka and carry out load balancing through Ribbon. Services can be horizontally expanded to achieve high availability.
In practical use, the API gateway layer is often for APP, Webapp, and customers to call the API. It will not be highly available if we register Zuul with Eureka, because it is impossible for your customers to operate your registry.
At this point, the best way is to use additional load balancers to achieve the high availability of Zuul, such as our most commonly used Nginx, or HAProxy, F5, etc.
This method is also the most commonly used load mode for a single project. When a user requests an address, it is forwarded through Nginx, and when a service dies, Nginx will rule it out.
If you want the API gateway to scale horizontally at any time, we can use scripts to dynamically modify the configuration of Nginx, operate Eureka through scripts, find new gateway services or offline gateway services, directly modify the upstream of Nginx, and then achieve the dynamic expansion of the gateway by reload configuration.
If you do not use the script combined with the registry to do it, you can only plan N nodes in advance, and then configure them manually.
The above is how SpringCloud uses Zuul to achieve fault-tolerant fallback. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are 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.