First, concept introduction.
- Ribbon: Load balancing is a multi-instance load balancing configuration for services.
- Hystrix: Fuse, when calling zuul Gateway specific business, may be affected by the network, code execution time and so on for a long time no response, at this time need to configure hystrix, to avoid thread occupying memory for a long time, resulting in memory leakage, service hang up, etc.
2. Ribbon configuration.
-
When using Eureka registry and zuul.routes routing to service Id, it is configured by ribbon.ReadTimeout and ribbon.ConnectTimeout. (That is to say, zuul routes differently and the configuration in effect is different)
ribbon: ReadTimeout:10000 # Timeout of request processing after connection is established ConnectTimeout:5000 # Time-out for connection establishment MaxAutoRetries:1 # Number of retries for the current instance MaxAutoRetriesNextServer:1 # Number of retries for switching instances
- When zuul.routes route url s, they are configured through zuul.host.connect-timeout-millis and zuul.host.socket-timeout-millis. (That is to say, zuul routes differently and the configuration in effect is different)
zuul: host: connect-timeout-millis: 30000 socket-timeout-millis: 30000
- If you want to configure a specific service, configure it as follows:
<serviceId>: ribbon: ReadTimeout:10000 # Timeout of request processing after connection is established ConnectTimeout:5000 # Time-out for connection establishment MaxAutoRetries:1 # Number of retries for the current instance MaxAutoRetriesNextServer:1 # Number of retries for switching instances OkToRetryOnAllOperations: true # All operation requests are retried
Explain:
(1) Service ID is the service name.
(2) Specific instructions can be consulted. Official website
3. Hytrix configuration.
- If zuul has a fallback fuse, the fuse timeout should also be configured. The configuration properties are as follows:
hystrix: command: default: execution: isolation: thread: timeoutInMilliseconds: 90000 # Fuse service request timeout
Explain:
(1) default represents the default configuration.
- If you want to match a fuse timeout strategy for a particular serviceId, configure it as follows:
hystrix: command: <serviceId>: execution: isolation: thread: timeoutInMilliseconds: 60000
Explanation:
(1) Service ID is the service name.
4. Complete examples.
ribbon: ReadTimeout: 30000 ConnectTimeout: 30000 hystrix: command: default: execution: isolation: thread: timeoutInMilliseconds: 60000 # Fuse service request timeout
4. Problems encountered.
1,WARN o.s.c.n.z.f.r.s.AbstractRibbonCommand.getHystrixTimeout 115 - The Hystrix timeout of 60000ms for the command igg-aggregate-server is set lower than the combination of the Ribbon read and connect timeout, 120000ms.
Reason: If hystrixTimeout is smaller than ribbonTimeout, it may trigger a fuse during the retry of the Ribbon handover instance. Otherwise, when hystrix breaks, ribbon retries are meaningless.
So why is the ribbon Timeout timeout 120 seconds? You can view the source code AbstractRibbon Command.class.
protected static int getHystrixTimeout(IClientConfig config, String commandKey) { int ribbonTimeout = getRibbonTimeout(config, commandKey); DynamicPropertyFactory dynamicPropertyFactory = DynamicPropertyFactory.getInstance(); // Get the default hytrix timeout int defaultHystrixTimeout = dynamicPropertyFactory.getIntProperty("hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds", 0).get(); // The hytrix timeout for obtaining the specific service should be hystrix. command. foo. execution. isolation. thread. timeout InMilliseconds int commandHystrixTimeout = dynamicPropertyFactory.getIntProperty("hystrix.command." + commandKey + ".execution.isolation.thread.timeoutInMilliseconds", 0).get(); int hystrixTimeout; // The priority of hystrixTimeout is service-specific hytrix timeout > Default hytrix timeout > ribbon timeout if(commandHystrixTimeout > 0) { hystrixTimeout = commandHystrixTimeout; } else if(defaultHystrixTimeout > 0) { hystrixTimeout = defaultHystrixTimeout; } else { hystrixTimeout = ribbonTimeout; } // If the default or specific service hytrix timeout time is less than ribbon timeout time, it will be warned if(hystrixTimeout < ribbonTimeout) { LOGGER.warn("The Hystrix timeout of " + hystrixTimeout + "ms for the command " + commandKey + " is set lower than the combination of the Ribbon read and connect timeout, " + ribbonTimeout + "ms."); } return hystrixTimeout; }
protected static int getRibbonTimeout(IClientConfig config, String commandKey) { int ribbonTimeout; // The default is 2s if (config == null) { ribbonTimeout = 2000; } else { // Here we get four parameters, ReadTimeout, Connect Timeout, MaxAutoRetries, MaxAutoRetries Next Server, Priority: Specific Services > Default // 1. Time-out of request processing, default 1s int ribbonReadTimeout = getTimeout(config, commandKey, "ReadTimeout", Keys.ReadTimeout, 1000); // 2. Time-out for requesting connections, default 1s int ribbonConnectTimeout = getTimeout(config, commandKey, "ConnectTimeout", Keys.ConnectTimeout, 1000); // 3. The number of retries for the current instance. Default 0 int maxAutoRetries = getTimeout(config, commandKey, "MaxAutoRetries", Keys.MaxAutoRetries, 0); // 4. The number of retries for switching instances, default 1 int maxAutoRetriesNextServer = getTimeout(config, commandKey, "MaxAutoRetriesNextServer", Keys.MaxAutoRetriesNextServer, 1); // Calculation Method of ribbon Timeout ribbonTimeout = (ribbonReadTimeout + ribbonConnectTimeout) * (maxAutoRetries + 1) * (maxAutoRetriesNextServer + 1); } return ribbonTimeout; }
In other words, ribbon Timeout's calculation method is as follows:
ribbonTimeout = (ribbonReadTimeout + ribbonConnectTimeout) * (maxAutoRetries + 1) * (maxAutoRetriesNextServer + 1);
Namely:
ribbonTimeout = (30000 + 30000) * (0 + 1) * (1 + 1) = 120000
Solution: Modify timeout time.
ribbon: ReadTimeout: 30000 ConnectTimeout: 15000 hystrix: command: default: execution: isolation: thread: timeoutInMilliseconds: 90000 # Fuse service request timeout
Author: magic_kid_2010, if you think my blog is helpful to you, welcome[ Reward]