SoFunction
Updated on 2025-03-08

Solve the problem of abnormal circuit breaking of zuulGateway gateway adding routes

zuulGateway is a very good component in spring cloud and is very frequently used. During use, you may occasionally encounter service routing abnormalities. If there is no abnormal circuit breaker, the application may be unresponsive, and even a system avalanche. Therefore, a circuit breaker mechanism is generally needed.

Just look at the code, it's very simple:

/*
  * File name: Copyright: Copyright by Description: Modified by: gogym Modified time: January 31, 2018 Tracking order number: Modified order number:
  * Modified content:
  */
 
package ; 
 
import ;
import ;
import ;
 
import ;
import ;
import ;
import ;
import ;
import ; 
 
/**
  * 〈Exceptional fuse〉
  *
  * @author gogym
  * @version January 31, 2018
  * @see ServerFallback
  * @since
  */
@Component
public class ServerFallback implements ZuulFallbackProvider
{
 
 @Override
 public String getRoute()
 {
  // api service id, return "*" or return null if all calls are required to support fallback  return "*"; 
 }
 
 @Override
 public ClientHttpResponse fallbackResponse()
 {
 
  // ------------------------------------------------------------------------------------------------------------------------------  return new ClientHttpResponse()
  {
 
   String responseStr = "{\"code\":10006,\"msg\":\"Service routing exception"}";
 
   @Override
   public InputStream getBody()
    throws IOException
   {
 
    return new ByteArrayInputStream(("UTF-8"));
 
   }
 
   @Override
   public HttpHeaders getHeaders()
   {
 
    HttpHeaders headers = new HttpHeaders();
    // The encoding is consistent with the content in the body, otherwise it is easy to get garbled    (MediaType.APPLICATION_FORM_URLENCODED);
    return headers; 
   }
 
   @Override
   public int getRawStatusCode()
    throws IOException
   {
 
    return (); 
   }
 
   @Override
   public HttpStatus getStatusCode()
    throws IOException
   {
    /**
      * The gateway requests to the API service failed, but the request initiated by the consumer client to the gateway is OK. The API's 404,500 and other problems should not be thrown to the client.
      * Gateway and API service clusters are black boxes for clients
      */
    return ; 
   }
 
   @Override
   public String getStatusText()
    throws IOException
   {
 
    return ();
 
   }
 
   @Override
   public void close()
   {
 
   }
  }; 
 }
}

Supplementary knowledge:springcloud zuul gateway load balancing routing to shutdown nodes, resulting in interface access failure problem resolved

Springcloud project: Two nodes are deployed in the same service. After one of the nodes is hanged up, the problem of routing to the stop node is broken, resulting in a 50% success rate in a short period of time.

Solution:

1. Project closing Call hook function Delete eureka service registration (Do not use kill -9 to force close the hook function to not execute)

().shutdownComponent();

2. The zuul gateway enables the retry function

#Whether to enable the retry function=true
<dependency>
 <groupId></groupId>
 <artifactId>spring-retry</artifactId>
</dependency>

This will enable the switching between springcloud project production and grayscale, and the single node lapse, without affecting the access problem of project interfaces. That is, it can continuously serve online projects

The above article solves the problem of abnormal routing circuit breaking of zuulGateway gateway is all the content I share with you. I hope you can give you a reference and I hope you can support me more.