一、需求描述
我们要做的,通过A服务项目信息的多个code调用B服务查询对应code的用户信息。
A服务的code rpc 调用 B服务的用户信息
二、示例代码
ProjectController
@ApiOperation(value = "查询项目列表", httpMethod = "GET")
@RequestMapping(value = "/list", method = RequestMethod.GET)
public Result<BasePagination<JSONObject>> queryProjectlist(@RequestBody ProjectListParm param){
if (StringUtils.isBlank(param.getName())) {
param.setName(null);
}else {
param.getName().trim();
}
try {
return Result.success(projectService.queryProjectlist(param));
}catch (Exception e){
return Result.fail(e.getMessage(), Result.ResultCodeEnum.A00003.name());
}
}
ServiceImpl 分页查询
public BasePagination<JSONObject> findProjectlist(ProjectListParm param) {
BasePagination<JSONObject> page = new BasePagination<>();
param.setPageIndex(param.getPageIndex() == null ? 1 : param.getPageIndex());
param.setPageSize(param.getPageSize() == null ? 20 : param.getPageSize());
List<ProjectData> list = crmAdPlanMapper.findProjectlist(param.getName(),param.getPageIndex(),param.getPageSize());
for (ProjectData item : list) {
// 查询产品code
List<String> productCodelist = productCrmMapper.findProductCodeByBid(item.getBid());
if (productCodelist.isEmpty()) {
item.setExperienceCourseCluesNum(0);
} else {
// 转换数组
String[] strs = productCodelist.toArray(new String[productCodelist.size()]);
// 调用查询线索数
Result<Integer> count = userCrmApiProvider.userCrmInfo(strs);
// 补全线索
item.setExperienceCourseCluesNum(count.getData());
}
}
String str = JSON.toJSONString(list,SerializerFeature.WriteMapNullValue);
List<JSONObject> json = JSONObject.parseArray(str, JSONObject.class);
page.setList(json);
return page;
}
这里Result<Integer> count = userCrmApiProvider.userCrmInfo(strs);为rpc远程调用
@FeignClient为B服务应用名,在application.yml的spring:application:name可以查看,如果没设置则默认为项目名,url则会指定ip地址,本地服务器测试可以加上,如果是多个服务可能会负载到不同的服务。
@FeignClient(value = QimaoApiConstants.API_USER_CRM_NAME)
//@FeignClient(value = QimaoApiConstants.API_USER_CRM_NAME, url = "http://127.0.0.1:8001")
public interface UserCrmApiProvider {
@RequestMapping(value = QimaoApiConstants.API_USER_CRM_PATH+"/userCrm/userCrmInfo")
Result<Integer> userCrmInfo(@RequestParam("productCode") String[] productCode);
}
B服务代码通过code查询对应用户商品信息
@ApiOperation("查询商品线索数量")
@RequestMapping("/userCrmInfo")
public Result<Integer> userCrmInfo(@RequestParam("productCode") String[] productCode) {
Result<Integer> result=crmService.queryExperienceCourseCluesNumByProductCode(Arrays.asList(productCode));
return result;
}
|