• 创建项目

    要使 Feign 与 Hystrix 进行整合,我们需要增加 Feign 和 Hystrix 的依赖,修改 POM.xml 中增加以下依赖项如下:

    <?xmlversion="1.0"encoding="UTF-8"?>

    <projectxmlns="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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>

     
     

    <groupId>org.lixue</groupId>

    <artifactId>spring-cloud-hystrix-client</artifactId>

    <version>0.0.1-SNAPSHOT</version>

    <packaging>jar</packaging>

     
     

    <name>spring-cloud-hystrix-client</name>

    <description>DemoprojectforSpringBoot</description>

     
     

    <parent>

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

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

    <version>1.5.4.RELEASE</version>

    <relativePath/><!--lookupparentfromrepository-->

    </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>Dalston.SR5</spring-cloud.version>

    </properties>

     
     

    <dependencies>

    <dependency>

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

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

    </dependency>

    <dependency>

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

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

    </dependency>

    <dependency>

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

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

    </dependency>

    <dependency>

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

    <artifactId>spring-cloud-starter-ribbon</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-test</artifactId>

    <scope>test</scope>

    </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>

 
 

  • 增加配置

    还需要在 src/main/resources/application.yml 中,开启 Feign 的配置,如下:

    #配置应用名称

    spring:

    application:

    name:spring-cloud-hystrix-client

    #服务端口

    server:

    #启用feign的hystrix支持

    feign:

    hystrix:

    enabled:true

    #设置eureka服务注册中心的地址,如果多个以逗号分割

    eureka:

    client:

    service-url:

    defaultZone:http://eurekaserver01:9000/eureka/,http://eurekaserver02:9000/eureka/

     
     

  • 创建 Feign 客户端

    创建 Feign 客户端接口,增加回退的类实现,在 @FeignClient 注解中,设置 fallback 属性,如下:

    package org.lixue;

     
     

    import org.springframework.cloud.netflix.feign.FeignClient;

    import org.springframework.stereotype.Component;

    import org.springframework.web.bind.annotation.RequestMapping;

    import org.springframework.web.bind.annotation.RequestMethod;

    import org.springframework.web.bind.annotation.RequestParam;

     
     

    @FeignClient(name="HELLOWORLD-PROVIDER",fallback=HelloWorldFeignClient.HelloWorldFallback.class)

    public interface HelloWorldFeignClient{

     
     

    @RequestMapping(path="/speaks",method=RequestMethod.GET)

    String speak(@RequestParam(name="names")Stringname);

     
     

    @Component

    class HelloWorldFallback implements HelloWorldFeignClient{

     
     

    @Override

    public String speak(String name){

    return"speakfallbackisname="+name;

    }

    }

    }

 
 

  • 创建调用类

    只需要使用 @Autowired 注解标注 HelloWorldFeignClient 实例进行自动注入,然后调用 HelloWorldFeignClient 实例的方法即完成调用,如下:

    package org.lixue;

     
     

    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.RequestParam;

    import org.springframework.web.bind.annotation.RestController;

     
     

    import java.util.List;

    import java.util.Map;

    import java.util.concurrent.ExecutionException;

    import java.util.concurrent.Future;

     
     

    @RestController

    public class InvokerController{

     
     

    @Autowired

    private HelloWorldFeignClient helloWorldFeignClient;

     
     

    @RequestMapping(method=RequestMethod.GET,path="/speakFeign")

    public String speakFeign(@RequestParam("name")String name){

    return helloWorldFeignClient.speak(name);

    }

    }

 
 

 
 

  • 启动类

    增加 @EnableCircuitBreaker 和 @EnableFeignClients 注解,启用 Feign 和 Hystrix 功能,如下:

    package org.lixue;

     
     

    import org.springframework.boot.SpringApplication;

    import org.springframework.boot.autoconfigure.SpringBootApplication;

    import org.springframework.boot.web.servlet.ServletComponentScan;

    import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;

    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

    import org.springframework.cloud.netflix.feign.EnableFeignClients;

     
     

    @SpringBootApplication

    @EnableDiscoveryClient

    @EnableCircuitBreaker

    @EnableFeignClients

    @ServletComponentScan

    public class SpringCloudHystrixClientApplication{

     
     

    public static void main(String[]args){

    SpringApplication.run(SpringCloudHystrixClientApplication.class,args);

    }

    }

     
     

  • 测试验证

    由于我们使用了 Ribbon 因此首先启动 eureka-server 和 service-provider 服务,然后启动该项目,访问 http://localhost:8077/speak?name=abc 可以看到能正常返回信息,如下:

    Hello World abc Port=8080

    此时,关闭 service-provider 服务,然后在访问地址 http://localhost:8077/speak?name=abc 可以看到,由于服务已经不可用,因此执行了该方法的回退返回,返回如下:

    speak fallback is name=abc

     
     

  • 配置说明

    Feign 与 Hystrix 整合使用时,会自动帮我们生成 CommandKey,格式为:Feign 客户端接口名称#方法名() ,生成的 GroupKey 为 @FeignClient 注解的 name 属性,例如,上面示例生成的 CommandKey 为 HelloWorldFeignClient#speak() ,Groupkey 为 HELLOWORLD-PROVIDER,如果需要针对该 Feign 客户端设置 Hystrix 的配置属性,在 application.yml 中增加配置如下:

    #设置Hystrix的属性

    hystrix:

    command:

    HelloWorldFeignClient#speak():

    execution:

    isolation:

    thread:

    如果需要设置默认全局属性,则修改 application.yml 配置如下:

    #设置Hystrix的属性

    hystrix:

    command:

    default:

    execution:

    isolation:

    thread:

     
     

Spring Cloud(Dalston.SR5)--Feign 与 Hystrix 断路器整合的更多相关文章

  1. Spring Cloud(Dalston.SR5)--Feign 声明式REST客户端

    Spring Cloud 对 Feign 进行了封装,集成了 Ribbon 并结合 Eureka 可以实现客户端的负载均衡,Spring Cloud 实现的 Feign 客户端类名为 LoadBala ...

  2. Spring Cloud(Dalston.SR5)--Zuul 网关-Hystrix 回退

    当我们对网关进行配置让其调用集群的服务时,将会执行 Ribbon 路由过滤器,该过滤器在进行转发时会封装为一个 Hystrix 命令予以执行,Hystrix 命令具有容错的功能,如果"源服务 ...

  3. Spring Cloud(Dalston.SR5)--Hystrix 断路器

    Spring Cloud 对 Hystrix 进行了封装,使用 Hystrix 是通过 @HystrixCommand 注解来使用的,被 @HystrixCommand 注解标注的方法,会使用 Asp ...

  4. Spring Cloud(Dalston.SR5)--Hystrix 断路器-缓存

    在 Spring Cloud 中可以使用注解的方式来支持 Hystrix 的缓存,缓存与合并请求功能需要先初始化请求上下文才能实现,因此,必须实现 javax.servlet.Filter 用于创建和 ...

  5. Spring Cloud(Dalston.SR5)--Hystrix 断路器-合并请求

    在 Spring Cloud 中可以使用注解的方式来支持 Hystrix 的合并请求,缓存与合并请求功能需要先初始化请求上下文才能实现,因此,必须实现 javax.servlet.Filter 用于创 ...

  6. Spring Cloud(Dalston.SR5)--Hystrix 监控

    在服务调用者加入 Actuator ,可以对服务调用者的健康情况进行实时监控,例如,断路器是否打开.当前负载情况等. 服务调用者 需要增加 actuator依赖, 修改 POM.xml 中增加以下依赖 ...

  7. Spring Cloud(Dalston.SR5)--Config 集群配置中心

    Spring Cloud Config 是一个全新的项目,用来为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持,他分为服务端和客户端两个部分.服务端也称为分布式配置中心,是一个独立的微服务 ...

  8. Spring Cloud (9) 服务容错保护-Hystrix断路器

    断路器 断路器本身是一种开关装置,用于在电路上保护线路过载,当线路中又电路发生短路时,断路器能够及时的切断故障电路,放置发生过载.发热.甚至起火等严重后果. 在分布式架构中,断路器模式的作用也是类似, ...

  9. Spring Cloud(Dalston.SR5)--Config 集群配置中心-刷新配置

    远程 SVN 服务器上面的配置修改后,需要通知客户端来改变配置,需要增加 spring-boot-starter-actuator 依赖并将 management.security.enabled 设 ...

随机推荐

  1. winform 使用线程

    我这里写一个线程里创建一个窗体调用父窗体的方法 private void button4_Click(object sender, EventArgs e) { button4.Text = &quo ...

  2. IDEA发布应用时发布到lib下面的包不全

    IDEA发布应用时发布到lib下面的包不全,Tomcate启动时就报:At least one JAR was scanned for TLDs yet contained no TLDs. Enab ...

  3. JS执行机制--事件循环--笔记

    JS的解析是由浏览器中的JS解析引擎完成的.JS是单线程运行,也就是说,在同一个时间内只能做一件事,所有的任务都需要排队,前一个任务结束,后一个任务才能开始.但是又存在某些任务比较耗时,如IO读写等, ...

  4. Linux fdisk命令操作磁盘(添加、删除、转换分区等)

    创建分区1->查看原始分区sudo fdisk -l Disk /dev/sda: 21.5 GB, 21474836480 bytes255 heads, 63 sectors/track, ...

  5. [转]HBASE 二级索引

    1.二级索引的核心思想是什么?2.二级索引由谁来管理?3.在主表中插入某条数据后,hbase如何将索引列写到索引表中去?4.scan查询的时候,coprocessor钩子的作用是什么?5.在split ...

  6. [LeetCode&Python] Problem 796. Rotate String

    We are given two strings, A and B. A shift on A consists of taking string A and moving the leftmost ...

  7. 陕西师范第七届I题----排队

    链接:https://www.nowcoder.com/acm/contest/121/I来源:牛客网 题目描述 ACM竞赛队内要开运动会啦!!!! 竞赛队内的一群阳光乐观积极的队员们迅速的在操场上站 ...

  8. const 和let的本质区别

    在let和const之间,建议优先使用const,尤其是在全局环境,不应该设置变量,只应设置常量. const优于let有几个原因.一个是const可以提醒阅读程序的人,这个变量不应该改变:另一个是c ...

  9. 再回首 基本数据类型和 if语句

    一 变量:(使用变量是不能加引号,要不就变成字符串了) 变量的命名规则: 1.数字,字母,下划线组成. 2.变量不能是数字开头 3.区分大小写 4.不要使用中文或者拼音 5.要有相应的意义 6.不能使 ...

  10. POJ 2456: Aggressive cows(二分,贪心)

    Aggressive cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20485   Accepted: 9719 ...