版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zyjcxc/article/details/83304640
解决springcloud 中,各个服务中调用外部依赖的api,在调试时不方便的问题
屏蔽外部依赖
Springcloud 开发中的同学肯定有过,代码中需要调用同项目中别的微服务的api接口,通常在项目中采用feign调用的方式,把应用启动起来同时把自己相关的代码跑一遍。
通常有几种做法:
- 本地把所有的服务都启动起来。
- 把注册中心换为开发环境,依赖开发环境的服务。
- 直接把代码推送到开发环境自测
看起来三种都可以,以前我也是这么干的。但还是有几个小问题:
- 本地启动有可能服务很多,全部起来电脑能不能撑住还两说,万一服务有问题就进行不下去了。
- 依赖开发环境的前提是网络打通,还有一个问题就是开发环境代码很不稳定很大可能会影响你的测试。
- 推送到开发环境应该是比较靠谱的方案,但如果想调试只有日志大法,没有本地 debug 的效率高效。
那如何解决问题呢?既可以在本地调试也不用启动其他服务。
其实也可以利用单测的做法,把其他外部依赖 Mock 掉就行了。
大致的流程分
- SpringBoot 启动之后在 Spring 中找出你需要屏蔽的那个 API 的 bean(通常情况下这个接口都是交给 Spring 管理的)。
- 手动从 bean 容器中删除该 bean。
- 重新创建一个该 API 的对象,只不过是通过 Mock 出来的。
- 再手动注册进 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>