Hystrix 概述与入门
Posted 码上筑梦
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Hystrix 概述与入门相关的知识,希望对你有一定的参考价值。
约定
示例项目采用的是Maven父子工程模式,父工程见[]
示例中采用的Spring Boot版本为1.5.9.RELEASE
示例中采用的Spring Cloud版本为Edgware.RELEASE
Hystrix 概述
使用Hystrix实现断路器
本节继续使用前面所创建的所有项目并部署到服务器启动。同时新增文档服务document-provider子项目,其做法同之前的子项目一样。
在此省略将子项目引入父工程,只贴出本项目依赖,重点关注spring-cloud-starter-hystrix依赖。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!--继承父工程-->
<parent>
<groupId>top.learninghub</groupId>
<artifactId>learninghub-parent</artifactId>
<version>1.0.0.RELEASE</version>
</parent>
<!--GV参数在父模块定义-->
<artifactId>document-provider</artifactId>
<packaging>jar</packaging>
<name>document-provider</name>
<description>document_consumer_micro_service</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--eureka客户端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!--ribbon客户端负载均衡-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
<!--配置中心客户端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!--消息总线,用于获取配置中心更新配置等-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<!--hystrix熔断器-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.0.4</version>
</dependency>
<!--druid链接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.4</version>
</dependency>
<!--druid-启动器-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<!--集成mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!--lombok插件-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.18</version>
<!--<optional>true</optional>-->
</dependency>
<!--json -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.68</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
(1)使用@EnableCircuitBreaker开启断路器
//开启Hystrix断路器,用于服务熔断、降级以及监控
public class DocumentProviderApplication {
public static void main(String[] args) {
SpringApplication.run(DocumentProviderApplication.class, args);
}
}
(2)使用@HystrixCommand注解并添加回调(服务降级)属性
public class DocumentController {
"hystrixFallback_GET") (fallbackMethod =
"/getUserByHystrix/{id}") (
public String getUserByHystrix( ("id") String id){
String s = restTemplate.getForObject("http://USER-PROVIDER/user/getUser/"+id, String.class);
log.info("返回结果是:{}",s);
return s;
}
//服务降级逻辑,需要注意方法参数要与上面方法一致,不然会报错
public String hystrixFallback_GET(String id){
return "请您稍后重试,感谢您的配合!";
}
}
(3)对用户服务编写相关接口
public class UserController {
public String getUser( String id){
try {
Thread.sleep(50);
//Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
return "我是码上筑梦";
}
}
}
(1)停止user-provider服务
(2)使用Thread.sleep()方法模拟服务调用超时
以上是关于Hystrix 概述与入门的主要内容,如果未能解决你的问题,请参考以下文章
Spring Cloud实战之初级入门— 利用Hystrix实现服务熔断与服务监控