前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >每天20分钟之hystrix

每天20分钟之hystrix

原创
作者头像
李子健
发布2022-08-07 16:59:34
2220
发布2022-08-07 16:59:34
举报
文章被收录于专栏:每日一善每日一善每日一善

概述

hystrix是奈飞开源的熔断限流组件,虽然线上停止了维护,但是使用的用户还是挺多的

很多线上的系统还在使用它。

它的原理是基于rxjava启动线程池(信号量)去处理每个请求,做到服务的隔离

提供的功能

1 基础应用

package cn.beckbi;

import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandProperties;
import com.netflix.hystrix.HystrixThreadPoolProperties;
import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext;

import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

public class Example1 extends HystrixCommand<String>
{

    private String name;

    public Example1(String  name) {
        super(
                HystrixCommand.Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey(name))
                        .andCommandPropertiesDefaults(
                                HystrixCommandProperties.Setter()
                                        .withExecutionIsolationStrategy(
                                                HystrixCommandProperties.ExecutionIsolationStrategy.THREAD
                                        )
                        ).andThreadPoolPropertiesDefaults(
                        HystrixThreadPoolProperties.Setter()
                        .withCoreSize(10)
                        .withMaxQueueSize(100)
                        .withMaximumSize(100)
                )
        );
        this.name = name;
    }

    @Override
    protected String run() {
        /*
        try{
            TimeUnit.MICROSECONDS.sleep(1);
        }catch (InterruptedException e) {
            e.printStackTrace();
        }*/
        System.out.println("get data");
        return "HystrixCommandName:"+this.name+"## currentThreadName="+Thread.currentThread().getName();
    }

    @Override
    protected String getFallback() {
        return Thread.currentThread().getName()+"失败了";
    }

    @Override
    protected String getCacheKey() {
        return String.valueOf(this.name);
    }

    public static void main(String[] args) throws Exception{

        HystrixRequestContext hystrixRequestContext = HystrixRequestContext.initializeContext();

        Example1 example1 = new Example1("test1");
        String result = example1.execute();
        System.out.println("## currentThreadName="+Thread.currentThread().getName());
        System.out.println(result);

        Future<String> future = new Example1("test2").queue();
        System.out.println("future:"+future.get());



        result = new Example1("test3").execute();

        future = new Example1("test3").queue();
        System.out.println("future:"+future.get());
        hystrixRequestContext.shutdown();



    }
}

2 在spring-cloud中使用hystrix

服务中使用

@GetMapping("/user/{uid}")
    @HystrixCommand(fallbackMethod = "defaultCall",
    commandProperties = {
            @HystrixProperty(
                    name = "execution.isolation.strategy",
                    value = "THREAD"
            )
    })
    public String info(@PathVariable long uid) throws JsonProcessingException {

        @Data
        class IdData {
            private String id;
            private Long uid;
        }

        IdData idData = new IdData();
        idData.setUid(uid);
        idData.setId("123");
        return mapper.writeValueAsString(idData);
    }

    public String defaultCall(long uid) {
        return "failed:"+uid;
    }

也可以和feigin一起使用

3 htstrix监控和查看监控数据

  1. 加入依赖 <dependency> <groupId>com.netflix.hystrix</groupId> <artifactId>hystrix-metrics-event-stream</artifactId> <version>${hystrix-core.version}</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-hystrix-dashboard --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId> <version>2.2.10.RELEASE</version> </dependency> @EnableHystrixDashboard @EnableHystrix @SpringBootApplication
  2. 增加监控

访问线上端口http://127.0.0.1:7330/hystrix

image.png
image.png

输入监控页面http://127.0.0.1:7330/actuator/hystrix.stream

配置页面

http://127.0.0.1:7330/hystrix/monitor?stream=http%3A%2F%2F127.0.0.1%3A7330%2Factuator%2Fhystrix.stream

服务监控

spring.application.name=khystrix-spring
server.port=7330
management.endpoints.web.exposure.include=*
management.endpoints.web.exposure.exclude=env,beans
hystrix.dashboard.proxy-stream-allow-list=localhost
image.png
image.png

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 概述
  • 提供的功能
    • 1 基础应用
      • 2 在spring-cloud中使用hystrix
        • 3 htstrix监控和查看监控数据
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档