spring-cloud 里面,各个服务之间的通信,通常是通过注册中心的服务发现实现的,我待过的公司,有两种实现方案:
1、使用httpclient 调用网关,微服务之间的通信都通过网关来交互;(有点像以前的企业服务总线)
2、使用spring的openfeign框架,微服务直接直接通信;
本文主要介绍如何在spring-cloud里面使用openfeign进行远程调用。
参考文档:Spring Cloud OpenFeign 中文文档
启用feign
自己根据项目情况添加依赖包
<dependencies>
<!-- 其他依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- 其他依赖 -->
</dependencies>
Spring Boot应用程序
@SpringBootApplication
@EnableFeignClients
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}