一、简介

前面我们是使用RestTemplate实现rest api调用的,代码如下:

@GetMapping("/user/{id}")
public User findById(@PathVariable Long id) throws Exception {
    return  this.restTemplate.getForObject("http://spring-ribbon-eureka-client2/" + id, User.class);
}

由上代码可知,我们是使用拼接URI的,如果参数过多,就会很麻烦了,解决的方式就是采用Feign。


Feign是Netflix开发的声明式、模板化的HTTP客户端,Feign可帮助我们更便捷、优雅的调用HTTP API。

Spring cloud对Feign进行了增强,是Feign支持了SpringMVC注解,并整合了Ribbon和Eureka。

二、为服务消费者整合Feign

2.1、添加依赖

<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-feign</artifactId>
</dependency>

2.2、创建一个Feign接口,并添加@FeignClient注解

package com.example.demo.feign;

import com.example.demo.pojo.User;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * 描述
 *
 * @Author: 我爱大金子
 * @Description: 描述
 * @Date: Create in 10:14 2017/7/17
 */
@FeignClient(name = "spring-ribbon-eureka-client2")
public interface UserFeignClient {
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public User findById(@PathVariable("id") Long id) throws Exception;
}

说明:

 1、@FeignClient注解中的spring-ribbon-eureka-client2是一个任意的客户端名称,用于创建Ribbon负载均衡器。

 2、由于这实例使用了Eureka,所以Ribbon会把spring-ribbon-eureka-client2解析成Eureka Server服务注册表中的服务。

 3、如果不想使用Eureka,可使用service.ribbon.listOfServers属性配置服务器列表。


2.3、在Controller中添加feign

@RestController
public class UserController {
    @Autowired
    private UserFeignClient userFeignClient;

    @GetMapping("/user/{id}")
    public User findById(@PathVariable Long id) throws Exception {
        if (null == id) {
            return null;
        }
        return  this.userFeignClient.findById(id);
    }
}


2.4、修改启动类,为其添加@EnableFeignClients注解

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;

@EnableEurekaClient
@EnableFeignClients
@SpringBootApplication
public class SpringFeignConsumerApplication {

   public static void main(String[] args) {
      SpringApplication.run(SpringFeignConsumerApplication.class, args);
   }
}

三、自定义Feign配置

在Spring Cloud中,Feign的默认配置类是FeignClientsConfiguration,该类定义了Feign默认使用的编码器、×××、所使用的契约等。

Spring Cloud允许通过注解@FeignClient的configura属性自定义Feign的配置,自定义配置的优先级比FeignClientsConfiguration要高

另外,有的配置尽管没有提供默认值,但是spirng也会扫描其中列出的类型(也就是说,这部分配置也可以自定义)


例:自定义Feign的配置,让它使用Feign自带的注解进行工作。

注意:代码是基于上面工程的基础上

3.1、创建Feign的配置类

package com.example.demo.config;

import com.example.demo.ExcludeFromComponentScan;
import feign.Contract;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Feign的配置类
 * 注意:此类不该在主应用程序上下文的@ComponetScan中
 * @Author: 我爱大金子
 * @Description: Feign的配置类
 * @Date: Create in 15:57 2017/7/17
 */
@Configuration
@ExcludeFromComponentScan
public class FeignConfiguration {
    /**
     * 将契约改为feign原生的默认契约。这样就可以使用feign自带的注解了
     * @Author: 我爱大金子
     * @Description: 将契约改为feign原生的默认契约。这样就可以使用feign自带的注解了
     * @Date: 16:02 2017/7/17
     * @return 默认的feign契约
     */
    @Bean
    public Contract feignContract() {
        return new Contract.Default();
    }
}
package com.example.demo;

public @interface ExcludeFromComponentScan {

}

3.2、修改Feign接口

package com.example.demo.feign;

import com.example.demo.config.FeignConfiguration;
import com.example.demo.pojo.User;
import feign.Param;
import feign.RequestLine;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * 描述
 *
 * @Author: 我爱大金子
 * @Description: 描述
 * @Date: Create in 10:14 2017/7/17
 */
@FeignClient(name = "spring-ribbon-eureka-client2", configuration = FeignConfiguration.class)
public interface UserFeignClient {

    /**
     * 使用feign自带的注解@RequestLine
     * @Author: 我爱大金子
     * @Description: 使用feign自带的注解@RequestLine
     * @Date: 17:42 2017/7/17
     * @param id 用户id
     * @return 用户信息
     * @throws Exception
     */
    @RequestLine("GET /{id}")
    public User findById(@Param("id") Long id) throws Exception;
}


OK了

3.3、测试

访问:http://localhost:8086/user/1 

wKiom1lsiPLTrwRfAAAUjMLU9oQ031.png


类似的,还可以自定义Feign的编码器、×××、日志打印,甚至为Feign添加拦截器。

例如,一些接口需要进行基于Http Basic的认证后才能调用,配置类可以这样写:

@Configuration
@ExcludeFromComponentScan
public class FeignConfiguration {
    @Bean
    public BasicAuthRuestInterceptor basicAuthRuestInterceptor() {
        return new BasicAuthRuestInterceptor("user","password");
    }
}


未完,待续...