SpringBoot 集成Feign的使用

前言:最近项目中经常用到Feign进行服务端之前的调用,因此小小总结一下
1. Feign介绍

Feign是Netflix公司开源的轻量级rest客户端,使用Feign可以非常方便的实现Http 客户端。Spring Cloud引入Feign并且集成了Ribbon实现客户端负载均衡调用。

2. pom.xml

不写入版本号的话会引入和springboot版本号相匹配的版本

<dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
3. 启动类加入注解 @EnableFeignClients
@SpringBootApplication
@EnableFeignClients // Feign注解
public class TestApplication {
    public static void main(String[] args) {
        SpringApplication.run(TestApplication.class, args);
    }
}
4. 添加Feign配置文件(如果不添加此配置文件会报如下错误)

错误信息: No qualifying bean of type ‘org.springframework.boot.autoconfigure.http.HttpMessag

import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;

import java.util.stream.Collectors;

/**
 * feign的http客户端配置
 */
@Configuration
public class FeignConfig {
    /**
    *No qualifying bean of type ‘org.springframework.boot.autoconfigure.http.HttpMessage
    */
    @Bean
    @ConditionalOnMissingBean
    public HttpMessageConverters messageConverters(ObjectProvider<HttpMessageConverter<?>> converters) {
        return new HttpMessageConverters(converters.orderedStream().collect(Collectors.toList()));
    }
  
}
5. 如果需要传递header参数,需要下述配置文件

bootstrap.yml 增加配置

  • 设置hystrix的隔离策略
    hystrix.command.default.execution.isolation.strategy = SEMAPHORE
import feign.Logger;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * feign的header配置
 */
@Configuration
public class FeignHeaderConfig {

    //feign请求转发是需要从原请求头复制的header信息
    final String[] copyHeaders = new String[]{"cookie"};
    /**
     * 重写后feign转发请求会携带原请求的Head信息
     */
    private class FeignBasicAuthRequestInterceptor implements RequestInterceptor {
        @Override
        public void apply(RequestTemplate requestTemplate) {
            // SystemConstant.COOKIE="Cookie"
            // new DefaultTokenHandler().getTokenInCookie() 为获取到的Cookie信息
            /**
             spring-boot-starter-webflux 框架走下述方法
             <dependency>
                 <groupId>org.springframework.boot</groupId>
                 <artifactId>spring-boot-starter-webflux</artifactId>
             </dependency>
             */
            requestTemplate.header(SystemConstant.COOKIE, new DefaultTokenHandler().getTokenInCookie());


            /**
             spring-boot-starter-web 框架走下面注释方法
             <dependency>
                 <groupId>org.springframework.boot</groupId>
                 <artifactId>spring-boot-starter-web</artifactId>
             </dependency>
             
             
             */
            /*
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder
                    .getRequestAttributes();
            if (attributes != null) {
                HttpServletRequest request = attributes.getRequest();
                Enumeration<String> headerNames = request.getHeaderNames();
                if (headerNames != null) {
                    while (headerNames.hasMoreElements()) {
                        String name = headerNames.nextElement();
                        String values = request.getHeader(name);
                        if (iscopy(name)) {
                            requestTemplate.header(name, values);
                        }
                    }
                }
            }
            */
        }
    }

    /**
     * @param name
     * @return
     */
    private Boolean iscopy(String name) {
        for (String header : copyHeaders) {
            if (header.equals(name)) {
                return true;
            }
        }
        return false;
    }

    /**
     * feign请求拦截器
     *
     * @return
     */
    @Bean
    public RequestInterceptor requestInterceptor() {
        return new FeignBasicAuthRequestInterceptor();
    }
    
    /**
     * 开启feign调用日志打印的debug模式
     *
     * @return
     */
    @Bean
    Logger.Level feignLoggerLevel() {
        return Logger.Level.FULL;
    }
}
6. 在Feign客户端中配置
  • FeignTest.class 客户端调用类
/**
 * 发布用户信息查询相关的接口
 */
@FeignClient(value = "base", configuration = FeignHeaderConfig.class, contextId = "FeignTest", fallbackFactory = FeignTestFactory.class)
public interface FeignTest {

    /**
     * 增量更新用户信息
     *
     * @param info
     */
    @PostMapping("user")
    public BaseResponse user(@RequestBody Info info);
}
  • FeignTestFactory.class 异常处理的工厂类
/**
 * 异常处理的工厂类
 */
@Component
public class FeignTestFactory<T> implements FallbackFactory<T>, LoggerBase {

    @Override
    public T create(Throwable cause) {
        if (cause.getMessage() != null)
            error("feign接口调用异常,{}", cause.getMessage());
        return (T) new TestFallBackImp(cause.getMessage());
    }
}
  • TestFallBackImp.class 服务降级处理类
/**
 * 服务降级处理类
 */
public class TestFallBackImp implements FeignTest {

    String errMsg;

    public TestFallBackImp(String errMsg) {
        this.errMsg = errMsg;
    }

    @Override
    public BaseResponse user(Info info) {
        return new BaseResponse().err(errMsg);
    }
}
至此,Feign已经配置好了
其他错误
  • The bean 'XXX.FeignClientSpecification', defined in null, could not be registered
    原因:2.1.0以上版本的,将不再默认支持 FeignClient 的name属性 的相同名字。
    即 :多个接口上的@FeignClient(“相同服务名”)会报错,overriding is disabled(覆盖 是 禁止的/关闭的)。
    可以添加配置
    spring.main.allow-bean-definition-overriding=true
    阅读源码后,发现也可以把 contextId 设置成不一样的
希望对你有所帮助

推荐阅读更多精彩内容