在分布式系统中,一种不可避免的情况就是某些服务会出现故障,导致依赖他们的其他服务出现远程调度的线程问题(雪崩效应)。而Hystrix提供的熔断器,通过隔离服务的访问点,能阻止这种分布式系统中出现的联动故障,并提供故障的解决方案,从而提高了整个分布式系统的弹性。
Ribbon中使用Hystrix(熔断器)
1.在Ribbon的pom.xml中添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
2.在application中增加注解启动熔断器Hystrix
package com.lxq.hello.spring.cloud.web.admin.ribbon;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
/**
* @Author Lxq
* @Date 2020/3/29 13:28
* @Version 1.0
*/
@EnableHystrix
@SpringBootApplication
@EnableDiscoveryClient
public class WebAdminRibbonApplication {
public static void main(String[] args) {
SpringApplication.run(WebAdminRibbonApplication.class, args);
}
}
3.在 Ribbon 调用方法上增加 @HystrixCommand
注解并指定 fallbackMethod
熔断方法
package com.lxq.hello.spring.cloud.web.admin.ribbon.service;
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;
/**
* @Author Lxq
* @Date 2020/3/29 13:43
* @Version 1.0
*/
@Service
public class AdminService {
@Autowired
private RestTemplate restTemplate;
@HystrixCommand(fallbackMethod = "hiError")
public String sayHi(String message) {
return restTemplate.getForObject("http://HELLO-SPRING-CLOUD-SERVICE-ADMIN/hi?message=" + message, String.class);
}
public String hiError(String message) {
return "Hi,your message is :\"" + message + "\" but request error.";
}
}
Feign使用熔断Hystrix
fegin自带Hystrix但是默认关闭 需要手动开启
在application.yml 中加入
feign:
hystrix:
enabled: true
Fegin的熔断是通过调用一个自定义熔断类来实现对应的service服务接口类
所以创建一个熔断类 并且实现 相应的需要实现熔断的服务
别忘记要在所被实现的服务接口类的@FeignClient的属性增加fallback=所被实现的服务接口类.class
package com.lxq.hello.spring.cloud.web.admin.feign.service;
import com.lxq.hello.spring.cloud.web.admin.feign.service.hystrix.AdminServiceHystrix;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
/**
* @Author Lxq
* @Date 2020/3/29 13:59
* @Version 1.0
* FeignClient:服务提供者的名称
*/
@FeignClient(value = "hello-spring-cloud-service-admin",fallback = AdminServiceHystrix.class)
public interface AdminService {
@RequestMapping(value = "hi", method = RequestMethod.GET)
String sayHi(@RequestParam("message") String message);
}
创建熔断类
package com.lxq.hello.spring.cloud.web.admin.feign.service.hystrix;
import com.lxq.hello.spring.cloud.web.admin.feign.service.AdminService;
import org.springframework.stereotype.Component;
/**
* @Author Lxq
* @Date 2020/3/29 14:20
* @Version 1.0
*/
@Component
public class AdminServiceHystrix implements AdminService {
@Override
public String sayHi(String message) {
return "Hi,your message is :\"" + message + "\" but request error.";
}
}