SpringCloud-Hystrix

Hystrix

Hystrix is a latency and fault tolerance library designed to isolate points of access to remote systems, services and 3rd party libraries, stop cascading failure and enable resilience in complex distributed systems where failure is inevitable.

Hystrix是一个延迟和容错库,旨在隔离远程系统、服务和第三方库的访问点,停止次级故障,并且不可避免的复杂分布式系统中实现自我恢复能力。

配置hystrix

pom

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.67</version>
        </dependency>
	  <!--web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
	   <!--Eureka client -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
	   <!-- Hystrix -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
	   <!-- 声明性REST客户端,使用注解修饰接口,动态实现 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
	   <!--为了后续放开hystrix.stream路径-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

application.yml

spring:
  application:
    name: eureka-consumer-feign-hystrix
# 当前应用名称
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7000/eureka/
 # 映射到Eureka server的路径
server:
  port: 9001
feign:
  hystrix:
    enabled: true
# an OpenFeign client will be wrapped with a Hystrix circuit breaker
# 一个OpenFeign客户端将包装有Hystrix断路器
management:
  endpoints:
    web:
      exposure:
        include: hystrix.stream
# 为了给Hystrix Dashboard提供使用

启动类配置

@EnableFeignClients
//扫描包下被@FeignClient注解的,需要和@Configuration同时使用
@EnableHystrix
//开启断路器,并且自动配置找到Hystrix类(if they are available)
@SpringBootApplication
public class CloudConsumerFeignHystrixApplication {

    public static void main(String[] args) {
        SpringApplication.run(CloudConsumerFeignHystrixApplication.class, args);
    }
}

远程调用

@Component
// name: the service id with optional protocol prefix.带有可选协议前缀的服务ID
// fallback: 指定的Feign客户端接口的后备类。fallback类必须实现该接口并且是一个有效的spring bean
//Fallback class for the specified Feign client interface. The fallback class must
// implement the interface annotated by this annotation and be a valid spring bean.
@FeignClient(name = "eureka-producer",fallback = HelloRemoteHystrix.class)
public interface HelloRemote {

    @RequestMapping("/hello")
    String hello(@RequestBody JSONObject requestJSON);
}

fallback类

@Component
public class HelloRemoteHystrix implements HelloRemote {
    @Override
    public String hello(JSONObject requestJSON) {
        return "hello world";
    }
}

controller

@RestController
public class HelloController {

    @Resource
    private HelloRemote helloRemote;


    @RequestMapping("/hello/{name}")
    public String hello(@PathVariable(name = "name")String name){
        JSONObject requestJSON = new JSONObject();
        requestJSON.put("name",name);
        return helloRemote.hello(requestJSON);
    }
}

测试

将eureka-server,producer,feign-hystrix三个项目启动,在eureka-server上可以看到producer和feign-hystrix都已经注册成功。
访问:localhost:9001/hello/huskyui
hello, huskyui Tue Mar 24 14:34:44 CST 2020
将producer关闭
访问:localhost:9001/hello/huskyui
hello world
再次将producer启动
hello, huskyui Tue Mar 24 14:40:27 CST 2020

这边,可以看到熔断能力,以及自我恢复能力
配置一个Hystrix dashboard

pom

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

application.yml

spring:
  application:
    name: hystrix-dashboard
server:
  port: 11000

添加注解@EnableHystrixDashboard

@SpringBootApplication
@EnableHystrixDashboard
public class CloudHystrixDashboardApplication {

    public static void main(String[] args) {
        SpringApplication.run(CloudHystrixDashboardApplication.class, args);
    }

}

测试

运行该项目
访问: http://localhost:11000/hystrix
在启动后输入http://localhost:9001/actuator/hystrix.stream
9001是feign-hystrix的项目的端口,当时我们引入了actuator,开放hystrix.stream
可以看到dashboard相关

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值