use of io.servicecomb.core.endpoint.EndpointsCache in project java-chassis by ServiceComb.
the class TestTransport method testAbstractTransport.
@Test
public void testAbstractTransport(@Mocked Microservice microservice) throws Exception {
EndpointsCache oEndpointsCache = new EndpointsCache("app", "testname", "test", "test");
try {
List<Endpoint> endpoionts = oEndpointsCache.getLatestEndpoints();
Assert.assertEquals(endpoionts.size(), 0);
} catch (Exception e) {
Assert.assertEquals(null, e.getMessage());
}
}
use of io.servicecomb.core.endpoint.EndpointsCache in project java-chassis by ServiceComb.
the class SimpleLoadBalanceHandler method handle.
@Override
public void handle(Invocation invocation, AsyncResponse asyncResp) throws Exception {
// 调用者未指定transport时,这里得到的是"",也直接使用,不必特殊处理
String transportName = invocation.getConfigTransportName();
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();
if (endpoints == null || endpoints.isEmpty()) {
asyncResp.consumerFail(ExceptionUtils.lbAddressNotFound(invocation.getMicroserviceName(), invocation.getMicroserviceVersionRule(), transportName));
return;
}
int idx = Math.abs(index.getAndIncrement());
idx = idx % endpoints.size();
Endpoint endpoint = endpoints.get(idx);
invocation.setEndpoint(endpoint);
invocation.next(asyncResp);
}
Aggregations