feign客户端-声明式REST调用
一、为什么要使用feign?
之前通过RestTemplate调用REST服务,代码是这样的:
@HystrixCommand(fallbackMethod = "errorReturn")
public Order createOrder2() {
//1.创建订单对象
Order order = new Order();
order.setOid(100010);
order.setUid(3309);
Product[] productArray = restTemplate.getForObject("http://service-product/product", Product[].class);
order.setProductList(Arrays.asList(productArray));
//2.返回
return order;
}
虽然使用了Ribbon和Hystrix可以实现负载均衡和容错处理,但是这个编码在实现大量业务时会显得太过于冗余(如,多参数的URL拼接)。
思考:有没有更加优雅的实现呢?feign
二、使用步骤
在订单微服务service-order中增加对Feign的支持。
1、导入依赖
<!--springboot 整合fegnin客户端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
2、创建一个ProductFeignClient接口
package com.oy.order.feign;
import com.oy.order.entity.Product;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import java.util.List;
/**
* 申明这是一个Feign客户端,并且指明服务id
*/
@Component//注意要记得加
@FeignClient("service-product")//要申明的服务(service-order中申明service-product)
public interface ProductFeignClient {
/**
* 这里定义了类似于SpringMVC用法的方法,就可以进行RESTful方式的调用了
*
*/
@RequestMapping(value = "/product/list", method = RequestMethod.GET)//value要与ProductController中映射的值要一致
public List<Product> list();//服务方参数列表使用注解@RequestParam、@PathVariable修饰
}
3、改造OrderService
@HystrixCommand(fallbackMethod = "errorReturn")
public Order createOrder3() {
//1.创建订单对象
Order order = new Order();
order.setOid(100010);
order.setUid(3309);
List<Product> list = productFeignClient.list();
order.setProductList(list);
//2.返回
return order;
}
在ordercontroller中调用createOrder3
@PostMapping("create3")
public Order create3() {
Order order = orderService.createOrder3();
return order;
}
4、在启动类(ServiceOrderApplication)中添加 @EnableFeignClients 注解
package com.oy.order;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableEurekaClient
@EnableHystrix
@EnableFeignClients
public class ServiceOrderApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceOrderApplication.class, args);
}
@Bean
@LoadBalanced //负载均衡 ribbon
public RestTemplate restTemplate() {
return new RestTemplate();//httpClient
}
}
5、重启测试
6、原理
流程分析:
1、由于我们在入口程序使用了@EnableFeignClients注解,Spring启动后会扫描标注了@FeignClient注解的接口,然后生成代理类。
2、我们在@FeignClient接口中指定了value,其实就是指定了在Eureka中的服务名称。
3、在FeignClient中的定义方法以及使用了SpringMVC的注解,Feign就会根据注解中的内容生成对应的URL,然后基于Ribbon的负载均衡去调用REST服务。
为什么使用的是SpringMVC的注解?
i.其实,Feign是有自己的注解的@RequestLine,是因为Spring Cloud对Feign做了增强,兼容了SpringMVC的注解,使我们的学习成本更低
ii.专业的解释是这样的:
org.springframework.cloud.netflix.feign.FeignClientsConfiguration
设置的默认契约是SpringMVC契约。