Java Code Examples for org.apache.servicecomb.core.Invocation#setEndpoint()

The following examples show how to use org.apache.servicecomb.core.Invocation#setEndpoint() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LoadbalanceHandler.java    From servicecomb-java-chassis with Apache License 2.0 6 votes vote down vote up
private boolean defineEndpointAndHandle(Invocation invocation, AsyncResponse asyncResp) throws Exception {
  Object endpoint = invocation.getLocalContext(SERVICECOMB_SERVER_ENDPOINT);
  if (endpoint == null) {
    return false;
  }
  if (endpoint instanceof String) {
    // compatible to old usage
    endpoint = parseEndpoint((String) endpoint);
  }

  invocation.setEndpoint((Endpoint) endpoint);
  invocation.next(resp -> {
    asyncResp.handle(resp);
  });
  return true;
}
 
Example 2
Source File: LoadbalanceHandler.java    From servicecomb-java-chassis with Apache License 2.0 6 votes vote down vote up
private void send(Invocation invocation, AsyncResponse asyncResp, LoadBalancer chosenLB) throws Exception {
  long time = System.currentTimeMillis();
  ServiceCombServer server = chosenLB.chooseServer(invocation);
  if (null == server) {
    asyncResp.consumerFail(new InvocationException(Status.INTERNAL_SERVER_ERROR, "No available address found."));
    return;
  }
  chosenLB.getLoadBalancerStats().incrementNumRequests(server);
  invocation.setEndpoint(server.getEndpoint());
  invocation.next(resp -> {
    // this stats is for WeightedResponseTimeRule
    chosenLB.getLoadBalancerStats().noteResponseTime(server, (System.currentTimeMillis() - time));
    if (isFailedResponse(resp)) {
      chosenLB.getLoadBalancerStats().incrementSuccessiveConnectionFailureCount(server);
      ServiceCombLoadBalancerStats.INSTANCE.markFailure(server);
    } else {
      chosenLB.getLoadBalancerStats().incrementActiveRequestsCount(server);
      ServiceCombLoadBalancerStats.INSTANCE.markSuccess(server);
    }
    asyncResp.handle(resp);
  });
}
 
Example 3
Source File: SimpleLoadBalanceHandler.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
@Override
public void handle(Invocation invocation, AsyncResponse asyncResp) throws Exception {
  if (invocation.getEndpoint() != null) {
    invocation.next(asyncResp);
    return;
  }

  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(),
        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);

  invocation.setEndpoint(endpoint);

  invocation.next(asyncResp);
}
 
Example 4
Source File: SimpleLoadBalanceFilter.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<Response> onFilter(Invocation invocation, FilterNode nextNode) {
  if (invocation.getEndpoint() != null) {
    return nextNode.onFilter(invocation);
  }

  invocation.setEndpoint(selectEndpoint(invocation));
  return nextNode.onFilter(invocation);
}
 
Example 5
Source File: EndpointMapper.java    From servicecomb-java-chassis with Apache License 2.0 4 votes vote down vote up
@Override
public void invocationArgumentToSwaggerArguments(SwaggerInvocation swaggerInvocation,
    Map<String, Object> swaggerArguments, Map<String, Object> invocationArguments) {
  Invocation invocation = (Invocation) swaggerInvocation;
  invocation.setEndpoint((Endpoint) invocationArguments.get(invocationArgumentName));
}