To sum up, if a downstream service fails and there are multiple upstream services calling it, then all upstream services fail. This will cause an avalanche effect, resulting in massive service failure.

This requires the method we define to degrade the downstream service when it times out or is abnormal. Or if the percentage of failures is greater than the configuration for a certain period of time, the fuse will turn on and degrade even if called correctly. After a period of time, it will try to call again. If the call fails, it will continue to fuse. If the call succeeds, it can be called normally.

1. The implementation

1. Modify the Application class

Modify Application to enable Hystrix.

@enableFeignClients # EnableFeign @enablecircuitbreaker # Enable HystrixCopy the code

2. Modify application. Yml

Change it to the following.

feign:
  hystrix:
    enabled: trueHystrix is not enabled by defaultribbon:
  ConnectTimeout: 5000The default connection timeout is set to1ReadTimeout: 5000# request processing timeouthystrix:
  command:
    default:  #defaultThe service ID is valid for the applicationexecution: timeout: # If enabled is set tofalse, the ribbon controls the timeout requesttrue, the timeout is used as the fuse breaker basisenabled: true
        isolation:
          strategy: SEMAPHORE
          thread:
            timeoutInMilliseconds: 5000# Circuit breaker timeout, default 1000mscircuitBreaker:
        enabled: true
        requestVolumeThreshold: 10# the default20, fusing threshold, how to meet user service error10The fuse will be turned on, even if the correct data is requested after order.sleepWindowInMilliseconds: 8000The default is 5S, after 5S, the fuse will be half-open, and then the correctness and error of the next request will determine whether the fuse is really closed and whether to continue to openerrorThresholdPercentage: 0.5
Copy the code

3. Create the UserClientFallBack fuse class

import org.springframework.stereotype.Component;

@Component
public class UserClientFallBack implements DcClient {

	@Override
	public String consumer() {
		String error= "Sorry is my fault again.";
		System.out.println(error);
		return error;
	}

	@Override
	public String consumer2() {
		// TODO Auto-generated method stub
		return null; }}Copy the code

Fallback = userClientFallback. class is added to DcClient, and when the call fails, it returns the defined error without throwing an exception.

4. Call

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;

@FeignClient(name = "eureka-client",fallback =UserClientFallBack.class )
public interface DcClient {

	@GetMapping("/aaa/dc? dk=3002")
	String consumer();

	@GetMapping("/aaa/dc? dk=3002")
	String consumer2();
}
Copy the code

The ribbon uses different request types that require different feign methods, which can be specified by interface parameters. Such as

#post request @postmapping ("/x/x") Result getDept(@RequestBody User user); # string @getMapping ()"/x/x/{userName}")
    Result getDept(@PathVariable String userName);
Copy the code

After normal call.A failed call fetch is a connection timeout.