spring cloud (四) 请求熔断 feign
1 pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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.example</groupId>
<artifactId>Spring-Cloud-Feign</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>Spring-Cloud-Feign</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.SR1</spring-cloud.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency> <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency> <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency> </dependencies> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>
2 配置文件
spring.application.name=feign-consumer
server.port=4001
eureka.client.serviceUrl.defaultZone=http://localhost:8080/eureka/
3 启动类
@EnableFeignClients
@EnableFeignClients
@SpringCloudApplication
public class SpringCloudFeignApplication { public static void main(String[] args) {
SpringApplication.run(SpringCloudFeignApplication.class, args);
}
}
4 根据服务名称来指定服务提供方
//根据服务名称来指定服务提供方
@FeignClient(name="service-a",fallback=TestServiceAFallBack.class)
public interface TestServiceA {
// 通过注解指定访问方式,访问路径,访问参数
@RequestMapping(method = RequestMethod.GET, value = "/getm")
String getMessage(@RequestParam("message") String message); }
5 回退类 当service-a getm不可访问是 会返回已经写好的信息
@Component
public class TestServiceAFallBack implements TestServiceA{ @Override
public String getMessage(String message) {
// TODO Auto-generated method stub
return "the server is error";
}
}
6 编写测试类
@RestController
public class TestController {
@Autowired
TestServiceA testServiceA; @GetMapping("/test")
public String test(String message) {
return testServiceA.getMessage(message);
} }
7 测试
启动 eureka注册中心 service-a 和feign三个项目
访问 http://127.0.0.1:4001/test?message=123 返回 hello world:123
把服务service-a停掉 再次访问 http://127.0.0.1:4001/test?message=123
返回
the server is error
spring cloud (四) 请求熔断 feign的更多相关文章
- Spring Cloud Hystrix 请求熔断与服务降级
在Java中,每一个HTTP请求都会开启一个新线程.而下游服务挂了或者网络不可达,通常线程会阻塞住,直到Timeout.你想想看,如果并发量多一点,这些阻塞的线程就会占用大量的资源,很有可能把自己本身 ...
- spring cloud 2.x版本 Feign服务发现教程(内含集成Hystrix熔断机制)
前言 本文采用Spring cloud本文为2.1.8RELEASE,version=Greenwich.SR3 本文基于前两篇文章eureka-server和eureka-client的实现. 参考 ...
- Spring Cloud第一次请求报错问题
一.原因 我们在使用Spring Cloud的Ribbon或Feign来实现服务调用的时候,第一次请求经常会经常发生超时报错,而之后的调用就没有问题了.造成第一次服务调用出现失败的原因主要是Ribbo ...
- spring Cloud中,解决Feign/Ribbon整合Hystrix第一次请求失败的问题?
Spring Cloud中,Feign和Ribbon在整合了Hystrix后,可能会出现首次调用失败的问题,要如何解决该问题呢? 造成该问题的原因 Hystrix默认的超时时间是1秒,如果超过这个时间 ...
- spring cloud(四) feign
spring cloud 使用feign进行服务间调用 1. 新建boot工程 pom引入依赖 <dependency> <groupId>org.springframewor ...
- spring Cloud 之 Eureka、Feign、Hystrix、Zuul、Config、Bus
一.服务发现——Netflix Eureka Eureka包含两个组件: Eureka Server和Eureka Client 1.创建Eureka Server服务端 (1).引入依赖 父工程po ...
- Spring Cloud系列文,Feign整合Ribbon和Hysrix
在本博客之前的Spring Cloud系列里,我们讲述了Feign的基本用法,这里我们将讲述下Feign整合Ribbon实现负载均衡以及整合Hystrix实现断路保护效果的方式. 1 准备Eureka ...
- Spring Cloud Alibaba Sentinel对Feign的支持
Spring Cloud Alibaba Sentinel 除了对 RestTemplate 做了支持,同样对于 Feign 也做了支持,如果我们要从 Hystrix 切换到 Sentinel 是非常 ...
- spring cloud Zuul + 路由熔断【服务降级】 --- 心得
1.前言 刚入门 时,使用 ribbon + hystrix + restTemplate ,实现了简单的 接口访问 + 客户端负载均衡 + 服务熔断保护 : 然后学习了 feign ,整合了 r ...
随机推荐
- 历时一年《Python自动化测试实战》终于出版!!!
一.为什么会写这本书 1.系统梳理.可以加深自己对测试知识体系的系统梳理 2.名气.增加个人的名气,比如:面试时,可以很自豪的说,我是xxxx书的作者 3.利他.帮助有需要的学习者更系统.完备的学习和 ...
- 修改Window服务器虚拟内存位置
系统采用的是windows server2008操作系统,硬件部门在分配磁盘的时候C盘只有50G,其中虚拟内存就占用了30G,再除去操作系统占用空间,可用自由支配空间较小,会出现在部分异常情况下C盘占 ...
- Mybaties的简单使用(全当做复习了)
在使用mybaties的时候,最容易忘掉的是他的动态SQL,不过网上有关这方面的文章很多. 在动态SQl中最常见的几种SQL的语法就是: if choose (when, otherwise) tri ...
- c++中共享内存原理及实现
共享内存 (也叫内存映射文件) 主要是通过映射机制实现的 , Windows 下进程的地址空间在逻辑上是相互隔离的 , 但在物理上却是重叠的 ; 所谓的重叠是指同一块内存区域可能被多个进程同时使用 , ...
- centos个性化命令行提示符
为了在满屏的命令中找到用户的命令行,所以很有必要设置一种字体颜色.我就设置最实用的一种,可以用蓝色字体显示当前所在路径 命令行输入: echo "PS1='[\${debian_chroot ...
- Asp Core部署到IIS服务器
之前有文章写了.Asp Core Kestrel服务器可以独立运行在linux下,也可以部署到Docker上面通过容器管理,当然也可以直接部署到IIS中 一:安装环境 1)首先需要在服务器安装对应环境 ...
- [转帖]sql server版本特性简介、版本介绍简介
sql server版本特性简介.版本介绍简介 https://www.cnblogs.com/gered/p/10986240.html 目录 1.1.sql server的版本信息 1.2.版本重 ...
- 解决docker容器中Centos7系统的中文乱码
解决docker容器中Centos7系统的中文乱码问题有如下两种方案: 第一种只能临时解决中文乱码: 在命令行中执行如下命令: # localedef -i zh_CN -f UTF-8 zh_CN. ...
- pytest_assert断言
前言 断言是写自动化测试基本最重要的一步,一个用例没有断言,就失去了自动化测试的意义了.什么是断言呢? 简单来讲就是实际结果和期望结果去对比,符合预期那就测试pass,不符合预期那就测试 failed ...
- 【题解】Luogu P5340 [TJOI2019]大中锋的游乐场
原题传送门 没想到省选也会出这种题??! 实际就是一个带有限制的最短路 因为\(k<=10\),所以我们珂以暴力将每个点的权值分为[-k,k],为了方便我们珂以转化成[0,2k],将汉堡的权值记 ...