OpenFeign
什么是OpenFeign组件
最先出现的Feign组件是Netflix推出的Feign,但是现在Feign已经进入维护状态,所以Spring团队吸取Feign的精华推出了OpenFeign,为了减少程序猿的开发成本,所以这两个组件的使用方式是一样的
简介
OpenFeign其实就是一个Rest Client,和RestTemplate作用是一致的都是一个http客户端。
RestTemplate:Spring框架封装HttpClient对象
OpenFeign:伪HttpClient客户端对象 他可以使服务间通信变得更加简单 Feign默认集成了Ribbon 实现请求负载均衡
使用简单:写一个接口加一个注解,调用服务代码更加简单,自动完成数据传递传承中对象转换
为什么使用OpenFeign
1、RestTemplate使用问题:路径写死、不能自动转换响应结果为对应对象、必须集成ribbon实现负载均衡
2、OpenFeign组件解决了RestTemplate实现服务间通信所有问题
SpringCloud整合OpenFeign
下面样例建立两个服务category和product为例
当然首先就是在父项目下创建两个SpringBoot项目这里就不详细介绍了
主要的依赖
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 
 | <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-web</artifactId>
 </dependency>
 
 <dependency>
 <groupId>org.springframework.cloud</groupId>
 <artifactId>spring-cloud-starter-consul-discovery</artifactId>
 </dependency>
 
 <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-actuator</artifactId>
 </dependency>
 
 | 
好了下面开始配置OpenFeign
依旧是老套路,引入依赖(在这里我们是category调用product 所以我们要在调用放导入依赖)
| 12
 3
 4
 5
 
 | <dependency>
 <groupId>org.springframework.cloud</groupId>
 <artifactId>spring-cloud-starter-openfeign</artifactId>
 </dependency>
 
 | 
修改配置文件(因为我日常挂着代理,经常导致访问超时,没有需求可以不加)
| 12
 3
 4
 5
 6
 7
 8
 
 | feign:client:
 config:
 default:
 
 ConnectTimeOut: 5000
 
 ReadTimeOut: 5000
 
 | 
添加注解
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 
 | 
 
 
 @EnableDiscoveryClient
 @SpringBootApplication
 @EnableFeignClients
 public class CategoryApplication {
 public static void main(String[] args) {
 SpringApplication.run(CategoryApplication.class,args);
 }
 }
 
 | 
然后我们创建一个包,这个包的作用就是调用其他服务的接口

然后在需要到的地方注入,使用即可

是不是方便了很多,Feign也是集成了负载均衡,默认使用的依旧是轮询策略。
服务通信之间参数传递和响应处理
参数类型:1、传递零散类型 2、传递对象类型  3、数组或者集合类型参数
零散类型方式传递参数
queryString方式传递参数:?name=zhangsan
| 12
 3
 4
 
 | @GetMapping("/get")public String getInfo(String name,Integer age){
 return name+" "+age+" "+ port;
 }
 
 | 
对应接口
| 12
 
 | @GetMapping("/get")String getInfo(@RequestParam String name, @RequestParam Integer age);
 
 | 
注意:一定要加注解@RequestParam,如果是旧版还需要填写对应的value @RequestParam(value = "name"),而且value一定要和参数对应,,一般为了防止报错,最好是一步到位,不管旧版新版都填加上value对应的值
路径传递参数	url/zhangsan
| 12
 3
 4
 5
 
 |     @GetMapping("test/{name}/{age}")public String getAll(@PathVariable("name") String name, @PathVariable(value = "age") Integer age){
 return  name+" "+age+" "+port;
 }
 }
 
 | 
对应接口
| 12
 
 | @GetMapping("test/{name}/{age}")String getAll(@PathVariable(value = "name") String name, @PathVariable(value = "age") Integer age);
 
 | 
注意:在接口声明时,必须添加注解@PathVarable并且value与参数对应,还需要和原来接口一致
传递对象
提示:两个服务都用到了一个实体类,所以我们通常将实体类创建在父项目中,这样所有的项目都可以访问这个实体类
创建一个实体
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 
 | 
 
 
 @Data
 @AllArgsConstructor
 @NoArgsConstructor
 public class ProductPojo {
 private Integer id;
 private String name;
 private Double price;
 private Date bir;
 }
 
 | 
在product服务中创建接口
| 12
 3
 4
 5
 
 | @PostMapping("post")public String product(@RequestBody ProductPojo productPojo){
 System.out.println(productPojo.toString());
 return "ok";
 }
 
 | 
在category服务中对应的接口
| 12
 
 | @PostMapping("post")String product(@RequestBody ProductPojo productPojo);
 
 | 
在这里要注意使用@RequestBody,一般情况下是使用此注解完成使用对象的信息传递。
传递数组,集合类型
OpenFeign在传递数组类型参数时必须在声明时使用@RequestParam注解标识
例:
| 12
 
 | @GetMapping("/test")String test(@RequestParam("ids") String[] ids);
 
 |