Search in sources :

Example 11 with ServiceCallResult

use of com.tencent.polaris.api.rpc.ServiceCallResult in project polaris-java-agent by polarismesh.

the class PolarisServiceObservability method updateServiceCallResult.

public static void updateServiceCallResult(Long delay, Throwable throwable, Integer status, PolarisAgentProperties polarisAgentProperties) {
    ServiceInstance instance = InvokeContextHolder.get().getServiceInstance();
    if (instance == null) {
        LOGGER.warn("fail to call updateServiceCallResult for polaris with instance empty");
        return;
    }
    ServiceCallResult serviceCallResult = new ServiceCallResult();
    serviceCallResult.setNamespace(polarisAgentProperties.getNamespace());
    serviceCallResult.setService(instance.getServiceId());
    serviceCallResult.setHost(instance.getHost());
    serviceCallResult.setPort(instance.getPort());
    serviceCallResult.setDelay(delay);
    serviceCallResult.setRetStatus(null == throwable ? RetStatus.RetSuccess : RetStatus.RetFail);
    serviceCallResult.setRetCode(null != status ? status : -1);
    try {
        PolarisAPIFactory.getConsumerApi().updateServiceCallResult(serviceCallResult);
        LOGGER.info("success to call updateServiceCallResult for polaris with status:{}, code:{}", serviceCallResult.getRetStatus(), serviceCallResult.getRetCode());
    } catch (PolarisException e) {
        LOGGER.error(e.getMessage());
    } finally {
        InvokeContextHolder.remove();
    }
}
Also used : PolarisException(com.tencent.polaris.api.exception.PolarisException) ServiceCallResult(com.tencent.polaris.api.rpc.ServiceCallResult) ServiceInstance(org.springframework.cloud.client.ServiceInstance)

Example 12 with ServiceCallResult

use of com.tencent.polaris.api.rpc.ServiceCallResult in project spring-cloud-tencent by Tencent.

the class PolarisFeignClient method execute.

@Override
public Response execute(Request request, Options options) throws IOException {
    final ServiceCallResult resultRequest = createServiceCallResult(request);
    try {
        Response response = delegate.execute(request, options);
        // HTTP code greater than 500 is an exception
        if (response.status() >= 500) {
            resultRequest.setRetStatus(RetStatus.RetFail);
        }
        LOG.debug("Will report result of {}. Request=[{}]. Response=[{}].", resultRequest.getRetStatus().name(), request, response);
        return response;
    } catch (IOException origin) {
        resultRequest.setRetStatus(RetStatus.RetFail);
        LOG.debug("Will report result of {}. Request=[{}].", resultRequest.getRetStatus().name(), request, origin);
        throw origin;
    } finally {
        consumerAPI.updateServiceCallResult(resultRequest);
    }
}
Also used : Response(feign.Response) ServiceCallResult(com.tencent.polaris.api.rpc.ServiceCallResult) IOException(java.io.IOException)

Example 13 with ServiceCallResult

use of com.tencent.polaris.api.rpc.ServiceCallResult in project spring-cloud-tencent by Tencent.

the class PolarisFeignClient method createServiceCallResult.

private ServiceCallResult createServiceCallResult(final Request request) {
    ServiceCallResult resultRequest = new ServiceCallResult();
    resultRequest.setNamespace(MetadataContext.LOCAL_NAMESPACE);
    String serviceName = request.requestTemplate().feignTarget().name();
    resultRequest.setService(serviceName);
    URI uri = URI.create(request.url());
    resultRequest.setMethod(uri.getPath());
    resultRequest.setRetStatus(RetStatus.RetSuccess);
    String sourceNamespace = MetadataContext.LOCAL_NAMESPACE;
    String sourceService = MetadataContext.LOCAL_SERVICE;
    if (StringUtils.isNotBlank(sourceNamespace) && StringUtils.isNotBlank(sourceService)) {
        resultRequest.setCallerService(new ServiceKey(sourceNamespace, sourceService));
    }
    resultRequest.setHost(uri.getHost());
    resultRequest.setPort(uri.getPort());
    return resultRequest;
}
Also used : ServiceCallResult(com.tencent.polaris.api.rpc.ServiceCallResult) ServiceKey(com.tencent.polaris.api.pojo.ServiceKey) URI(java.net.URI)

Example 14 with ServiceCallResult

use of com.tencent.polaris.api.rpc.ServiceCallResult in project polaris-java by polarismesh.

the class Consumer method invokeByNameResolution.

private static String invokeByNameResolution(String namespace, String service, String value, ConsumerAPI consumerAPI) {
    System.out.println("namespace " + namespace + ", service " + service);
    // 1. we need to do naming resolution to get a load balanced host and port
    GetOneInstanceRequest getOneInstanceRequest = new GetOneInstanceRequest();
    getOneInstanceRequest.setNamespace(namespace);
    getOneInstanceRequest.setService(service);
    InstancesResponse oneInstance = consumerAPI.getOneInstance(getOneInstanceRequest);
    Instance[] instances = oneInstance.getInstances();
    System.out.println("instances count is " + instances.length);
    Instance targetInstance = instances[0];
    System.out.printf("target instance is %s:%d%n", targetInstance.getHost(), targetInstance.getPort());
    // 2. invoke the server by the resolved address
    String urlStr = String.format("http://%s:%d%s?value=%s", targetInstance.getHost(), targetInstance.getPort(), PATH, value);
    long startMillis = System.currentTimeMillis();
    HttpResult httpResult = httpGet(urlStr);
    long delay = System.currentTimeMillis() - startMillis;
    System.out.printf("invoke %s, code is %d, delay is %d%n", urlStr, httpResult.code, delay);
    // 3. report the invoke result to polaris-java, to eliminate the fail address
    RetStatus status = RetStatus.RetSuccess;
    if (httpResult.code != 200) {
        status = RetStatus.RetFail;
    }
    ServiceCallResult result = new ServiceCallResult();
    result.setNamespace(namespace);
    result.setService(service);
    result.setHost(targetInstance.getHost());
    result.setPort(targetInstance.getPort());
    result.setRetCode(httpResult.code);
    result.setDelay(delay);
    result.setRetStatus(status);
    consumerAPI.updateServiceCallResult(result);
    System.out.println("success to call updateServiceCallResult");
    return httpResult.message;
}
Also used : RetStatus(com.tencent.polaris.api.pojo.RetStatus) ServiceCallResult(com.tencent.polaris.api.rpc.ServiceCallResult) Instance(com.tencent.polaris.api.pojo.Instance) GetOneInstanceRequest(com.tencent.polaris.api.rpc.GetOneInstanceRequest) InstancesResponse(com.tencent.polaris.api.rpc.InstancesResponse)

Example 15 with ServiceCallResult

use of com.tencent.polaris.api.rpc.ServiceCallResult in project polaris-java by polarismesh.

the class APIFacade method updateServiceCallResult.

public static boolean updateServiceCallResult(String namespace, String service, String method, String host, int port, long delay, boolean success, int code) {
    if (!inited.get()) {
        LOGGER.info("polaris not inited, updateServiceCallResult fail");
        return false;
    }
    ServiceCallResult serviceCallResult = new ServiceCallResult();
    serviceCallResult.setNamespace(namespace);
    serviceCallResult.setService(service);
    serviceCallResult.setMethod(method);
    serviceCallResult.setHost(host);
    serviceCallResult.setPort(port);
    serviceCallResult.setDelay(delay);
    serviceCallResult.setRetStatus(success ? RetStatus.RetSuccess : RetStatus.RetFail);
    serviceCallResult.setRetCode(code);
    consumerAPI.updateServiceCallResult(serviceCallResult);
    LOGGER.debug("success to call updateServiceCallResult, status:{}", serviceCallResult.getRetStatus());
    return true;
}
Also used : ServiceCallResult(com.tencent.polaris.api.rpc.ServiceCallResult)

Aggregations

ServiceCallResult (com.tencent.polaris.api.rpc.ServiceCallResult)20 Test (org.junit.Test)7 PolarisException (com.tencent.polaris.api.exception.PolarisException)5 Instance (com.tencent.polaris.api.pojo.Instance)5 ConsumerAPI (com.tencent.polaris.api.core.ConsumerAPI)4 StatInfo (com.tencent.polaris.api.plugin.stat.StatInfo)4 InstancesResponse (com.tencent.polaris.api.rpc.InstancesResponse)4 Configuration (com.tencent.polaris.api.config.Configuration)3 RetriableException (com.tencent.polaris.api.exception.RetriableException)3 CommonProviderRequest (com.tencent.polaris.api.plugin.server.CommonProviderRequest)3 GetInstancesRequest (com.tencent.polaris.api.rpc.GetInstancesRequest)3 ProviderAPI (com.tencent.polaris.api.core.ProviderAPI)1 CommonProviderResponse (com.tencent.polaris.api.plugin.server.CommonProviderResponse)1 ReportClientRequest (com.tencent.polaris.api.plugin.server.ReportClientRequest)1 ReportClientResponse (com.tencent.polaris.api.plugin.server.ReportClientResponse)1 CircuitBreakerStatus (com.tencent.polaris.api.pojo.CircuitBreakerStatus)1 RetStatus (com.tencent.polaris.api.pojo.RetStatus)1 ServiceKey (com.tencent.polaris.api.pojo.ServiceKey)1 GetOneInstanceRequest (com.tencent.polaris.api.rpc.GetOneInstanceRequest)1 InstanceRegisterResponse (com.tencent.polaris.api.rpc.InstanceRegisterResponse)1