使用zuul会发生大量的CLOSE_WAIT

时间:2018-06-29 09:08:48

标签: spring-cloud netflix-zuul

当我使用springcloud zuul时,会发生大量的TCP CLOSE_WAIT状态,有人知道它为什么会发生吗?

这是我的zuul配置

父母pom

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.9.RELEASE</version>
    <relativePath />
</parent>
  1. pom.xml

    <dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</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-zuul</artifactId>
    </dependency>
    <dependency>
        <groupId>com.squareup.okhttp3</groupId>
        <artifactId>okhttp</artifactId>
    </dependency>
           <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>Edgware.SR2</version>
        <type>pom</type>
        <scope>import</scope>
       </dependency>
    </dependencies>
    
  2. application.properties

    server.port=8081
    spring.application.name=zuul-server-test
    eureka.instance.preferIpAddress=true
    eureka.instance.instance-id=${spring.cloud.client.ipAddress}:${server.port}
    eureka.instance.leaseRenewalIntervalInSeconds=5
    eureka.instance.leaseExpirationDurationInSeconds=10
    eureka.client.serviceUrl.defaultZone=http://127.0.0.1:8080/eureka/
    
    zuul.add-host-header=true
    zuul.sensitive-headers=
    
    hystrix.command.default.execution.timeout.enable=true
    hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=60000
    zuul.host.connect-timeout-millis=60000
    zuul.host.socket-timeout-milllis=60000
    
    ribbon.eureka.enabled=true
    ribbon.ReadTimeout=60000
    ribbon.ConnectTimeout=30000
    ribbon.MaxAutoRetries=0
    ribbon.MaxAutoRetriesNextServer=1
    ribbon.OkToRetryOnAllOperations=false
    ribbon.httpclient.enabled=false
    ribbon.okhttp.enabled=true
    
    zuul.routes.hello.path=/hello/**
    zuul.routes.hello.sensitiveHeaders=
    zuul.routes.hello.service-id=hello-server
    

java

    @SpringBootApplication
    @EnableDiscoveryClient
    @EnableZuulProxy
    public class DevcloudZuulApplication {
        public static void main(String[] args) {
            SpringApplication.run(DevcloudZuulApplication.class, args);
        }
    }

当我访问http://localhost:8081/hello/index.html时,创建了tcp链接,端口8082是hello服务器

    tcp        0      0 ::ffff:192.168.120.20:8082 ::ffff:192.168.120.20:21505 ESTABLISHED 47968/java          
    tcp        0      0 ::ffff:192.168.120.20:21505 ::ffff:192.168.120.20:8082 ESTABLISHED 132689/java         
    tcp        0      0 ::ffff:192.168.120.20:8082 ::ffff:192.168.120.20:21510 ESTABLISHED 47968/java  
    tcp        0      0 ::ffff:192.168.120.20:21510 ::ffff:192.168.120.20:8082 ESTABLISHED 132689/java        
    tcp        0      0 ::ffff:192.168.120.20:8082 ::ffff:192.168.120.20:21504 ESTABLISHED 47968/java          
    tcp        0      0 ::ffff:192.168.120.20:21504 ::ffff:192.168.120.20:8082 ESTABLISHED 132689/java

然后它改变

    tcp        0      0 ::ffff:192.168.120.20:8082 ::ffff:192.168.120.20:21505 FIN_WAIT2   -                   
    tcp        1      0 ::ffff:192.168.120.20:21505 ::ffff:192.168.120.20:8082 CLOSE_WAIT  132689/java         
    tcp        0      0 ::ffff:192.168.120.20:8082 ::ffff:192.168.120.20:21510 FIN_WAIT2    -        
    tcp        0      0 ::ffff:192.168.120.20:21510 ::ffff:192.168.120.20:8082 CLOSE_WAIT 132689/java 
    tcp        0      0 ::ffff:192.168.120.20:8082 ::ffff:192.168.120.20:21504 FIN_WAIT2   -                   
    tcp        1      0 ::ffff:192.168.120.20:21504 ::ffff:192.168.120.20:8082 CLOSE_WAIT  132689/java  

最后

    tcp        1      0 ::ffff:192.168.120.20:21505 ::ffff:192.168.120.20:8082 CLOSE_WAIT  132689/java         
    tcp        1      0 ::ffff:192.168.120.20:21504 ::ffff:192.168.120.20:8082 CLOSE_WAIT  132689/java         
    tcp        1      0 ::ffff:192.168.120.20:21510 ::ffff:192.168.120.20:8082 CLOSE_WAIT  132689/java 

,此状态将持续几个小时。

1 个答案:

答案 0 :(得分:0)

CLOSE_WAIT在这里描述:TCP connection status

  

此端点已收到来自远程端点的关闭请求,并且此TCP现在正在等待来自本地应用程序的连接终止请求。

Ribbon使用连接池将请求发送到上游。每次,它都会释放连接而不是关闭连接,以避免建立新连接的开销。

通过配置ribbon.PoolKeepAliveTime(默认值为15 * 60秒),池中的连接将保持活动状态。

public class DefaultClientConfigImpl implements IClientConfig {
    public static final long DEFAULT_POOL_KEEP_ALIVE_TIME = 15 * 60L;
    public static final TimeUnit DEFAULT_POOL_KEEP_ALIVE_TIME_UNITS = TimeUnit.SECONDS;
}


public class OkHttpRibbonConfiguration {
@Value("${ribbon.client.name}")
private String name = "client";

@Configuration
protected static class OkHttpClientConfiguration {
    private OkHttpClient httpClient;

    @Bean
    @ConditionalOnMissingBean(ConnectionPool.class)
    public ConnectionPool httpClientConnectionPool(IClientConfig config, OkHttpClientConnectionPoolFactory connectionPoolFactory) {
        Integer maxTotalConnections = config.getPropertyAsInteger(
                CommonClientConfigKey.MaxTotalConnections,
                DefaultClientConfigImpl.DEFAULT_MAX_TOTAL_CONNECTIONS);
        Object timeToLiveObj = config
                .getProperty(CommonClientConfigKey.PoolKeepAliveTime);
        Long timeToLive = DefaultClientConfigImpl.DEFAULT_POOL_KEEP_ALIVE_TIME;
        Object ttlUnitObj = config
                .getProperty(CommonClientConfigKey.PoolKeepAliveTimeUnits);
        TimeUnit ttlUnit = DefaultClientConfigImpl.DEFAULT_POOL_KEEP_ALIVE_TIME_UNITS;
        if (timeToLiveObj instanceof Long) {
            timeToLive = (Long) timeToLiveObj;
        }
        if (ttlUnitObj instanceof TimeUnit) {
            ttlUnit = (TimeUnit) ttlUnitObj;
        }
        return connectionPoolFactory.create(maxTotalConnections, timeToLive, ttlUnit);
    }

}