天天看點

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();
    }
}
           

繼續閱讀