Java Code Examples for org.apache.servicecomb.foundation.common.utils.JsonUtils#writeValueAsBytes()

The following examples show how to use org.apache.servicecomb.foundation.common.utils.JsonUtils#writeValueAsBytes() . 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: ServiceRegistryClientImpl.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
@Override
public String registerMicroservice(Microservice microservice) {
  Holder<CreateServiceResponse> holder = new Holder<>();
  IpPort ipPort = ipPortManager.getAvailableAddress();
  try {
    CreateServiceRequest request = new CreateServiceRequest();
    request.setService(microservice);
    byte[] body = JsonUtils.writeValueAsBytes(request);

    if (LOGGER.isDebugEnabled()) {
      LOGGER.debug("register microservice: {}", new String(body, Charset.defaultCharset()));
    }

    CountDownLatch countDownLatch = new CountDownLatch(1);
    restClientUtil.post(ipPort,
        Const.REGISTRY_API.MICROSERVICE_OPERATION_ALL,
        new RequestParam().setBody(body),
        syncHandler(countDownLatch, CreateServiceResponse.class, holder));
    countDownLatch.await();
    if (holder.value != null) {
      return holder.value.getServiceId();
    }
  } catch (Exception e) {
    LOGGER.error("register microservice {}/{}/{} failed",
        microservice.getAppId(),
        microservice.getServiceName(),
        microservice.getVersion(),
        e);
  }
  return null;
}
 
Example 2
Source File: ServiceRegistryClientImpl.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
@Override
public String registerMicroserviceInstance(MicroserviceInstance instance) {
  Holder<RegisterInstanceResponse> holder = new Holder<>();
  IpPort ipPort = ipPortManager.getAvailableAddress();

  try {
    RegisterInstanceRequest request = new RegisterInstanceRequest();
    request.setInstance(instance);
    byte[] body = JsonUtils.writeValueAsBytes(request);

    if (LOGGER.isDebugEnabled()) {
      LOGGER.debug("register microservice: {}", new String(body, Charset.defaultCharset()));
    }

    CountDownLatch countDownLatch = new CountDownLatch(1);
    restClientUtil.post(ipPort,
        String.format(Const.REGISTRY_API.MICROSERVICE_INSTANCE_OPERATION_ALL, instance.getServiceId()),
        new RequestParam().setBody(body),
        syncHandler(countDownLatch, RegisterInstanceResponse.class, holder));
    countDownLatch.await();
    if (holder.value != null) {
      return holder.value.getInstanceId();
    }
  } catch (Exception e) {
    LOGGER.error("register microservice instance {} failed", instance.getServiceId(), e);
  }
  return null;
}
 
Example 3
Source File: ServiceRegistryClientImpl.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
@Override
public boolean updateMicroserviceProperties(String microserviceId, Map<String, String> serviceProperties) {
  Holder<HttpClientResponse> holder = new Holder<>();
  IpPort ipPort = ipPortManager.getAvailableAddress();

  try {
    UpdatePropertiesRequest request = new UpdatePropertiesRequest();
    request.setProperties(serviceProperties);
    byte[] body = JsonUtils.writeValueAsBytes(request);

    if (LOGGER.isDebugEnabled()) {
      LOGGER.debug("update properties of microservice: {}", new String(body, Charset.defaultCharset()));
    }

    CountDownLatch countDownLatch = new CountDownLatch(1);
    restClientUtil.put(ipPort,
        String.format(Const.REGISTRY_API.MICROSERVICE_PROPERTIES, microserviceId),
        new RequestParam().setBody(body),
        syncHandler(countDownLatch, HttpClientResponse.class, holder));

    countDownLatch.await();
    if (holder.value != null) {
      if (holder.value.statusCode() == Status.OK.getStatusCode()) {
        return true;
      }
      LOGGER.warn(holder.value.statusMessage());
    }
  } catch (Exception e) {
    LOGGER.error("update properties of microservice {} failed",
        microserviceId,
        e);
  }
  return false;
}
 
Example 4
Source File: ServiceRegistryClientImpl.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
@Override
public boolean updateInstanceProperties(String microserviceId, String microserviceInstanceId,
    Map<String, String> instanceProperties) {
  Holder<HttpClientResponse> holder = new Holder<>();
  IpPort ipPort = ipPortManager.getAvailableAddress();

  try {
    UpdatePropertiesRequest request = new UpdatePropertiesRequest();
    request.setProperties(instanceProperties);
    byte[] body = JsonUtils.writeValueAsBytes(request);

    if (LOGGER.isDebugEnabled()) {
      LOGGER.debug("update properties of microservice instance: {}",
          new String(body, Charset.defaultCharset()));
    }

    CountDownLatch countDownLatch = new CountDownLatch(1);
    restClientUtil.put(ipPort,
        String.format(Const.REGISTRY_API.MICROSERVICE_INSTANCE_PROPERTIES, microserviceId, microserviceInstanceId),
        new RequestParam().setBody(body),
        syncHandler(countDownLatch, HttpClientResponse.class, holder));

    countDownLatch.await();
    if (holder.value != null) {
      if (holder.value.statusCode() == Status.OK.getStatusCode()) {
        return true;
      }
      LOGGER.warn(holder.value.statusMessage());
    }
  } catch (Exception e) {
    LOGGER.error("update properties of microservice instance {}/{} failed",
        microserviceId,
        microserviceInstanceId,
        e);
  }
  return false;
}
 
Example 5
Source File: ServiceRegistryClientImpl.java    From servicecomb-java-chassis with Apache License 2.0 4 votes vote down vote up
@Override
public boolean registerSchema(String microserviceId, String schemaId, String schemaContent) {
  Holder<ResponseWrapper> holder = new Holder<>();
  IpPort ipPort = ipPortManager.getAvailableAddress();

  try {
    CreateSchemaRequest request = new CreateSchemaRequest();
    request.setSchema(schemaContent);
    request.setSummary(RegistryUtils.calcSchemaSummary(schemaContent));
    byte[] body = JsonUtils.writeValueAsBytes(request);

    CountDownLatch countDownLatch = new CountDownLatch(1);
    restClientUtil.put(ipPort,
        String.format(Const.REGISTRY_API.MICROSERVICE_SCHEMA, microserviceId, schemaId),
        new RequestParam().setBody(body),
        syncHandlerEx(countDownLatch, holder));
    countDownLatch.await();

    if (holder.value == null) {
      LOGGER.error("Register schema {}/{} failed.", microserviceId, schemaId);
      return false;
    }

    if (!Status.Family.SUCCESSFUL.equals(Status.Family.familyOf(holder.value.response.statusCode()))) {
      LOGGER.error("Register schema {}/{} failed, statusCode: {}, statusMessage: {}, description: {}.",
          microserviceId,
          schemaId,
          holder.value.response.statusCode(),
          holder.value.response.statusMessage(),
          holder.value.bodyBuffer.toString());
      return false;
    }

    LOGGER.info("register schema {}/{} success.",
        microserviceId,
        schemaId);
    return true;
  } catch (Exception e) {
    LOGGER.error("register schema {}/{} fail.",
        microserviceId,
        schemaId,
        e);
  }
  return false;
}