关于springboot:FeignClient自调用问题处理

4次阅读

共计 911 个字符,预计需要花费 3 分钟才能阅读完成。

我的项目中应用 feignClient 时,有时会遇到自调用状况,即 A 服务通过 feignClient,调用了本人服务的接口。这种状况个别是因为谬误的办法应用,不正确的 feignClient 依赖导致,应予以修改。

但也有一些状况,比方依赖的内部 sdk 包中的 service 办法里,通过 feignClient 调用了近程办法,而这个近程办法的服务方正是本人(比方 serviceA)。在这种状况下,能够通过以下步骤进行解决,将近程调用变为本地办法间接执行:

  1. 在定义近程调用的 FeignClient 类上,申明 primary 值为 true:

    @FeignClient(value = "serviceA", contextId = "testFeign", path = "busitest", primary = false)
    public interface TestFeign {
     @GetMapping
     public String get();}
  2. 将对应实现该 FeignClient 办法的 Controller 类上,补全继承关系,并申明 @Primary:

    @RequestMapping("busitest")
    @RestController
    @Primary
    public class BusiTestController implements TestFeign{
     
     @GetMapping
     public String get(){return "get";}
    }

    实现以上步骤后,重新部署 api 包。此时如果某服务的 sdk 包中存在如下 Service:

    @Service
    public class BusiService{
      @Autowired
      private TestFeign testFeign;
     
     
      public Strinmg busiMethod(){testFeign.get();
      }
    }

    则该 Service 在 serviceA 之外的服务中应用时,执行 busiMethod 办法,会通过 feignClient 向 serviceA 发动近程调用;而在 serviceA 服务自身应用这个 Service 时,执行 busiMethod 办法,会间接执行 BusiTestController 的 get() 办法,而不再通过 feignClient 去解决申请。

正文完
 0