feign 熔断工厂 fallbackFactory的简单实现

普通的熔断方法实现方式    是实现接口,当有错误响应是无法获取response信息

现在有一个方法就是熔断工厂,再也不用实现接口就能捕获错误接口的返回信息了

1.引入jar包

<dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-openfeign</artifactId>
                
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
              
            </dependency>

2.启动类加启动feign注解

@SpringBootApplication
@EnableFeignClients
public class TestApplication {
}

3.请求接口实现

package com.ylz.service.remote;

import com.ylz.base.BaseResult;
import com.ylz.pojo.MenuPojo;
import com.ylz.service.remote.impl.UserMenuFeignFallBackFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * @Description:feign
 * @Author: 
 * @Date: 2020/12/15
 * @Time: 11:11
 **/
@FeignClient(name = "userFeign",url = "${rest.service.user-server-host}",fallbackFactory = UserMenuFeignFallBackFactory.class)
public interface UserMenuFeign {

    /*
     * @Author LCW
     * @Description 新增或者修改用户中心菜单
     * @Date 2021/1/20 14:19
     * @param menuPojoReq :
     * @return com.ylz.base.BaseResult<java.lang.Object>
     **/
    @RequestMapping(method = RequestMethod.POST,value = "/menuController/addOrUpdateMenu",consumes  = MediaType.APPLICATION_JSON_VALUE)
    BaseResult<Object> addOrUpdateMenu(@RequestHeader("Cookie") String cookie, @RequestBody MenuPojo menuPojoReq);

    /*
     * @Author LCW
     * @Description 删除菜单
     * @Date 2021/1/20 14:19
     * @param menuPojoReq :
     * @return com.ylz.base.BaseResult<java.lang.Object>
     **/
    @RequestMapping(method = RequestMethod.POST,value = "/menuController/deleteMenu",consumes  = MediaType.APPLICATION_JSON_VALUE)
    BaseResult<Object> deleteMenu(@RequestHeader("Cookie") String cookie, @RequestBody MenuPojo menuPojoReq);

    /*
     * @Author LCW
     * @Description  根据条件查询指定菜单
     * @Date 2021/1/20 14:39
     * @param cookie :
     * @param menuPojoReq :
     * @return com.ylz.base.BaseResult<java.lang.Object>
     **/
    @RequestMapping(method = RequestMethod.POST,value = "/menuController/queryMenuListByCondition",consumes  = MediaType.APPLICATION_JSON_VALUE)
    BaseResult<Object> queryMenuListByCondition(@RequestHeader("Cookie") String cookie, @RequestBody MenuPojo menuPojoReq);

    /*
     * @Author LCW
     * @Description 新增或修改菜单和权限赋予
     * @Date 2021/1/20 14:19
     * @param menuPojoReq :
     * @return com.ylz.base.BaseResult<java.lang.Object>
     **/
    @RequestMapping(method = RequestMethod.POST,value = "/menuController/addOrUpdateMenuAndRole",consumes  = MediaType.APPLICATION_JSON_VALUE)
    BaseResult<Object> addOrUpdateMenuAndRole(@RequestHeader("Cookie") String cookie, @RequestBody MenuPojo menuPojoReq);

    /*
     * @Author LCW
     * @Description 删除菜单和删除角色赋予权限
     * @Date 2021/1/20 14:19
     * @param menuPojoReq :
     * @return com.ylz.base.BaseResult<java.lang.Object>
     **/
    @RequestMapping(method = RequestMethod.POST,value = "/menuController/deleteMenuAndRole",consumes  = MediaType.APPLICATION_JSON_VALUE)
    BaseResult<Object> deleteMenuAndRole(@RequestHeader("Cookie") String cookie, @RequestBody MenuPojo menuPojoReq);
}

4.熔断接口实现

package com.ylz.service.remote.impl;

import com.ylz.base.BaseResult;
import com.ylz.base.BaseResultGenerator;
import com.ylz.pojo.MenuPojo;
import com.ylz.service.remote.UserMenuFeign;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @Description: 熔断工厂
 * @ClassName: FallBackFactory
 * @Author: 
 * @Date: 2021/1/20 16:23
 * @Version: 1.0
 */
@Slf4j
@Component
public class UserMenuFeignFallBackFactory implements FallbackFactory<UserMenuFeign> {
    @Override
    public UserMenuFeign create(Throwable throwable) {
        log.error("请求用户中心出错:" + throwable.getMessage());
        return new UserMenuFeign() {
            @Override
            public BaseResult<Object> addOrUpdateMenu( String cookie,MenuPojo menuPojoReq) {
                log.error("cookieValue:" + cookie);
                log.error(throwable.getMessage());
                log.error("创建菜单失败,登录失败,请重新登录");
                return BaseResultGenerator.failureLoginOut("创建菜单失败,登录失败,请重新登录");
            }

            @Override
            public BaseResult<Object> deleteMenu(String cookie, MenuPojo menuPojoReq) {
                log.error("cookieValue:" + cookie);
                log.error(throwable.getMessage());
                log.error("删除失败,登录失败,请重新登录");
                return BaseResultGenerator.failureLoginOut("删除失败,登录失败,请重新登录");
            }

            @Override
            public BaseResult<Object> queryMenuListByCondition(String cookie, MenuPojo menuPojoReq) {
                log.error("cookieValue:" + cookie);
                log.error(throwable.getMessage());
                log.error("获取菜单失败,登录失败,请重新登录");
                return BaseResultGenerator.failureLoginOut("获取菜单失败,登录失败,请重新登录");
            }

            @Override
            public BaseResult<Object> addOrUpdateMenuAndRole(String cookie, MenuPojo menuPojoReq) {
                log.error("cookieValue:" + cookie);
                log.error(throwable.getMessage());
                log.error("创建菜单失败,服务异常!请联系管理员");
                return BaseResultGenerator.failureLoginOut("创建菜单失败,服务异常!请联系管理员");
            }

            @Override
            public BaseResult<Object> deleteMenuAndRole(String cookie, MenuPojo menuPojoReq) {
                log.error("cookieValue:" + cookie);
                log.error(throwable.getMessage());
                log.error("删除菜单失败,服务异常");
                return BaseResultGenerator.failureLoginOut("删除菜单失败,服务异常!请联系管理员");
            }
        };
    }
}

 

通过feign注解的fallbackFactory 属性找到指定的熔断实现方法

@FeignClient(name = "userFeign",url = "${rest.service.user-server-host}",fallbackFactory = UserMenuFeignFallBackFactory.class)

在方法
UserMenuFeignFallBackFactory 重写相对应的错误熔断返回即可;

在这里我们可以通过  throwable.getMessage()   获取到其他服务响应的错误信息反馈;

 

 

 

posted @ 2021-01-22 15:33  进击小螺号  阅读(11323)  评论(0编辑  收藏  举报