Spring Cloud Alibaba-全局配置自定义和支持的配置项( 二 )

增加类配置:
package com.nx.user.interceptor;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.InterceptorRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;@Configurationpublic class WebMvcConfig extends WebMvcConfigurationSupport {@Overrideprotected void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(new AuthInterceptor());}复制代码优先级:
全局代码<全局属性<细粒度代码<细粒度属性复制代码4、Client 设置Feign 中默认使用 JDK 原生的 URLConnection 发送 HTTP 请求,我们可以集成别的组件来替换掉 URLConnection,比如 Apache HttpClient,OkHttp 。
4.1 扩展点Feign发起调用真正执行逻辑:feign.Client#execute (扩展点)
public interface Client {Response execute(Request request, Options options) throws IOException; }4.2 配置Apache HttpClient

  1. 引入依赖
    <dependency><groupId>io.github.openfeign</groupId><artifactId>feign-httpclient</artifactId></dependency>
  2. 修改yml配置
    开启feign ,这里可以不用配置,可以参考源码分析
    feign:httpclient:#使用apache httpclient做请求,而不是jdk的HttpUrlConnectionenabled: true# feign最大链接数 默认200max-connections: 200#feign 单个路径的最大连接数默认 50max-connections-per-route: 50
  3. 源码分析 FeignAutoConfiguration

Spring Cloud Alibaba-全局配置自定义和支持的配置项

文章插图
此时默认增加一个ApacheHttpCient实现类
Spring Cloud Alibaba-全局配置自定义和支持的配置项

文章插图
4.3 设置OkHttp
  1. 引入依赖
    <dependency><groupId>io.github.openfeign</groupId><artifactId>feign-okhttp</artifactId></dependency>复制代码
  2. 增加配置
    feign:okhttp:enabled: true#线程池可以使用httpclient的配置httpclient:max-connections: 200max-connections-per-route: 50复制代码

Spring Cloud Alibaba-全局配置自定义和支持的配置项

文章插图
3、源码分析 FeignAutoConfiguration
Spring Cloud Alibaba-全局配置自定义和支持的配置项

文章插图
5、超时配置通过 Options 可以配置连接超时时间和读取超时时间,Options 的第一个参数是连接的超时时间(ms),默认值是 10s;第二个是请求处理的超时时间(ms),默认值是 60s 。
Request.Options
Spring Cloud Alibaba-全局配置自定义和支持的配置项

文章插图
5.1 代码配置@Beanpublic Request.Options options(){return new Request.Options(2000,50000);}msb-stock改造
@GetMapping("query")public User queryInfo(User user){try {Thread.sleep(10*1000);} catch (InterruptedException e) {e.printStackTrace();}return user;}
Spring Cloud Alibaba-全局配置自定义和支持的配置项

文章插图

【Spring Cloud Alibaba-全局配置自定义和支持的配置项】


推荐阅读