Spring Cloud OpenFeign 超时设置与开启重试

超时设置

#数据中台HRestful API请求
feign:
  okhttp:
    enabled: true
  client:
    config:
      default:
        #日志打印级别
        loggerLevel: basic
        #跨服务接口请求超时
        readTimeout: 20000
        #跨服务请求连接超时
        connectTimeout: 5000

开启Feign重试

package com.cspg.snlsct.cs.config;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

import java.io.IOException;

/**
 * 重试拦截器
 * @Date 2023/7/7 上午10:45
 **/
public class RetryIntercepter implements Interceptor {

    public int maxRetry;//最大重试次数
    private int retryNum = 0;//假如设置为3次重试的话,则最大可能请求4次(默认1次+3次重试)

    public RetryIntercepter(int maxRetry) {
        this.maxRetry = maxRetry;
    }

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        Response response = chain.proceed(request);
        while (!response.isSuccessful() && retryNum < maxRetry) {
            retryNum++;
            response = chain.proceed(request);
        }
        return response;
    }
}

添加在Feign的配置类

package com.cspg.snlsct.cs.config;

import okhttp3.ConnectionPool;
import okhttp3.OkHttpClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

/**
 * 配置okhttp
 * @Date 2023/7/7 上午10:40
 **/
@Configuration
public class FeignOkHttpConfig {

    @Bean
    public ConnectionPool pool() {
        //最大连接数、连接存活时间、存活时间单位(分钟)
        return new ConnectionPool(200, 30, TimeUnit.MINUTES);
    }

    @Bean
    public OkHttpClient okHttpClient() {
        return new OkHttpClient.Builder()
                .retryOnConnectionFailure(true)    //是否开启缓存
                .connectionPool(pool())             //连接池
                .connectTimeout(20L, TimeUnit.SECONDS) // 连接超时时间
                .readTimeout(20L, TimeUnit.SECONDS) // 读取超时时间
                .followRedirects(true) // 是否允许重定向
                .addInterceptor(new RetryIntercepter(5))
                .build();
    }

}

Feign全局加自定义请求头(其他)

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Data
@Component
@ConfigurationProperties(prefix = "datam")
public class DatamConfig {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty("地址")
    private String url;

    @ApiModelProperty("API-KEY")
    private String apikey;

}
import cn.hutool.extra.spring.SpringUtil;
import feign.Logger;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class FeignRequestInterceptor implements RequestInterceptor {

    private DatamConfig datamConfig;

    @ApiModelProperty("API-KEY")
    @Getter
    public static String apiKey;

    @Bean
    Logger.Level photovoltFeignLevel(){
        //这里记录所有,根据实际情况选择合适的日志level
        return Logger.Level.FULL;
    }

    //请求头携带token
    @Override
    public void apply(RequestTemplate requestTemplate) {
        this.datamConfig = SpringUtil.getBean(DatamConfig.class);
        this.apiKey = datamConfig.getApikey();
        requestTemplate.header("API-KEY", apiKey);
    }
 
}

你可能感兴趣的:(spring,cloud,java,数据库)