Hystrix熔断器的使用

1.在RestTemplate上使用

        在之前介绍RestTemplate结合Ribbon实现客户端负载均衡调用服务的基础上进行改造。之前写了通过RestTemplate调用eureka-client1的“/hello”API接口,并用Ribbon做了负载均衡。本文在此基础上加了Hystrix熔断器功能。

        首先在pom文件中引用Hystrix的起步依赖spring-cloud-starter-netflix-hystrix,代码如下:

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

        然后在Spring Boot的启动类EurekaRibbonHystrixApplication加上@EnableHystrix注解开启Hystrix的熔断器功能,代码如下:

@SpringBootApplication
@EnableHystrix
@EnableEurekaClient
public class EurekaRibbonHystrixApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaRibbonHystrixApplication.class, args);
    }

}

        新建RibbonService类,在RibbonService里的hi()方法上加上@HystrixCommand注解。有了@HystrixCommand注解,hi()方法就启动用了Hystrix熔断器的功能,其中fallbackMethod为处理回退(fallBack)逻辑的方法。本代码中直接返回了一个字符串。在熔断器打开的情况下会,会执行fallback逻辑。fallback的逻辑最好是返回一些静态的字符串,不需要处理复杂的逻辑,也不需要远程调度其它服务,这样方便执行快速失败,释放线程资源。如果一定要在fallback逻辑中远程调度其他服务,最好在远程调度其他服务时也加上熔断器。案例代码如下:

@Service
public class RibbonService {

    @Autowired
    public RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "hiError")
    public String hi(String name)
    {
        return  restTemplate.getForObject("http://eureka-client1/hello/{name}", String.class,name);

    }


    public String hiError(String name)
    {
        return "hi,"+name+",Sorry Error!";
    }
}

在Controll中调用Ribbon的Service方法:

@Controller
public class TestController {

    @Autowired
    private RibbonService ribbonService;

    @GetMapping("/hello/{name}")
    @ResponseBody
    public String router(@PathVariable String name) {
        String result = ribbonService.hi(name);
        return result;
    }
}

        依次启动eureka注册服务、eureka客户端和本程序。都启动完成,在浏览器上访问http://localhost:8769/hello/yangyayun,浏览器会交替返回以下内容:

hello!yangyayun======端口号:8763
hello!yangyayun======端口号:8762

      关闭端口为8763和8762这两个服务,使它们处于不可用状态,此时我们没法调用eureka-client1的“/hello”方法接口,访问之前的地址,浏览器会显示:

hi,yangyayun,Sorry Error!

      由于eureka-client1没有响应,判定eureka-client1不可用,开启了熔断器,最后进入了fallbackMethod的逻辑。当熔断器打开了,之后的请求会直接执行fallbackMethos的逻辑。这样做的好处就是通过快速失败,请求能够得到及时处理,线程不再阻塞。

2.在Ribbon上使用

第一步:由于Feign的起步依赖中已经引入了Hystrix的依赖,所以只需要开启Hystrix的功能,在properties文件中添加以下配置:

feign.hystrix.enabled=true

.第二步:在Feign的接口上添加Hystrix(断路由)

@FeignClient(name = "这里写服务名称", fallbackFactory = InsuranceCompany4OthersHystrixFactory.class)
@RequestMapping("/basebusiness/insurancecompany/4others")
public interface InsuranceCompany4OthersApi{

  @RequestMapping("getAllInsuranceCompany")
  public RetDTO getAllInsuranceCompany();

}

第三步:编写InsuranceCompany4OthersHystrixFactory类,类上加@Component注解,注入Ioc容器中。

@Component
public class InsuranceCompany4OthersHystrixFactory implements FallbackFactory<InsuranceCompany4OthersApi>{

  @Override
  public InsuranceCompany4OthersApi create(Throwable arg0) {
    return new InsuranceCompany4OthersApi() {

      @Override
      public RetDTO getAllInsuranceCompany() {   

        RetDTO retDTO = new RetDTO();
        retDTO.setRetStatus(RetDTO.SUCCESS);
        retDTO.setRetData("服务不通");
        return retDTO;

      }

    };

  }

}

第四部:测试。把你需要调用的服务挂掉,使用Feign调用服务,如果返回 “服务不通“ 则Hystrix(断路由)生效。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值