在我的前一篇文章中已经讲解了什么是Feign,他的作用有什么,还有一个简单的Feign例子。 声明式服务调用Feign
这篇文章主要讲解Feign的复杂多参数情况、GZIP、连接池、日志情况和负载均衡。
我的api和product与上一篇文章中一样,没有什么变化。大家可以参考上一篇文章中的api和product,我们在这里为了减少重复的代码和页面,我只讲consumer中的需要修改的地方。
1、处理复杂参数
目录结构:
加入依赖:
<dependency>
<groupId>${
project.parent.groupId}</groupId>
<artifactId>e-book-product-api</artifactId>
<version>${
project.parent.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
ProductController中的代码:
@RestController
public class ProductController {
@Autowired
private ProductService productService;
@RequestMapping(value="list",method=RequestMethod.GET)
public List<Product> listProduct(){
List<Product> list= this.productService.listProduct();
return list;
}
//一个参数
@RequestMapping(value="get",method=RequestMethod.GET)
public Product getProduct(@RequestParam("id") Integer id) {
return this.productService.getProduct(id);
}
//------------------------多参数----------------------
//不能这么写,因为feign会自动转换为post
@RequestMapping(value="get1",method=RequestMethod.GET)
public Product getProduct1(Product obj) {
return this.productService.getProduct1(obj);
}
@RequestMapping(value="get2",method=RequestMethod.GET)
public Product getProduct2(@RequestParam("id") Integer id,@RequestParam("name") String name) {
return this.productService.getProduct