天天看点

Hystrix实现服务隔离与降级

一、服务雪崩效应

        服务雪山崩效应产生服务堆积在同一个线程池中,因为在同一个线程池中,所有请求全部到一个服务进行访问,这时候会导致其他服务没有线程接收请求访问,所以就会产生服务雪崩效应。

二、Hystrix作用

        1、服务保护,当服务产生堆积的时候,对服务实现保护功能。

        2、服务隔离,保证每个服务互不影响,使用信号量和线程池方式。

        3、服务降级,当服务不可用的时候,不会被等待,直接返回一个友好的提示。

        4、服务熔断,当服务器达到最大的承受能力后,直接拒绝访问服务,会调用服务降级方法,返回友好提示,目的保证服务不被宕机掉。

三、实现

        1、导包

<dependency>
      <groupId>com.netflix.hystrix</groupId>
      <artifactId>hystrix-metrics-event-stream</artifactId>
      <version>1.5.12</version>
    </dependency>
    <dependency>
      <groupId>com.netflix.hystrix</groupId>
      <artifactId>hystrix-javanica</artifactId>
      <version>1.5.12</version>
    </dependency>
           

        2、先定义HystrixCommand

//OrderHystrixCommand :

import com.alibaba.fastjson.JSONObject;
import com.mg.service.MemberService;
import com.netflix.hystrix.*;
/**
 * Created by MiaoGa on 2021/8/16.
 */
public class OrderHystrixCommand extends HystrixCommand<JSONObject> {
    private MemberService memberService;

    public OrderHystrixCommand(MemberService memberService){
        super(setter());
        this.memberService=memberService;
    }
    //配置服务的隔离机制
    private static Setter setter() {
        // 服务分组
        HystrixCommandGroupKey groupKey = HystrixCommandGroupKey.Factory.asKey("order");
        // 服务标识
        HystrixCommandKey commandKey = HystrixCommandKey.Factory.asKey("order");
        // 线程池名称,每个服务都有一个线程池
        HystrixThreadPoolKey threadPoolKey = HystrixThreadPoolKey.Factory.asKey("order-pool");
        // 线程池配置 线程池大小为10,线程存活时间15秒 队列等待的阈值为100,超过100执行拒绝策略
        HystrixThreadPoolProperties.Setter threadPoolProperties = HystrixThreadPoolProperties.Setter().withCoreSize(10)
                .withKeepAliveTimeMinutes(15).withQueueSizeRejectionThreshold(100);
        // 命令属性配置Hystrix 开启超时
        HystrixCommandProperties.Setter commandProperties = HystrixCommandProperties.Setter()
                // 采用线程池方式实现服务隔离
                .withExecutionIsolationStrategy(HystrixCommandProperties.ExecutionIsolationStrategy.THREAD)
                // 禁止超时
                .withExecutionTimeoutEnabled(false);
        return HystrixCommand.Setter.withGroupKey(groupKey).andCommandKey(commandKey).andThreadPoolKey(threadPoolKey)
                .andThreadPoolPropertiesDefaults(threadPoolProperties).andCommandPropertiesDefaults(commandProperties);

    }
    //服务降级
    @Override
    protected JSONObject getFallback() {
        JSONObject result=new JSONObject();
        result.put("code","500");
        result.put("msg","服务繁忙,请稍后重试");
        return result;
    }

    //服务执行内容
    @Override
    protected JSONObject run() throws Exception {
        JSONObject result=memberService.getMember();
        return result;
    }
}

//MemberService :
@Service
public class MemberService {
    public JSONObject getMember(){
        JSONObject result = HttpClientUtils.httpGet("http://127.0.0.1:8081/member/memberIndex");
        return result;
    }
}
           

        3、Controller中调用:

@RestController
public class OrderController {
    @Autowired
    private MemberService memberService;
    @RequestMapping("/getMember")
    public String getMember(HttpServletRequest request, Map<String,Object> result){
        JSONObject json=new OrderHystrixCommand(memberService).execute();
        return json.toJSONString();
    }
}
           

继续阅读