前言
在前面的示例中,我们消费spring boot提供的Restful服务的时候,使用的是RestTemplate来实现的,实现起来还是比较复杂的,尤其是在消费复杂的Restful服务的时候,还需要进行一系列的转换,编解码等,使用Feign就完全不用考虑这个问题了。
一、Feign简介
Feign是一种声明式、模板化的HTTP客户端。在Spring Cloud中使用Feign, 我们可以做到使用HTTP请求远程服务时能与调用本地方法一样的编码体验,开发者完全感知不到这是远程方法,更感知不到这是个HTTP请求,这整个调用过程和Dubbo的RPC非常类似。开发起来非常的优雅。
二、在spring cloud中添加Feign支持
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
三、新建Spring Starter Project
建工程的过程在前面的示例中已经重复过很多次了,这里不做冗余的介绍,目录结构如下:
该示例是从前面的
使用RestTemplate消费spring boot的Restful服务
示例改造而来,调用的都是springboot-h2服务四、新建接口,并实现Feign client
package com.chhliu.springboot.restful.feignclient;
import java.util.List;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PathVari