Feign涨姿势的机会

  已经对SpringCloud和熟悉了,但是今天在用Feign去调用其他微服务的时候遇到了两个问题:

  1.把其他微服务当做service层来调用时,怎样传递一个对象参数,而不是基本类型的参数;

  2.当我传递基本类型的参数时,服务总是启动不起来;

问题再现1

  @RequestMapping(value = "/callLog/saveCallLog",method = RequestMethod.POST)
    ResultVO<Boolean> saveCallLog(BuTaskCallLogDTO buTaskCallLogDTO,@RequestParam("companyCode")Long companyCode);

对方的微服务里需要传递一个对象BuTaskCallLogDTO 

其实这种问题需要对方的微服务处理一下即可

  @ApiOperation(value = "添加呼叫记录")
        @PostMapping("/saveCallLog")
        public  ResultVO saveCallLog(@RequestBody BuTaskCallLogDTO buTaskCallLogDTO,@RequestParam("companyCode")Long companyCode){
            String date=getThisMonth();
            String tableName=tableStart+companyCode+"_"+date.replace("-","_");
            buTaskCallLogDTO.setTableName(tableName);
            /**  添加呼叫记录   */
           callLogService.saveOrUpdateCallLog(buTaskCallLogDTO);
            return  ResultVOUtil.success(true);
        }

 标红的地方就是需要注意的点

问题再现2

  

    @GetMapping("/user/testUserTimeOut")
    ResultVO<Boolean> testUserTimeOut(Long mills,String token);

服务启动时报错:

2018-11-27 14:11:22,387 [localhost-startStop-1] WARN  org.apache.catalina.loader.WebappClassLoaderBase - The web application [ROOT] appears to have started a thread named [RxIoScheduler-1 (Evictor)] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
 sun.misc.Unsafe.park(Native Method)
 java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:215)
 java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.awaitNanos(AbstractQueuedSynchronizer.java:2078)
 java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:1093)
 java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:809)
 java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1067)
 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1127)
 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
 java.lang.Thread.run(Thread.java:745)
Disconnected from the target VM, address: '127.0.0.1:61806', transport: 'socket'

其实对于这种错,很懵逼的。。。你不知道具体错误出现在哪里,只是报内存泄露!

对于这种错,那就用一个方法:排除法

case1: 把自己刚才写的代码注释掉,运行项目能起来那就是以前的问题,需要继续排除。。。

case2: 代码注释掉之后程序能正常运行,那就说明是自己刚写的bug。。。

我刚才就注释掉自己的代码,发现项目能正常运行,哈哈哈,找到问题的所在之处:

反复对比之前写的代码,竟然发现自己没有加参数的注解 

  

 @GetMapping("/user/testUserTimeOut")
    ResultVO<Boolean> testUserTimeOut(@RequestParam(value = "mills")Long mills,@RequestParam(value = "token")String token);

结论:Feign调用其他微服务时,必须有必要的参数注解 

@RequestParam 这个是默认的参数注解
@PathVariable 这个是路径传参时要用的

  

原文地址:https://www.cnblogs.com/pangyangqi/p/10026200.html