微服务远程调用 RestTemplate

public class RostDispatchApplication {

    public static void main(String[] args) {
        //下面语句使得日志输出使用异步处理,减小输出日志对性能的影响
        System.setProperty("Log4jContextSelector", "org.apache.logging.log4j.core.async.AsyncLoggerContextSelector");
        SpringApplication.run(RostDispatchApplication.class, args);
    }
    /**
     * 创建RestTemplate并注入Spring容器
     */
    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

  二、实现类

public class RealityScheduleServiceImpl  implements IRealityScheduleService {

    @Resource
    RealityScheduleMapper realityScheduleMapper;
    @Resource
    GlobalExceptionHandler globalExceptionHandler;
    @Resource
    PropertiesConfig config;
    // 进行依赖注入
    @Autowired
    private RestTemplate restTemplate;

  

    /**
     * 分页查询实时排班信息
     * @param params 查询条件
     * @param pageIndex 页码
     * @param pageCount 每页条数
     * @return
     */
    @Override
    public ResponseData findListByPage(Map<String , Object> params,Integer pageIndex, Integer pageCount){
        pageIndex = pageIndex == null || pageIndex < 1 ? 1 : pageIndex;
        pageCount = pageCount == null ? 0 : pageCount;
        try{
                Page<RealitySchedule> page = new Page<>(pageIndex, pageCount);
                Page<RealityScheduleEX> list = realityScheduleMapper.selectPageList(page,params);
                List<RealityScheduleEX> resultList = list.getRecords();
                String basicUrl =config.getBasicUrl();
                // 进行数据的处理
                if(CollectionUtils.isNotEmpty(resultList)){
                    /*--------------------微服务远程调用数据----------------------*/
                   // 获得线路id
                   List<BaseLineInfo> lineList = new ArrayList();
                   List<BaseVehicleInfo> vehicleList = new ArrayList();
                   Map<String,Object> vehicleSetList = new HashMap<>();
                   Map<String,Object> branchList = new HashMap<>();
                   List<Long> lineIds =resultList.stream().map(e -> e.getLineId()).collect(Collectors.toList());
                   // 获得车辆id
                   List<Long> vehicleIds = resultList.stream().map(e -> e.getVehicleId()).collect(Collectors.toList());
                   // 获得车组人员信息
                   List<String> vehicleSets = resultList.stream().map(e -> e.getVehicleSet()).collect(Collectors.toList());
                   // 获得网点信息
                   List<String> branches= resultList.stream().map(e -> e.getBranches()).collect(Collectors.toList());
                   String lineUrl = basicUrl+"base-line-info/getObjectByIds";
                   ResponseData result =restTemplate.postForObject(lineUrl,lineIds,ResponseData.class);
                   // 线路返回结果数据
                   if(result.getCode() == 200){
                       lineList = JsonUtil.json2List(JsonUtil.obj2json(result.getData()), BaseLineInfo.class);
                   }
                   // 获得车辆信息
                   ResponseData vehicleResult =restTemplate.postForObject(basicUrl+"base-vehicle-info/getObjectByIds",vehicleIds,ResponseData.class);
                    if(vehicleResult.getCode() == 200){
                        vehicleList = JsonUtil.json2List(JsonUtil.obj2json(vehicleResult.getData()), BaseVehicleInfo.class);
                    }
                    // 获得车组人员信息
                    ResponseData vehicleSetsResult =restTemplate.postForObject(basicUrl+"base-escort-person-info/getObjectByIds",vehicleSets,ResponseData.class);
                    if(vehicleSetsResult.getCode() == 200){
                        vehicleSetList = (Map<String,Object>)vehicleSetsResult.getData();
                    }
                    // 获得网点信息
                    ResponseData branchResult =restTemplate.postForObject(basicUrl+"base-branch-info/getObjectByIds",branches,ResponseData.class);
                    if(branchResult.getCode() == 200){
                        branchList = (Map<String,Object>)branchResult.getData();
                    }
                    /*--------------------对列表查询数据进行整合,供页面展示用----------------------*/
                    List<BaseLineInfo> finalLineList = lineList;
                    List<BaseVehicleInfo> finalVehicleList = vehicleList;
                    int num = resultList.size();
                    for(int i =0 ;i< num;i++){
                        RealityScheduleEX ex = resultList.get(i);
                        // 线路信息
                        BaseLineInfo  lineInfo =finalLineList.stream().filter(line ->line.getId() == ex.getLineId()).findAny().orElse(null);
                        ex.setLineName(lineInfo.getLineName());
                        // 车牌号
                        BaseVehicleInfo vehicleInfo = finalVehicleList.stream().filter(v -> v.getId() == ex.getVehicleId()).findAny().orElse(null);
                        ex.setPlateNumber(vehicleInfo.getPlateNumber());
                        // 车组人员信息
                        ex.setPersonList(JsonUtil.json2List(JsonUtil.obj2json(vehicleSetList.get(String.valueOf(i))), BaseEscortPersonInfo.class));
                        // 网点信息,根据任务类型进行网点信息的赋值
                        String taskType = ex.getTaskType();
                        List<BaseBranchInfo> branch = JsonUtil.json2List(JsonUtil.obj2json(branchList.get(String.valueOf(i))), BaseBranchInfo.class);
                        if(taskType.equals(ConstantEnum.EARLY_TASK.getCode())) {
                            ex.setEarlyBranch(branch);
                        }
                        else if(taskType.equals(ConstantEnum.LATE_TASK.getCode())) {
                            ex.setLateBranch(branch);
                        }
                        else if(taskType.equals(ConstantEnum.SELF_TASK.getCode())) {
                            ex.setSelfDevices(branch);
                        }
                        else if(taskType.equals(ConstantEnum.BUSINESS_TASK.getCode())) {
                            ex.setDoorstep(branch);
                        }
                        else{
                            ex.setTempBranch(branch);
                        }
                    }
                }
                return ResponseUtil.success(list);
           } catch (Exception e) {
            log.error("", e);
            return globalExceptionHandler.exceptionGet(e);
        }
    }

  三、基础服务中的方法实现

   

    public ResponseData getObjectByIds(@RequestBody List<String> ids ){
        if (CollectionUtils.isNotEmpty(ids)) {
            return baseBranchInfoService.getObjectByIds(ids);
        }
        else{
            return ResponseUtil.error("未获取到网点信息!");
        }
    }

  

 @Override
    public ResponseData getObjectByIds(List<Long> ids) {
        try {
            QueryWrapper<BaseLineInfo> wrapper = new QueryWrapper<>();
            wrapper.in("id", ids);
            List<BaseLineInfo> list = baseLineInfoMapper.selectList(wrapper);
            if (CollectionUtils.isNotEmpty(list)) {
                return ResponseUtil.success(list);
            } else {
                return ResponseUtil.error(511,"未查找对应的线路信息");
            }

        } catch (Exception e) {
            log.error("", e);
            return globalExceptionHandler.exceptionGet(e);
        }
    }
}

  

 

posted @ 2023-09-19 14:07  flyComeOn  阅读(34)  评论(0编辑  收藏  举报