spring-cloud-openfeign自定义某接口请求超时时间;所有服务及单个服务超时时间设置

Openfeign及Okhttp版本
该版本无Ribbon,使用Sentinel代替Hystrix

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
    <version>3.5.1</version>
</dependency>
<dependency>
    <groupId>io.github.openfeign</groupId>
    <artifactId>feign-okhttp</artifactId>
    <version>11.10</version>
</dependency>
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
    <version>2021.0.4.0</version>
</dependency>

1. 自定义某接口的超时时间

controller通过@FeignClient调用almond-file服务,同时在接口中定义超时时间

@GetMapping("/login")
    public AjaxResult login(){
        Request.Options options = new Request.Options(3000L, TimeUnit.MILLISECONDS,
                3000L, TimeUnit.MILLISECONDS, true);
        AjaxResult upload = feignFileService.upload(options);
        return upload;
    }
@Component
@FeignClient(value = "almond-file", fallback = FeignFileServiceFallback.class)
public interface FeignFileService {

    @GetMapping("/file/upload")
    public AjaxResult upload(Request.Options options);

    @GetMapping("/file/download")
    public AjaxResult download();

}

2.默认所有服务超时时间(yml)

# Feign超时时间
feign.httpclient.enabled=false
feign.okhttp.enabled=true
feign.client.config.default.connectTimeout=5000
feign.client.config.default.readTimeout=5000

3.某个服务auth超时时间(yml)

# Feign超时时间
feign.httpclient.enabled=false
feign.okhttp.enabled=true
feign.client.config.auth.connectTimeout=5000
feign.client.config.auth.readTimeout=5000

你可能感兴趣的:(java,spring,spring,boot)