Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,16 @@ public abstract class AbstractSchemaFactory<CONTEXT extends SchemaContext> {
@Inject
protected MicroserviceMetaManager microserviceMetaManager;

@Inject
protected SchemaLoader schemaLoader;

@Inject
protected CompositeSwaggerGeneratorContext compositeSwaggerGeneratorContext;

@Inject
public void setSchemaLoader(SchemaLoader schemaLoader) {
this.schemaLoader = schemaLoader;
}

public void setMicroserviceMetaManager(MicroserviceMetaManager microserviceMetaManager) {
this.microserviceMetaManager = microserviceMetaManager;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class ExceptionUtils {
ERROR_DESC_MGR.register(CSE_PRODUCER_OPERATION_NOT_EXIST,
"Producer operation not exist, schemaId=%s, operationName=%s");
ERROR_DESC_MGR.register(CSE_LB_NO_AVAILABLE_ADDRESS,
"No available address found. microserviceName=%s, version=%s, transportName=%s");
"No available address found. microserviceName=%s, version=%s, discoveryGroupName=%s");
}

protected ExceptionUtils() {
Expand Down Expand Up @@ -84,10 +84,10 @@ public static CseException handlerRefNotExist(String id) {
}

public static CseException lbAddressNotFound(String microserviceName, String microserviceVersionRule,
String transportName) {
String discoveryGroupName) {
return createCseException(CSE_LB_NO_AVAILABLE_ADDRESS,
microserviceName,
microserviceVersionRule,
transportName);
discoveryGroupName);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 2017 Huawei Technologies Co., Ltd
*
* Licensed 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 io.servicecomb.core.filter;

import io.servicecomb.core.CseContext;
import io.servicecomb.core.Endpoint;
import io.servicecomb.core.Invocation;
import io.servicecomb.core.Transport;
import io.servicecomb.serviceregistry.api.registry.MicroserviceInstance;
import io.servicecomb.serviceregistry.discovery.AbstractEndpointDiscoveryFilter;
import io.servicecomb.serviceregistry.discovery.DiscoveryContext;
import io.servicecomb.serviceregistry.discovery.DiscoveryTreeNode;

public class EndpointDiscoveryFilter extends AbstractEndpointDiscoveryFilter {
@Override
public int getOrder() {
return (int) Short.MAX_VALUE;
}

@Override
protected String findTransportName(DiscoveryContext context, DiscoveryTreeNode parent) {
Invocation invocation = context.getInputParameters();
return invocation.getConfigTransportName();
}

@Override
protected Object createEndpoint(String transportName, String endpoint, MicroserviceInstance instance) {
Transport transport = CseContext.getInstance().getTransportManager().findTransport(transportName);
if (transport == null) {
return null;
}

return new Endpoint(transport, endpoint, instance);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,13 @@ private void loadDefaultChainDef() {
}

private List<Class<Handler>> convertToChainClass(String chainDef) {
List<Class<Handler>> result = new ArrayList<>();
if (StringUtils.isEmpty(chainDef)) {
return result;
}

String[] handlerIds = chainDef.split(",");
Map<String, Class<Handler>> handlerMaps = config.getHandlerClassMap();
List<Class<Handler>> result = new ArrayList<>();
for (String handlerId : handlerIds) {
if (handlerId != null) {
handlerId = handlerId.trim();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,47 +21,61 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.servicecomb.core.Endpoint;
import io.servicecomb.core.Handler;
import io.servicecomb.core.Invocation;
import io.servicecomb.core.endpoint.EndpointsCache;
import io.servicecomb.core.exception.ExceptionUtils;
import io.servicecomb.core.filter.EndpointDiscoveryFilter;
import io.servicecomb.foundation.common.cache.VersionedCache;
import io.servicecomb.serviceregistry.discovery.DiscoveryContext;
import io.servicecomb.serviceregistry.discovery.DiscoveryFilter;
import io.servicecomb.serviceregistry.discovery.DiscoveryTree;
import io.servicecomb.swagger.invocation.AsyncResponse;

/**
* 内置轮询lb,方便demo之类的场景,不必去依赖lb包
*/
public class SimpleLoadBalanceHandler implements Handler {
private AtomicInteger index = new AtomicInteger();
private static final Logger LOGGER = LoggerFactory.getLogger(SimpleLoadBalanceHandler.class);

// key为transportName
private volatile Map<String, EndpointsCache> endpointsCacheMap = new ConcurrentHashMap<>();
private DiscoveryTree discoveryTree = new DiscoveryTree();

@Override
public void handle(Invocation invocation, AsyncResponse asyncResp) throws Exception {
// 调用者未指定transport时,这里得到的是"",也直接使用,不必特殊处理
String transportName = invocation.getConfigTransportName();
// key为grouping filter qualified name
private volatile Map<String, AtomicInteger> indexMap = new ConcurrentHashMap<>();

EndpointsCache endpointsCache = endpointsCacheMap.get(transportName);
if (endpointsCache == null) {
synchronized (this) {
endpointsCache = endpointsCacheMap.get(invocation.getConfigTransportName());
if (endpointsCache == null) {
endpointsCache = new EndpointsCache(invocation.getAppId(), invocation.getMicroserviceName(),
invocation.getMicroserviceVersionRule(), transportName);
endpointsCacheMap.put(transportName, endpointsCache);
}
}
}
List<Endpoint> endpoints = endpointsCache.getLatestEndpoints();
public SimpleLoadBalanceHandler() {
discoveryTree.loadFromSPI(DiscoveryFilter.class);
discoveryTree.addFilter(new EndpointDiscoveryFilter());
discoveryTree.sort();
}

if (endpoints == null || endpoints.isEmpty()) {
@Override
public void handle(Invocation invocation, AsyncResponse asyncResp) throws Exception {
DiscoveryContext context = new DiscoveryContext();
context.setInputParameters(invocation);
VersionedCache endpointsVersionedCache = discoveryTree.discovery(context,
invocation.getAppId(),
invocation.getMicroserviceName(),
invocation.getMicroserviceVersionRule());
if (endpointsVersionedCache.isEmpty()) {
asyncResp.consumerFail(ExceptionUtils.lbAddressNotFound(invocation.getMicroserviceName(),
invocation.getMicroserviceVersionRule(),
transportName));
endpointsVersionedCache.name()));
return;
}

List<Endpoint> endpoints = endpointsVersionedCache.data();
AtomicInteger index = indexMap.computeIfAbsent(endpointsVersionedCache.name(), name -> {
LOGGER.info("Create loadBalancer for {}.", name);
return new AtomicInteger();
});
LOGGER.debug("invocation {} use discoveryGroup {}.",
invocation.getMicroserviceQualifiedName(),
endpointsVersionedCache.name());

int idx = Math.abs(index.getAndIncrement());
idx = idx % endpoints.size();
Endpoint endpoint = endpoints.get(idx);
Expand Down
1 change: 0 additions & 1 deletion core/src/test/java/io/servicecomb/core/TestConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ public void testConfigurationSpringInitializer() throws Exception {
try {
oConf.test();
} catch (Exception e) {
System.out.println(e.getMessage());
Assert.assertEquals(e.getMessage().contains("can not find config for testkey:testvalue"), true);
failed = true;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* Copyright 2017 Huawei Technologies Co., Ltd
*
* Licensed 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 io.servicecomb.core.filter;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import io.servicecomb.core.Const;
import io.servicecomb.core.CseContext;
import io.servicecomb.core.Endpoint;
import io.servicecomb.core.Invocation;
import io.servicecomb.core.Transport;
import io.servicecomb.core.transport.TransportManager;
import io.servicecomb.serviceregistry.api.registry.MicroserviceInstance;
import io.servicecomb.serviceregistry.discovery.DiscoveryContext;
import mockit.Expectations;
import mockit.Mocked;

public class TestEndpointDiscoveryFilter {
EndpointDiscoveryFilter filter = new EndpointDiscoveryFilter();

DiscoveryContext context = new DiscoveryContext();

@Mocked
Invocation invocation;

@Mocked
TransportManager transportManager;

@Before
public void setup() {
CseContext.getInstance().setTransportManager(transportManager);
context.setInputParameters(invocation);
}

@After
public void teardown() {
CseContext.getInstance().setTransportManager(null);
}

@Test
public void getOrder() {
Assert.assertEquals(Short.MAX_VALUE, filter.getOrder());
}

@Test
public void getTransportName() {
new Expectations() {
{
invocation.getConfigTransportName();
result = Const.RESTFUL;
}
};

Assert.assertEquals(Const.RESTFUL, filter.findTransportName(context, null));
}

@Test
public void createEndpointNullTransport() {
new Expectations() {
{
transportManager.findTransport(Const.RESTFUL);
result = null;
}
};

Assert.assertNull(filter.createEndpoint(Const.RESTFUL, "", null));
}

@Test
public void createEndpointNormal(@Mocked Transport transport, @Mocked MicroserviceInstance instance) {
String endpoint = "rest://ip:port";
Object address = new Object();

new Expectations() {
{
transportManager.findTransport(Const.RESTFUL);
result = transport;
transport.parseAddress(endpoint);
result = address;
}
};

Endpoint ep = (Endpoint) filter.createEndpoint(Const.RESTFUL, endpoint, instance);
Assert.assertSame(transport, ep.getTransport());
Assert.assertSame(address, ep.getAddress());
Assert.assertSame(instance, ep.getMicroserviceInstance());
Assert.assertEquals(endpoint, ep.getEndpoint());
}
}
Loading