SpringCloud 集成了 Netflix 开源的 Hystrix 组件,该组件实现了熔断器模型,它使得我们很方便地实现熔断器。
在实际项目中,一个请求调用多个服务是比较常见的,如果较底层的服务发生故障将会发生连锁反应。这对于一个大型项目是灾难性的。因此,我们需要利用 Hystrix 组件,当特定的服务不可用达到一个阈值(Hystrix 默认 5 秒 20 次),将打开熔断器,即可避免发生连锁反应。
这篇在Spring Cloud (四) 服务消费者 feign的基础上进行
OpenFeign 是默认自带熔断器的,但是默认关闭的,我们可以在 application.yml 中开启它:
feign:
hystrix:
enabled: true #开启熔断器
新建一个类 ApiServiceError.java 并实现 ApiService:
import org.springframework.stereotype.Component;
/**
* @author 作者 : 小布
* @version 创建时间 : 2019年5月8日 上午10:53:23
* @explain 类说明 :
*/
@Component
public class ApiServiceError implements ApiService {
@Override
public String index() {
return "服务发生故障!";
}
}
然后在 ApiService 的注解中指定 fallback:
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
* @author 作者 : 小布
* @version 创建时间 : 2019年5月8日 上午10:33:10
* @explain 类说明 :
*/
@FeignClient(value = "SpringCloud-client-A",fallback = ApiServiceError.class)
public interface ApiService {
@RequestMapping(value = "/hello/index",method = RequestMethod.GET)
String index();
}
创建 Controller 类:
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.springcloud.service.ApiService;
/**
* @author 作者 : 小布
* @version 创建时间 : 2019年5月8日 上午10:44:19
* @explain 类说明 :
*/
@RestController
@RequestMapping("/hello")
public class TestController {
@Autowired
private ApiService apiService;
@RequestMapping("/index")
public Map<String, Object> index(){
Map<String, Object> mp = new HashMap<String, Object>();
mp.put("a", apiService.index());
mp.put("b", apiService.index());
mp.put("c", apiService.index());
mp.put("d", apiService.index());
mp.put("e", apiService.index());
return mp;
}
}
然后开启多个 SpringCloud-client-A :
访问路径:http://localhost:8088/hello/index
效果图:
当我们停止 SpringCloud-client-A 时 :
Hystrix 给我们提供了一个强大的功能,那就是 Dashboard。Dashboard 是一个 Web 界面,它可以让我们监控 Hystrix Command 的响应时间、请求成功率等数据。
在 SpringCloud-feign 的 Maven 添加 以下:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
修改 SpringCloud-feign 启动类:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
@SpringBootApplication
@EnableFeignClients
@EnableHystrixDashboard
public class SpringCloudFeignApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudFeignApplication.class, args);
System.out.println("启动feign");
}
@Bean
public ServletRegistrationBean getServlet(){
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet );
registrationBean.setLoadOnStartup(1);
registrationBean.addUrlMappings("/hystrix.stream");
registrationBean.setName("HystrixMetricsStreamServlet");
return registrationBean;
}
}
访问 http://localhost:8088/hystrix
效果图:
上一篇 Spring Cloud (四) 服务消费者 feign