1) Zuul timeout tuning
Why optimize Zuul's timeout
Interpretation based on the figure above
Zuul's underlying Hystrix calls Ribbon and services to communicate. By default, it uses Hystrix thread pool isolation to open a thread in the gateway to access remote services. At this time, requests through network management services need to be made through Hystrix and Ribbon for data requests.
Thread pool isolation default timeout time is one second, ribbon is five seconds, if there is a request for data returned normally now is three seconds, if in accordance with the default configuration, data has not returned before, Hystrix thought that the request timeout closed the connection, then there will be a timeout exception.
Gateway timeout tuning is necessary, but it involves the timeout settings of Hystrix and Ribbon. When setting the timeout time, ribbon's timeout key must be less than Hystrix's timeout time. The reason is that if Ribbon's timeout time is less than Hystrix's, in practical application if Ribbon's timeout time is less than Hystrix's, Ribbon's timeout time is less than Hystrix's. Data is not returned in time, Hystrix timeouts are not passed, and the service will continue to access the data in a polling manner.
2) Simulated timeout
1. Create projects
pom file
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.7.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.sxt</groupId> <artifactId>10-zuul-timeout</artifactId> <version>0.0.1-SNAPSHOT</version> <name>10-zuul-timeout</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Greenwich.SR2</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!-- <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> </dependency> --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-zuul</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
Global Profile Configuration Hytrix and Ribbon Timeouts
spring.application.name=10-zuul-timeout server.port=9020 eureka.client.service-url.defaultZone=http://peer1:8081/eureka/,http://peer2:8082/eureka/,http://peer3:8083/eureka/ #Setting the level of the log logging.level.com.sxt=info zuul.routes.1-e-product-provider.path=/product/**
Test sleeping on the server when no timeout settings are configured
/** * @param * @description: All queries * @return: * @author: shinelon * @time: 2019/9/5:15:28 */ public List<Product> selectAllProduct() { try { Thread.sleep(3000); //Test Zuul timeout } catch (InterruptedException e) { e.printStackTrace(); } return this.productMapper.selectAllProduct(); }
Timeout anomaly
Add timeout configuration
spring.application.name=10-zuul-timeout server.port=9020 eureka.client.service-url.defaultZone=http://peer1:8081/eureka/,http://peer2:8082/eureka/,http://peer3:8083/eureka/ #Setting the level of the log logging.level.com.sxt=info zuul.routes.1-e-product-provider.path=/product/** #Setting Hystrix timeout hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=8000 #Setting Ribbon's timeout ribbon.ConnectTimeout=5000 # Time-out for request processing: default 5s ribbon.ReadTimeout=5000