springcloud Feign断路器实战和问题总结

断路由是防止该服务调用其他外服务时,外服务宕机或者出差时,影响到本服务的宕机,引起大面积的瘫痪,所以才有了断路由的由来。

springcloud Feign已经实现了Hystrix ,所以不用再引Hystrix的maven依赖

1. 搭建一个eureka注册中心和service参考下面的文章

springcloud项目实战

2. 创建项目如下

2.1 pom 文件

  1. <?xml version="1.0" encoding="UTF-8"?>

  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  4. <modelVersion>4.0.0</modelVersion>

  5. <groupId>com.xue</groupId>

  6. <artifactId>spring-curstomA</artifactId>

  7. <version>0.0.1-SNAPSHOT</version>

  8. <packaging>jar</packaging>

  9. <name>spring-curstomA</name>

  10. <description>Demo project for Spring Boot</description>

  11. <parent>

  12. <groupId>org.springframework.boot</groupId>

  13. <artifactId>spring-boot-starter-parent</artifactId>

  14. <version>1.5.9.RELEASE</version>

  15. <relativePath/> <!-- lookup parent from repository -->

  16. </parent>

  17. <properties>

  18. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

  19. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

  20. <java.version>1.8</java.version>

  21. </properties>

  22. <dependencies>

  23. <dependency>

  24. <groupId>org.springframework.boot</groupId>

  25. <artifactId>spring-boot-starter</artifactId>

  26. </dependency>

  27. <dependency>

  28. <groupId>org.springframework.cloud</groupId>

  29. <artifactId>spring-cloud-starter-config</artifactId>

  30. </dependency>

  31. <!-- sping cloud 注册服务 -->

  32. <dependency>

  33. <groupId>org.springframework.cloud</groupId>

  34. <artifactId>spring-cloud-starter-eureka</artifactId>

  35. </dependency>

  36. <dependency>

  37. <groupId>org.springframework.boot</groupId>

  38. <artifactId>spring-boot-starter-test</artifactId>

  39. <scope>test</scope>

  40. </dependency>

  41. <dependency>

  42. <groupId>org.springframework.boot</groupId>

  43. <artifactId>spring-boot-starter-web</artifactId>

  44. </dependency>

  45. <dependency>

  46. <groupId>org.springframework.cloud</groupId>

  47. <artifactId>spring-cloud-starter-feign</artifactId>

  48. </dependency>

  49. </dependencies>

  50. <dependencyManagement>

  51. <dependencies>

  52. <dependency>

  53. <groupId>org.springframework.cloud</groupId>

  54. <artifactId>spring-cloud-dependencies</artifactId>

  55. <version>Dalston.RELEASE</version>

  56. <type>pom</type>

  57. <scope>import</scope>

  58. </dependency>

  59. </dependencies>

  60. </dependencyManagement>

  61. <build>

  62. <plugins>

  63. <plugin>

  64. <groupId>org.springframework.boot</groupId>

  65. <artifactId>spring-boot-maven-plugin</artifactId>

  66. </plugin>

  67. </plugins>

  68. </build>

  69. </project>

2.2 application.properties配置文件

  1. #服务名称

  2. spring.application.name=ribbon-consumer

  3. #端口号

  4. server.port=5555

  5. #注册中心

  6. eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/

  7. spring.cloud.config.discovery.enabled=true

  8. #注册中心的服务id

  9. spring.cloud.config.discovery.serviceId=compute-server

  10. #打开对Hystrix断路由

  11. feign.hystrix.enabled=true

2.3 启动类注解

  1. @SpringBootApplication

  2. @EnableEurekaClient

  3. @EnableFeignClients

  4. public class SpringCurstomAApplication {

  5. public static void main(String[] args) {

  6. SpringApplication.run(SpringCurstomAApplication.class, args);

  7. }

  8. }

2.4 controller层

  1. @RestController

  2. public class MyController {

  3. @Autowired

  4. HelloService service;

  5. @RequestMapping("/feign-customer")

  6. public String testA() {

  7. return service.hello();

  8. }

  9. }

2.5 feign 接口

  1. //服务名 和回调类

  2. @FeignClient(value="COMPUTE-SERVICE1",fallback=HelloServiceFallback.class)

  3. public interface HelloService {

  4. @RequestMapping("/info") //对应具体服务中的接口地址(具体服务controller 层的暴露接口)可以指定具体的get/post

  5. String hello();

  6. }

2.6 feign 实现类

  1. @Component//该注解不能少,否则报错

  2. public class HelloServiceFallback implements HelloService {

  3. @Override

  4. public String hello() {

  5. return "request error";

  6. }

  7. }

效果 把要调用的服务停止后 如下图

总结

如果项目还是报错,要检查三个地方

1. pom文件

  1.         <dependencyManagement>

  2. <dependencies>

  3. <dependency>

  4. <groupId>org.springframework.cloud</groupId>

  5. <artifactId>spring-cloud-dependencies</artifactId>

  6. <version>Dalston.RELEASE</version>//一定要是Dalston 不能是 Brixton

  7. <type>pom</type>

  8. <scope>import</scope>

  9. </dependency>

  10. </dependencies>

  11. </dependencyManagement>

2. application.properties

  1. #打开对Hystrix断路由

  2. feign.hystrix.enabled=true

3. 启动类注解

  1. @SpringBootApplication

  2. @EnableEurekaClient

  3. @EnableFeignClients//该注解不能少

  4. public class SpringCurstomAApplication {

  5. public static void main(String[] args) {

  6. SpringApplication.run(SpringCurstomAApplication.class, args);

  7. }

  8. }

springcloud Feign断路器实战和问题总结相关推荐

  1. SpringCloud feign 的三种超时时间配置

    1.负载均衡 Feign调用服务的默认时长是1秒钟,也就是如果超过1秒没连接上或者超过1秒没响应,就会相应的报错.Feign 的负载均衡底层用的是 Ribbon,其配置如下: ribbon:ReadT ...

  2. SpringCloud 企业级应用实战

    SpringCloud 企业级应用实战 基础教程 SpringCloud 企业级应用实战-基础教程父工程概述 基于Eureka Server实现服务注册 基于Eureka Server实现服务注册高可 ...

  3. SpringCloud Feign声明式服务调用

    SpringCloud Feign声明式服务调用 1. 加入pom依赖 2. Application.java上声明@EnableFeignClients 3. @FeignClient声明接口调用服 ...

  4. springcloud gateway断路器抛的错default failed and fallback disabled

    有没有springcloud的大神碰到这个问题? 这是springcloud gateway断路器抛的错 default failed and fallback disabled., httpStat ...

  5. SpringCloud之Eureka实战和架构设计解析

    SpringCloud之Eureka实战和架构设计解析 Netflix Eureka(后文简称Eureka)是由Netflix开源的一款基于REST的服务发现组件,包括Eureka Server及Eu ...

  6. SpringCloud Feign 传参问题及传输Date类型参数的时差

    1.多参数表单类型传输 @PostMapping("/service/system/advertiser/save")Response<Boolean> saveAdv ...

  7. Feign、OpenFeign及SpringCloud Feign的区别

    Feign.OpenFeign及SpringCloud Feign的区别 1. 三者概念 2. 依赖配置 1. 三者概念 Feign是Spring Cloud组件中一个轻量级RESTful的HTTP服 ...

  8. SpringCloud Feign实战(二)

    一,新建Feign工程microservicecloud-consumer-dept-feign,拷贝microservicecloud-consumer-dept-80这个工程的配置和程序 二,修改 ...

  9. 微服务架构,springcloud核心组件和实战,docker容器

    文章目录 前言 一.微服务开发基础 1.微服务架构开发 1.1单体架构的应用的困境 1.2 微服务架构 1.2.1 理解微服务架构 1.2.1 微服务的优缺点 1.3 微服务架构设计 1.3.1 微服 ...

最新文章

  1. 3分钟看完一篇论文,这个AI文本生成模型把今年NeurIPS 2300+篇总结了个遍
  2. 阿里员工哀叹不读书已经没有出路,招聘简历基本都是985
  3. AI Studio 不同环境下的执行速度
  4. python接口 同花顺_这是真的么 | 学会了用Python预测股票价格
  5. python自动测试p-python网络爬虫之自动化测试工具selenium[二]
  6. mysql中int型的zerofill参数
  7. Android-NDK-hello-jniCallback
  8. React中的各个目录文件功能
  9. oracle 库存管理系统,库存管理系统
  10. 设计模式在业务系统中的应用
  11. frm考试可以用计算机,FRM考试,考生自己可以携带计算器吗?
  12. mysql查询时候返回一个序号
  13. ShopNc安装过程
  14. DiscuzNT 交易插件设计之商品添加,编辑和删除(CUD)
  15. FTP协议是一种用于什么的协议
  16. 家用空调什么牌子好又省电质量又好
  17. java.lang.IllegalArgumentException: Failed to decrypt.
  18. rust墙壁升级点什么_明日之后屋子墙壁怎么升级?墙壁升级条件方法一览
  19. Java实现图片上传到服务器
  20. 新手C语言开发详细教程

热门文章

  1. boost any 实现万能容器_全面剖析 C++ Boost 智能指针!| CSDN 博文精选
  2. c语言利用线程交替打印奇偶数,两个线程交替打印奇偶数
  3. oracle字符串使用函数,oracle函数大全-字符串处理函数
  4. 系统设计题:如何设计一个电商平台积分兑换系统!
  5. CURL 模拟post和get请求
  6. python 第13天作业
  7. 金明的预算方案(分组背包)
  8. 二道Const,readonly 和 override, new的面试题
  9. mysql 事务类型表的用法
  10. CenOS下安装Eclipse并配置PyDev