本篇文章给大家分享的是有关Spring Cloud 中Hystrix有什么用,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
Netflix has created a library called Hystrix that implements the circuit breaker pattern. In a microservice architecture it is common to have multiple layers of service calls.
Netflix创建了一个名为Hystrix的库,该库实现了断路器模式。在微服务架构中,通常有多个服务调用层。
较底层的服务如果出现故障,会导致连锁故障。当对特定的服务的调用的不可用达到一个阀值(Hystric 是5秒20次) 断路器将会被打开。
断路打开后,可用避免连锁故障,fallback方法可以直接返回一个固定值。
在之前工程的基础上, 启动eureka-server,端口为9090;启动eureka-client, 端口为8040。
改造rebbon-service
工程的代码,首先在pox.xml文件中加入spring-cloud-starter-netflix-hystrix
的起步依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
在项目启动类上注解@EnableHystrix, 开启断路器能力:
@EnableEurekaClient
@SpringBootApplication
@EnableHystrix
public class RibbonServiceApplication {
public static void main(String[] args) {
SpringApplication.run(RibbonServiceApplication.class, args);
}
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
改造HelloController
类, 在hello
方法加上@HystrixCommand
注解。 该注解给方法低通了熔断器的能力, 指定fallbackMethod
熔断方法, 当远程服务调用时候后执行熔断方法:
@RestController
public class HelloController {
private final RestTemplate restTemplate;
@Autowired
public HelloController(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
@HystrixCommand(fallbackMethod = "helloError")
@GetMapping("/hello")
public String hello(@RequestParam("name") String name) {
return restTemplate.getForObject("http://HELLO-ERUEKA-CLIENT/hello?name=" + name, String.class);
}
public String helloError(String name) {
return String.format("Hello, %s! Access remote service fail!", name);
}
}
启动ribbon-service项目,在浏览器访问 http://localhost:8050/hello?name=Mars:
Hello, My name is Mars, I'm from port: 8040
这时候我们关闭eureka-service项目, 再次访问 http://localhost:8050/hello?name=Mars:
Hello, Mars! Access remote service fail!
这就说明eureka-service服务不可达时, ribbon-service调用接口会快速失败, 直接调用熔断方法, 而不是等待响应超时, 这很好的控制了容器的线程阻塞。
Feign已经集成了断路器, 基于feign-service项目进行改造, 只需要在@FeignClient注解中加上fallback
的指定类就行:
@FeignClient(value = "hello-eureka-client", fallback = FeignServiceHystrix.class)
public interface FeignService {
@GetMapping(value = "/hello")
String hello(@RequestParam(value = "name") String name);
}
FeignServiceHystrix
需要实现FeignService
接口,并注入到Ioc容器中:
@Component
public class FeignServiceHystrix implements FeignService {
@Override
public String hello(String name) {
return String.format("Hello, %s! Access remote service fail!", name);
}
}
先启动eureka-client和eureka-server项目, 然后再启动feign-service项目,在浏览器访问 http://localhost:8080/hello?name=Mars:
Hello, My name is Mars, I'm from port: 8040
这时候我们关闭eureka-service项目, 再次访问 http://localhost:8080/hello?name=Mars:
Hello, Mars! Access remote service fail!
以上就是Spring Cloud 中Hystrix有什么用,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://my.oschina.net/u/4198363/blog/3125657