当前位置: 移动技术网 > IT编程>软件设计>架构 > spring cloud微服务快速教程之(四)熔断器(Hystrix)及其工具(Dashboard、Turbine)

spring cloud微服务快速教程之(四)熔断器(Hystrix)及其工具(Dashboard、Turbine)

2020年01月08日  | 移动技术网IT编程  | 我要评论

0-为什么需要熔断器

  在分布式系统中,各个服务相互调用相互依赖,如果某个服务挂了,很可能导致其他调用它的一连串服务也挂掉或者在不断等待中耗尽服务器资源,这种现象称之为雪崩效应

  未来防止系统雪崩,熔断机制必不可少,就是当一个服务挂掉后,调用它的服务能快速熔断,不再耗费资源,快速失败并提供回退方案;

   hystrix】:是spring cloud的熔断器组件,提供了熔断器功能,能够阻止联动故障,并提供故障的解决方案,提供系统弹性;

  【hystrix dashboard】:是熔断器的监控面板,通过它,能直观的了解熔断器的状况;

  【turbine】: 在dashboard中,我们要输入一个一个单独的服务地址进行监控和了解;那么多服务,一个一个输入那不是累死人,有没有一个工具能把众多分散的微服务熔断器监控状况聚合到一起,使得我们在一个dashboard就能了解众多服务的熔断器监控状况呢,有,这个工具就是turbine;它的作用就是聚合众多微服务的hystrix监控数据到一起,使得我们只需要在一个dashboard就能了解所有;

一、使用hystrix

1.1、添加依赖

 

<!-- 断路器 hystrix-->
        <dependency>
            <groupid>org.springframework.cloud</groupid>
            <artifactid>spring-cloud-starter-netflix-hystrix</artifactid>
        </dependency>

1.2、配置文件中开启熔断器开关

#开启熔断器开关
feign:
  hystrix:
    enabled: true

1.3、启动类中增加 @enablehystrix 注解和bean

@springbootapplication
@enableeurekaclient
@enablehystrix
@enablefeignclients
public class application
{
    public  static void main(string[] args)
    {
        springapplication.run(application.class);
@bean
public servletregistrationbean getservlet(){
hystrixmetricsstreamservlet streamservlet = new hystrixmetricsstreamservlet();
servletregistrationbean registrationbean = new servletregistrationbean(streamservlet);
registrationbean.setloadonstartup(1);
registrationbean.addurlmappings("/actuator/hystrix.stream");
registrationbean.setname("hystrixmetricsstreamservlet");
return registrationbean;
}
 
} }

1.4、增加熔断器类,实现feign的接口

package com.anson.service.feign;

import org.springframework.stereotype.component;
import org.springframework.web.bind.annotation.requestmapping;

/**
 * @description: 熔断器类
 * @author: anson
 * @date: 2020/1/7 11:24
 */
@component
public class fuserservicehystrix implements fuserservice
{
    @requestmapping("/user/hello")
    @override
    public  string hello()
    {
        return "对不起,user服务不可达,请稍后再试!";
    }
}

1.5、增加熔断器配置类feignconfig

package com.anson.config;

import feign.retryer;
import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.configuration;
import java.util.concurrent.timeunit;
@configuration
public class feignconfig {
    @bean
    public retryer feignretryer() {
        /*
         * 参数说明:
         * 第一个> 重试间隔为100毫秒
         * 第二个> 最大重试时间为1秒
         * 第三个> 最大重试次数为5次
         */
        return new retryer.default(100, timeunit.seconds.tomillis(1), 5);
    }
}

 

1.6、在feign接口注解中增加fallback,指向熔断器类

package com.anson.service.feign;

import org.springframework.cloud.openfeign.feignclient;
import org.springframework.web.bind.annotation.requestmapping;

@feignclient(name = "user",configuration = feignconfig.class,fallback = fuserservicehystrix.class)
public interface fuserservice
{
@requestmapping("/user/hello")
public string hello();
}

1.7、运行测试

user服务正常时:

 

 

 user服务关闭时:

 

 

 ---------------------------华丽丽的分割线-------------------------------------------------------

二、hystrix dashboard的使用

2.1、熔断器加了,那么我们要监控和查看hystrix的运行状态,这时候dashboard上场;

2.2、添加依赖:

        <!-- 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>

2.3、启动类添加注解 @enablehystrixdashboard

2.4、完成,运行测试,打开,http://localhost:8767/hystrix

在里面填入要监控的微服务地址  +/actuator/histrix.stream,如:http://localhost:8768/actuator/histrix.stream,title也填入,点击按钮就可以看到结果了:如图

 

 

 

 可以看到我们加入的熔断器的运行状态

----------------------------------------------华丽丽的分割线-----------------------------------------------------------------

三、turbine 聚合监控数据

3.1、上面dashboard中,我们输入http://localhost:8767/actuator/histrix.stream,它只是现实端口8767这个微服务实例的监控数据而已,要看其他的,还得重新输入,那能不能有个工具将所有微服务的监控数据都聚合到一起,显示到dashboard中呢,这个工具就是turbine 

3.2、新建一个模块,添加相关依赖:

 <dependencies>
        <dependency>
            <groupid>org.springframework.boot</groupid>
            <artifactid>spring-boot-starter-web</artifactid>
        </dependency>

        <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>

        <dependency>
            <groupid>org.springframework.cloud</groupid>
            <artifactid>spring-cloud-starter-netflix-turbine</artifactid>
        </dependency>
        <dependency>
            <groupid>org.springframework.cloud</groupid>
            <artifactid>spring-cloud-starter-netflix-eureka-client</artifactid>
        </dependency>

3,.3、修改配置文件,其中app-config是要监控的服务名,多个用逗号分隔;

server:
  port: 8768

spring:
  application:
    name: turbine

eureka:
  client:
    serviceurl:
      defaultzone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true

turbine:
  combine-host-port: true
  app-config: order
  cluster-name-expression: new string("default")
  instanceurlsuffix: actuator/hystrix.stream
  aggregator:
    cluster-config: default

 

3.4、在启动类中添加注解@enableturbine;

package com.anson;

import org.springframework.boot.springapplication;
import org.springframework.boot.autoconfigure.springbootapplication;
import org.springframework.cloud.netflix.eureka.enableeurekaclient;
import org.springframework.cloud.netflix.hystrix.dashboard.enablehystrixdashboard;
import org.springframework.cloud.netflix.turbine.enableturbine;

/**
 * @description: todo
 * @author: anson
 * @date: 2020/1/6 14:27
 */
@springbootapplication
@enableeurekaclient
@enableturbine
@enablehystrixdashboard
public class application
{
    public  static void main(string[] args)
    {
        springapplication.run(application.class);
    }

}

完成,启动运行后,他就会将所有配置文件中添加进来的服务全部的熔断器监控数据聚合到一起了,

在任意项目的dashboard面板中填入http://turbine-hostname:port/turbine.stream 的地址样式,就能看到所有的监控数据了 

,如 : http://localhost:8768/turbine.stream

 

 我们只加了一个熔断器,你可以多加几个看看效果;

好了,熔断器hystrix简单讲解到着,,更深入的后续再详聊,git源码后续再放出

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网