springcloud学习记录-Hystrix

springcloud学习记录-Hystrix

在分布式环境中,不可避免地会有许多服务依赖项中的某些失败。 Hystrix是一个库,可通过添加等待时间容限和容错逻辑来帮助您控制这些分布式服务之间的交互。 Hystrix通过隔离服务之间的访问点,停止服务之间的级联故障并提供后备选项来实现此目的,所有这些都可以提高系统的整体弹性。

-提供保护并控制通过第三方客户端库(通常是通过网络)访问的依赖项的延迟和失败。
-停止复杂的分布式系统中的级联故障。
-快速失败并快速恢复。
-后备并在可能的情况下正常降级。
-启用近乎实时的监视,警报和操作控制。

1、通过线程池隔离(默认方式)
在这里插入图片描述
舱壁模式:
在这里插入图片描述
在这里插入图片描述
​ hystrix工作流程:

​ 1、当调用服务报错时,会开启一个时间窗口(默认为10s)

​ 2、在这个时间窗口内,判断调用次数是否得到设定的最低调用数。

​ ——如果没有:则重置信息,回到第1步。

​ (假如设定为至少调用5次,此时只调用了3次,且3次全部失败,将会回到第1步)

​ ——如果有: (失败次数/总次数) > 阈值 ? 跳闸 (不调用):重置回到第1步

​ 3、如果跳闸,开启一个活动窗口(默认5s),每隔5s,hystrix会允许一个请求通过

​ ——如果请求调用成功:重置断路器,回到第1步

​ ——如果请求失败:回到第3步

在这里插入图片描述

项目搭建

可以在上一篇文章中的Eureka-client-consumer基础上进行修改,这里记录从新建开始。
1.新建module:Eureka-client-consumer
在这里插入图片描述
pom.xml依赖如下:

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</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>
    </dependencies>

2.在启动类加上注解:@EnableHystrix

@SpringBootApplication
@EnableDiscoveryClient
@EnableHystrix
public class EurekaClientConsumerApplication {

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

}

3.yml配置

server:
  port: 8011

spring:
  application:
    name: eurekaconsumer1

eureka:
  client:
    serviceUrl:
      defaultZone: http://eureka1:8761/eureka/,http://eureka2:8762/eureka/,http://eureka3:8763/eureka/

Hystirx有很多配置也可以在yml进行配置,这里不写,只是搭建最简单的环境。

4.写RestTemplateConfiguration 类,开启ribbon负载均衡

@Configuration
public class RestTemplateConfiguration {

    @Bean
    @LoadBalanced //表明开启负载均衡访问功能
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

5.写一个简单的测试

@RestController
public class ribbonController {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping(value = "hi")
//    @HystrixCommand(fallbackMethod = "fsd") //Hystrix
    @HystrixCommand(
            threadPoolKey = "sayHi",
            threadPoolProperties = {
                    @HystrixProperty(name = "coreSize",value = "5"),
                    @HystrixProperty(name = "maxQueueSize",value = "20"),
            },
            commandProperties = {
                    @HystrixProperty(
                            name = "execution.isolation.thread.timeoutInMilliseconds",
                            value = "2000"
                    ),//当调用的服务超过2s没有响应则断开,不再等待
                    @HystrixProperty(name = "metrics.rollingStats.timeInMilliseconds",value = "3000"),
                    @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "2"),
                    @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "50"),
                    @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "3000")
            },
            fallbackMethod = "fsd"
    )
    public  String sayHi(String message){
        System.out.println(1111);
       if (message!="2"){
           throw  new RuntimeException("2435476");
       }
        return restTemplate.getForObject("http://eureka-client-provider/hiya?message={1}" , String.class,message);
    }

    public String fsd(String message){
        return "accccccccccc";
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值