package com.ytpm.config.feign;

import feign.Feign;
import feign.Request;
import feign.Retryer;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.support.SpringDecoder;
import org.springframework.cloud.openfeign.support.SpringEncoder;
import org.springframework.cloud.openfeign.support.SpringMvcContract;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

import java.util.concurrent.TimeUnit;

/**
 * 自定义FeignClient 构造
 * @author lih
 * @date 2025-10-24 09:41
 */
@Configuration
public class FeignBuilderConfig {
    @Value("${feign.client.config.default.connectTimeout:3000}")
    private Integer connectTimeoutMillis;

    @Value("${feign.client.config.default.readTimeout:3000}")
    private Integer readTimeoutMillis;

    @Bean
    @Scope("prototype")
    public Feign.Builder feignBuilder(ObjectFactory<HttpMessageConverters> messageConverters) {
        return Feign.builder()
                .contract(new SpringMvcContract())
                .encoder(new SpringEncoder(messageConverters))
                .decoder(new SpringDecoder(messageConverters))
                .retryer(new Retryer.Default(100, TimeUnit.SECONDS.toMicros(1), 3))
                .options(new Request.Options(connectTimeoutMillis, readTimeoutMillis))
                .requestInterceptor(new FeignConfiguration());

    }
}