服务熔断框架hystrix学习概要

一:框架

springboot、hystrix、maven

二:depend

<dependency>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-starter-web</artifactId>
				<version>${springboot.version}</version>
			</dependency>
			<dependency>
				<groupId>com.netflix.hystrix</groupId>
				<artifactId>hystrix-core</artifactId>
				<version>${hystrix.version}</version>
			</dependency>
			<dependency>
				<groupId>com.netflix.hystrix</groupId>
				<artifactId>hystrix-metrics-event-stream</artifactId>
				<version>${hystrix.version}</version>
			</dependency>
			<dependency>
				<groupId>com.netflix.hystrix</groupId>
				<artifactId>hystrix-javanica</artifactId>
				<version>${hystrix.version}</version>
			</dependency>
三:下载hystrix-dashboard-1.5.5.war

地址:http://search.maven.org/remotecontent?filepath=com/netflix/hystrix/hystrix-dashboard/1.5.5/hystrix-dashboard-1.5.5.war

然后部署到tomcat容器中,此处省略步骤,不会的找块豆腐撞死TAT

部署完成后访问http://127.0.0.1:8080/hystrix-dashboard-1.5.5/,可以看到如下图



四:部署一个web应用

此处用springboot开发了一个简单的ok web应用,起主要作用的有两个类HystrixConfiguration.java和MainServer.java

HystrixConfiguration:注册spring bean和servlet

MainServer:服务应用入口

代码如下:

package com.eden.main;

import java.util.Random;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * @author eden.ding1991@gmail.com 2016年8月31日 上午10:02:06
 */
@SpringBootApplication
@RestController
public class MainServer {

    private static final Logger LOG = LoggerFactory.getLogger(MainServer.class);

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

    @RequestMapping(value = "/ok", method = RequestMethod.GET)
    @HystrixCommand(fallbackMethod = "okFallback", threadPoolProperties = {
            @HystrixProperty(name = "coreSize", value = "30"), @HystrixProperty(name = "maxQueueSize", value = "100"),
            @HystrixProperty(name = "queueSizeRejectionThreshold", value = "20") }, commandProperties = {
                    @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "100"),
                    @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "50")

    })
    public String ok() throws Exception {
        int l = new Random().nextInt(200);
        LOG.info(String.format("l=%s", l));
        TimeUnit.MILLISECONDS.sleep(l);
        return "ok";
    }

    public String okFallback(Throwable e) {
        System.out.println("execute okFallback!" + e.getMessage());
        LOG.error("error", e);
        return "fallback";
    }

}

package com.eden.main.hystrix;

import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.netflix.hystrix.contrib.javanica.aop.aspectj.HystrixCommandAspect;
import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;

/**
 *
 * @author eden.ding1991@gmail.com 2016年8月31日 上午11:14:16
 */
@Configuration
public class HystrixConfiguration {

    //注册Hystrix框架的拦截器,使得框架的注解能够生效
    @Bean
    public HystrixCommandAspect hystrixAspect() {
        return new HystrixCommandAspect();
    }

    //注入servlet,拦截url="/hystrix.stream"
    //测试git dev commit
    @Bean
    public ServletRegistrationBean hystrixMetricsStreamServlet() {
        ServletRegistrationBean registration = new ServletRegistrationBean(new HystrixMetricsStreamServlet());
        registration.addUrlMappings("/hystrix.stream");
        return registration;
    }
}


@HystrixProperty这个部分注解的含义请参考hystrix的github https://github.com/Netflix/Hystrix/wiki/Configuration


五:启动这个简单的web应用,访问http://127.0.0.1:8081/ok

端口号是8081,这个地方注意,为了防止与dashboard冲突,所以在web应用的application.yml将端口号改成了8081

上面注册的spring bean中有一个servlet是HystrixMetricsStreamServlet,拦截的url是/hystrix.stream,这个地方主要是生成监控需要的数据,可以直接访问这个url:http://127.0.0.1:8081/hystrix.stream  你会看到不停生成一个json格式的数据,里面全是监控的服务的各项metrics


六:开启监控上述web应用



七:监控界面


上述界面是用的测试案例中模拟的,并发60个线程访问,超时时间随机数0-200,系统设计的阈值是100ms,所以看到上图中有28个请求超出阈值导致熔断器打开,然后从界面中也能看到多少请求成功,多少失败,错误率,活跃线程数,排队线程数等

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值