Spring Cloud 之 Ribbon,Spring RestTemplate 调用服务使用Hystrix熔断器

在服务调用中增加Hystrix熔断器,是在单个服务出现故障的时候快速失败,也可以提供回退方案,避免因为线程等待耗尽整个服务的线程资源;Hystrix DashBoard监控组件,可以实时监控熔断器的状态。

在之前操作的基础上,在eurekaclient中调用logservice的API,

1、maven依赖

<!--hystrix 依赖包-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<!--hystrix DashBoard 依赖包-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>

2、启动类上添加注解,

@EnableHystrix
@EnableHystrixDashboard

@EnableHystrix注解包含了@EnableCircuitBreaker注解,所以这里不需要再添加@EnableCircuitBreaker

3、启动类中注册一个Bean,

@Bean
public ServletRegistrationBean getServlet() {
    HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
    ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
    registrationBean.setLoadOnStartup(1);
    registrationBean.addUrlMappings("/actuator/hystrix.stream");
    registrationBean.setName("hystrixStreamServlet");
    return registrationBean;
}

配置文件增加如下内容:

hystrix:
  dashboard:
    proxy-stream-allow-list: "*"

4、在eurekaclient服务中写一个API接口,

@GetMapping("getClientInfo2")
public String getClientInfo2() {
    String info = "log info2";
    return logServiceClient.logInfo2(info);
}

Service层新增一个方法,

@HystrixCommand(commandProperties = {
        @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value="2000")}
        ,fallbackMethod = "logInfoFallback")
public String logInfo2(String logInfo) {
    return restTemplate.getForObject("http://logservice/log/logInfo2?logInfo="+logInfo, String.class);
}

其中,@HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value="3000") 设置服务调用超时时间为3s,默认1s,

fallbackMethod = "logInfoFallback" 指定回退方法,也就是服务调用失败后返回的信息,是服务降级的一种方式,

public String logInfoFallback(String logInfo) {
    return "loginfo service timeout";
}

5、在logservice中写一个API,用于eurekaclient服务调用,

@GetMapping("logInfo2")
public String logInfo2(String logInfo) throws InterruptedException {
    TimeUnit.SECONDS.sleep(2L);
    System.out.println(logInfo);
    return logInfo;
}

6、启动logservice两个实例,

7、浏览器中输入 http://localhost:8080/client/getClientInfo2, 查看接口返回信息,

8、浏览器中输入  http://localhost:8080/hystrix

在Hystrix Dashboard下方的输入框中输入http://localhost:8080/actuator/hystrix.stream

端口号后面的地址也就是在第3步中注入的ServletRegistrationBean里配置registrationBean.addUrlMappings("/actuator/hystrix.stream"),这两个地方要一致,

title输入内容随意,然后单击Monitor Stream,进入熔断器监控页面,

9、测试

调用http://localhost:8080/client/getClientInfo2接口,也就是通过@HystrixCommand注解的方法,多调用几次,然后在熔断器监控页面可以看到如下类似的画面:

10、将@HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value="2000",出现服务调用超时问题,

调用http://localhost:8080/client/getClientInfo2接口,提示loginfo service timeout,也就是用到了Hystrix的后备方法

 

猜你喜欢

转载自blog.csdn.net/suoyx/article/details/114211673