diff --git a/core/src/main/java/io/servicecomb/core/transport/AbstractTransport.java b/core/src/main/java/io/servicecomb/core/transport/AbstractTransport.java index 1e9785d0a7f..d58079e8f12 100644 --- a/core/src/main/java/io/servicecomb/core/transport/AbstractTransport.java +++ b/core/src/main/java/io/servicecomb/core/transport/AbstractTransport.java @@ -29,6 +29,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.netflix.config.DynamicLongProperty; import com.netflix.config.DynamicPropertyFactory; import io.servicecomb.core.Const; @@ -50,25 +51,13 @@ public abstract class AbstractTransport implements Transport { public static final String ENDPOINT_KEY = "cse.endpoint"; private static final long DEFAULT_TIMEOUT_MILLIS = 30000; + + private static final String REQUEST_TIMEOUT_KEY = "cse.request.timeout"; - private static Long msRequestTimeout = null; - - public static long getRequestTimeout() { - if (msRequestTimeout != null) { - return msRequestTimeout; - } - - long msTimeout = DynamicPropertyFactory.getInstance() - .getLongProperty("cse.request.timeout", DEFAULT_TIMEOUT_MILLIS) - .get(); - if (msTimeout <= 0) { - msTimeout = DEFAULT_TIMEOUT_MILLIS; - } - - msRequestTimeout = msTimeout; - return msRequestTimeout; + public static DynamicLongProperty getRequestTimeoutProperty(){ + return DynamicPropertyFactory.getInstance().getLongProperty(REQUEST_TIMEOUT_KEY, DEFAULT_TIMEOUT_MILLIS); } - + // 所有transport使用同一个vertx实例,避免创建太多的线程 protected Vertx transportVertx = VertxUtils.getOrCreateVertxByName("transport", null); diff --git a/core/src/test/java/io/servicecomb/core/transport/TestAbstractTransport.java b/core/src/test/java/io/servicecomb/core/transport/TestAbstractTransport.java index b39545b5b2f..c1096c6fe57 100644 --- a/core/src/test/java/io/servicecomb/core/transport/TestAbstractTransport.java +++ b/core/src/test/java/io/servicecomb/core/transport/TestAbstractTransport.java @@ -124,7 +124,7 @@ public void testMyAbstractTransport() throws Exception { transport.setListenAddressWithoutSchema(null); Assert.assertNull(transport.getEndpoint().getEndpoint()); Assert.assertNull(transport.parseAddress(null)); - Assert.assertEquals(30000, AbstractTransport.getRequestTimeout()); + Assert.assertEquals(30000, AbstractTransport.getRequestTimeoutProperty().get()); } @Test(expected = NumberFormatException.class) diff --git a/transports/transport-highway/src/main/java/io/servicecomb/transport/highway/HighwayClient.java b/transports/transport-highway/src/main/java/io/servicecomb/transport/highway/HighwayClient.java index cd3534e831d..85b61f6c1d1 100644 --- a/transports/transport-highway/src/main/java/io/servicecomb/transport/highway/HighwayClient.java +++ b/transports/transport-highway/src/main/java/io/servicecomb/transport/highway/HighwayClient.java @@ -16,9 +16,13 @@ package io.servicecomb.transport.highway; +import org.omg.DynamicAny.DynAnyFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.netflix.config.DynamicLongProperty; +import com.netflix.config.DynamicPropertyFactory; + import io.servicecomb.codec.protobuf.definition.OperationProtobuf; import io.servicecomb.codec.protobuf.definition.ProtobufManager; import io.servicecomb.core.Invocation; @@ -62,7 +66,14 @@ public void init(Vertx vertx) throws Exception { private TcpClientConfig createTcpClientConfig() { TcpClientConfig tcpClientConfig = new TcpClientConfig(); - tcpClientConfig.setRequestTimeoutMillis(AbstractTransport.getRequestTimeout()); + DynamicLongProperty prop = AbstractTransport.getRequestTimeoutProperty(); + //support cse.request.timeout dynamic refresh + prop.addCallback(new Runnable(){ + public void run(){ + tcpClientConfig.setRequestTimeoutMillis(prop.get()); + } + }); + tcpClientConfig.setRequestTimeoutMillis(AbstractTransport.getRequestTimeoutProperty().get()); if (this.sslEnabled) { SSLOptionFactory factory = diff --git a/transports/transport-highway/src/test/java/io/servicecomb/transport/highway/TestHighwayClient.java b/transports/transport-highway/src/test/java/io/servicecomb/transport/highway/TestHighwayClient.java index 5e364605a15..fb5aa4014b3 100644 --- a/transports/transport-highway/src/test/java/io/servicecomb/transport/highway/TestHighwayClient.java +++ b/transports/transport-highway/src/test/java/io/servicecomb/transport/highway/TestHighwayClient.java @@ -18,17 +18,23 @@ import java.util.concurrent.Executor; +import org.apache.commons.configuration.AbstractConfiguration; import org.junit.Assert; +import org.junit.BeforeClass; import org.junit.Test; import org.mockito.Mockito; +import com.netflix.config.DynamicPropertyFactory; + import io.netty.buffer.ByteBuf; import io.protostuff.runtime.ProtobufCompatibleUtils; import io.servicecomb.codec.protobuf.definition.OperationProtobuf; import io.servicecomb.codec.protobuf.definition.ProtobufManager; +import io.servicecomb.config.ConfigUtil; import io.servicecomb.core.Endpoint; import io.servicecomb.core.Invocation; import io.servicecomb.core.definition.OperationMeta; +import io.servicecomb.core.transport.AbstractTransport; import io.servicecomb.foundation.vertx.VertxUtils; import io.servicecomb.foundation.vertx.client.ClientPoolManager; import io.servicecomb.foundation.vertx.client.tcp.TcpClientConfig; @@ -61,7 +67,22 @@ public class TestHighwayClient { Endpoint endpoint = Mockito.mock(Endpoint.class); Executor excutor = Mockito.mock(Executor.class); + + @BeforeClass + public static void beforeCls() { + ConfigUtil.installDynamicConfig(); + AbstractConfiguration configuration = + (AbstractConfiguration) DynamicPropertyFactory.getBackingConfigurationSource(); + configuration.addProperty("cse.request.timeout", 2000); + } + + @Test + public void testRequestTimeout() { + Assert.assertEquals(AbstractTransport.getRequestTimeoutProperty().get(), 2000); + + } + @Test public void testHighwayClientSSL(@Mocked Vertx vertx) throws Exception { new MockUp() { diff --git a/transports/transport-rest/transport-rest-client/src/main/java/io/servicecomb/transport/rest/client/http/VertxHttpMethod.java b/transports/transport-rest/transport-rest-client/src/main/java/io/servicecomb/transport/rest/client/http/VertxHttpMethod.java index 9a086d0911b..0b56fbccc4d 100644 --- a/transports/transport-rest/transport-rest-client/src/main/java/io/servicecomb/transport/rest/client/http/VertxHttpMethod.java +++ b/transports/transport-rest/transport-rest-client/src/main/java/io/servicecomb/transport/rest/client/http/VertxHttpMethod.java @@ -100,7 +100,7 @@ public void doMethod(HttpClientWithContext httpClientWithContext, Invocation inv // 从业务线程转移到网络线程中去发送 httpClientWithContext.runOnContext(httpClient -> { this.setCseContext(invocation, clientRequest); - clientRequest.setTimeout(AbstractTransport.getRequestTimeout()); + clientRequest.setTimeout(AbstractTransport.getRequestTimeoutProperty().get()); try { restClientRequest.end(); } catch (Exception e) {