Hystrix是通过添加延迟容忍和容错逻辑,从而控制分布式服务之间的交互。
在高流量的情况下,一个后端的延迟可能会导致所哟服务的资源在数秒变得饱和,后续结果如果有再有请求将无法提供服务,应用会出现故障。
Hystrix对第三方客户端访问依赖的延迟和故障进行保护和控制,Hystrix实现这一目标的思路为:
(1)将外部依赖的访问请求封装在独立的线程中,进行资源隔离
(2)对于超出设定阙值的服务调用,直接进行超时处理,不允许器耗费过长时间阻塞线程。
(3)没个依赖服务维护一个独立的线程池,一旦线程池满了,直接拒绝服务的调用。
(4)统计依赖服务调用的成功次数,失败次数,拒绝次数,超时次数等结果。
(5)在一段时间内,如果服务调用的异常次数超过一定阙值,就会触发熔断器,停止对特定服务的所有请求。
(6)如果某个服务出现调用失败、被拒绝、超时等异常情况,就自动调用fallback降级机制。
(7)实现监控指标和配置变化。
示例
(1)搭建eureka-server
(2)创建一个eureka-hystrix-client的Spring Boot项目加入Eureka Client、Web、Feign、Ribbon、Hystrix依赖
(3)在启动类中添加@EnableHystrix
(4)创建一个config包,添加HystrixConfig类
package com.itheima.eurekahystrixclient.config;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class HystrixConfig {
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
(5)新建controller类,新建LocalController类
package com.itheima.eurekahystrixclient.controller;
import com.itheima.eurekahystrixclient.service.LocalItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class LocalIntemController {
@Autowired
LocalItemService localItemService;
@GetMapping("/hi")
public String hi(String id){
return localItemService.hi(id);
}
}
(6)新建一个service包,新建LocalItemService类
package com.itheima.eurekahystrixclient.service;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class LocalItemService{
@Autowired
private RestTemplate restTemplate;
@HystrixCommand(fallbackMethod = "hiError")
public String hi(String id) {
return restTemplate.getForObject("http://hystrix-provider/hi?id=" + id,String.class);
}
public String hiError(String id) {
return String.format("Hi,your message is : %s but request bad.",id);
}
}
(7)新建一个hystrix-provider的Spring Boot项目。新建一个controller包,中新建一个HystrixController类
package com.itheima.hystrixprovider.controler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HystrixController {
@GetMapping("/hi")
public String hi(String id){
return "Hello World, I'm from hystix!"+id;
}
}
(9)启动服务,访问http://localhost:8764/hi?id=12
(1)开启Hystrix熔断功能,在eureka-hysterix-client项目配置文件application.xml
feign:
hystrix:
enabled: true
(2)在eureka-hystrix-client启动类中添加@EnableFeignClients
(3)在项目eureka-hystrix-client中创建一个LocalServiceImpl接口指定LocalServiceImpl类为失败逻辑处理类
package com.itheima.eurekahystrixclient.service;
import com.itheima.eurekahystrixclient.config.HystrixConfig;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
@FeignClient(value = "hystrix-provider",configuration = HystrixConfig.class,fallback = LocalItemServiceImpl.class)
public interface LocalItemService {
@RequestMapping(value = "/hi",method = RequestMethod.GET)
public String hi(@RequestParam(value = "id") String id);
}
(4)创建一个失败逻辑处理类LocalItemServiceImpl。
package com.itheima.eurekahystrixclient.service;
import org.springframework.stereotype.Component;
@Component
public class LocalItemServiceImpl implements LocalItemService{
@Override
public String hi(String id){
return String.format("Hi,your message is : %s but request bad.",id);
}
}
(5)启动服务访问http://localhost:8764/hi?id=hello
(6)关闭provider,访问http://localhost:8764/hi?id=hello
(1)在eureka-hystrix-client的pom.xml中增加
org.springframework.cloud
spring-cloud-netflix-hystrix-dashboard
1.4.0.RELEASE
org.springframework.boot
spring-boot-starter-actuator
(2)在启动类中增加@EnableHystrixDashboard
(3)创建hystrix.stream的Servlet配置。在config包下创建一个HystrixDashboardConfiguration类。
package com.itheima.eurekahystrixclient.config;
import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class HystrixDashboardConfiguration {
@Bean
public ServletRegistrationBean getServlet(){
HystrixMetricsStreamServlet streamServlet=new HystrixMetricsStreamServlet();
ServletRegistrationBean registrationBean=new ServletRegistrationBean(streamServlet);
registrationBean.setLoadOnStartup(1);
registrationBean.addUrlMappings("/hystrix.stream");
registrationBean.setName("HystrixStreamServlet");
return registrationBean;
}
}
(4)启动项目eureka-hystrix-client,访问http://localhost:8769/hystrix
在第一个框中输入http://localhost:8764/hystrix.stream,点击“Monitor Stream”。在打开一个标签页访问http://localhost:8764/hi?id=hello
使用Hystrix Dashboard组件监控服务的熔断器状态是,每个服务都有一个HystrixDashboard主页,监控非常不方便,为了同时监控多个服务熔断器的状况要用到Turbine,Turbine将每个服务的Hystrix Dashboard数据进行了整合。
步骤:
(1)搭建eureka-hystrix-client1和hystrix-provider1的服务消费者与服务提供买吧eureka-hystrix-client1项目的端口设置为8765,hystrix-provider1项目端口号设置为7006。
(2)创建一个turbine-server的SpringBoot项目,添加Eureka Client、Web、Test、Turbine和dashboard依赖。
org.springframework.cloud
spring-cloud-starter-netflix-turbine
org.springframework.cloud
spring-cloud-netflix-hystrix-dashboard
1.4.0.RELEASE
(3)在项目turbine-server的配置文件application.yml中进行相关配置,具体代码如下:
server:
port: 8769
spring:
application:
name: service-turbine
security:
basic:
enable: false
turbine:
instanceUrlSuffix: /hystrix.stream
aggregator:
clusterConfig: default
appConfig: eureka-hystrix-client
clusterNameExpression: new String("default")
eureka:
client:
serviceUrl:
defaultZone: http://localhost:7000/eureka/
(4)在启动类上添加@EnableTurbine注解,开启聚合监控功能,添加@EnableHystrixDashboard注解,启用Hystrix-Dashboard功能。
(5)启动项目访问http://localhost:8769/hystrix
在输入框中输入http://localhost:8769/turbine.stream点击MonitorStream进入监控页面