openFeign解决服务间调用超时问题

mac2024-06-06  46

在使用 openFeign 和 sentinel 进行服务间调用时,提示 read timeout

解决

1、 新建配置类 ServiceFeignConfiguration

import feign.Request; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * Feign配置 * * @author Zachary 2019-09-19 */ @Configuration public class ServiceFeignConfiguration { @Value("${service.feign.connectTimeout:60000}") private int connectTimeout; @Value("${service.feign.readTimeOut:60000}") private int readTimeout; @Bean public Request.Options options() { return new Request.Options(connectTimeout, readTimeout); } }

2、在需要使用 feign 的接口上使用配置

import com.landscape.landscape.service.user.config.ServiceFeignConfiguration; import com.landscape.landscape.service.user.service.fallback.FeignImServiceFallback; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; /** * @author Zachary 2019-09-06 */ @Component @FeignClient(value = "landscape-service-im", configuration = ServiceFeignConfiguration.class, fallback = FeignImServiceFallback.class) public interface FeignImService { /** * 腾讯IM注册用户 * * @param json * @return */ @PostMapping("/im/user/import") String importUser(@RequestBody String json); }
最新回复(0)