Feign Hystrix
1、Feign整合Hystrix
- 添加依赖
- 编写接口与实现回退
1.1、调用者引入依赖
<!-- Feign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
1.2、启动类使用注解
package com.atm.cloud;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableEurekaClient
@EnableCircuitBreaker//打开Hystrix断路器
@ServletComponentScan//扫描缓存
@EnableFeignClients//打开Feign注解
public class HystrixInvokerApp {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
new SpringApplicationBuilder(HystrixInvokerApp.class).web(true).run(
args);
}
}
1.3、新建Feign接口(测试回退)
1.3.1、回顾服务提供者MyRestController
- 去调用服务提供者的接口,以下代码是服务提供者的接口代码
package com.atm.cloud;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MyRestController {
@RequestMapping(value = "/person/{personId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public Person findPerson(@PathVariable("personId") Integer personId) {
Person person = new Person();
person.setId(personId);
person.setAge(18);
person.setName("atm");
return person;
}
@GetMapping("/hello")
@ResponseBody
public String sayHello(){
return "Hello World!";
}
}
1.3.2、回顾服务调用者HelloClient
package com.atm.cloud.feignCtr;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@FeignClient("atm-eureka-hystrix-provider")
public interface HelloClient {
@RequestMapping(method=RequestMethod.GET,value="/hello")
String sayHello();
}
1.3.3、回顾服务调用者FeignController
package com.atm.cloud.feignCtr;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class FeignController {
@Autowired
private HelloClient helloClient;
@RequestMapping(value = "ivHello", method = RequestMethod.GET)
public String ivHello() {
return helloClient.sayHello();
}
}
1.3.4、修改application.yml
server:
port: 9000
spring:
application:
name: atm-eureka-hystrix-invoker
##开启feign开关
feign:
hystrix:
enabled: true
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
1.3.5、新增HelloClientFallBack
package com.atm.cloud.feignCtr;
import org.springframework.stereotype.Component;
//HelloClient类去Spring容器中寻找HelloClientFallBack
//所以添加注解Component
@Component
public class HelloClientFallBack implements HelloClient {
public String sayHello() {
return "fallBack Hello";
}
}
1.3.6、修改HelloClient
package com.atm.cloud.feignCtr;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
//当HellClien访问atm-eureka-hystrix-provider失败时,需要有一个后备服务
//后备服务需要实现该接口
@FeignClient(name = "atm-eureka-hystrix-provider", fallback = HelloClientFallBack.class)
public interface HelloClient {
@RequestMapping(method = RequestMethod.GET, value = "/hello")
String sayHello();
}
1.4、测试断路器
断路器打开的要求
- 10秒内连续20个同样的请求
- 失败率过50%
断路器关闭
- 休眠期5分钟
- 5分钟后尝试请求,请求成功则关闭
1.4.1、服务提供者MyRestController
package com.atm.cloud;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MyRestController {
@GetMapping("/tmhello")
@ResponseBody
public String timeOutHello() throws InterruptedException {
// 此方法处理最少需要1s
Thread.sleep(1000);
return "TimeOut Hello";
}
}
1.4.2、服务调用者HelloClient
package com.atm.cloud.feignCtr;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
//当HellClien访问atm-eureka-hystrix-provider失败时,需要有一个后备服务
//后备服务需要实现该接口
@FeignClient(name = "atm-eureka-hystrix-provider", fallback = HelloClientFallBack.class)
public interface HelloClient {
@RequestMapping(method = RequestMethod.GET, value = "/tmhello")
String toHello();
}
package com.atm.cloud.feignCtr;
import org.springframework.stereotype.Component;
@Component
public class HelloClientFallBack implements HelloClient {
public String toHello() {
return "fallBack timeOut Hello";
}
}
1.4.3、服务调用者FeignController
package com.atm.cloud.feignCtr;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.netflix.hystrix.HystrixCircuitBreaker;
import com.netflix.hystrix.HystrixCommandKey;
@RestController
public class FeignController {
@Autowired
private HelloClient helloClient;
@RequestMapping(value = "tmHello", method = RequestMethod.GET)
public String tmHello() {
String result = helloClient.toHello();
HystrixCircuitBreaker breaker = HystrixCircuitBreaker.Factory
.getInstance(HystrixCommandKey.Factory
.asKey("HelloClient#toHello()"));
System.out.println("断路器状态:" + breaker.isOpen());
return result;
}
}
1.4.4、服务调用者application.yml
server:
port: 9000
spring:
application:
name: atm-eureka-hystrix-invoker
feign:
hystrix:
enabled: true
hystrix:
command:
##全局方法使用default
HelloClient#toHello():
execution:
isolation:
thread:
##超时时间
timeoutInMilliseconds: 500
circuitBreaker:
##每秒3次请求
requestVolumeThreshold: 3
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
1.4.5、服务调用者HttpClientMain
package com.atm.cloud.feignCtr;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class HttpClientMain {
public static void main(String[] args) throws Exception{
final CloseableHttpClient httpclient = HttpClients.createDefault();
final String url = "http://localhost:9000/tmHello";
for(int i = 0; i < 6; i++) {
Thread t = new Thread() {
@Override
public void run() {
try {
HttpGet httpget = new HttpGet(url);
HttpResponse response = httpclient.execute(httpget);
System.out.println(EntityUtils.toString(response.getEntity()));
} catch (Exception e ) {
e.printStackTrace();
}
}
};
t.start();
}
Thread.sleep(15000);
}
}
1.5、Hystrix监控
- 客户端被监控
- 谁去监控客户端,一般由其他项目监控,这里新建一个maven项目进行监控
1.5.1、服务提供者引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>1.5.3.RELEASE</version>
</dependency>
1.5.2、监控引入依赖
<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 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.atm.cloud</groupId>
<artifactId>atm_eureka_hystrix_visit</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>atm_eureka_hystrix_visit Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>1.5.3.RELEASE</version>
</dependency>
</dependencies>
<build>
<finalName>atm_eureka_hystrix_visit</finalName>
</build>
</project>
1.5.3、监控启动
- 监控服务调用者
package com.atm.cloud;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
@SpringBootApplication
@EnableHystrixDashboard
public class HystrixVisitApp {
public static void main(String[] args) {
new SpringApplicationBuilder(HystrixVisitApp.class).properties(
"server.port=8082").run(args);
}
}
- 浏览器访问http://127.0.0.1:9000/hystrix.stream
- 浏览器访问http://127.0.0.1:9000/ivHello
- 浏览器访问http://127.0.0.1:8082/hystrix
- 执行HttpClientMain
Feign Hystrix的更多相关文章
- 笔记:Spring Cloud Feign Hystrix 配置
在 Spring Cloud Feign 中,除了引入了用户客户端负载均衡的 Spring Cloud Ribbon 之外,还引入了服务保护与容错的工具 Hystrix,默认情况下,Spring Cl ...
- Spring Cloud微服务实战:手把手带你整合eureka&zuul&feign&hystrix
转载自:https://www.jianshu.com/p/cab8f83b0f0e 代码实现:https://gitee.com/ccsoftlucifer/springCloud_Eureka_z ...
- springcloud(七) feign + Hystrix 整合 、
之前几章演示的熔断,降级 都是 RestTemplate + Ribbon 和 RestTemplate + Hystrix ,但是在实际开发并不是这样,实际开发中都是 Feign 远程接口调用. ...
- springBoot Feign Hystrix
1.引入依赖包 <!-- 引入关于 hystrix的依赖 --> <dependency> <groupId>org.springframework.cloud&l ...
- Feign + Hystrix 服务熔断和服务降级
本机IP为 192.168.1.102 1. 新建 Maven 项目 feign 2. pom.xml <project xmlns="http://maven.apa ...
- Spring Cloud中五大神兽总结(Eureka/Ribbon/Feign/Hystrix/zuul)
Spring Cloud中五大神兽总结(Eureka/Ribbon/Feign/Hystrix/zuul) 1.Eureka Eureka是Netflix的一个子模块,也是核心模块之一.Eureka是 ...
- Spring Cloud Feign+Hystrix自定义异常处理
开启Hystrix spring-cloud-dependencies Dalston版本之后,默认Feign对Hystrix的支持默认是关闭的,需要手动开启. feign.hystrix.enabl ...
- 深入Eureka/Feign/Hystrix原理学习(1)
第一步: 创建注册中心项目,引入cloud discovery相关依赖. ①在pom文件中引入相关依赖. ②在启动类上加上@EnableEurekaServer注解,标注这是一个注 册中心. ③在ap ...
- feign hystrix 线程池伸缩控制
当前使用的版本 <parent> <groupId>org.springframework.boot</groupId> <artifactId>spr ...
随机推荐
- 【Linux】【Jenkins】Jenkins安装和配置等
Jenkins配置详解: http://lib.csdn.net/article/git/18342 Jenkins介绍 Jenkins是基于Java开发的一种持续集成工具,用于监控持续重复的工作,功 ...
- JavaScript正则表达式以及字符串处理
正则表达式之基本概念 在我们写页面时,往往需要对表单的数据比如账号.身份证号等进行验证,而最有效的.用的最多的便是使用正则表达式来验证.那什么是正则表达式呢? 正则表达式(Regular Expres ...
- 虚拟机安装 gentoo 的时候,通过 filezilla 上传 stage3 文件
最近需要在 虚拟机里面安装gentoo,但因为虚拟机里面自动下载的 stage3 太慢了,所以也在寻找解决办法,最终发现 filezilla 是个好办法. 主要参考 https://www.linux ...
- 笔记:js疑难复习
apply 和 call的区别 call 和 apply 的区别只在于这两个函数接受的参数形式不同 var Person = function(name,age){ this.name = name; ...
- <转载>apache 配置 http://www.blogjava.net/bukebushuo/articles/229103.html
基于 NCSA 服务器的配置文件 由 Rob McCool 编写,龙子翻译 Apache服务器主配置文件. 包括服务器指令的目录设置. 详见 <URL:http://www.apache.org ...
- 利用strstr和sscanf解析GPS信息
比如说我们要做一个GPS导航的项目,需要读取GPS模块以ASCII码的形式发送过来的数据,然后对这些数据进行处理,提取我们需要的信息.这就涉及到很多操作字符串的问题.下面就以此为例,利用strstr函 ...
- Navicat for MySQL 安装和破解(完美)
Navicat for MySQL 安装软件和破解补丁: 链接:https://pan.baidu.com/s/1oKcErok_Ijm0CY9UjNMrnA 密码:4xb1 navicat fo ...
- day27-反射
1.介绍 反射的概念是由Smith在1982年首次提出的,主要是指程序可以访问.检测和修改它本身状态或行为的一种能力(自省).这一概念的提出很快引发了计算机科学领域关于应用反射性的研究.它首先被程序语 ...
- ueditor修改工具栏固定位置和显示空白div
ueditor.all.js
- jquery查找筛选器
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...