深入理解cloud 题外话1:httpPool连接池工具类配置,logback.xml,feign日志配置

首页 / 新闻资讯 / 正文

@ConfigurationpublicclassHttpPool{@BeanpublicHttpClienthttpClient(){System.out.println("init feign httpclient configuration ");// 生成默认请求配置RequestConfig.Builder requestConfigBuilder=RequestConfig.custom();// 超时时间         requestConfigBuilder.setSocketTimeout(5*1000);// 连接时间         requestConfigBuilder.setConnectTimeout(5*1000);RequestConfig defaultRequestConfig= requestConfigBuilder.build();// 连接池配置// 长连接保持30秒finalPoolingHttpClientConnectionManager pollingConnectionManager=newPoolingHttpClientConnectionManager(30,TimeUnit.MILLISECONDS);// 总连接数         pollingConnectionManager.setMaxTotal(5000);// 同路由的并发数         pollingConnectionManager.setDefaultMaxPerRoute(100);// httpclient 配置HttpClientBuilder httpClientBuilder=HttpClientBuilder.create();// 保持长连接配置,需要在头添加Keep-Alive         httpClientBuilder.setKeepAliveStrategy(newDefaultConnectionKeepAliveStrategy());         httpClientBuilder.setConnectionManager(pollingConnectionManager);         httpClientBuilder.setDefaultRequestConfig(defaultRequestConfig);HttpClient client= httpClientBuilder.build();// 启动定时器,定时回收过期的连接Timer timer=newTimer();         timer.schedule(newTimerTask(){@Overridepublicvoidrun(){//        System.out.println("=====closeIdleConnections===");                 pollingConnectionManager.closeExpiredConnections();                 pollingConnectionManager.closeIdleConnections(5,TimeUnit.SECONDS);}},10*1000,5*1000);System.out.println("===== Apache httpclient 初始化连接池===");return client;}}
@ConfigurationpublicclassFeignClientConfig{@BeanLogger.LevelfeignLoggerLevel(){// 设置日志returnLogger.Level.FULL;}}
<!-- if debug set to true, context info always print otherwise the contex info will print when error occour --><configurationscan="true"scanPeriod="60 seconds"debug="false"><appendername="stdout"class="ch.qos.logback.core.ConsoleAppender"><layoutclass="ch.qos.logback.classic.PatternLayout"><pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{40} - %msg%n</pattern></layout></appender><!-- 所有日志文件--><appendername="AllLog"class="ch.qos.logback.core.rolling.RollingFileAppender"><rollingPolicyclass="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"><fileNamePattern>D:/temp/idea/%d{yyyy-MM-dd}.log</fileNamePattern><maxHistory>30</maxHistory></rollingPolicy><encoder><pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{40} - %msg%n</pattern></encoder></appender><!-- <appender name="TaskLog" class="ch.qos.logback.core.rolling.RollingFileAppender">         <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">             <fileNamePattern>log/abcchina/task/%d{yyyy-MM-dd}.log</fileNamePattern>             <maxHistory>30</maxHistory>         </rollingPolicy>         <encoder>             <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{40} - %msg%n</pattern>         </encoder>     </appender>     <appender name="SendLog" class="ch.qos.logback.core.rolling.RollingFileAppender">         <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">             <fileNamePattern>log/abcchina/send/%d{yyyy-MM-dd}.log</fileNamePattern>             <maxHistory>30</maxHistory>         </rollingPolicy>         <encoder>             <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{40} - %msg%n</pattern>         </encoder>     </appender>     &lt;!&ndash; 包下的所有类指定Appender 这里也可以指定类名称例如:com.aa.bb.ClassName&ndash;&gt;     <logger name="com.aa.bb" >         <appender-ref ref="TaskLog" />     </logger>      <logger name="com.aa.cc" >         <appender-ref ref="SendLog" />     </logger>--><rootlevel="DEBUG"><appender-refref="AllLog"/><appender-refref="stdout"/></root></configuration>
Top