springcloud系列文章已经出到hystrix,中间知识追寻者跑去学了其它知识,回来感受spingcloud系列出的也很多了;须要彻底理解这篇文章对于初学者须要有必定的基础知识,若是看不懂请移步知识追寻者的springcloud专栏进行学习;学完本篇,你将得到学会使用Hstrix进行服务容错,学会Hystrix熔断监控;能学完本篇内容请留下赞呦!!!!java
首先须要搭建工程hystrix-client
引入eureka和 hystrix依赖git
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency> </dependencies>
在service层定义一个得到用户的方法getUser()用于提供服务,方法内容对用户名进行判断,若是是zszxz
, 返回回用户说明,不然抛出异常;定义默认用户方法defaultUser()用于容错处理,在得到用户方法上面添加注释@HystrixCommand
而且将容错回调指向defaultUser()方法;有关注释详细使用请读者参考以下官方文档;github
/** * @Author lsc * <p> </p> */ @Component public class UserService { // 为getUser方法添加容错回调处理 @HystrixCommand(fallbackMethod = "defaultUser") public String getUser(String params){ if (params.equals("zszxz")){ return "the user is zszxz"; }else { // 抛出异常时会直接调用fallbackMethod中指定的方法 throw new RuntimeException(); } } /* * * @Author lsc * <p> 出错回调处理</p> * @Param [params] * @Return java.lang.String */ public String defaultUser(String params){ return "it is the user thar is not exist in project"; } }
表现层直接调用服务层方法,参数为用户名username
spring
/** * @Author lsc * <p> </p> */ @RestController public class UserController { @Autowired UserService userService; @GetMapping("user") public String getUser(String username){ return userService.getUser(username); } }
配置文件中指定端口8094
, 而且指定应用名称为 hystrix-client
, 后面是向eureka-server进行服务注册;app
server: port: 8094 spring: application: name: hystrix-client # 应用名称 eureka: client: service-url: # 服务注册地址 defaultZone: http://peer1:10081/eureka/,http://peer2:10082/eureka/,http://peer3:10083/eureka/
在启动类上添加注释@EnableHystrix
,表示启用 hystrix功能;ide
/** * @Author lsc * <p> </p> */ @SpringBootApplication @EnableHystrix // 启用 hystrix @EnableDiscoveryClient// 服务注册发现 public class HystrixApp { public static void main(String[] args) { SpringApplication.run(HystrixApp.class, args); } }
启动eureka-server, eureka-client, hystrix-client工程;访问地址http://localhost:8094/user?user=zszxz;得出正确访问结果以下;函数
修改用户名参数不是zszxz 发现报的不是异常,而是defualtuser中默认的容错处理,说明使用hystrix成功;spring-boot
学会了简单使用hystrix, 但远远不够,一般Feign中自带了hystrix功能,这边须要使用Feign进行开启hystrix,而后进行容错处理;学习
在以前的eureka-client
表现层中添加一个 getFH() 方法用于Feign客户端调用;
/* * * @Author lsc * <p> feign-hystix 集成测试</p> * @Param [username] * @Return java.lang.String */ @GetMapping("zszxz/fh") public String getFH(String username){ if (username.equals("zszxz")){ return "the user is zszxz on fh project"; }else { throw new RuntimeException(); } }
新建工程feign-hystrix
用于集成测试,依赖引入 openfeign 便可;
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> </dependencies>
service层使用FeignClient进行远程过程调用,而且指定 回调类,其内容在3.4节
/** * @Author lsc * <p> </p> */ @FeignClient( name = "eureka-client", value = "eureka-client", fallback = FHServiceFallback.class) public interface FHService { @GetMapping("zszxz/fh") public String getFeign(String username); }
回调函数实现FHService接口,而且在类上添加注解@Component
表示会被spirng IO容器扫描注入;
/** * @Author lsc * <p> </p> */ @Component public class FHServiceFallback implements FHService { @Override public String getFeign(String username) { return "sorry feilure,you can try it again"; } }
controller层直接能够调用service层接口;
/** * @Author lsc * <p> feign 表现层 </p> */ @RestController public class FHController { @Autowired FHService fhService; @GetMapping("zszxz/feign") public String getFeign(String username){ // 调用 getFeign方法 return fhService.getFeign(username); } }
配置文件中指定端口,应用名称,关键是要开启feign自带的hystix 功能
server: port: 8095 spring: application: name: hystrix-feign # 应用名称 eureka: client: service-url: # 服务注册地址 defaultZone: http://peer1:10081/eureka/,http://peer2:10082/eureka/,http://peer3:10083/eureka/ # 开启hystix feign: hystrix: enabled: true
启动类中使用注解 @EnableFeignClients
开启Feign功能;
/** * @Author lsc * <p> </p> */ @SpringBootApplication @EnableFeignClients public class FeignHystrixApp { public static void main(String[] args) { SpringApplication.run(FeignHystrixApp.class,args); } }
正常访问结果正确,结果以下
输入用户名非zszxz,进行容错处理友好提示以下,说明feign集成使用hystix成功;
学会使用hystix仍是皮毛中的皮毛,咱们还须要对hystrix进行容错监控;
在feign-hystrix
工程中添加 actuator
, hystrix
依赖;
pom.xml
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency>
appication.yml配置文件中指定暴露端点;
management: endpoints: web: exposure: include: hystrix.stream
启动类中添加@EnableHystrix
注解表示开启Hystrix
/** * @Author lsc * <p> </p> */ @SpringBootApplication @EnableFeignClients @EnableHystrix public class FeignHystrixApp { public static void main(String[] args) { SpringApplication.run(FeignHystrixApp.class,args); } }
新建工程hystrix-dashboard 用于专门的容错监控;引入依赖以下;
pom.xml
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId> </dependency> </dependencies>
application.yml 指定当前应用名称和端口;
server: port: 8096 spring: application: name: hystrix-dashboard # 应用名称 eureka: client: service-url: # 服务注册地址 defaultZone: http://peer1:10081/eureka/,http://peer2:10082/eureka/,http://peer3:10083/eureka/
启动类上表名注解 @EnableHystrixDashboard
表示启动容错监控;
/** * @Author lsc * <p> </p> */ @SpringBootApplication @EnableDiscoveryClient @EnableHystrixDashboard//开启监控 public class HystixDashboard { public static void main(String[] args) { SpringApplication.run(HystixDashboard.class,args); } }
访问 http://localhost:8096/hystrix 出现一头豪猪图页面表示初步配置成功;
页面中的输入url有以下三种:
在页面输入地址 http://localhost:8095/actuator/hystrix.stream 访问单体应用,页面会处于loading状态
当咱们访问 http://localhost:8095/zszxz/feign 会出现仪表盘,其具体的监控指标内容意义请读者参考官方文档,再也不详细说明。
在第四节中读者已经学会如何使用hystix dashboard进行容错监控;可是缺点也很明显,那就是一个单体应用须要开启一个dashboard 页面;若是使用turbine进行集成管理,那么全部hystix应用都会显示在一个 dashboard上,方便咱们查阅,排查;
新建 hystrix-feign2 工程;修改配置文件端口为8098,应用名称为hystrix-feign2,其它内容跟 hystrix-feign 工程同样;
新建turbine-monitor工程,添加新依赖 turbine
pom.xml
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-turbine</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId> </dependency> </dependencies>
application.xml 中内容以下 指定了监控的hystrix应用是hystrix-feign
和 hystrix-feign
, 而且采用默认名称方式;
server: port: 8097 spring: application: name: turbine-monitor # 应用名称 eureka: client: service-url: # 服务注册地址 defaultZone: http://peer1:10081/eureka/,http://peer2:10082/eureka/,http://peer3:10083/eureka/ turbine: appConfig: hystrix-feign,hystrix-feign2 clusterNameExpression: "'default'"
启动类中添加新的注解@EnableTurbine
表示开启turbine 功能;
/** * @Author lsc * <p> </p> */ @SpringBootApplication @EnableDiscoveryClient @EnableTurbine @EnableHystrixDashboard public class TurbineApp { public static void main(String[] args) { SpringApplication.run(TurbineApp.class, args); } }
关于源码请移步专栏说明便可得到知识追寻者springcloud所有学习内容;