Search in sources :

Example 1 with EndpointsCache

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());
    }
}
Also used : EndpointsCache(io.servicecomb.core.endpoint.EndpointsCache) Test(org.junit.Test)

Example 2 with EndpointsCache

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);
}
Also used : Endpoint(io.servicecomb.core.Endpoint) EndpointsCache(io.servicecomb.core.endpoint.EndpointsCache) Endpoint(io.servicecomb.core.Endpoint)

Aggregations

EndpointsCache (io.servicecomb.core.endpoint.EndpointsCache)2 Endpoint (io.servicecomb.core.Endpoint)1 Test (org.junit.Test)1