Feign介绍

  1. Feign简介
    Feign is a declarative web service client. It makes writing web service clients easier. To use Feign create an interface and annotate it. It has pluggable annotation support including Feign annotations and JAX-RS annotations. Feign also supports pluggable encoders and decoders. Spring Cloud adds support for Spring MVC annotations and for using the sameHttpMessageConverters used by default in Spring Web. Spring Cloud integrates Ribbon and Eureka to provide a load balanced http client when using Feign.

    这是官网API中的介绍,主要是说Feign是一个声明式的web 服务客户端;它支持可插拔的注解,包含Feign注解和JAX-RS注解;并且支持可插拔的编码、解码;Spring Cloud增加了对Spring MVC注解的集成;Feign集成了Ribbon和Eureka来提供对http客户端的负载均衡。

  2. 怎么使用
    在官网API 中给出了例子,如下为官网中的例子:

@Configuration
@ComponentScan
@EnableAutoConfiguration
@EnableEurekaClient
@EnableFeignClients
public class Application {
  public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
  }
}

@FeignClient("stores")
public interface StoreClient {
@RequestMapping(method = RequestMethod.GET, value = "/stores")
List<Store> getStores();
@RequestMapping(method = RequestMethod.POST, value = "/stores/{storeId}", consumes = "application/json")
Store update(@PathVariable("storeId") Long storeId, Store store);
}

根据上面的例子,我们知道我们需要作三步:
1) 首先要使用,那就需要引进jar包,在pom.xml中配置feign的starter
2) 然后,在Application类增加注解@EnableFeignClients
3) 最后再新建一个接口类,并声明一些方法,并做一些注解,这里要注意注解的value值要与需要访问的实例的对应接口的名称一样。

  1. 怎么使用
    我们这里以上次的microservice-consumer-movie工程为基础,重新复制一个并重新命名为microservice-consumer-movie-feign,修改pom.xml、.project中相应的工程名称。
    好了,按照上面的三步来进行:
    1) 配置pom.xml,引进spring-cloud-starter-feign;
    2) 在Application类添加注解@EnableFeignClients
    3) 创建接口类,这里我创建的是UserFeignClient,代码如下:
@FeignClient("microservice-provider-user")
public interface UserFeignClient {

    /**
     * 这里需要注意的两个地方
     * <p>
     * 1、在这里不支持GetMapping注解,如果用这个注解,不能启动
     * <p>
     * 2、@PathVariable需要设置value,如果不设置也不能成功启动
     * 
     * @param id
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "/simple/{id}")
    public User findById(@PathVariable("id") Long id);
}

完成这三步之后,还需要MovieController类,因为我们现在使用Feign了,所以就将之前的RestTemplate等去掉,添加UserFeignClient实例注解,在方法findById中,通过UserFeignClient中的方法findByid来返回User类。代码如下:

@RestController
public class MovieController {

    @Autowired
    private UserFeignClient userFeignClient;

    @GetMapping("/movie/{id}")
    public User findById(@PathVariable Long id){
        return userFeignClient.findById(id);
    }
}

完成上面的操作后,我们先后启动eureka-server 、microservice-provider-user,在启动feign工程;完成之后,通过浏览器访问:http://ip:port/movie/2,获得结果:
{“id”:2,”username”:”user2”,”name”:”李四”,”age”:20,”balance”:100.22}

4、需要注意的坑点总结
1) 接口中我们即使是通过GET请求,但是也不能通过@GetMapping的方式,大家可以代码实验下;
2) 方法中参数的注解@PathVariable必须要设置value值,不然不能成功启动;
3) 先给出接口中的方法代码:

/**
     * 
     * 注意:这里是一个坑,该请求不会成功,只要参数是复杂对象,即使指定了GET方法,feign依然会以post方法进行发送请求
     * <p>
     * 如报错:<code>{"timestamp":1491976241173,"status":405,"error":"Method Not Allowed","exception":"org.springframework.web.HttpRequestMethodNotSupportedException","message":"Request method 'POST' not supported","path":"/get-user"}</code>
     * 
     * @param user
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "/get-user")
    public User getUser(@RequestBody User user);

在这个方法中,我使用的是GET请求,并且用User作为参数对象,但是在我请求的时候,控制台给我打印了个错误,其中主要错误内容:
{“timestamp”:1491976241173,”status”:405,”error”:”Method Not Allowed”,”exception”:”org.springframework.web.HttpRequestMethodNotSupportedException”,”message”:”Request method ‘POST’ not supported”,”path”:”/get-user”}
这就是说,因为我们使用的是User作为参数,Feign默认还是为我们通过POST来请求的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值