Spring Boot微服务异常:服务X重复调用服务Y问题求助
When using Feign with Eureka, repeated calls to a slow microservice almost always stem from default retry mechanisms in Feign/Ribbon or misconfigured timeout settings. Let’s break down the causes and actionable fixes:
1. Feign's Default Retry Behavior
Feign includes a built-in retryer that automatically retries requests on certain failures—including perceived timeouts if not properly configured. This is likely triggering multiple calls when Microservice Y takes too long to respond.
Fix: Disable or Customize Feign Retries
You can turn off retries entirely for your Feign client, or adjust the retry logic to fit your needs:
Option 1: Use a Configuration Class
Create a dedicated config class for your Feign client:
import feign.Retryer; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class FeignClientConfig { @Bean public Retryer feignRetryer() { // Disable all retries return Retryer.NEVER_RETRY; } }
Reference this config in your Feign client annotation:
@FeignClient(name = "XYZ", configuration = FeignClientConfig.class) public interface YourFeignClient { @RequestMapping(path="/catalogs/getCatalogList",method = RequestMethod.GET) ResponseEntity<RestResponse> getCatalogList() throws RestException; }
Option 2: Configure via Properties
Add these lines to your application.properties or application.yml:
# Disable retries for all Feign clients feign.client.config.default.retryer=feign.Retryer.NEVER_RETRY # Or disable only for the XYZ service client feign.client.config.XYZ.retryer=feign.Retryer.NEVER_RETRY
2. Ribbon Retry Policies
If you’re using Ribbon (common with Eureka for client-side load balancing), it has its own set of retry rules that might be kicking in for slow responses.
Fix: Adjust Ribbon Retry Settings
Override these properties to limit or disable retries:
# No retries on the same server instance ribbon.MaxAutoRetries=0 # No retries on alternative server instances ribbon.MaxAutoRetriesNextServer=0 # Don’t retry all operation types (stick to safe ones like GET only, or disable entirely) ribbon.OkToRetryOnAllOperations=false
3. Timeout Mismatch
If your client’s read timeout is shorter than Microservice Y’s actual response time, the client will interpret the delay as a failure and trigger retries.
Fix: Increase Timeout Values
Set timeout limits that match or exceed Y’s expected response time:
Feign Timeout Configuration
# For all Feign clients feign.client.config.default.connectTimeout=5000 # 5 seconds for connection feign.client.config.default.readTimeout=30000 # 30 seconds for response # For the XYZ client specifically feign.client.config.XYZ.connectTimeout=5000 feign.client.config.XYZ.readTimeout=30000
Ribbon Timeout Configuration
ribbon.ConnectTimeout=5000 ribbon.ReadTimeout=30000
4. Check for Custom Retry Logic
Double-check your code in Microservice X to ensure there’s no manual retry logic (like Spring Retry annotations or custom loops) that could be causing repeated calls.
Final Testing
After applying these changes, test the scenario again with a slow response from Microservice Y. You should now see only one call being made instead of multiple retries.
内容的提问来源于stack exchange,提问作者gaurav singh

