博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring Cloud学习笔记23——如何集成 Hystrix
阅读量:3941 次
发布时间:2019-05-24

本文共 3366 字,大约阅读时间需要 11 分钟。

Hystrix会监控微服务的调用状况,失败调用达到一定阀值时,会启用熔断机制

开发环境

  • JDK8+
  • Gradle4+
  • Spring Boot 2.0.0.M3
  • Spring Cloud Starter Netflix Eureka Client Finchley.M2
  • Spring Cloud Starter OpenFeign Finchley.M2
  • Spring Cloud Starter Netflix Hystrix Finchley.M2

创建项目

以之前的micro-weather-eureka-client-feign为蓝本,创建micro-weather-eureka-client-feign-hystrix项目

在这里插入图片描述

修改源码

修改build.gradle配置,添加Hystrix依赖:

//依赖关系dependencies {
//Eureka Client compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-client') //Feign compile('org.springframework.cloud:spring-cloud-starter-openfeign:2.0.0.M3') //Hystrix compile('org.springframework.cloud:spring-cloud-starter-netflix-hystrix') //该依赖用于测试阶段 testCompile('org.springframework.boot:spring-boot-starter-test')}

修改com.study.spring.cloud.weather包下的Application类,加入@EnableCircuitBreaker注解,开启Hystrix的使用:

package com.study.spring.cloud.weather;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;import org.springframework.cloud.netflix.feign.EnableFeignClients;/* * @SpringBootApplication注解声明Spring Boot应用 * 作用等同于@Configuration, @EnableAutoConfiguration, @ComponentScan, * 简化Spring配置*/@SpringBootApplication//启用可发现的客户端@EnableDiscoveryClient//启用Feign@EnableFeignClients//启用Hystrix@EnableCircuitBreaker//Application类一定要处于整个工程的根目录下,这样它才能根据配置去扫描子节点下的Spring的Beanpublic class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args); }}

这里我们还可以使用Spring Cloud应用中的@SpringCloudApplication注解来修饰应用主类,该注解中包含了上面我们所引用的@SpringBootApplication@EnableDiscoveryClient@EnableCircuitBreaker三个注解,这也意味着一个Spring Cloud标准应用应包含服务发现以及断路器。

修改com.study.spring.cloud.weather.controller包下的CityController类,为具体执行逻辑的函数上增加@HystrixCommand注解来指定服务降级方法:

package com.study.spring.cloud.weather.controller;import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;import com.study.spring.cloud.weather.service.CityClient;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;//用于处理rest请求的controller@RestControllerpublic class CityController {
@Autowired private CityClient cityClient; @GetMapping("/cities") //启用断路器 @HystrixCommand(fallbackMethod = "defaultCities") public String listCity() {
//通过Feign客户端来查找 String body=cityClient.listCity(); return body; } public String defaultCities(){
return "City Data Server is down!"; } }

上面我们使用了@HystrixCommand来将具体执行逻辑的函数包装成了Hystrix命令(Hystrix命令就是HystrixCommand,它用来封装具体的依赖服务调用逻辑),这里除了定义服务降级之外,Hystrix框架会自动的为这个函数实现调用的隔离。所以,依赖隔离、服务降级在使用时候都是一体化实现的,这样利用Hystrix来实现服务容错保护在编程模型上就非常方便,并且考虑更为全面。

修改application.properties配置文件:

#应用名称spring.application.name=micro-weather-eureka-client-feign-hystrix#注册服务器的URLeureka.client.service-url.defaultZone=http://localhost:8761/eureka/#请求服务时的超时时间feign.client.config.feignName.connect-timeout=5000#读数据时的超时时间feign.client.config.feignName.read-timeout=5000

运行

先在IDE上运行micro-weather-eureka-server

再通过命令行指定端口(如8085)运行msa-weather-city-eureka

最后在IDE上运行micro-weather-eureka-client-feign-hystrix

访问http://localhost:8080/cities页面:

在这里插入图片描述

msa-weather-city-eureka断掉,再刷新http://localhost:8080/cities页面:

在这里插入图片描述

转载地址:http://uwswi.baihongyu.com/

你可能感兴趣的文章
提高网页在IE和Firefox上的…
查看>>
提高网页在IE和Firefox上的…
查看>>
php的正则表达式 '/\b\w…
查看>>
ThinkPHP的标签制作及标签调用解析…
查看>>
jQuery.proxy()代理、回调方法
查看>>
php操作memcache的使用测试总结
查看>>
JS创建类和对象
查看>>
完整ASCII字符表(转)
查看>>
jquery事件重复绑定解决办法
查看>>
jQuery.extend 函数详解
查看>>
mysqli_query和mysql_query有何区…
查看>>
mysqli->multi_query()多条语句的…
查看>>
php引用(&)变量引用,函数引用,对…
查看>>
[转]yii执行流程(一 目录文…
查看>>
无需重启服务器让系统环境变量生效…
查看>>
配置CakePHP
查看>>
JQuery中$.ajax()方法参数详…
查看>>
JS 简易滚动条
查看>>
PHP __call()方法
查看>>
JS中的call()和apply()方法
查看>>