@PUT @Path("v1/service/{name}/{id}") @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public Response putService(ServiceInstance<T> instance, @PathParam("name") String name, @PathParam("id") String id) { if ( !instance.getId().equals(id) || !instance.getName().equals(name) ) { log.info("Request where path id and/or name doesn't match entity"); return Response.status(Response.Status.BAD_REQUEST).build(); } if ( instance.getServiceType().isDynamic() ) { log.info("Service type cannot be dynamic"); return Response.status(Response.Status.BAD_REQUEST).build(); } try { context.getServiceDiscovery().registerService(instance); } catch ( Exception e ) { ThreadUtils.checkInterrupted(e); log.error("Trying to register service", e); return Response.serverError().build(); } return Response.status(Response.Status.CREATED).build(); }
private void checkService(String name) { try { Collection<ServiceInstance<Object>> instances = discovery.queryForInstances(name); for ( ServiceInstance<Object> instance : instances ) { if ( instance.getServiceType() == ServiceType.STATIC ) { if ( (System.currentTimeMillis() - instance.getRegistrationTimeUTC()) > instanceRefreshMs ) { discovery.unregisterService(instance); } } } } catch ( Exception e ) { ThreadUtils.checkInterrupted(e); log.error(String.format("GC for service: %s", name), e); } } }
localVariables.put(FIELD_SSL_PORT, nullCheck(serviceInstance.getSslPort())); localVariables.put(FIELD_REGISTRATION_TIME_UTC, nullCheck(serviceInstance.getRegistrationTimeUTC())); localVariables.put(FIELD_SERVICE_TYPE, (serviceInstance.getServiceType() != null) ? serviceInstance.getServiceType().name().toLowerCase() : ""); if ( serviceInstance.getSslPort() != null )
localVariables.put(FIELD_SSL_PORT, nullCheck(serviceInstance.getSslPort())); localVariables.put(FIELD_REGISTRATION_TIME_UTC, nullCheck(serviceInstance.getRegistrationTimeUTC())); localVariables.put(FIELD_SERVICE_TYPE, (serviceInstance.getServiceType() != null) ? serviceInstance.getServiceType().name().toLowerCase() : ""); if ( serviceInstance.getSslPort() != null )
switch (service.getServiceType()) { case DYNAMIC: mode = CreateMode.EPHEMERAL;
switch (service.getServiceType()) { case DYNAMIC: mode = CreateMode.EPHEMERAL;
JsonObject payload = instance.getPayload(); record.setMetadata(payload); record.getMetadata().put("zookeeper-service-type", instance.getServiceType().toString()); record.getMetadata().put("zookeeper-address", instance.getAddress()); record.getMetadata().put("zookeeper-registration-time",
JsonObject payload = instance.getPayload(); record.setMetadata(payload); record.getMetadata().put("zookeeper-service-type", instance.getServiceType().toString()); record.getMetadata().put("zookeeper-address", instance.getAddress()); record.getMetadata().put("zookeeper-registration-time",
public ServiceInstance<JsonObject> deserialize(byte[] bytes) throws Exception { ServiceInstance rawServiceInstance = this.mapper.readValue(bytes, this.type); ServiceInstanceBuilder<JsonObject> builder = ServiceInstance.<JsonObject>builder() .address(rawServiceInstance.getAddress()) .id(rawServiceInstance.getId()) .name(rawServiceInstance.getName()) .payload(new JsonObject(rawServiceInstance.getPayload().toString())) .registrationTimeUTC(rawServiceInstance.getRegistrationTimeUTC()) .serviceType(rawServiceInstance.getServiceType()); if (rawServiceInstance.getSslPort() != null) { builder.sslPort(rawServiceInstance.getSslPort()); } if (rawServiceInstance.getPort() != null) { builder.sslPort(rawServiceInstance.getPort()); } if (rawServiceInstance.getUriSpec() != null) { builder.uriSpec(rawServiceInstance.getUriSpec()); } return builder.build(); } }
@Override public byte[] serialize(ServiceInstance<T> instance) throws Exception { if ( compatibleSerializationMode ) { OldServiceInstance<T> compatible = new OldServiceInstance<T>(instance.getName(), instance.getId(), instance.getAddress(), instance.getPort(), instance.getSslPort(), instance.getPayload(), instance.getRegistrationTimeUTC(), instance.getServiceType(), instance.getUriSpec()); return mapper.writeValueAsBytes(compatible); } return mapper.writeValueAsBytes(instance); } }
public ServiceInstance<JsonObject> deserialize(byte[] bytes) throws Exception { ServiceInstance rawServiceInstance = this.mapper.readValue(bytes, this.type); ServiceInstanceBuilder<JsonObject> builder = ServiceInstance.<JsonObject>builder() .address(rawServiceInstance.getAddress()) .id(rawServiceInstance.getId()) .name(rawServiceInstance.getName()) .payload(new JsonObject(rawServiceInstance.getPayload().toString())) .registrationTimeUTC(rawServiceInstance.getRegistrationTimeUTC()) .serviceType(rawServiceInstance.getServiceType()); if (rawServiceInstance.getSslPort() != null) { builder.sslPort(rawServiceInstance.getSslPort()); } if (rawServiceInstance.getPort() != null) { builder.sslPort(rawServiceInstance.getPort()); } if (rawServiceInstance.getUriSpec() != null) { builder.uriSpec(rawServiceInstance.getUriSpec()); } return builder.build(); } }
static<T> ObjectNode writeInstance(ObjectMapper mapper, ServiceInstance<T> instance, DiscoveryContext<T> context) { ObjectNode node = mapper.createObjectNode(); node.put("name", instance.getName()); node.put("id", instance.getId()); node.put("address", instance.getAddress()); putInteger(node, "port", instance.getPort()); putInteger(node, "sslPort", instance.getSslPort()); node.put("registrationTimeUTC", instance.getRegistrationTimeUTC()); node.put("serviceType", instance.getServiceType().name()); try { context.marshallJson(node, "payload", instance.getPayload()); } catch ( Exception e ) { ThreadUtils.checkInterrupted(e); throw new WebApplicationException(e); } return node; }