Spring Cloud 学习笔记(6)-Hystrix熔断

简介

Hystrix是由Netflix开源的一个延迟和容错库,用于隔离访问远程系统、服务或者第三方库,防止级联失败,从而提升系统的可用性、容错性与局部应用的弹性,是一个实现了超时机制和断路器模式的工具类库。

设计原则

防止任何单独的依赖耗尽资源(线程)
过载立即切断并快速失败,防止排队
尽可能提供回退以保护用户免受故障
使用隔离技术(例如隔板,泳道和断路器模式)来限制任何一个依赖的影响
通过近实时的指标,监控和告警,确保故障被及时发现
通过动态修改配置属性,确保故障及时恢复
防止整个依赖客户端执行失败,而不仅仅是网络通信

工作原理

使用命令模式将所有对外部服务(或依赖关系)的调用包装在HystrixCommand或HystrixObservableCommand对象中,并将该对象放在单独的线程中执行。
每个依赖都维护着一个线程池(或信号量),线程池被耗尽则拒绝请求(而不是让请求排队)。
记录请求成功,失败,超时和线程拒绝。
服务错误百分比超过了阈值,熔断器开关自动打开,一段时间内停止对该服务的所有请求。
请求失败,被拒绝,超时或熔断时执行降级逻辑。
近实时地监控指标和配置的修改。
当使用Hystrix封装每个基础依赖项时,每个依赖项彼此隔离,受到延迟时发生饱和的资源的限制,并包含回退逻辑,该逻辑决定了在依赖项中发生任何类型的故障时做出什么响应。

使用熔断器

在订单服务上使用熔断器

pom引入包

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

在启动类添加 @EnableHystrix注解


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

    /**
     * 向Spring容器中定义RestTemplate对象
     * @return
     */
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

在需要做熔断的方法上增加 @HystrixCommand注解

package com.joery.order.controller;

import com.joery.order.entity.Order;
import com.joery.order.service.OrderService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Value("${joery.name}")
    private String name;


    @HystrixCommand(fallbackMethod = "queryDefaultOrderById")
    @GetMapping(value = "order/{orderId}")
    public Order queryOrderById(@PathVariable("orderId") String orderId) {
        Order order =  this.orderService.queryOrderById(orderId);
        order.setTestname("配置中心读取了:"+name);
        return order;
    }

    public Order queryDefaultOrderById(String orderId)
    {
        System.out.println("熔断,默认回调函数");
        return  new Order();
    }
}

启动后验证,如果停止商品服务,系统也不会出现问题,只是反回熔断方法内容。

Feign在整合到Spring Cloud时已经自带了hystrix模块,所以pom.xml中不需要额外引入feign依赖。
下面介绍在feign内使用熔断器的方式:

修改yml配置如下:

server:
  port: 8090 #服务端口
###起个名字作为服务名称(该服务注册到eureka注册中心的名称,比如订单服务)
spring:
    application:
        name: app-order
    cloud:
      config:
        discovery:
          enabled: true
          service-id: config-server  # 注册中心的服务名
        name: order
        profile: dev  # 指定配置文件的环境
###服务注册到eureka注册中心的地址
eureka:
  client:
    service-url:
           defaultZone: http://127.0.0.1:8091/eureka
    ###因为该应用为服务提供者,是eureka的一个客户端,需要注册到注册中心
    register-with-eureka: true
    ###是否需要从eureka上检索服务
    fetch-registry: true

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

在商品接口服务类上指定熔断器回调类
@FeignClient( value = “app-goods” , fallback = ItemServiceImpl.class)
通过以上的方式进行注解

package com.joery.order.service;

import com.joery.order.entity.Item;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

//@FeignClient( value = "app-goods" )
@FeignClient( value = "app-goods" , fallback = ItemServiceImpl.class)
public interface IItemServiceInterface {
    @GetMapping(value = "item/{id}")
    Item getItem(@PathVariable("id") Long id);
}

实现ItemServiceImpl类,如下:

package com.joery.order.service;

import com.joery.order.entity.Item;
import com.joery.order.entity.Order;
import org.springframework.stereotype.Component;

@Component
public class ItemServiceImpl implements IItemServiceInterface{
    @Override
    public Item getItem(Long id) {
        System.out.println("熔断,默认回调函数");
        return  new Item();
    }
}

注:经过测试,发现所有的请求都是会调到熔断器返回结果,经过排查,需要在yml显示注册。修改yml如下:

server:
  port: 8090 #服务端口
###起个名字作为服务名称(该服务注册到eureka注册中心的名称,比如订单服务)
spring:
    application:
        name: app-order
    cloud:
      config:
        discovery:
          enabled: true
          service-id: config-server  # 注册中心的服务名
        name: order
        profile: dev  # 指定配置文件的环境
###服务注册到eureka注册中心的地址
eureka:
  client:
    service-url:
           defaultZone: http://127.0.0.1:8091/eureka
    ###因为该应用为服务提供者,是eureka的一个客户端,需要注册到注册中心
    register-with-eureka: true
    ###是否需要从eureka上检索服务
    fetch-registry: true

feign:
      hystrix:
        # feign熔断器开关
        enabled: true
      client:
        #开始没有用这个,一直调用fallback
        fetch-registy: true
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值