WillemJiang closed pull request #538: SCB-315 add common AddressResover configuration to foundation-vertx, and use it in service-registry and config-cc to support dynamic config by user URL: https://github.com/apache/incubator-servicecomb-java-chassis/pull/538
This is a PR merged from a forked repository. As GitHub hides the original diff on merge, it is displayed below for the sake of provenance: As this is a foreign pull request (from a fork), the diff is supplied below (as it won't show otherwise due to GitHub magic): diff --git a/dynamic-config/config-cc/src/main/java/org/apache/servicecomb/config/client/ConfigCenterClient.java b/dynamic-config/config-cc/src/main/java/org/apache/servicecomb/config/client/ConfigCenterClient.java index 89ff0053c..602f4a921 100644 --- a/dynamic-config/config-cc/src/main/java/org/apache/servicecomb/config/client/ConfigCenterClient.java +++ b/dynamic-config/config-cc/src/main/java/org/apache/servicecomb/config/client/ConfigCenterClient.java @@ -45,6 +45,7 @@ import org.apache.servicecomb.foundation.ssl.SSLCustom; import org.apache.servicecomb.foundation.ssl.SSLOption; import org.apache.servicecomb.foundation.ssl.SSLOptionFactory; +import org.apache.servicecomb.foundation.vertx.AddressResolverConfig; import org.apache.servicecomb.foundation.vertx.VertxTLSBuilder; import org.apache.servicecomb.foundation.vertx.VertxUtils; import org.apache.servicecomb.foundation.vertx.client.ClientPoolManager; @@ -59,6 +60,7 @@ import io.netty.handler.codec.http.HttpResponseStatus; import io.vertx.core.DeploymentOptions; import io.vertx.core.Vertx; +import io.vertx.core.VertxOptions; import io.vertx.core.http.CaseInsensitiveHeaders; import io.vertx.core.http.HttpClientOptions; import io.vertx.core.http.HttpClientRequest; @@ -152,7 +154,9 @@ private void refreshMembers(MemberDiscovery memberDiscovery) { } private void deployConfigClient() throws InterruptedException { - Vertx vertx = VertxUtils.getOrCreateVertxByName("config-center", null); + VertxOptions vertxOptions = new VertxOptions(); + vertxOptions.setAddressResolverOptions(AddressResolverConfig.getAddressResover(SSL_KEY)); + Vertx vertx = VertxUtils.getOrCreateVertxByName("config-center", vertxOptions); HttpClientOptions httpClientOptions = createHttpClientOptions(); clientMgr = new ClientPoolManager<>(vertx, new HttpClientPoolFactory(httpClientOptions)); diff --git a/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/AddressResolverConfig.java b/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/AddressResolverConfig.java new file mode 100644 index 000000000..698178dba --- /dev/null +++ b/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/AddressResolverConfig.java @@ -0,0 +1,127 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.servicecomb.foundation.vertx; + +import java.util.Arrays; +import java.util.List; + +import com.netflix.config.DynamicPropertyFactory; + +import io.vertx.core.dns.AddressResolverOptions; + +public class AddressResolverConfig { + + /** + * get the target endpoints with custom address resolve config + * @param tag config tag, such as sc.consumer or cc.consumer + * @return AddressResolverOptions + */ + public static AddressResolverOptions getAddressResover(String tag) { + AddressResolverOptions addressResolverOptions = new AddressResolverOptions(); + addressResolverOptions + .setServers(getStringListProperty(AddressResolverOptions.DEFAULT_SEACH_DOMAINS, + "addressResolver." + tag + ".servers", + "addressResolver.servers")); + addressResolverOptions + .setOptResourceEnabled(getBooleanProperty(AddressResolverOptions.DEFAULT_OPT_RESOURCE_ENABLED, + "addressResolver." + tag + ".optResourceEnabled", + "addressResolver.optResourceEnabled")); + addressResolverOptions + .setCacheMinTimeToLive(getIntProperty(AddressResolverOptions.DEFAULT_CACHE_MIN_TIME_TO_LIVE, + "addressResolver." + tag + ".cacheMinTimeToLive", + "addressResolver.cacheMinTimeToLive")); + addressResolverOptions + .setCacheMaxTimeToLive(getIntProperty(AddressResolverOptions.DEFAULT_CACHE_MAX_TIME_TO_LIVE, + "addressResolver." + tag + ".cacheMaxTimeToLive", + "addressResolver.cacheMaxTimeToLive")); + addressResolverOptions + .setCacheNegativeTimeToLive(getIntProperty(AddressResolverOptions.DEFAULT_CACHE_NEGATIVE_TIME_TO_LIVE, + "addressResolver." + tag + ".cacheNegativeTimeToLive", + "addressResolver.cacheNegativeTimeToLive")); + addressResolverOptions + .setQueryTimeout(getIntProperty(AddressResolverOptions.DEFAULT_QUERY_TIMEOUT, + "addressResolver." + tag + ".queryTimeout", + "addressResolver.queryTimeout")); + addressResolverOptions + .setMaxQueries(getIntProperty(AddressResolverOptions.DEFAULT_MAX_QUERIES, + "addressResolver." + tag + ".maxQueries", + "addressResolver.maxQueries")); + addressResolverOptions + .setRdFlag(getBooleanProperty(AddressResolverOptions.DEFAULT_RD_FLAG, + "addressResolver." + tag + ".rdFlag", + "addressResolver.rdFlag")); + addressResolverOptions + .setSearchDomains(getStringListProperty(AddressResolverOptions.DEFAULT_SEACH_DOMAINS, + "addressResolver." + tag + ".searchDomains", + "addressResolver.searchDomains")); + addressResolverOptions + .setNdots(getIntProperty(AddressResolverOptions.DEFAULT_CACHE_MIN_TIME_TO_LIVE, + "addressResolver." + tag + ".ndots", + "addressResolver.ndots")); + addressResolverOptions + .setRotateServers(getBooleanProperty(AddressResolverOptions.DEFAULT_ROTATE_SERVERS, + "addressResolver." + tag + ".rotateServers", + "addressResolver.rotateServers")); + return addressResolverOptions; + } + + private static List<String> getStringListProperty(List<String> defaultValue, String... keys) { + String property = null; + for (String key : keys) { + property = DynamicPropertyFactory.getInstance().getStringProperty(key, null).get(); + if (property != null) { + break; + } + } + if (property != null) { + return Arrays.asList(property.split(",")); + } else { + return defaultValue; + } + } + + private static int getIntProperty(int defaultValue, String... keys) { + int property = -1; + for (String key : keys) { + property = DynamicPropertyFactory.getInstance().getIntProperty(key, -1).get(); + if (property > 0) { + break; + } + } + if (property > 0) { + return property; + } else { + return defaultValue; + } + } + + private static boolean getBooleanProperty(boolean defaultValue, String... keys) { + String property = null; + for (String key : keys) { + property = DynamicPropertyFactory.getInstance().getStringProperty(key, null).get(); + if (property != null) { + break; + } + } + if (property != null) { + return Boolean.parseBoolean(property); + } else { + return defaultValue; + } + } +} diff --git a/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/TestAddressResolverConfig.java b/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/TestAddressResolverConfig.java new file mode 100644 index 000000000..0163f6063 --- /dev/null +++ b/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/TestAddressResolverConfig.java @@ -0,0 +1,85 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.servicecomb.foundation.vertx; + +import static org.hamcrest.CoreMatchers.*; + +import java.util.Arrays; + +import org.apache.servicecomb.foundation.test.scaffolding.config.ArchaiusUtils; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; + +import io.vertx.core.dns.AddressResolverOptions; + +public class TestAddressResolverConfig { + + @BeforeClass + public static void classSetup() { + ArchaiusUtils.resetConfig(); + } + + @AfterClass + public static void classTeardown() { + ArchaiusUtils.resetConfig(); + } + + + @Test + public void testGetResover() { + ArchaiusUtils.resetConfig(); + ArchaiusUtils.setProperty("addressResolver.servers", "8.8.8.8,8.8.4.4"); + ArchaiusUtils.setProperty("addressResolver.optResourceEnabled", true); + ArchaiusUtils.setProperty("addressResolver.cacheMinTimeToLive", 0); + ArchaiusUtils.setProperty("addressResolver.cacheMaxTimeToLive", 10000); + ArchaiusUtils.setProperty("addressResolver.cacheNegativeTimeToLive", 0); + ArchaiusUtils.setProperty("addressResolver.queryTimeout", 1000); + ArchaiusUtils.setProperty("addressResolver.maxQueries", 3); + ArchaiusUtils.setProperty("addressResolver.test.maxQueries", 3); + ArchaiusUtils.setProperty("addressResolver.rdFlag", true); + ArchaiusUtils.setProperty("addressResolver.searchDomains", + "default.svc.local.cluster,svc.local.cluster,local.cluster"); + ArchaiusUtils.setProperty("addressResolver.test.searchDomains", + "test.svc.local.cluster,svc.local.cluster,local.cluster"); + ArchaiusUtils.setProperty("addressResolver.ndots", 3); + ArchaiusUtils.setProperty("addressResolver.rotateServers", true); + AddressResolverOptions aroc = AddressResolverConfig.getAddressResover("test"); + Assert.assertThat(aroc.getServers(), is(Arrays.asList("8.8.8.8", "8.8.4.4"))); + Assert.assertThat(aroc.getSearchDomains(), + is(Arrays.asList("test.svc.local.cluster", "svc.local.cluster", "local.cluster"))); + AddressResolverOptions aroc1 = AddressResolverConfig.getAddressResover("test1"); + Assert.assertThat(aroc1.getSearchDomains(), + is(Arrays.asList("default.svc.local.cluster", "svc.local.cluster", "local.cluster"))); + } + + @Test + public void testGetResoverDefault() { + ArchaiusUtils.resetConfig(); + ArchaiusUtils.setProperty("addressResolver.servers", "8.8.8.8,8.8.4.4"); + ArchaiusUtils.setProperty("addressResolver.maxQueries", 3); + ArchaiusUtils.setProperty("addressResolver.rdFlag", false); + AddressResolverOptions aroc = AddressResolverConfig.getAddressResover("test"); + Assert.assertThat(aroc.getServers(), is(Arrays.asList("8.8.8.8", "8.8.4.4"))); + Assert.assertEquals(3, aroc.getMaxQueries()); + Assert.assertEquals(Integer.MAX_VALUE, aroc.getCacheMaxTimeToLive()); + Assert.assertTrue(aroc.isOptResourceEnabled()); + Assert.assertNull(aroc.getSearchDomains()); + } +} diff --git a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/client/http/AbstractClientPool.java b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/client/http/AbstractClientPool.java index cb9b876c4..be8759a74 100644 --- a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/client/http/AbstractClientPool.java +++ b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/client/http/AbstractClientPool.java @@ -17,6 +17,7 @@ package org.apache.servicecomb.serviceregistry.client.http; +import org.apache.servicecomb.foundation.vertx.AddressResolverConfig; import org.apache.servicecomb.foundation.vertx.VertxUtils; import org.apache.servicecomb.foundation.vertx.client.ClientPoolManager; import org.apache.servicecomb.foundation.vertx.client.ClientVerticle; @@ -28,6 +29,7 @@ import io.vertx.core.DeploymentOptions; import io.vertx.core.Vertx; +import io.vertx.core.VertxOptions; import io.vertx.core.http.HttpClientOptions; /** @@ -50,7 +52,9 @@ public HttpClientWithContext getClient() { public void create() { // ?????????????????????????????????vertx?? - Vertx vertx = VertxUtils.getOrCreateVertxByName("registry", null); + VertxOptions vertxOptions = new VertxOptions(); + vertxOptions.setAddressResolverOptions(AddressResolverConfig.getAddressResover(SSL_KEY)); + Vertx vertx = VertxUtils.getOrCreateVertxByName("registry", vertxOptions); HttpClientOptions httpClientOptions = createHttpClientOptions(); clientMgr = new ClientPoolManager<>(vertx, new HttpClientPoolFactory(httpClientOptions)); ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services