Spring Cloud Hystrix (Fuse)

Due to network reasons or its own reasons, the service cannot guarantee 100% availability. If a single service has problems, the invocation of this service will be blocked. At this time, if a large number of requests flood in, the thread resources of the Servlet container will be consumed and the service will break down. The “avalanche” effect of service failures is the spread of service-to-service dependencies, which can have catastrophic consequences for the entire microservice system. Avalanche coping strategies:

  • Flow control: There are many ways to control flow, such as queues, tokens, leaky buckets, etc.
  • The gateway current-limitingBecause:NginxHigh performance, at present a large number of frontline Internet companies adoptNginx+LuaThe gateway for flow control, from thisOpenRestyAlso more and more popularOpenRestyAnd it is fromNginx corePlus a lot of third party modules, its biggest highlight is the default integrationLua development environment,NginxCan be used as aWeb ServerUse. With the aid ofNginxtheEvent-driven modelandNon-blocking IO, can achieve high performance Web applications.

    andOpenRestyProvides a number of components such asMysql, Redis, MemcachedWait, make inNginxOn the developmentThe Web applicationMore convenient and easier. At present, it is used in jingdong real-time price, SEC kill, dynamic service, single product page, list page and so onNginx+LuaStructure, other companies such as Taobao, Qunar, etc.
  • User interaction traffic limiting: A friendly prompt to limit incoming traffic from the source.

The framework implemented by Hystrix, an open source framework based on Netflix, aims to provide greater fault tolerance for delays and failures by controlling those nodes that access remote systems, services, and third-party libraries. Hystrix has powerful features such as service degradation, service meltdown, thread isolation, request caching, request consolidation, and service monitoring.

Add the dependent

<! -- Hystrix Breaker -->
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
Copy the code

RestTempate integration hystrix

@EnableHystrix
@EnableDiscoveryClient
@SpringBootApplication
public class RibbonConsumerApplication {

    @LoadBalanced
    @Bean
    RestTemplate restTemplate(a) {
        return new RestTemplate();
    }

	public static void main(String[] args) { SpringApplication.run(RibbonConsumerApplication.class, args); }}Copy the code

Feign comes with its own circuit breaker, if in Dalston version, it is not turned on by default, through configuration

feign:
  hystrix:
    enabled: true
Copy the code

@hystrixCommand indicates that this method is a Hystrix package that can isolate, degrade, fail quickly, retry quickly, and so on dependent services

  • fallbackMethodThe drop method
  • commandPropertiesGeneral configuration properties, you can configure HystrixCommand properties, such as thread pool or semaphore isolation, fuse fuse rules, and so on
  • ignoreExceptionsIgnore the exception, the default HystrixBadRequestException not plan to fail
  • groupKey()Group name, class name is used by default
  • commandKeyCommand name, default method name

Consumer Delivery methods (defaultStores)

RestTemplate is used

@RestController
public class ConsumerController {

    @Autowired
    private RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "defaultStores")
    @GetMapping(value = "/hello")
    public String hello(a) {
        return restTemplate.getForEntity("http://eureka-provider/", String.class).getBody();
    }

    public String defaultStores(a) {
        return "Ribbon + Hystrix, provider service is down"; }}Copy the code

Feign configuration is used

@EnableHystrix
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class FeignConsumerApplication {

	public static void main(String[] args) { SpringApplication.run(FeignConsumerApplication.class, args); }}Copy the code
@FeignClient(value ="eureka-provider",fallbackFactory = HystrixClientFallbackFactory.class)
public interface  HomeClient {

    @GetMapping("/")
    String consumer(a);
}
Copy the code
@Component
public class HystrixClientFallbackFactory implements FallbackFactory<HomeClient> {

    @Override
    public HomeClient create(Throwable throwable) {
        return() - >"Feign + Hystrix, provider service is down."; }}Copy the code

Hystrix DashboardIs a component of circuit breaker status that provides data monitoring and a user-friendly graphical interface.

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>
Copy the code

Add to the entry of the program@EnableHystrixDashboardAnnotation, openHystrixDashboard

conclusion

There is a full deployment of Spring Cloud on Github. Other related articles Spring Cloud (1)- Introduction and options Spring Cloud (2)- Service Discovery (Eureka, Consul) Spring Cloud (3)- Load Balancing (Feign, Ribbon Spring Cloud (4)- Hystrix Spring Cloud (5)- Zuul Spring Cloud (6)- Configuration Management and Refresh (Config, Bus) Give a star ~ personal blog ~ Jane book ~