Version: Next
OpenFeign 服务调用
Open Feign 使用步骤
- 接口 + 注解:
微服务调用接口
+@FeignClient
- 新建
cloud-consumer-feign-order80
POM
<!-- openfeign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
完整pom依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--监控-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--热部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.example</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--eureka client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- openfeign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
application.yaml
server:
port: 80
#这里只把feign做客户端用,不注册进eureka
eureka:
client:
#表示是否将自己注册进EurekaServer默认为true
register-with-eureka: false
service-url:
#defaultZone: http://localhost:7001/eureka
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/
主启动类
添加注解
@EnableFeignClients
@SpringBootApplication@EnableFeignClientspublic class OrderFeignMain80 {public static void main(String[] args) {SpringApplication.run(OrderFeignMain80.class, args);}}
业务类
- 业务逻辑接口 +
@FeignClient
配置调用 provider 服务- 新建
PaymentFeignService
接口,并新增注解@FeignClient
- 编写 Controller
正常创建 Service 层的接口
PaymentFeignService
@FeignClient(value = "要调用的服务名称"
注解- 用
GetMapping
、PostMapping
指定对方微服务 Controller 暴露的路由地址即可(直接调对方 Service 方法也行),与 Spring MVC 画风一致Service 层接口@FeignClient(value = "CLOUD-PAYMENT-SERVICE")public interface PaymentFeignService {@GetMapping(value = "/payment/get/{id}")CommonResult<Payment> getPaymentById(@PathVariable("id") Long id);}
Controller
注入 Service 层
书写Controller方法,通过 OpenFeign 去 Eureka 注册中心找具体微服务节点,然后调用这个节点的 Controller 方法
@RestController@Slf4jpublic class OrderFeignController {@Resourceprivate PaymentFeignService paymentFeignService;@GetMapping("/consumer/payment/get/{id}")public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id) {// 通过 openFeign 去 Eureka 找微服务,调那个微服务的 Controller 接口return paymentFeignService.getPaymentById(id);}}
测试
- 启动 Eureka 注册中心节点
7001
、7002
- 启动
provider8001
、provider8002
- 启动
consumer-feign-order80
- 访问
localhost:80/consumer/payment/get/2
响应
{
"code": 200,
"message": "查询成功,端口号: 8081",
"data": {
"id": 2,
"serial": "长大002"
}
}
- 多次访问,可以发现端口号是轮询的,
8001
、8002
交替 - Feign 自带负载均衡配置项