Spring Cloud Feign使用okhttp3

在这里插入图片描述
在这里插入图片描述

package com.orchard.common.annotation;


import com.orchard.common.config.feign.FeignOkHttpConfig;
import com.orchard.common.interceptor.OkHttpTokenInterceptor;
import org.springframework.context.annotation.Import;

import java.lang.annotation.*;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Import({OkHttpTokenInterceptor.class,FeignOkHttpConfig.class})
@Documented
@Inherited
public @interface EnableFeignOkHttp {
}

package com.orchard.common.config.feign;

import com.orchard.common.interceptor.OkHttpTokenInterceptor;
import feign.Feign;
import okhttp3.ConnectionPool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.cloud.openfeign.FeignAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

@Configuration
@ConditionalOnClass(Feign.class)
@AutoConfigureBefore(FeignAutoConfiguration.class)
public class FeignOkHttpConfig {
	
    @Autowired
    OkHttpTokenInterceptor okHttpTokenInterceptor;

    private int feignOkHttpReadTimeout = 60;
    private int feignConnectTimeout = 60;
    private int feignWriteTimeout = 120;

    @Bean
    public okhttp3.OkHttpClient okHttpClient() {
        return new okhttp3.OkHttpClient.Builder().readTimeout(feignOkHttpReadTimeout, TimeUnit.SECONDS).connectTimeout(feignConnectTimeout, TimeUnit.SECONDS)
                .writeTimeout(feignWriteTimeout, TimeUnit.SECONDS).connectionPool(new ConnectionPool())
                .addInterceptor(okHttpTokenInterceptor)
                .build();
    }
}

package com.orchard.common.interceptor;

import com.orchard.common.base.BaseContextHandler;
import com.orchard.common.constants.CommonConstants;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Request.Builder;
import okhttp3.Response;
import org.springframework.stereotype.Component;

import java.io.IOException;


/**
 * @author ace
 */
@Component
public class OkHttpTokenInterceptor implements Interceptor {

	@Override
	public Response intercept(Chain chain) throws IOException {

		Request newRequest;

		Builder header = chain.request()
				.newBuilder();

		Object spm=BaseContextHandler.get(CommonConstants.JWT_SPM_HEADER);
		Object token=BaseContextHandler.get(CommonConstants.JWT_TOKEN_HEADER);
        Object session = BaseContextHandler.getUserSession();
        
        if (spm != null) header.header(CommonConstants.JWT_SPM_HEADER, spm.toString());
		if(token!=null) header.header(CommonConstants.JWT_TOKEN_HEADER,token.toString());
		if(session!=null) header.header(CommonConstants.BS_CRED,session.toString());

		newRequest = header.build();
		Response response = chain.proceed(newRequest);
		return response;
	}
}

spring cloud feign使用okhttp3

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页