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:
Nginx
High performance, at present a large number of frontline Internet companies adoptNginx+Lua
The gateway for flow control, from thisOpenResty
Also more and more popularOpenResty
And it is fromNginx core
Plus a lot of third party modules, its biggest highlight is the default integrationLua development environment
,Nginx
Can be used as aWeb Server
Use. With the aid ofNginx
theEvent-driven model
andNon-blocking IO
, can achieve high performance Web applications.
andOpenResty
Provides a number of components such asMysql, Redis, Memcached
Wait, make inNginx
On the developmentThe Web application
More 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+Lua
Structure, 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
fallbackMethod
The drop methodcommandProperties
General configuration properties, you can configure HystrixCommand properties, such as thread pool or semaphore isolation, fuse fuse rules, and so onignoreExceptions
Ignore the exception, the default HystrixBadRequestException not plan to failgroupKey()
Group name, class name is used by defaultcommandKey
Command 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 Dashboard
Is 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@EnableHystrixDashboard
Annotation, 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 ~