我的项目中应用feignClient时,有时会遇到自调用状况,即A服务通过feignClient,调用了本人服务的接口。这种状况个别是因为谬误的办法应用,不正确的feignClient依赖导致,应予以修改。
但也有一些状况,比方依赖的内部sdk包中的service办法里,通过feignClient调用了近程办法,而这个近程办法的服务方正是本人(比方serviceA)。在这种状况下,能够通过以下步骤进行解决,将近程调用变为本地办法间接执行:
在定义近程调用的FeignClient类上,申明primary值为true:
@FeignClient(value = "serviceA", contextId = "testFeign", path = "busitest", primary = false)public interface TestFeign { @GetMapping public String get();}
将对应实现该FeignClient办法的Controller类上,补全继承关系,并申明@Primary:
@RequestMapping("busitest")@RestController@Primarypublic class BusiTestController implements TestFeign{ @GetMapping public String get(){ return "get"; }}
实现以上步骤后,重新部署api包。此时如果某服务的sdk包中存在如下Service:
@Servicepublic class BusiService{ @Autowired private TestFeign testFeign; public Strinmg busiMethod(){ testFeign.get(); }}
则该Service在serviceA之外的服务中应用时,执行busiMethod办法,会通过feignClient向serviceA发动近程调用;而在serviceA服务自身应用这个Service时,执行busiMethod办法,会间接执行BusiTestController的get()办法,而不再通过feignClient去解决申请。