9.Hystrix服务搭建:服务熔断,超时设置,搭建控制台(DashBoard)

说在前边:这里pom文件中统一没有添加版本号,是因为在父项目中规定了对应的版本号。

父项目中规定的版本:

    <!--定义版本号-->
<properties>
    <java.version>1.8</java.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <spring-cloud.version>Hoxton.SR8</spring-cloud.version>
    <spring-cloud-alibaba.version>2.2.3.RELEASE</spring-cloud-alibaba.version>
</properties>
<!--只负责jar的管理不负责加载的下载  如何子模块需要使用某个jar
    需要在子模块中自己引用下载 只是在子模块中无需再引用本版号-->
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-dependencies</artifactId>
            <version>${spring-cloud-alibaba.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

Hystrix 简介

Hystrix是由Netflix开源的一个延迟和容错库,用于隔离访问远程系统、服务或者第三方库,防止级联失 败,从而提升系统的可用性与容错性。Hystrix主要通过以下几点实现延迟和容错。

  • 跳闸机制:当某服务的错误率超过一定的阈值时,Hystrix可以自动或手动跳闸,停止请求该服务一段时间。
  • 资源隔离:Hystrix为每个依赖都维护了一个小型的线程池(或者信号量)。如果该线程池已满, 发往该依赖的请求就被立即拒绝,而不是排队等待,从而加速失败判定。
  • 监控:Hystrix可以近乎实时地监控运行指标和配置的变化,例如成功、失败、超时、以及被拒绝的请求等。
  • 回退机制:当请求失败、超时、被拒绝,或当断路器打开时,执行回退逻辑。回退逻辑由开发人员自行提供,例如返回一个缺省值。
  • 自我修复:断路器打开一段时间后,会自动进入“半开”状态。

2. 实现服务熔断

1. Rest实现服务熔断

4步:
1. 在pom中添加依赖
2. 修改主启动类
3. 修改业务使用

1. pom文件

在pom中添加hystrix相关依赖

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

2. 修改主启动类

在主启动类上添加注解 @EnableCircuitBreaker

@SpringBootApplication
// 使用 eureka 作为注册中心
@EnableEurekaClient
// 开启熔断器
@EnableCircuitBreaker
public class OrderApp {
    public static void main(String[] args) {
        SpringApplication.run(OrderApp.class, args);
    }
}

3. 修改业务

在可能需要熔断的方法上加上注解 @HystrixCommand

@RestController
@RequestMapping("order")
public class OrderController {

    @Autowired
    private RestTemplate restTemplate;
    @GetMapping("buy/{id}")
    @HystrixCommand(fallbackMethod = "orderFallBack") //SentinelResources
    public Product buy(@PathVariable("id") Long id){
        return restTemplate.getForObject("http://SHOP-PRODUCT-SERVICE/product/"+id,Product.class);
    }
    
    //降级方法
    public Product orderFallBack(Long id){
        Product product = new Product();
        product.setProductDesc("由于网络原因,请稍后在访问!");
        return product;
    }
}

注意:

为 findById方法编写一个回退方法orderFallBack,回退方法容错方法具有相同的参数与返回值类型,该方法返回一个默认的错误信息。


4. 默认Fallback

为一个类指定一个默认的 fallback 方法进行兜底

@RestController
@RequestMapping("order")
// 指定默认的 fallback 进行托底
@DefaultProperties(defaultFallback = "orderFallbackMethod")
public class OrderController {

    @GetMapping("addOrder/{pid}")
    public String addOrder(@PathVariable("pid") Integer pid) {
        return res;
    }

	// 默认的兜底方法
    public String orderFallbackMethod(){
        return "fallbackMethod 返回的~~~~~~~~~~~";
    }
}

注意:

默认的兜底方法不能有参数,必须是一个无参的方法。

5. 超时时长设置

Hystix的默认超时时长为1,我们可以通过配置修改这个值:

hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 3000



2. Feign 方式实现熔断

SpringCloud Fegin默认已为Feign整合了hystrix,所以添加Feign依赖后就不用在添加hystrix,那么怎么才能让Feign的熔断机制生效呢,只要按以下步骤开发:

注: 我还是添加了 hystrix 的依赖,去掉之后我的项目会类找不到的错误

4步:

  • 修改pom文件
  • 修改yml文件
  • 修改主启动类
  • 修改业务类

1. pom文件

在pom文件中添加 openfeign 的依赖

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

2. yml文件

在yml文件中配置 feign 使用 hystrix 熔断

feign:
  hystrix:
    enabled: true

3. 主启动类

在主启动类配置 @FeignClient

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableCircuitBreaker
public class OrderApp {
    public static void main(String[] args) {
        SpringApplication.run(OrderApp.class, args);
    }
}

4. 写业务

1. 编写 Feign 接口

该接口代理调用的其他服务的方法

@FeignClient(name = "shop-product", fallback = IProductFeignImpl.class)
public interface IProductFeign {

	// 要访问的服务的方法的路径
    @GetMapping("/product/getProduct/{pid}")
    String getProduct(@PathVariable("pid") Integer pid);
}

@FeignClient(name = "shop-product", fallback = IProductFeignImpl.class)
该注解 name 属性是远程服务的名称, fallback 是实现了该接口的一个类,对应接口的实现方法就是对应的兜底方法(降级方法)

2. 写实现类

Feign接口的实现类的具体业务,就是兜底方法的业务。

@Component // 这个注解不能少
public class IProductFeignImpl implements IProductFeign {
    @Override
    public String getProduct(Integer pid) {
       // 降级的业务逻辑
    }
}

@Component 这个注解不能少

修改 feign 的超时时间

默认feign的超时时间是 1s
可以通过 yml 配置 feign 的超时时间,由于 feign 内部使用了 Ribbon,所以只需要设置 Ribbon的超时间就可以修改 feign 的超时时间了。

ribbon:
  ReadTimeout: 3000 # 设置为3s




3.搭建 Hystrix 的DashBoard控制台

Hystrix官方还提供了基于图形化的DashBoard(仪表板)监控平 台。Hystrix仪表板可以显示每个断路器(被@HystrixCommand注解的方法)的状态。

三步:

  • 修改pom文件
  • 修改yml文件
  • 修改主启动类

1. pom文件

在pom文件中添加 actuator、hystrix、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</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>

2. yml 文件

在yml中配置可以监控哪些资源

hystrix:
  dashboard:
    proxy-stream-allow-list: "*"
management:
  endpoints:
    web:
      exposure:
        include: "*"

3. 修改启动类

在启动类上使用 @EnableHystrixDashBoard 的注解

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableCircuitBreaker
@EnableHystrixDashboard
public class OrderApp {
    public static void main(String[] args) {
        SpringApplication.run(OrderApp.class, args);
    }
}

4. 测试访问

在浏览器上输入 : http://localhost:服务端口号/hystrix/ 进入查看详细数据
这个服务端口号,就是配置有hystrix熔断控制台的服务的端口号
在这里插入图片描述

在这里插入图片描述
在框住的位置中输入对应的地址,只需要修改为自己的端口即可

http://localhost:端口号/actuator/hystrix.stream

然后点击 下方的 Monitor Stream 按钮,就可以进入监控页面了
在这里插入图片描述
这时再访问这个服务中的任意方法,这里都可以监控到。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值