Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SCB-2063]when configure multiple service centers, should support usi… #1914

Merged
merged 1 commit into from
Aug 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@

package org.apache.servicecomb.demo.multiServiceCenterClient;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.apache.servicecomb.demo.CategorizedTestCaseRunner;
import org.apache.servicecomb.demo.TestMgr;
import org.apache.servicecomb.springboot2.starter.EnableServiceComb;
Expand All @@ -39,10 +43,29 @@ public static void main(final String[] args) throws Exception {

public static void runTest() throws Exception {
CategorizedTestCaseRunner.runCategorizedTestCase("demo-multi-service-center-serverA");

testRegistryThreads();
TestMgr.summary();
if (!TestMgr.errors().isEmpty()) {
throw new IllegalStateException("tests failed");
}
}

private static void testRegistryThreads() throws Exception {
Set<Thread> threadSet = Thread.getAllStackTraces().keySet();
List<String> expectedThread = new ArrayList<>();
threadSet.forEach(thread -> {
if (thread.getName().contains("registry-")) {
expectedThread.add(thread.getName());
}
});
//registry-watch-vert.x-eventloop-thread-1
//registry-watch-vert.x-eventloop-thread-0
//registry-watch-serverB-vert.x-eventloop-thread-1
//registry-watch-serverB-vert.x-eventloop-thread-0
//registry-vert.x-eventloop-thread-1
//registry-vert.x-eventloop-thread-0
//registry-serverB-vert.x-eventloop-thread-1
//registry-serverB-vert.x-eventloop-thread-0
TestMgr.check(expectedThread.size(), 8);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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.demo.multiServiceCenterClient;

import org.apache.servicecomb.serviceregistry.client.http.RegistryHttpClientOptionsSPI;

public class ServerBRegistryHttpClientOptionsSPI extends RegistryHttpClientOptionsSPI {
public static final String CLIENT_NAME = "registry-serverB";

@Override
public String clientName() {
return CLIENT_NAME;
}

@Override
public String getConfigTag() {
return "sc.serverB.consumer";
}

@Override
public boolean isSsl() {
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* 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.demo.multiServiceCenterClient;

import org.apache.servicecomb.serviceregistry.config.ServiceRegistryConfig;

import com.netflix.config.DynamicPropertyFactory;

public class ServerBRegistryWatchHttpClientOptionsSPI extends ServerBRegistryHttpClientOptionsSPI {
public static final String CLIENT_NAME = "registry-watch-serverB";

private ServiceRegistryConfig serviceRegistryConfig = ServiceRegistryConfig.INSTANCE;

@Override
public String clientName() {
return CLIENT_NAME;
}

@Override
public int getOrder() {
// low priority than registry
return super.getOrder() + 1;
}

@Override
public boolean enabled() {
return serviceRegistryConfig.isWatch();
}

@Override
public boolean isWorker() {
return true;
}


@Override
public String getWorkerPoolName() {
return "pool-worker-service-center-watch-serverB";
}

@Override
public int getWorkerPoolSize() {
return DynamicPropertyFactory.getInstance()
.getIntProperty(ServiceRegistryConfig.WORKER_POOL_SIZE, 2).get();
}

@Override
public boolean isProxyEnable() {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ public class ServerBServiceCenterConfiguration {
public ServiceRegistryConfig serverBServiceCenterConfig() {
ServiceRegistryConfig config = ServiceRegistryConfig.buildFromConfiguration();
return ServiceRegistryConfigCustomizer.from(config)
.addressListFromConfiguration("servicecomb.service.registry-serverB.address").get();
.addressListFromConfiguration("servicecomb.service.registry-serverB.address")
// use a different http client instance
.setClientName("registry-serverB")
.setWatchClientName("registry-watch-serverB")
.get();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#
# 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.
#

org.apache.servicecomb.demo.multiServiceCenterClient.ServerBRegistryHttpClientOptionsSPI
org.apache.servicecomb.demo.multiServiceCenterClient.ServerBRegistryWatchHttpClientOptionsSPI
Original file line number Diff line number Diff line change
Expand Up @@ -253,15 +253,6 @@ public static void addExtraServiceRegistry(ServiceRegistry serviceRegistry) {
EXTRA_SERVICE_REGISTRIES.put(serviceRegistry.getName(), serviceRegistry);
}

/**
* @throws NullPointerException serviceRegistryConfig is null
* @throws IllegalArgumentException config value is illegal
*/
public static void validateRegistryConfig(ServiceRegistryConfig serviceRegistryConfig) {
Objects.requireNonNull(serviceRegistryConfig);
validateRegistryName(serviceRegistryConfig.getRegistryName());
}

/**
* To validate whether the name is legal value.
* @param name name of the {@link ServiceRegistry}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
import org.apache.servicecomb.registry.cache.InstanceCache;
import org.apache.servicecomb.registry.cache.InstanceCacheManager;
import org.apache.servicecomb.registry.cache.InstanceCacheManagerNew;
import org.apache.servicecomb.serviceregistry.config.ServiceRegistryConfig;
import org.apache.servicecomb.registry.consumer.AppManager;
import org.apache.servicecomb.registry.definition.DefinitionConst;
import org.apache.servicecomb.serviceregistry.config.ServiceRegistryConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -73,6 +73,7 @@ public IpPortManager(ServiceRegistryConfig serviceRegistryConfig) {
}
int initialIndex = new Random().nextInt(defaultIpPort.size());
currentAvailableIndex = new AtomicInteger(initialIndex);
LOGGER.info("Initial service center address is {}", getAvailableAddress());
maxRetryTimes = defaultIpPort.size();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,14 @@
package org.apache.servicecomb.serviceregistry.client.http;

class HttpClientPool extends AbstractClientPool {
private String clientName;

HttpClientPool(String clientName) {
this.clientName = clientName;
}

@Override
public String getName() {
return RegistryHttpClientOptionsSPI.CLIENT_NAME;
return this.clientName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ final class RestClientUtil {
this.authHeaderProviders = serviceRegistryConfig.getAuthHeaderProviders();
this.requestTimeout = serviceRegistryConfig.getRequestTimeout();
this.tenantName = serviceRegistryConfig.getTenantName();
this.httpClientPool = new HttpClientPool();
this.httpClientPool = new HttpClientPool(serviceRegistryConfig.getClientName());
}

public void httpDo(RequestContext requestContext, Handler<RestResponse> responseHandler) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
package org.apache.servicecomb.serviceregistry.client.http;

public class WebsocketClientPool extends AbstractClientPool {
private String clientName;

/**
* The default instance, for default sc cluster.
*/
public static final WebsocketClientPool INSTANCE = new WebsocketClientPool();
WebsocketClientPool(String clientName) {
this.clientName = clientName;
}

@Override
public String getName() {
return RegistryWatchHttpClientOptionsSPI.CLIENT_NAME;
return this.clientName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public final class WebsocketClientUtil {
private List<AuthHeaderProvider> authHeaderProviders;

WebsocketClientUtil(ServiceRegistryConfig serviceRegistryConfig) {
websocketClientPool = new WebsocketClientPool();
websocketClientPool = new WebsocketClientPool(serviceRegistryConfig.getWatchClientName());
authHeaderProviders = serviceRegistryConfig.getAuthHeaderProviders();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ public class ServiceRegistryConfig {
// TODO SCB-1691 getter of this field's behavior changed, should check
private boolean ssl = true;

private String clientName;

private String watchClientName;

private ArrayList<IpPort> ipPort;

private int connectionTimeout;
Expand Down Expand Up @@ -175,6 +179,24 @@ public ServiceRegistryConfig setSsl(boolean ssl) {
return this;
}

public String getClientName() {
return this.clientName;
}

public ServiceRegistryConfig setClientName(String clientName) {
this.clientName = clientName;
return this;
}

public String getWatchClientName() {
return this.watchClientName;
}

public ServiceRegistryConfig setWatchClientName(String watchClientName) {
this.watchClientName = watchClientName;
return this;
}

public ArrayList<IpPort> getIpPort() {
return ipPort;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import org.apache.servicecomb.foundation.common.net.NetUtils;
import org.apache.servicecomb.foundation.common.utils.SPIServiceUtils;
import org.apache.servicecomb.foundation.vertx.VertxConst;
import org.apache.servicecomb.serviceregistry.client.http.RegistryHttpClientOptionsSPI;
import org.apache.servicecomb.serviceregistry.client.http.RegistryWatchHttpClientOptionsSPI;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -50,6 +52,8 @@ public ServiceRegistryConfig build() {
.setInstances(getInstances())
.setIpPort(getIpPort())
.setSsl(isSsl())
.setClientName(RegistryHttpClientOptionsSPI.CLIENT_NAME)
.setWatchClientName(RegistryWatchHttpClientOptionsSPI.CLIENT_NAME)
.setConnectionTimeout(getConnectionTimeout())
.setIdleConnectionTimeout(getIdleConnectionTimeout())
.setRequestTimeout(getRequestTimeout())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ public static ServiceRegistryConfigCustomizer from(ServiceRegistryConfig origina
return new ServiceRegistryConfigCustomizer(original);
}

public ServiceRegistryConfigCustomizer setClientName(String clientName) {
this.original.setClientName(clientName);
return this;
}

public ServiceRegistryConfigCustomizer setWatchClientName(String watchClientName) {
this.original.setWatchClientName(watchClientName);
return this;
}

public ServiceRegistryConfigCustomizer addressListFromConfiguration(String configuration) {
String address = DynamicPropertyFactory.getInstance()
.getStringProperty(configuration, null)
Expand Down