openFeign 一次请求返回多次重复的数据
可能是因为 openFeign 默认启用了重试机制,当请求失败时会自动重试,导致数据重复返回。可以通过设置 FeignClient 的配置来禁用重试机制,例如:
@FeignClient(name = "example-service", configuration = {DisableRetryConfiguration.class})
public interface ExampleFeignClient {
// ...
}
@Configuration
public class DisableRetryConfiguration {
@Bean
public Retryer feignRetryer() {
return Retryer.NEVER_RETRY;
}
}
另外,如果服务端返回的数据确实有多个重复的部分,可以在 FeignClient 中设置 @RequestMapping 的 produces 参数来限制接受的响应类型,例如:
@FeignClient(name = "example-service")
public interface ExampleFeignClient {
@RequestMapping(method = RequestMethod.GET, value = "/example", produces = "application/json;charset=UTF-8")
List<Example> getExamples();
}
这样就只接受 application/json;charset=UTF-8 类型的响应,避免了重复数据的问题
原文地址: https://www.cveoy.top/t/topic/ggfj 著作权归作者所有。请勿转载和采集!