美文网首页
Spring Cloud Feign自定义配置

Spring Cloud Feign自定义配置

作者: 杨健kimyeung | 来源:发表于2020-08-21 11:10 被阅读0次

    一、@EnableFeignClients详解

    1、作用

    扫描和注册所有使用注解@FeignClient定义的feign客户端

    2、源码

    <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="java" cid="n5" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background: rgb(51, 51, 51); position: relative !important; padding: 10px 10px 10px 30px; width: inherit; color: rgb(184, 191, 198); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">public @interface EnableFeignClients {
    String[] value() default {};
    String[] basePackages() default {};
    Class<?>[] basePackageClasses() default {};
    Class<?>[] defaultConfiguration() default {};
    Class<?>[] clients() default {};</pre>

    3、属性说明

    属性 说明
    String[] value() 包路径。比如 com.sbc.service,会扫描这个包路径下带有 @FeignClient 注解的类并处理
    String[] basePackages()
    Class<?>[] basePackageClasses() 跟 basePackages 作用一致,basePackages 是个 String 数组,而 basePackageClasses 是个 Class 数组,用于扫描这些类对应的 package
    Class<?>[] defaultConfiguration() 默认的配置类,对于所有的 Feign Client,这些配置类里的配置都会对它们生效,可以在配置类里构造 feign.codec.Decoder, feign.codec.Encoderfeign.Contract 等bean;
    Class<?>[] clients() 注解修饰的类集合,如果指定了该属性,那么扫描功能相关的属性就是失效。比如 value、basePackages 和 basePackageClasses

    二、@FeignClient

    1、作用

    创建声明是API接口,该接口是RESTful风格的。Feign被设计成插拔式的,可注入其他组件和Feign一起使用。默认情况下Feign会和Ribbon相结合进行负载均衡

    2、源码

    <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="java" cid="n30" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background: rgb(51, 51, 51); position: relative !important; padding: 10px 10px 10px 30px; width: inherit; color: rgb(184, 191, 198); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">public @interface FeignClient {
    @AliasFor("name")
    String value() default "";
    @Deprecated
    String serviceId() default "";
    String contextId() default "";
    @AliasFor("value")
    String name() default "";
    String qualifier() default "";
    String url() default "";
    boolean decode404() default false;
    Class<?>[] configuration() default {};
    Class<?> fallback() default void.class;
    Class<?> fallbackFactory() default void.class;
    String path() default "";
    boolean primary() default true;</pre>

    3、属性说明

    属性 说明
    String value() 提供服务的名称
    String path() 请求路径,定义当前Feign客服端统一前缀
    String name() 跟 value 属性作用一致
    String qualifier() 给 FeignClient 设置 @Qualifier 注解
    String url() 手动调用url地址,一般用于测试,用于替换服务名。优先级比服务名高。
    boolean decode404() 默认是 false,表示对于一个 http status code 为 404 的请求是否需要进行 decode,默认不进行 decode,当成一个异常处理。设置为true之后,遇到 404 的 response 还是会解析 body
    Class<?>[] configuration() 对于单个 FeignClient 的配置,而 @EnableFeignClients 里的 defaultConfiguration 属性是作用域全局的,针对所有的 FeignClient
    Class<?> fallback() 表示 fallback 类,需要实现 FeignClient 对应的接口,当调用方法发生异常的时候会调用这个 Fallback 类对应的 FeignClient 接口方法
    Class<?> fallbackFactory() 表示生产 fallback 类的 Factory,可以实现 feign.hystrix.FallbackFactory 接口,FallbackFactory 内部会针对一个 Throwable 异常返回一个 Fallback 类进行 fallback 操作 ,用于封装通用的容错逻辑,减少重复代码
    boolean primary() 默认是 true,表示当前这个 FeignClient 生成的 bean 是否是 primary。

    个人建议 编码器,解码器,重试器,调用解析器一般使用默认的就行

    三、自定义配置

    1、说明

    在实际开发中有可能Feign默认配置不能满足需求,这时就需要自定义Feign配置。

    按着配置方式来来划分:

    • java代码配置

    • 配置文件配置

    按照作用来说又可以分为:

    • 全局配置

    • 局部配置(针对单个Feign接口)

    配置文件配置格式如下:

    <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="yml" cid="n83" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background: rgb(51, 51, 51); position: relative !important; padding: 10px 10px 10px 30px; width: inherit; color: rgb(184, 191, 198); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">feign:
    client:

    修改默认配置的名称 默认名default

    default-config: default
    config:

    feign的名称

    feign-name:

    连接超时时间

    connection-timeout: 1000

    读取超时时间

    read-timeout: 5000

    日志配置级别

    logger-level: full

    错误解码器

    error-decoder: com.xxx

    重试机制

    retryer: feign.Default

    自定义拦截器

    request-interceptors:

    decoder:
    encoder:

    Spring MVC 注解支持

    contract:</pre>

    FeignClientConfiguration配置类信息

    <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="java" cid="n85" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background: rgb(51, 51, 51); position: relative !important; padding: 10px 10px 10px 30px; width: inherit; color: rgb(184, 191, 198); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">public static class FeignClientConfiguration {
    //. 日志级别
    private Logger.Level loggerLevel;
    //连接超时时间 java.net.HttpURLConnection#getConnectTimeout(),如果使用Hystrix,该配置无效
    private Integer connectTimeout;
    //读取超时时间 java.net.HttpURLConnection#getReadTimeout(),如果使用Hystrix,该配置无效
    private Integer readTimeout;
    //重试接口实现类,默认实现 feign.Retryer.Default
    private Class<Retryer> retryer;
    // 错误编码
    private Class<ErrorDecoder> errorDecoder;
    // 请求拦截器
    private List<Class<RequestInterceptor>> requestInterceptors;
    //是否开启404编码
    private Boolean decode404;
    }</pre>

    核心配置

    默认的请求connectionTimeout为10s,readTimeout为60s

    Java Config配置

    全局配置

    <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="java" cid="n90" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background: rgb(51, 51, 51); position: relative !important; padding: 10px 10px 10px 30px; width: inherit; color: rgb(184, 191, 198); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">@SpringBootApplication
    @EnableFeignClients(defaultConfiguration = FeignClientsConfiguration.class)
    public class NetflixFeignClientApplication {
    public static void main(String[] args) {
    SpringApplication.run(NetflixFeignClientApplication.class, args);
    }
    }</pre>

    局部配置

    <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="java" cid="n92" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background: rgb(51, 51, 51); position: relative !important; padding: 10px 10px 10px 30px; width: inherit; color: rgb(184, 191, 198); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">@Configuration
    public class FeignCustomConfiguration {
    }

    @FeignClient(configuration= FeignCustomConfiguration.class)
    public interface FeignUserService {
    }</pre>

    四、网络请求配置

    1、httpclient配置

    1、添加依赖

    <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="xml" cid="n97" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background: rgb(51, 51, 51); position: relative !important; padding: 10px 10px 10px 30px; width: inherit; color: rgb(184, 191, 198); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"><dependency>
    <groupId>io.github.openfeign</groupId>
    <artifactId>feign-httpclient</artifactId>
    </dependency></pre>

    2、配置信息

    <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="yml" cid="n99" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background: rgb(51, 51, 51); position: relative !important; padding: 10px 10px 10px 30px; width: inherit; color: rgb(184, 191, 198); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">feign:
    httpclient:

    让feign使用apache httpclient作请求

    enabled: true

    feign的最大连接数

    max-connections: 200

    feign单个路径的最大连接数

    max-connections-per-route: 50</pre>

    2、OkHttp(推荐)

    1、添加依赖

    <pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="xml" cid="n102" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background: rgb(51, 51, 51); position: relative !important; padding: 10px 10px 10px 30px; width: inherit; color: rgb(184, 191, 198); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"><dependency>
    <groupId>io.github.openfeign</groupId>
    <artifactId>feign-okhttp</artifactId>
    </dependency></pre>

    2、配置信息

    <pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="java" cid="n104" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background: rgb(51, 51, 51); position: relative !important; padding: 10px 10px 10px 30px; width: inherit; color: rgb(184, 191, 198); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">feign:
    httpclient:
    enabled: false
    okhttp:
    enabled: true</pre>

    四、Gzip压缩

    1、说明

    Spring Cloud Feign支持请求和响应Gzip压缩,提供传输效率。只需要在配置文件配置即可

    2、配置信息

    <pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="yml" cid="n109" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background: rgb(51, 51, 51); position: relative !important; padding: 10px 10px 10px 30px; width: inherit; color: rgb(184, 191, 198); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">feign:
    client:
    config:
    # 默认全局配置 默认配置名称 default
    default:
    compression:
    request:
    # 配置请求支持GZIP压缩,默认为false
    enable: true
    #配置压缩支持的Mime Type
    mime-types: application/json
    # 配置压缩数据大小的上下限
    min-request-size: 2048
    reponse:
    #配置响应支持GZIP压缩,默认为false
    enable: true</pre>

    1、Date类型参数的时差

    <pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="java" cid="n111" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background: rgb(51, 51, 51); position: relative !important; padding: 10px 10px 10px 30px; width: inherit; color: rgb(184, 191, 198); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">@Component
    public class DateFormatRegister implements FeignFormatterRegistrar{

    public DateFormatRegister(){
    }

    @Override
    public void registerFormatters(FormatterRegistry registry) {
    registry.addConverter(Date.class, String.class, new Date2StringConverter());
    }

    private class Date2StringConverter implements Converter<Date,String>{

    @Override
    public String convert(Date source) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    return sdf.format(source);
    }

    }
    }</pre>

    参考

    https://blog.csdn.net/andy_zhang2007/article/details/86680622#_2

    相关文章

      网友评论

          本文标题:Spring Cloud Feign自定义配置

          本文链接:https://www.haomeiwen.com/subject/ockujktx.html