In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how to achieve Zuul fault-tolerant fallback and high availability, the article is very detailed, has a certain reference value, interested friends must read it!
Fault tolerance and fallback of zuul
As mentioned earlier, using Hystrix to achieve fault tolerance and fallback of micro services, in fact, Zuul has integrated Hystrix by default, and it is relatively easy to use:
Add to the original zuul-gateway project, implement ZuulFallbackProvider interface, and implement getRoute and fallbackResponse methods.
Import com.google.gson.Gson;import com.simons.cn.util.CommonEnum;import com.simons.cn.util.CommonResult;import org.springframework.cloud.netflix.zuul.filters.route.ZuulFallbackProvider;import org.springframework.http.HttpHeaders;import org.springframework.http.HttpStatus;import org.springframework.http.MediaType;import org.springframework.http.client.ClientHttpResponse;import org.springframework.stereotype.Component;import java.io.ByteArrayInputStream;import java.io.IOException;import java.io.InputStream;import java.nio.charset.Charset @ Componentpublic class UserProviderZuulFallBack implements ZuulFallbackProvider {private static final String USER_PROVIDER_SERVICE = "user-provider"; @ Override public String getRoute () {return USER_PROVIDER_SERVICE / / return the microservice for which you need to provide fallback} @ Override public ClientHttpResponse fallbackResponse () {return new ClientHttpResponse () {/ * return Http status code ID * / @ Override public HttpStatus getStatusCode () throws IOException {return HttpStatus.OK } / * returns the corresponding number of Http status code: 200. for more information, please see HttpStatus * / @ Override public int getRawStatusCode () throws IOException {return 200. } / * * returned Http status code corresponds to Chinese translation: OK. For more information, please see HttpStatus * / @ Override public String getStatusText () throws IOException {return HttpStatus.OK.getReasonPhrase () } @ Override public void close () {} / * body response body * / @ Override public InputStream getBody () throws IOException {String message = new Gson () .toJson (CommonResult.success (CommonEnum.SERVICE_NOT_AVAILABLE.getCode ()) CommonEnum.SERVICE_NOT_AVAILABLE.getMessage ()) Return new ByteArrayInputStream (message.getBytes ("UTF-8"));} / * set HttpHeaders * / @ Override public HttpHeaders getHeaders () {HttpHeaders headers = new HttpHeaders (); / / MediaType mediaType = new MediaType ("application/json;charset=utf-8") / / this is the wrong way to write MediaType mediaType = new MediaType ("application", "json", Charset.forName ("utf-8")); headers.setContentType (mediaType); return headers;}};}}
Enumerations in CommonEnum (part):
SERVICE_NOT_AVAILABLE ("5001", "the current service is not available, please try again later!")
Test:
Start the zuul-gateway project and the user-provider-eureka and discovery-eureka projects, and visit http://localhost:10010/user/getuserinfo?name=jack in the browser, as shown in the figure:
Close the user-provider-eureka service, and then access http://localhost:10010/user/getuserinfo?name=jack. The result is as shown below:
As you can see, when the user microservice is not available, the custom json is returned by default.
High availability Strategy of zuul
1. If Zuul is also registered with the service discovery component Eureka, then zuul has implemented a high availability configuration, which is consistent with the high availability of ordinary service consumers and providers
2. If zuul is not registered to Eureka, you can assign the request to one of the nodes of zuul through load balancers such as Nginx/HAProxy. This is more about the configuration of load balancers. For example, nginx:
The above is all the contents of the article "how to achieve fault-tolerant fallback and high availability of Zuul". Thank you for reading! Hope to share the content to help you, more related knowledge, 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.