1.OpenFeign配置
首先,在需要调用其他的微服务的微服务中引入相关依赖。(大多数项目中各微服务需要互相调用,可以直接在每个微服务中引入依赖)
<!--服务调用-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
当openfeign版本较高时,还需要引入其他依赖,具体详见:
https://www.cnblogs.com/yiMro/p/16018149.html
其次,需要在需要调用其他的微服务的微服务的启动类中加入注解@EnableFeignClients。
@EnableFeignClients
public class ServiceEmsApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceEmsApplication.class, args);
}
}
- 远程调用
首先,可以先在需要调用其他的微服务的微服务中创建client包,包中的接口都是用于远程调用其他的微服务。如下图,CoreUserInfoClient接口可能都是来远程调用和用户信息相关的微服务。

其次,在相应的接口上加入注解@FeignClient,其value值即为需要调用的微服务名称。


随后在接口中写入要调用的接口的URL以及函数。

然后可以在需要调用其他的微服务的微服务中相应的Controller中引入相应的Client(文中则引入)。随后可在相应方法中直接调用接口。

