解决springcloud 中各服务之间调用外部依赖的clint api时,在调试时不方便的问题

屏蔽外部依赖

Springcloud 开发中的同学肯定有过,代码中需要调用同项目中别的微服务的api接口,通常在项目中采用feign调用的方式,把应用启动起来同时把自己相关的代码跑一遍。
通常有几种做法:

  1. 本地把所有的服务都启动起来。
  2. 把注册中心换为开发环境,依赖开发环境的服务。
  3. 直接把代码推送到开发环境自测

看起来三种都可以,以前我也是这么干的。但还是有几个小问题:

  1. 本地启动有可能服务很多,全部起来电脑能不能撑住还两说,万一服务有问题就进行不下去了。
  2. 依赖开发环境的前提是网络打通,还有一个问题就是开发环境代码很不稳定很大可能会影响你的测试。
  3. 推送到开发环境应该是比较靠谱的方案,但如果想调试只有日志大法,没有本地 debug 的效率高效。

那如何解决问题呢?既可以在本地调试也不用启动其他服务。

其实也可以利用单测的做法,把其他外部依赖 Mock 掉就行了。

大致的流程分

  1. SpringBoot 启动之后在 Spring 中找出你需要屏蔽的那个 API 的 bean(通常情况下这个接口都是交给 Spring 管理的)。
  2. 手动从 bean 容器中删除该 bean。
  3. 重新创建一个该 API 的对象,只不过是通过 Mock 出来的。
  4. 再手动注册进 bean 容器中。

具体实践

调用接口

// fegin调用的client
@Autowired
private ToolsClient toolsClient;

@RequestMapping(value = {"/query/cars"})
public HttpCommandResultWithData getProductSalesCarList(@RequestBody SalesCarListCommand command){
        ToBBusinessMessageCommand msgCommand = new ToBBusinessMessageCommand();
        // fegin 调用的位置
        HttpCommandResultWithData resultWithData = toolsClient.msgPushTools(msgCommand);
        logger.info("test result : {}", resultWithData);
        ....
        return result;
    }

如果按上述接口地址访问测试,发现必定会报错,因为ToolsClient所在的服务并为启动

2018-10-23 11:30:59.560|ERROR|http-nio-8799-exec^调用接口[/qingqi/sellbusiness/query/cars]发生异常,message:[{}]com.netflix.hystrix.exception.HystrixRuntimeException: ToolsClient#msgPushTools(ToBBusinessMessageCommand) failed and no fallback available.

替换原有的 Bean

@Component
public class ToolsClientMockTest implements CommandLineRunner {

    protected static final Logger logger = LoggerFactory.getLogger(ToolsClientMockTest.class);

    @Autowired
    private ApplicationContext applicationContext;


    @Override
    public void run(String... strings) throws Exception {

        DefaultListableBeanFactory defaultListableBeanFactory =
                (DefaultListableBeanFactory) applicationContext.getAutowireCapableBeanFactory();
        ToolsClient toolsClient = defaultListableBeanFactory.getBean(ToolsClient.class);
        logger.info("===== Tools Client :{} =====", toolsClient.getClass());
        defaultListableBeanFactory.removeBeanDefinition(ToolsClient.class.getCanonicalName());

        ToolsClient toolsClientApi = PowerMockito.mock(ToolsClient.class, invocationOnMock -> {
            HttpCommandResultWithData result = new HttpCommandResultWithData().fillResult(ReturnCode.OK);
            Map<String, Object> map = new HashMap();
            map.put("datakey", "abc");
            map.put("datakey2", 2);
            result.setData(map);
            return result;
        });

        defaultListableBeanFactory.registerSingleton(ToolsClient.class.getCanonicalName(), toolsClientApi);

    }
}

测试

|2018-10-23 11:42:03.804|INFO|http-nio-8799-exec-test result : HttpCommandResultWithData{data={datakey=abc, datakey2=2}} CommonResult{resultCode=200, message='OK'}|

maven依赖

	<!-- https://mvnrepository.com/artifact/org.powermock/powermock-api-mockito -->
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-api-mockito</artifactId>
            <version>1.6.6</version>
        </dependency>

        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-module-junit4</artifactId>
            <version>1.6.6</version>
        </dependency>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值