疯狂Spring Cloud连载(19)Spring Cloud整合Hystrix
来源:互联网 发布:淘宝客优惠券采集工具 编辑:程序博客网 时间:2024/06/06 04:06
本文节选自《疯狂Spring Cloud微服务架构实战》
Spring Cloud教学视频:http://blog.csdn.net/boxiong86/article/details/78399104
Spring Cloud电子书:http://blog.csdn.net/boxiong86/article/details/78488226
19 Spring Cloud中使用Hystrix
Hystrix主要用于保护调用服务的一方,如果被调用的服务发生故障,符合一定条件,就开启断路器,对调用的程序进行隔离。在开始讲述本章的内容前,先准备测试项目,本章例子所使用的项目如下:
spring-hystrix-server:Eureka服务器,端口为8761,代码目录codes\06\6.4\spring-hystrix-server。
spring-hystrix-provider:服务提供者,本例只需要启动一个实例,端口为8080,默认提供“/person/{personId}”服务,根据personId参数返回一个Person实例,另外再提供一个“/hello”服务,返回普通的字符串。代码目录为codes\06\6.4\spring-hystrix-provider。
spring-hystrix-invoker:服务调用者,9000端口,代码目录codes\06\6.4\spring-hystrix-invoker。
整合Hystrix
为服务调用者(spring-hystrix-invoker)项目添加依赖,添加后的依赖如下:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
在服务调用者的应用启动类中,加入启用断路器的注解,请见以下代码片断:
@SpringBootApplication
@EnableDiscoveryClient
@EnableCircuitBreaker
public class InvokerApplication {
@LoadBalanced
@Bean
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(InvokerApplication.class, args);
}
}
新建服务类,在服务方法中调用服务,请见代码清单6-17。
代码清单6-17:
codes\06\6.4\spring-hystrix-invoker\src\main\java\org\crazyit\cloud\PersonService.java
@Component
public class PersonService {
@Autowired
private RestTemplate restTemplate;
@HystrixCommand(fallbackMethod = "getPersonFallback")
public Person getPerson(Integer id) {
//使用RestTemplate调用Eureka服务
Person p = restTemplate.getForObject(
"http://spring-hystrix-provider/person/{personId}",
Person.class, id);
return p;
}
/**
*回退方法,返回一个默认的Person
*/
public Person getPersonFallback(Integer id) {
Person p = new Person();
p.setId(0);
p.setName("Crazyit");
p.setAge(-1);
p.setMessage("request error");
return p;
}
}
服务类中注入了RestTemplate,服务方法使用了@HystrixCommand注解进行修饰,并且配置了回退方法。@HystrixCommand注解由Hystrix的“javanica”项目提供,该项目主要是为了简化Hystrix的使用。被@HystrixCommand修饰的方法,Hystrix(javanica)会使用AspectJ对其进行代理,Spring会将相关的类转换为Bean放到容器中,在Spring Cloud中,我们无需过多关心Hystrix的命令管理。
接下来,编写控制器,调用服务类的方法,请见代码清单6-18。
代码清单6-18:
codes\06\6.4\spring-hystrix-invoker\src\main\java\org\crazyit\cloud\InvokerController.java
@RestController
@Configuration
public class InvokerController {
@Autowired
private PersonService personService;
@RequestMapping(value = "/router/{personId}", method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
public Person router(@PathVariable Integer personId) {
Person p = personService.getPerson(personId);
return p;
}
}
控制器实现较为简单,直接注入PersonService,调用方法即可,按以下步骤启动集群:
启动“spring-hystrix-server”,本例中配置端口为8761。
启动“spring-hystrix-provider”,启动一个实例,端口为8080。
启动“spring-hystrix-invoker”,端口为9000。
打开浏览器访问:http://localhost:9000/router/1,输出如下:
{"id":1,"name":"Crazyit","age":33,"message":"http://localhost:8080/person/1"}
停止服务提供者(spring-hystrix-provide),即停止8080端口,再访问9000端口的地址,输出如下:
{"id":0,"name":"Crazyit","age":-1,"message":"request error"}
根据输出可知,由于调用失败,触发了回退方法。
命令配置
Spring Cloud中使用@HystrixCommand来声明一个命令,命令的相关配置,也可以在该注解中进行,以下的代码片断,配置了几个属性:
/**
*测试配置,对3个key进行命名
*设置命令执行超时时间为1000毫秒
*设置命令执行的线程池大小为1
*/
@HystrixCommand(
fallbackMethod="testConfigFallback", groupKey="MyGroup",
commandKey="MyCommandKey", threadPoolKey="MyCommandPool",
commandProperties={
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",
value = "1000")
},
threadPoolProperties={
@HystrixProperty(name = "coreSize",
value = "1")
})
除了以上的几个配置外,@HystrixCommand注解还可以使用ignoreExceptions来处理异常的传播,请见以下代码片断:
/**
*声明了忽略MyException,如果方法抛出MyException,则不会触发回退
*/
@HystrixCommand(ignoreExceptions = {MyException.class},
fallbackMethod="testExceptionFallBack")
public String testException() {
throw new MyException();
}
Hystrix的命令、线程配置较多,由于篇幅所限,本小节仅简单地列举几个,读者可举一反三,按需要进行配置。
默认配置
对于一些默认的配置,例如命令组的key等,可以使用@DefaultProperties注解,这样就减少了@HystrixCommand注解的代码量。以下代码片断展示如何使用@DefaultProperties:
@DefaultProperties(groupKey="GroupPersonKey")
public class PersonService {
@HystrixCommand // group key将使用“GroupPersonKey”
public String hello() {
return "";
}
}
除了定义GroupKey外,还支持@HystrixCommand的其余配置,例如线程属性、命令属性等。
本文节选自《疯狂Spring Cloud微服务架构实战》
Spring Cloud教学视频:http://blog.csdn.net/boxiong86/article/details/78399104
Spring Cloud电子书:http://blog.csdn.net/boxiong86/article/details/78488226
本书代码共享地址:https://gitee.com/yangenxiong/SpringCloud
- 疯狂Spring Cloud连载(19)Spring Cloud整合Hystrix
- 疯狂Spring Cloud连载(21)Feign与Hystrix整合
- 疯狂Spring Cloud连载(14)Spring Cloud整合Feign
- 疯狂Spring Cloud连载(15)第一个Hystrix程序
- 疯狂Spring Cloud连载(16)Hystrix运作流程
- 疯狂Spring Cloud连载(1)Spring Cloud概述
- 疯狂Spring Cloud连载(8)Spring Cloud与Ribbon
- 疯狂Spring Cloud连载(17)Hystrix属性配置与回退
- 疯狂Spring Cloud连载(18)Hystrix断路器的开启和关闭
- 疯狂Spring Cloud连载(20)Hystrix缓存与合并请求
- Spring Cloud Hystrix--熔断器
- spring cloud-hystrix
- spring cloud Hystrix
- spring-cloud-hystrix(一)
- Spring Cloud Hystrix Dashboard
- spring cloud hystrix
- spring cloud hystrix dashboard
- Spring Cloud Hystrix
- PE文件结构详解(五)延迟导入表
- Python爬取个人博客,带你制作高逼格的数据聚合云图
- C/C++中有符号数和无符号数的运算
- ccache的使用
- ios11适配 以及会有的坑
- 疯狂Spring Cloud连载(19)Spring Cloud整合Hystrix
- PE文件结构详解(六)重定位
- android EditView无法编辑
- 【财富空间】陈春花:互联网的下半场,是一个高效响应和创造顾客需求的逻辑
- c# ThoughtWorks.QRCode 二维码
- 【智能金融】信贷工厂:未来银行风控体系体系搭建
- 查看CentOS版本
- app应用软件开发流程是怎样的?
- 利用HTML5 figure标签插入图片