OpenFeign 简介
OpenFeign 提供了一种声明式的远程调用接口。
OpenFeign 能做什么
目的是为了简易HTTP客户端的编写。
之前在 笔记中介绍了 Ribbon + RestTemplate 的使用。Ribbon + RestTemplate 是多http请求做了封装处理,形成了模版化的调用。但是在实际的开发中,由于对服务依赖的调用可能不止一处,往往一个接口被多处调用,所以需要对每个微服务进行封装。鉴于此 Feign 在此基础上为我们提供了封装操作,由Feign 帮我门定义和实现依赖服务接口的定义。因此简化了我们的操作,只需要创建一个接口并使用注解的形式来配置它(比如Mapper接口上标注@Mapper注解,现在是在一个微服务接口上标注一个@FeignClient注解),就可以完成对服务接口的绑定,简化了Spring cloud Ribbon使用时候封装客户端的开发量
如何使用
Declarative REST Client: Feign creates a dynamic implementation of an interface decorated with JAX-RS or Spring MVC annotations
在主启动类上通过注解 @EnableFeignClients、 接口上通过注解 @FeignClient 实现
@SpringBootApplication
@EnableFeignClients
public class WebApplication {
public static void main(String[] args) {
SpringApplication.run(WebApplication.class, args);
}
@FeignClient("name")
static interface NameService {
@RequestMapping("/")
public String getName();
}
}
OpenFeign 使用Example
- eureka服务集群
127.0.0.1:7001
127.0.0.1:7002
- 微服务 - 消费者服务
consumner-order
, 服务注册 eureka - 微服务 - 支付服务
payment8001
,payment8002
, 服务注册进eureka。服务名叫CLOUD-PAYMENT-SERVICE
1、新建子模块 consumner-order
依赖pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
2、配置文件application.yaml'
server:
port: 80
eureka:
client:
register-with-eureka: false
service-url:
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/
3、主启动类
@EnableFeignClients 开启 Feign 客户端
@SpringBootApplication
@EnableFeignClients
public class FeignMain80 {
public static void main(String[] args) {
SpringApplication.run(FeignClient.class, args);
}
}
4、接口层 service/PaymentFeignService.java
@Component
@FeignClient(value = "CLOUD-PAYMENT-SERVICE")
public interface PaymentFeignService {
@GetMapping(value = "/payment/get/{id}")
CommonResult<Payment> getPaymentById(@PathVariable("id") Long id);
}
5、业务控制器 controller/OrderController.java
@RestController
public class OrderController {
@Resource
private PaymentFeignService paymentFeignService;
@GetMapping("/consumer/payment/get/{id}")
public CommonResult<Payment> paymentLb(@PathVariable("id") Long id) {
return paymentFeignService.getPaymentById(id);
}
}
6、启动eureka7001、eureka7002、payment8001、payment8002、consumer-order
访问 127.0.0.1:80//consumer/payment/get/4
# 第一次
{"code":200,"message":"查询成功, serverPort: 8001","data":{"id":4,"serial":"jeffcail00004"}}
# 第二次
{"code":200,"message":"查询成功, serverPort: 8002","data":{"id":4,"serial":"jeffcail00004"}}
# 第三次
{"code":200,"message":"查询成功, serverPort: 8001","data":{"id":4,"serial":"jeffcail00004"}}
# 第四次
{"code":200,"message":"查询成功, serverPort: 8002","data":{"id":4,"serial":"jeffcail00004"}}