spring cloud : 远程调用容错 (降权Hystrix)

1.     业务微服务添加Feign

@FeignClient(name="tcloud-msg")

publicinterface MsgFeign {

……

 

2.     依赖jar中的捕获远程调用异常

在所依赖的项目tcloud-commons-api中添加Feign远程调用异常捕获。

ExceptionHandlerAdvice类中进行如下操作:

1.     handleOtherException()、switchCallException()、Exception4CallUtil()中捕获处理FeignException

    private SystemResponse handleFeignException(FeignException e) {

。。。。。。

       if (msg.contains("MsgFeign#")) {

           return Exception4CallUtil.responseTcloudMsg();

       }

。。。。。。

    }

    publicstatic SystemResponse responseTcloudMsg() {

       return ResultUtil.fail("sys_common_1027", "短信服务异常");

    }

 

2.     handleOtherException()、switchCallException()、Exception4CallUtil()中捕获处理ClientException

    private SystemResponse switchCallException(Exception e) {

。。。。。。

       if (msg.contains(Const.CLIENT_EXCEPTION + "tcloud-msg")) {

           callResponse = Exception4CallUtil.responseTcloudMsg();

       }

。。。。。。

    }

 

3.     调用:feign调用结果检查

@Service

publicclass VpCmnService {

    @Autowired

    private MsgFeign msgFeign;

。。。。。。

    publicvoid pushTextMsg(PushMsgEntityVO pushMsg) {

       SystemResponse response = msgFeign.pushTextMsg(pushMsg);

       MsValidateUtil.checkRespose(response);

    }

。。。。。。

由于远程调用,微服务A调用B(用户->A->B),如果A调用B的时候有偶发性失败,用户看到失败,会大量重复请求(类似电流过载),导致系统崩溃

=》解决办法:设置请求1秒以上超时、或者使用断路器Hystrix(嗨氏睡去瑞克斯) 理解成自动电闸就可以

l  生命周期:

关闭(远程调用正常)=》开启=》半开(允许一个远程调用看看线路情况)=》关闭

1.     添加依赖

消费者(调用方)tcloud-user-consumer添加依赖

        <dependency>

            <groupId>org.springframework.cloud</groupId>

            <artifactId>spring-cloud-starter-hystrix</artifactId>

        </dependency>

2.     启动类开启功能

@SpringBootApplication

@EnableDiscoveryClient

@EnableFeignClients

@EnableHystrix

publicclass ConsumerApplication {

……

3.     给Feign远程调用方法添加断路控制

package com.svw.tbox.tcloud.user.consumer.feign;

 

import java.util.List;

import org.springframework.cloud.netflix.feign.FeignClient;

import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import com.svw.tbox.tcloud.user.consumer.entity.User;

 

@FeignClient(name = "tcloud-user-provider",fallback = UserFeignClientFallback.class)

publicinterface UserFeignClient {

  @RequestMapping(value = "/likeName/{name}", method = RequestMethod.GET)

  public List<User> findByName(@PathVariable("name") String name);

}

添加回退类 UserFeignClientFallback编写对应回退方法

 

package com.svw.tbox.tcloud.user.consumer.feign;

 

import java.math.BigDecimal;

import java.util.ArrayList;

import java.util.List;

import org.springframework.stereotype.Component;

import com.svw.tbox.tcloud.user.consumer.entity.User;

 

@Component

publicclass UserFeignClientFallback implements UserFeignClient{

    @Override

    public List<User> findByName(String name) {

        List<User> defaultUsers = new ArrayList<>();

        defaultUsers.add(new User(121L, "xiaobanban", "默认用户", 23, new BigDecimal(10021332)));

        returndefaultUsers;

    }

}

4.     效果

启动tcloud-user-eurekaserver 2个实例

启动tcloud-user-provider 2个实例

启动tcloud-user-consumer 1个实例

访问http://localhost:9000/user/hrf


spring cloud : 远程调用容错 (降权Hystrix)

spring cloud : 远程调用容错 (降权Hystrix)