Spring Cloud 对 Hystrix 进行了封装,使用 Hystrix 是通过 @HystrixCommand 注解来使用的,被 @HystrixCommand 注解标注的方法,会使用 AspectJ 对其进行代理,Spring 会将相关的类转换为 Bean 放到容器中,在 @HystrixCommand 注解中,还可以通过 commandProperties、threadPoolProperties 属性来设置命令的配置。

Hystrix 示例如下:

  • 创建项目

    创建名称为 spring-cloud-hystrix-client 的 Spring Cloud 项目,因为项目示例使用到了 Ribbon 因此也增加了相关依赖,修改 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-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>

     
     

  • 创建服务调用

    对于一些默认配置,例如命令组的 Key 等,可以使用 @DefaultProperties 注解标注到类,这样就减少了 @HystrixCommand 注解的配置代码量

    package org.lixue;

     
     

    import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;

    import org.springframework.beans.factory.annotation.Autowired;

    import org.springframework.stereotype.Component;

    import org.springframework.web.client.RestTemplate;

     
     

    @DefaultProperties(groupKey="hello-world")

    @Component

    public class HelloWorldClient{

     
     

    @Autowired

    private RestTemplate restTemplate;

     
     

    @HystrixCommand(fallbackMethod="speakFallback",commandKey="hello-world",

    threadPoolKey="hello-world",

    commandProperties={

    @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value="500")

    },

    threadPoolProperties={

    @HystrixProperty(name="coreSize",value="50")

    }

    )

    public String speak(Stringname){

    if(name==null ||"".equals(name)){

    name="null";

    }

     
     

    return restTemplate.getForObject("http://HELLOWORLD-PROVIDER/speaks?names="+name,String.class);

    }

     
     

    private String speakFallback(String name){

    return"call error,name is "+name;

    }

    }

     
     

  • 创建 REST 服务

    在服务调用类中声明 HelloWorldFeignClient 类的实例,并使用 @Autowired 注解标注,表示使用 Spring 的自动注入实例,在

    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;

     
     

    @RestController

    public class InvokerController{

     
     

    @Autowired

    private HelloWorldClient helloWorldClient;

     
     

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

    public String speak(@RequestParam("name")Stringname){

    return helloWorldClient.speak(name);

    }

    }

  • 修改启动类

    package org.lixue;

     
     

    import org.springframework.boot.SpringApplication;

    import org.springframework.boot.autoconfigure.SpringBootApplication;

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

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

    import org.springframework.cloud.client.loadbalancer.LoadBalanced;

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

    import org.springframework.context.annotation.Bean;

    import org.springframework.web.client.RestTemplate;

     
     

    @SpringBootApplication

    @EnableDiscoveryClient

    @EnableCircuitBreaker

    public class SpringCloudHystrixClientApplication{

     
     

    public static void main(String[]args){

    SpringApplication.run(SpringCloudHystrixClientApplication.class,args);

    }

     
     

    @LoadBalanced

    @Bean

    publicRestTemplate restTemplate(){

    return new RestTemplate();

    }

    }

     
     

  • 增加配置

    #配置应用名称

    spring:

    application:

    name:spring-cloud-hystrix-client

    #服务端口

    server:

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

    eureka:

    client:

    service-url:

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

     
     

     
     

  • 测试验证

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

    {"abc":"Hello World abc Port=8002"}

    这时关闭 service-provider 服务,再次访问这时返回的是我们 speakFallback 方法返回的信息,表示调用出错 Hystrix 进行和回退,如下:

    call error,name is abc

     
     

 
 

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

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

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

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

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

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

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

  4. Spring Cloud入门教程-Hystrix断路器实现容错和降级

    简介 Spring cloud提供了Hystrix容错库用以在服务不可用时,对配置了断路器的方法实行降级策略,临时调用备用方法.这篇文章将创建一个产品微服务,注册到eureka服务注册中心,然后我们使 ...

  5. Spring Cloud(Dalston.SR5)--Feign 与 Hystrix 断路器整合

    创建项目 要使 Feign 与 Hystrix 进行整合,我们需要增加 Feign 和 Hystrix 的依赖,修改 POM.xml 中增加以下依赖项如下: <?xmlversion=" ...

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

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

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

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

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

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

  9. Spring Cloud(Dalston.SR5)--Zuul 网关-微服务集群

    通过 url 映射的方式来实现 zuul 的转发有局限性,比如每增加一个服务就需要配置一条内容,另外后端的服务如果是动态来提供,就不能采用这种方案来配置了.实际上在实现微服务架构时,服务名与服务实例地 ...

随机推荐

  1. python day02作业

  2. 【Python】Excel操作-1

    #练习:创建Excel 如果要创建的Excel已经存在并打开,会报错 from openpyxl import Workbook wb=Workbook() #创建文件对象 ws=wb.active ...

  3. RIP路由协议(一)

    实验要求:使用RIPv2配置路由器,使路由器能接收到所有的路由条目 拓扑如下: 配置如下: R1enable 进入特权模式configure terminal 进入全局模式interface s0/0 ...

  4. MySQL添加可重复执行列

    一.information_schema数据库表说明: SCHEMATA表:提供了当前mysql实例中所有数据库的信息.是show databases的结果取之此表. TABLES表:提供了关于数据库 ...

  5. 利用sql语句进行增删改查

    1.查询 函数:raw(sql语句) 语法:Entry.objects.raw(sql) 返回:QuerySet 2.增删改 from django.db import connection def ...

  6. SyntaxError: Non-UTF-8 code starting with '\xe5' in file ***.py on line 105, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for

    用charles抓包时, 对抓到的html,放到pycharm中解析, 结果报错: SyntaxError: Non-UTF-8 code starting with '\xe5' in file * ...

  7. Unity 3D委托entrust

    Unity 3D委托的多种用法 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- 心分 ...

  8. MySql查询出来的值为 boolean类型的值

    解决方案: status_flag * 1 as status_flag 乘以1之后就不会是boolean类型的值了

  9. c#接口与虚函数的实验报告

    1)定义Student类,用string型变量name存储学生姓名,用int型变量age存储学生年龄.Student类实现IComparable接口.要求从键盘输入学生的姓名和年龄,并注意可能出现的异 ...

  10. mac电脑读写NTFS格式的移动硬盘命令

    diskutil info /Volumes/SAMSUNG | grep UUID echo "UUID=38EBE5E4-016F-44B7-9D55-BB4AF6DC3E1D none ...