(五)Spring Cloud实践:使用Netflix hystrix实现断路器,并使用Netflix hystrix dashboard实现监控

本文介绍了在Spring Cloud中如何使用Netflix Hystrix实现断路器,防止微服务连锁调用失败,并通过Hystrix Dashboard进行监控。在模块5中,详细说明了配置Hystrix依赖、启用断路器、添加@HystrixCommand注解以及设置fallback类的过程。同时,展示了如何在模块5和6中配置Hystrix Dashboard以监控断路器指标。
摘要由CSDN通过智能技术生成

Spring Cloud框架下,微服务之间的调用可能会是多层次的,底层服务故障会造成连锁式的异常。Spring Cloud使用Netflix hystrix实现断路器,可以通过回调来阻止上述连锁调用失败的情况。另外, Spring Cloud还使用Netflix hystrix dashboard实现了断路器的监控。

Netflix hystrix

对于模块5 ribbonservice, 需要加入hystrix依赖库,在pom.xml中添加如下内容:

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

然后,在启动类中添加@EnableCircuitBreaker注解;

再次,在访问服务的方法上添加@HystrixCommand注解, 修改后的代码如下:

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;

/**
 * @program: springlearn
 * @description: 创建获取一个获取Hello内容的service类
 * @author: Mr.Young
 * @create: 2018-10-25 15:53
 **/
@Service
public class HelloService {
    @Autowired
    RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "callFailure")
    public String getHelloContent() {
        return restTemplate.getForObject("http://CONFIG-CLIENT/",String.class);
    }

    @HystrixCommand(fallbackMethod = "callFailure")
    public String getConfigTest(){
        return restTemplate.getForObject("http://CONFIG-CLIENT/configtest",String.class);
    }
    public String callFailure(){
        return "service is not available!";
    }
}

对于模块6 feignservice,由于Feign内部已经支持断路器,不需要再添加hystrix依赖,只需要在启动类添加@EnableCircuitBreaker注解,并在接口类HelloWorldService的@FeignClient注解中添加fallback类,具体修改为:@FeignClient(value = "CONFIG-CLIENT",fallback = HelloWorldServiceFailure.class),HelloWorldServiceFailure类实现了HelloWorldService中的接口。比如我们创建一个名为HelloWorldServiceFailure的类,其内容如下:

import org.springframework.stereotype.Component;

/**
 * @program: springlearn
 * @description: failure
 * @author: Mr.Young
 * @create: 2018-11-02 17:13
 **/
@Component
public class HelloWorldServiceFailure implements HelloWorldService{
    @Override
    public String sayHello() {
        return "service is not available !";
    }
    @Override
    public String configTest(){
        return "configtest service is not available !";
    }
}

另外,在application.yml文件添加如下配置:

feign:
  hystrix:
    enabled: true

Netflix hystrix dashboard

Spring Cloud可以利用Netflix hystrix dashboard实现断路器指标数据监控,具体做法如下。

在模块5 ribbonservice中,修改pom.xml 文件,添加Netflix hystrix dashboard依赖库,内容如下:

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

修改启动类,添加@EnableHystrixDashboard注解。

在配置文件application.yml中,添加如下内容:

management:
  security: false
  endpoints:
    web:
      exposure:
        include: hystrix.stream
      base-path: /

在浏览器中输入:http://localhost:8091/hystrix,显示如下界面。

然后,在该界面Hystrix Dashboard下面的输入框中输入:http://localhost:8091/hystrix.stream,会跳转到下面的页面:

一直显示loading,直到你发出请求,比如通过postman发送Post请求,请求Url为http://localhost:8091/,成功后,会看到监控信息。如下:

自此,在模块5 中,已经实现了断路器及断路器监控功能。

在模块6中, 要实现dashboard仪表板,需要在pom.xml中添加如下依赖。

<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>
<!-- https://mvnrepository.com/artifact/com.netflix.hystrix/hystrix-javanica -->
<dependency>
	<groupId>com.netflix.hystrix</groupId>
	<artifactId>hystrix-javanica</artifactId>
	<version>1.5.12</version>
</dependency>

springboot 2.X以上,一定添加hystrix-javanica依赖,否则会报错。

其它的修改同模块5,实现的效果也一样。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值