Feign + Hystrix 服务熔断和服务降级
本机IP为 192.168.1.102
1. 新建 Maven 项目 feign
2. pom.xml
<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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>
<groupId>com.java</groupId>
<artifactId>feign</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>${project.artifactId}</name> <!-- 配置版本常量 -->
<properties>
<jdk.version>1.8</jdk.version>
<spring.cloud.version>2.0.0.RELEASE</spring.cloud.version>
</properties> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
</parent> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>${spring.cloud.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>${spring.cloud.version}</version>
</dependency> <dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.49</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency> <!-- 热部署 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
<version>1.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency> </dependencies> <build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin> <plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
3. application.yml
server:
port: 80 feign:
hystrix:
enabled: true eureka:
client:
register-with-eureka: false
service-url:
defaultZone: http://192.168.1.102:8080/eureka/
#defaultZone: http://s0.com:8080/eureka/,http://s1.com:8080/eureka/,http://s2.com:8080/eureka/
4. HostService.java
package com.java.feign.service; import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable; import com.alibaba.fastjson.JSONObject; @FeignClient(value = "MICROSERVICE", fallbackFactory = HostServiceFallbackFactory.class)
public interface HostService { @GetMapping("/getHostMessage/{id}")
public JSONObject getHostMessage(@PathVariable(value = "id") String id); }
5. HostServiceFallbackFactory.java
package com.java.feign.service; import org.springframework.stereotype.Component; import com.alibaba.fastjson.JSONObject; import feign.hystrix.FallbackFactory; @Component
public class HostServiceFallbackFactory implements FallbackFactory<HostService> { @Override
public HostService create(Throwable cause) {
return new HostService() { @Override
public JSONObject getHostMessage(String id) {
JSONObject json = new JSONObject();
json.put("id", id);
json.put("description", "服务异常演习专用!");
json.put("msg", cause.getMessage());
return json;
}
};
} }
6. HostController.java
package com.java.feign.controller; 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; import com.alibaba.fastjson.JSONObject;
import com.java.feign.service.HostService; @RestController
public class HostController { @Autowired
private HostService hostService; @GetMapping("/getHostMessage/{id}")
public JSONObject getHostMessage(@PathVariable String id) {
return hostService.getHostMessage(id);
} }
7. FeignStarter.java
package com.java.feign; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.ComponentScan; @EnableEurekaClient
@SpringBootApplication
@EnableFeignClients(basePackages = { "com.java.feign.service" })
@ComponentScan(basePackages = { "com.java.feign" })
public class FeignStarter extends SpringBootServletInitializer { public static void main(String[] args) {
SpringApplication.run(FeignStarter.class, args);
} @Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(FeignStarter.class);
} }
8. 运行测试
启动 Eureka 服务注册中心,参考 https://www.cnblogs.com/jonban/p/eureka.html
启动 MicroService 微服务, 参考 https://www.cnblogs.com/jonban/p/microservice.html
启动 Feign服务,运行 FeignStarter.java
浏览器输入URL
http://192.168.1.102/getHostMessage/hello
或
http://127.0.0.1/getHostMessage/hello
返回数据如下:
{"hostname":"F6RK2EXYAFARPPS","hostAddress":"192.168.1.102","id":"hello"}
截图如下:

搭建成功,程序正常运行。
下面开始测试异常
关掉微服务提供者 microservice 服务器
浏览器输入URL
http://192.168.1.102/getHostMessage/hello
或
http://127.0.0.1/getHostMessage/hello
返回数据如下:
{"msg":"com.netflix.client.ClientException: Load balancer does not have available server for client: MICROSERVICE","description":"服务异常演习专用!","id":"hello"}
截图如下:

服务异常生效,符合预期结果。
.
Feign + Hystrix 服务熔断和服务降级的更多相关文章
- SpringCloud Netflix (五) : Hystrix 服务熔断和服务降级
什么是Hystrix 在分布式环境中,许多服务依赖项中的一些服务依赖不可避免地会失败.Hystrix是一个库,通过添加延迟容忍和容错逻辑,帮助您控制这些分布式服务之间的交互.Hystrix通过隔离服务 ...
- 服务容错保护断路器Hystrix之六:服务熔断和服务降级
伴随着微服务架构被宣传得如火如荼,一些概念也被推到了我们面前(管你接受不接受),其实大多数概念以前就有,但很少被提的这么频繁(现在好像不提及都不好意思交流了).想起有人总结的一句话,微服务架构的特点就 ...
- Hystrix请求熔断与服务降级
Hystrix请求熔断与服务降级 https://www.cnblogs.com/huangjuncong/p/9026949.html SpringCloud实战-Hystrix请求熔断与服务降级 ...
- Spring Cloud实战之初级入门(四)— 利用Hystrix实现服务熔断与服务监控
目录 1.环境介绍 2.服务监控 2.1 加入依赖 2.2 修改配置文件 2.3 修改启动文件 2.4 监控服务 2.5 小结 3. 利用hystrix实现消费服务熔断 3.1 加入服务熔断 3.2 ...
- Hystrix(服务熔断,服务降级)
一.Hystrix 1.服务雪崩 多个微服务之间调用的时候,假设微服务A调用微服务B和微服务C,微服务B和微服务C有调用其他的微服务,这就是所谓的”扇出”,如扇出的链路上某个微服务的调用响应式过长或者 ...
- SpringCloud实战-Hystrix请求熔断与服务降级
我们知道大量请求会阻塞在Tomcat服务器上,影响其它整个服务.在复杂的分布式架构的应用程序有很多的依赖,都会不可避免地在某些时候失败.高并发的依赖失败时如果没有隔离措施,当前应用服务就有被拖垮的风险 ...
- SpringCloud学习之Hystrix请求熔断与服务降级(六)
我们知道大量请求会阻塞在Tomcat服务器上,影响其它整个服务.在复杂的分布式架构的应用程序有很多的依赖,都会不可避免地在某些时候失败.高并发的依赖失败时如果没有隔离措施,当前应用服务就有被拖垮的风险 ...
- Hystrix断路器中的服务熔断与服务降级
一.Hystrix断路器 微服务架构特点就是多服务,多数据源,支撑系统应用.这样导致微服务之间存在依赖关系.如果其中一个服务故障,可能导致系统宕机,这就是所谓的雪崩效应. 1.为什么需要断路器 服务雪 ...
- Spring Cloud Hystrix 请求熔断与服务降级
在Java中,每一个HTTP请求都会开启一个新线程.而下游服务挂了或者网络不可达,通常线程会阻塞住,直到Timeout.你想想看,如果并发量多一点,这些阻塞的线程就会占用大量的资源,很有可能把自己本身 ...
随机推荐
- Linux学习常用命令大全
Linux知识大全 转载须说明出处,整理不易 一.常用的linux命令 1.2 ls 命令说明 1.3 ls 通配符的使用 2.切换目录cd命令 3.创建和删除文件操作 4.移动和拷贝文件 4.3.m ...
- java.lang.ClassCastException: java.util.ArrayList cannot be cast to com.github.pagehelper.Page
出现这个错误,首先看配置mybatis-config.xml中的<plugins> <plugin interceptor="com.github.pagehelper.P ...
- POJ1030 Rating
题目来源:http://poj.org/problem?id=1030 题目大意:有100支队伍(编号1->100),有两场比赛.以下表的形式列出了两场比赛的名次.(有的队伍没有参赛或只参加了一 ...
- 牛客假日团队赛1 I.接机
链接: https://ac.nowcoder.com/acm/contest/918/I 题意: 一场别开生面的牛吃草大会就要在Farmer John的农场举办了! 世界各地的奶牛将会到达当地的机场 ...
- Flask&&人工智能AI --4
一.flask请求上下文源码解读 通过上篇源码分析,我们知道了有请求发来的时候就执行了app(Flask的实例化对象)的__call__方法,而__call__方法返回了app的wsgi_app(en ...
- Docker从入门到实战(一)
Docker从入门到实战(一) 一:容器技术与Docker概念 1 什么是容器 容器技术并不是一个全新的概念,它又称为容器虚拟化.虚拟化技术目前主要有硬件虚拟化.半虚拟化.操作系统虚拟化等.1.1关于 ...
- IE8浏览器总是无响应或卡死崩溃怎么办
IE8浏览器总是无响应或卡死崩溃怎么办 2016-05-11 11:22:31 来源:百度经验 作者:qq675495787 编辑:Jimmy51 我要投稿 IE在打开某些网页的时候经常崩溃或无响应, ...
- hive与hbase关联
进入hbase: hbase shell 进入HIVE: hive hbase中建一张t_student_info表,添加两个列族 create 't_student_info','st1','st2 ...
- 让最新的 Android Q Beta 3 强制重启的 Project Mainline,到底是什么?
一. 序 最新的 Android 版本 Q,已经发布了 Android Q Beta 3,虽然没有正式发布,但是不少用户已经加入了测试计划,抢先体验 Android Q 的新功能. 近期不少体验用户反 ...
- abp(net core)+easyui+efcore仓储系统——解决方案介绍(二)
abp(net core)+easyui+efcore仓储系统目录 abp(net core)+easyui+efcore仓储系统——ABP总体介绍(一) ABP框架 首先介绍一下abp框架,abp其 ...