一、官网文档阅读

较低级别的服务中的服务故障可能导致级联故障一直到用户。 当对特定服务的调用超过circuitBreaker.requestVolumeThreshold(默认值:20个请求)且失败百分比大于circuit.rolllingStats.timeInMilliseconds定义的滚动窗口中的circuitBreaker.errorThresholdPercentage(默认值:> 50%)时(默认值:10秒) ,电路打开,没有拨打电话。 在出现错误和开路的情况下,开发人员可以提供回退。

二、示例

添加hystrix依赖

        <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>

修改movie服务的controller

 package com.zwjk.cloud.controller;

 import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.zwjk.cloud.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate; /**
* @author : Jixiaohu
* @Date : 2019-04-11.
* @Time : 9:38.
* @Description :
*/
@RestController
public class MovieController { @Autowired
private RestTemplate restTemplate; @Value("${user.userServicePath}")
private String userServicePath; @GetMapping("/movie/{id}")
@HystrixCommand(fallbackMethod = "findByIdFallback")
public User findById(@PathVariable Long id) {
return this.restTemplate.getForObject(this.userServicePath + id, User.class);
} public User findByIdFallback(Long id) {
User user = new User();
user.setId(0L);
user.setName("失败");
return user;
}
}

这边需要注意的是,fallbackMethod方法的返回值和入参,必须和原方法一致,下面进行测试:

可以看到,这里返回了正常的结果,下面停掉user服务

可以看到,这里返回的结果,就是fallbackMethod里面返回的内容,hystrix就起了作用。

下面,继续看一下

Feign Hystrix Support

参考官网使用示例:

 @FeignClient(name = "hello", fallback = HystrixClientFallback.class)
protected interface HystrixClient {
@RequestMapping(method = RequestMethod.GET, value = "/hello")
Hello iFailSometimes();
} static class HystrixClientFallback implements HystrixClient {
@Override
public Hello iFailSometimes() {
return new Hello("fallback");
}
}

在配置文件中,增加开启feign使用断路器

feign:
hystrix:
enabled: true

按照示例,我们在movie服务中,新增UserFeignClientFallback类

 package com.zwjk.cloud.fegin;

 import com.zwjk.cloud.entity.User;
import org.springframework.stereotype.Component; /**
* @author : Jixiaohu
* @Date : 2019-04-17.
* @Time : 18:20.
* @Description :
*/
@Component
public class UserFeignClientFallback implements UserFeignClient {
@Override
public User findById(Long id) {
User user = new User();
user.setId(0L);
return user;
}
}

在fegin上增加相应的注解:

 package com.zwjk.cloud.fegin;

 import com.zwjk.cloud.entity.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody; /**
* @author : Jixiaohu
* @Date : 2019-04-12.
* @Time : 16:50.
* @Description :
*/
@FeignClient(name = "microservice-provider-user", fallback = UserFeignClientFallback.class)
public interface UserFeignClient {
//@PathVariable得设置value
@GetMapping("/simple/{id}")
User findById(@PathVariable("id") Long id); //@PathVariable得设置value
}

启动项目,进行测试,

同样的,停掉user服务,再次访问这个地址:

就可以实现熔断服务

当一个项目中,有个feign时,如何禁用一些feign的hystrix?

查看一下官网文档

通过配置加上feignBuilder,就可以禁用指定fegin的hystrix

查看一下代码实现:

 package com.zwjk.cloud.controller;

 import com.zwjk.cloud.entity.User;
import com.zwjk.cloud.fegin.UserFeignClient2;
import com.zwjk.cloud.fegin.UserFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController; /**
* @author : Jixiaohu
* @Date : 2019-04-11.
* @Time : 9:38.
* @Description :
*/
@RestController
public class MovieController { @Autowired
private UserFeignClient userFeignClient; @Autowired
private UserFeignClient2 userFeignClient2; @GetMapping("/movie/{id}")
public User findById(@PathVariable Long id) {
return this.userFeignClient.findById(id);
} @GetMapping("/{serviceName}")
public String findServiceInfoFromEurekaByServiceName(@PathVariable String serviceName) {
return this.userFeignClient2.findServiceInfoFromEurekaByServiceName(serviceName);
} }

两个fegin:

 package com.zwjk.cloud.fegin;

 import com.zwjk.cloud.entity.User;
import com.zwjk.config.UserConfiguration;
import feign.Param;
import feign.RequestLine;
import org.springframework.cloud.openfeign.FeignClient; /**
* @author : Jixiaohu
* @Date : 2019-04-12.
* @Time : 16:50.
* @Description :
*/
@FeignClient(name = "microservice-provider-user", configuration = UserConfiguration.class, fallback =
UserFeignClientFallback.class)
public interface UserFeignClient {
//@PathVariable得设置value
@RequestLine("GET /simple/{id}")
User findById(@Param("id") Long id); //@PathVariable得设置value }
 package com.zwjk.cloud.fegin;

 import com.zwjk.config.UserConfiguration2;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping; /**
* @author : Jixiaohu
* @Date : 2019-04-13.
* @Time : 11:23.
* @Description :
*/
@FeignClient(name = "xxxx", url = "http://192.168.1.114:8761/", configuration = UserConfiguration2.class)
public interface UserFeignClient2 {
@RequestMapping(value = "/eureka/apps/{serviceName}")
public String findServiceInfoFromEurekaByServiceName(@PathVariable("serviceName") String serviceName);
}

两个fallback

 package com.zwjk.cloud.fegin;

 import org.springframework.stereotype.Component;

 /**
* @author : Jixiaohu
* @Date : 2019-04-17.
* @Time : 18:20.
* @Description :
*/
@Component
public class UserFeignClient2Fallback implements UserFeignClient2 { @Override
public String findServiceInfoFromEurekaByServiceName(String serviceName) {
return "hahahha";
}
}
 package com.zwjk.cloud.fegin;

 import com.zwjk.cloud.entity.User;
import org.springframework.stereotype.Component; /**
* @author : Jixiaohu
* @Date : 2019-04-17.
* @Time : 18:20.
* @Description :
*/
@Component
public class UserFeignClientFallback implements UserFeignClient {
@Override
public User findById(Long id) {
User user = new User();
user.setId(0L);
return user;
}
}

看一下两个配置文件

 package com.zwjk.config;

 import feign.Contract;
import feign.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; /**
* @author : Jixiaohu
* @Date : 2019-04-13.
* @Time : 10:20.
* @Description :
*/
@Configuration
public class UserConfiguration { @Bean
public Contract feignContract() {
return new feign.Contract.Default();
} @Bean
Logger.Level feignLoggerLevel() {
return Logger.Level.FULL;
}
}
 package com.zwjk.config;

 import feign.Feign;
import feign.auth.BasicAuthRequestInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope; /**
* @author : Jixiaohu
* @Date : 2019-04-13.
* @Time : 11:25.
* @Description :
*/
@Configuration
public class UserConfiguration2 {
@Bean
public BasicAuthRequestInterceptor basicAuthRequestInterceptor() {
return new BasicAuthRequestInterceptor("user", "password123");
} @Configuration
public class FooConfiguration {
@Bean
@Scope("prototype")
public Feign.Builder feignBuilder() {
return Feign.builder();
}
}
}

启动服务,进行测试

下面,我们停掉eureka,和user服务,在一次查看返回结果

启用hystrix的返回结果如下:

没有启用hystrix的返回结果,提示500错误

从中,可以看出,hytrix的熔断,可以有很细的粒度。

如果想要查看fallback回退的原因,可以使用可以使用@FeignClient中的fallbackFactory属性。

看一下如何实现?

参考一下官网文档:

先写一个hystrixFactory:

 package com.zwjk.cloud.fegin;

 import com.zwjk.cloud.entity.User;
import feign.hystrix.FallbackFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component; /**
* @author : Jixiaohu
* @Date : 2019-04-17.
* @Time : 19:37.
* @Description :
*/
@Component
public class UserFeginClientFactory implements FallbackFactory<UserFeignClient> { private static final Logger Log = LoggerFactory.getLogger(UserFeginClientFactory.class); @Override
public UserFeignClient create(Throwable cause) {
UserFeginClientFactory.Log.info("fallback:reason was : {}", cause.getMessage());
return id -> {
User user = new User();
user.setId(-1L);
user.setName("haha");
return user;
};
}
}

修改一下原feignClient

 package com.zwjk.cloud.fegin;

 import com.zwjk.cloud.entity.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody; /**
* @author : Jixiaohu
* @Date : 2019-04-12.
* @Time : 16:50.
* @Description :
*/
@FeignClient(name = "microservice-provider-user", fallbackFactory =
UserFeginClientFactory.class)
public interface UserFeignClient {
//@PathVariable得设置value
@GetMapping("/simple/{id}")
User findById(@PathVariable("id") Long id); //@PathVariable得设置value
}

启动user服务,movie服务,先正常访问:

然后关闭user服务,会发现控制台开始打印日志:

这是为什么呢?因为此时,断路器并没有打开,实际的原因是TimeoutException

当断路器打开时,就会答应异常的信息。

从零开始学spring cloud(十) -------- hystrix简单代码示例的更多相关文章

  1. 从零开始学spring cloud(十一) -------- hystrix监控

    一.官方文档阅读 服务启动后,可以通过/health和hystrix.stream查看效果,实际上,访问上述两个地址,会出现404,这是因为spring boot版本的问题, 我在这里使用的sprin ...

  2. 从零开始学spring cloud(七) -------- Spring Cloud OpenFegin

    一.OpenFegin 介绍 Feign是一个声明性的Web服务客户端. 它使编写Web服务客户端变得更容易. 要使用Feign,请创建一个界面并对其进行注释. 它具有可插入的注释支持,包括Feign ...

  3. 从零开始学spring cloud(六) -------- Ribbon

    一.Ribbon介绍 Ribbon就是客户端侧负责均衡实现的一种方式,那么Ribbon是什么呢? Ribbon是Netflix发布的云中间层服务开源项目,其主要功能是提供客户端侧负载均衡算法.Ribb ...

  4. 从零开始学spring cloud(四) -------- 基础项目搭建

    1.创建一个spring cloud项目 1.1.使用工具创建--idea 点击creat new project,选择spring initializr 点击next,选择下一步 填入自己的Grou ...

  5. 从零开始学spring cloud(五) -------- 将服务注册到Eureka上

    一.开发前准备工作: 官方文档地址:https://cloud.spring.io/spring-cloud-static/spring-cloud-netflix/2.1.0.RELEASE/mul ...

  6. 从零开始学spring cloud(一) -------- spring cloud 简介

    1.微服务简介 1.1.单体架构 一个归档包(例如war格式)包含了应用所有功能的应用程序,我们通常称之为单体应用.架构单体应用的方法论,我们称之为单体应用架构. 缺点:1. 复杂性高以笔者经手的一个 ...

  7. 从零开始学spring cloud(九) -------- 超时机制,断路器模式介绍

    目前存在的问题: 现在我们假设一下,服务提供者响应非常缓慢,那么消费者对提供者的请求就会被强制等待,直到服务返回.在高负载场景下,如果不做任何处理,这种问题很可能造成所有处理用户请求的线程都被耗竭,而 ...

  8. 从零开始学spring cloud(八) -------- Eureka 高可用机制

    一.Eureka高可用机制介绍 Eureka服务器没有后端存储,但注册表中的服务实例都必须发送心跳以使其注册保持最新(因此可以在内存中完成). 客户端还有一个Eureka注册的内存缓存(因此,他们不必 ...

  9. 从零开始学spring cloud(三) -------- Eureka简介

    1.服务发现组件:Eureka Eureka的开源文档介绍地址:https://github.com/Netflix/eureka/wiki/Eureka-at-a-glance What is Eu ...

随机推荐

  1. Windows和MacOS的比较——不断完善和补充,欢迎吐槽

    1. 鼠标滚轮的方向不一样,Windows上滚轮朝下,页面滚动条也会朝下.而Mac上则相反. 2. Windows上有Home和End键,经常可以Ctrl+Home,Ctrl+End,Ctrl+Shi ...

  2. IO多路复用(Python)

    1. select: 监听多个文件描述符(当文件描述符条件不满足时,select会阻塞),当某个文件描述符状态改变后,将该文件描述符添加到对应返回的列表 调用: fd_r_list, fd_w_lis ...

  3. Ubuntu系统配置

    0.基本配置 0.1初始设置 (1)开户root账号并重启系统: 打开gedit /usr/share/lightdm/lightdm.conf.d/50-ubuntu.conf 添加greeter- ...

  4. winform界面特效470多例

    一共470多例winform 界面特效的源码. 实例030 窗口颜色的渐变 实例说明 在程序设计时,可以通过设置窗体的BackColor属性来改变窗口的背景颜色.但是这个属性改变后整个窗体的客户区都会 ...

  5. iOS XML解析使用-韩国庆

    欢迎-------(北京-iOS移动开发金牌教师QQ:2592675215)韩老师给你带来XML解析课程 今天给大家讲解下xml解析的第三方简单用法:首先我解释下,json和xml解析格式. JSON ...

  6. Windows下MongoDB的安装

    1.安装Window2008 R2 并安装 SP1(我安装的是英文版,之前有安装中文版出错) 2.安装vc——redist_x64.exe(不然mongo运行不起来),没安装Sp1前,安装此部会出错. ...

  7. codeblock 生成和使用makefile

    下载cbp2make 文件名:cbp2make-stl-rev138.tar.gz 里面有个cbp文件用codeblock打开,编译,生成的bin目录下有个执行文件. 使用命令生成Makefile . ...

  8. 记一次bond引起的网络故障

    本案中3个关键服务器 物理服务器:192.168.6.63,简称P,(Physical server) KVM-VM:192.168.6.150,是物理服务器P上的一个KVM虚机,简称VM NAS:外 ...

  9. Kafka(2)--kafka基本原理之消息的分发与接收

    关于 Topic 和 Partition Topic 在 kafka 中,topic 是一个存储消息的逻辑概念,可以认为是一个消息集合.每条消息发送到 kafka 集群的消息都有一个类别.物理上来说, ...

  10. 将String类型的json字符串转换成java对象

    1,import com.fasterxml.jackson.databind.ObjectMapper; ObjectMapper mapper = new ObjectMapper(); Mycl ...