use of com.tencent.polaris.api.plugin.server.CommonProviderResponse in project polaris-java by polarismesh.
the class CompositeConnector method registerInstance.
@Override
public CommonProviderResponse registerInstance(CommonProviderRequest req) throws PolarisException {
checkDestroyed();
CommonProviderResponse response = null;
for (DestroyableServerConnector sc : serverConnectors) {
CommonProviderResponse temp = sc.registerInstance(req);
if (DefaultPlugins.SERVER_CONNECTOR_GRPC.equals(sc.getName())) {
response = temp;
}
}
return response;
}
use of com.tencent.polaris.api.plugin.server.CommonProviderResponse in project polaris-java by polarismesh.
the class ConsulAPIConnector method registerInstance.
@Override
public CommonProviderResponse registerInstance(CommonProviderRequest req) throws PolarisException {
if (!ieRegistered) {
ServiceKey serviceKey = new ServiceKey(req.getNamespace(), req.getService());
try {
LOG.info("Registering service to Consul");
NewService service = buildRegisterInstanceRequest(req);
this.consulClient.agentServiceRegister(service);
CommonProviderResponse resp = new CommonProviderResponse();
consulContext.setInstanceId(service.getId());
resp.setInstanceID(service.getId());
resp.setExists(true);
LOG.info("Registered service to Consul: " + service);
ieRegistered = true;
return resp;
} catch (ConsulException e) {
throw new RetriableException(ErrorCode.NETWORK_ERROR, String.format("fail to register host %s:%d service %s", req.getHost(), req.getPort(), serviceKey), e);
}
}
return null;
}
use of com.tencent.polaris.api.plugin.server.CommonProviderResponse in project polaris-java by polarismesh.
the class GrpcConnector method registerInstance.
@Override
public CommonProviderResponse registerInstance(CommonProviderRequest req) throws PolarisException {
checkDestroyed();
Connection connection = null;
ServiceKey serviceKey = new ServiceKey(req.getNamespace(), req.getService());
try {
waitDiscoverReady();
connection = connectionManager.getConnection(GrpcUtil.OP_KEY_REGISTER_INSTANCE, ClusterType.SERVICE_DISCOVER_CLUSTER);
req.setTargetServer(connectionToTargetNode(connection));
PolarisGRPCGrpc.PolarisGRPCBlockingStub stub = PolarisGRPCGrpc.newBlockingStub(connection.getChannel());
GrpcUtil.attachRequestHeader(stub, GrpcUtil.nextInstanceRegisterReqId());
ResponseProto.Response registerInstanceResponse = stub.registerInstance(buildRegisterInstanceRequest(req));
GrpcUtil.checkResponse(registerInstanceResponse);
if (!registerInstanceResponse.hasInstance()) {
throw new PolarisException(ErrorCode.SERVER_USER_ERROR, "invalid register response: missing instance");
}
CommonProviderResponse resp = new CommonProviderResponse();
resp.setInstanceID(registerInstanceResponse.getInstance().getId().getValue());
resp.setExists(registerInstanceResponse.getCode().getValue() == ServerCodes.EXISTED_RESOURCE);
return resp;
} catch (Throwable t) {
if (t instanceof PolarisException) {
throw t;
}
if (null != connection) {
connection.reportFail();
}
throw new RetriableException(ErrorCode.NETWORK_ERROR, String.format("fail to register host %s:%d service %s", req.getHost(), req.getPort(), serviceKey), t);
} finally {
if (null != connection) {
connection.release(GrpcUtil.OP_KEY_REGISTER_INSTANCE);
}
}
}
use of com.tencent.polaris.api.plugin.server.CommonProviderResponse in project polaris-java by polarismesh.
the class DefaultProviderAPI method register.
@Override
public InstanceRegisterResponse register(InstanceRegisterRequest req) throws PolarisException {
checkAvailable("ProviderAPI");
Validator.validateInstanceRegisterRequest(req);
long retryInterval = sdkContext.getConfig().getGlobal().getAPI().getRetryInterval();
long timeout = getTimeout(req);
while (timeout > 0) {
long start = System.currentTimeMillis();
ServiceCallResult serviceCallResult = new ServiceCallResult();
CommonProviderRequest request = req.getRequest();
try {
CommonProviderResponse response = serverConnector.registerInstance(request);
LOG.info("register {}/{} instance {} succ", req.getNamespace(), req.getService(), response.getInstanceID());
serviceCallResult.setRetStatus(RetStatus.RetSuccess);
serviceCallResult.setRetCode(ErrorCode.Success.getCode());
return new InstanceRegisterResponse(response.getInstanceID(), response.isExists());
} catch (PolarisException e) {
serviceCallResult.setRetStatus(RetStatus.RetFail);
serviceCallResult.setRetCode(exceptionToErrorCode(e).getCode());
if (e instanceof RetriableException) {
LOG.warn("instance register request error, retrying.", e);
Utils.sleepUninterrupted(retryInterval);
continue;
}
throw e;
} finally {
long delay = System.currentTimeMillis() - start;
serviceCallResult.setDelay(delay);
reportServerCall(serviceCallResult, request.getTargetServer(), "register");
timeout -= delay;
}
}
throw new PolarisException(ErrorCode.API_TIMEOUT, "instance register request timeout.");
}
Aggregations