Java Code Examples for org.apache.curator.x.discovery.ServiceInstance#getPayload()

The following examples show how to use org.apache.curator.x.discovery.ServiceInstance#getPayload() . 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: StaticQpsTransportPool.java    From jigsaw-payment with Apache License 2.0 6 votes vote down vote up
@Override
protected void onInstanceAdded(ServiceInstance<RpcPayload> instance) {
    if (!this.instances.contains(instance)) {
        if (instance.getPayload() == null) {
            this.instances.offer(instance);
        } else {
            int count = (int) instance.getPayload().getMaxQps() / BASE_NUM;
            count++;
            LOG.info("Max qps :" + instance.getPayload().getMaxQps()
                    + "  Count" + count);
            for (int i = 0; i < count; i++) {
                this.instances.offer(instance);
            }
        }
        LOG.info("Add an instance to pool:  " + instance);
        this.failedCount.put(instance, new AtomicInteger(0));
    }
}
 
Example 2
Source File: AbstractTransportPool.java    From jigsaw-payment with Apache License 2.0 6 votes vote down vote up
/**
 * 根据rc的设置来确定创建什么类型的transport;
 *
 * @param instance
 * @return
 */
protected TTransport createNativeTransport(
        ServiceInstance<RpcPayload> instance) {
    TSocket socket = new TSocket(instance.getAddress(), instance.getPort());
    socket.setTimeout(socketTimeout);

    RpcPayload server = instance.getPayload();
    if ((server == null) || (server.getTransport() == null)
            || (server.getTransport().equals("socket"))) {
        return socket;
    } else if ("framed-transport".equals(server.getTransport())) {
        return new TFramedTransport(socket);
    }

    // for default, use TSocket;
    return socket;
}
 
Example 3
Source File: ZooKeeperDiscovery.java    From soabase with Apache License 2.0 6 votes vote down vote up
@Override
public void setForcedState(String serviceName, String instanceId, ForcedState forcedState)
{
    try
    {
        ServiceInstance<Payload> foundInstance = discovery.queryForInstance(serviceName, instanceId);
        if ( foundInstance != null )
        {
            DiscoveryInstance soaInstance = toSoaInstance(foundInstance);
            Payload oldPayload = foundInstance.getPayload();
            Payload newPayload = new Payload(null, oldPayload.getAdminPort(), oldPayload.getMetaData(), forcedState, oldPayload.getHealthyState());
            ServiceInstance<Payload> updatedInstance = buildInstance(serviceName, HostAndPort.fromParts(soaInstance.getHost(), soaInstance.getPort()), newPayload, instanceId, soaInstance.getHost());
            discovery.updateService(updatedInstance);
        } // TODO else?
    }
    catch ( Exception e )
    {
        log.error("Could not update service: " + (serviceName + ":" + instanceId), e);
        throw new RuntimeException(e);
    }
}
 
Example 4
Source File: ZooKeeperDiscovery.java    From soabase with Apache License 2.0 6 votes vote down vote up
@Override
public ServiceProvider<Payload> load(String serviceName) throws Exception
{
    InstanceFilter<Payload> filter = new InstanceFilter<Payload>()
    {
        @Override
        public boolean apply(ServiceInstance<Payload> instance)
        {
            Payload payload = instance.getPayload();
            if ( payload.getForcedState() == ForcedState.CLEARED )
            {
                return (payload.getHealthyState() == HealthyState.HEALTHY);
            }
            return (payload.getForcedState() == ForcedState.REGISTER);
        }
    };
    ServiceProvider<Payload> provider = discovery
        .serviceProviderBuilder()
        .serviceName(serviceName)
        .additionalFilter(filter)
        .build();
    provider.start();
    return provider;
}
 
Example 5
Source File: ZooKeeperDiscovery.java    From soabase with Apache License 2.0 6 votes vote down vote up
private void updateRegistration(Payload newPayload)
{
    if ( !soaInfo.isRegisterInDiscovery() )
    {
        return;
    }

    ServiceInstance<Payload> localUs = us.get();
    Payload currentPayload = localUs.getPayload();
    if ( !newPayload.equals(currentPayload) )
    {
        try
        {
            ServiceInstance<Payload> updatedInstance = buildInstance(newPayload, localUs.getId());
            us.set(updatedInstance);
            discovery.updateService(updatedInstance);
        }
        catch ( Exception e )
        {
            log.error("Could not update registration for local instance: " + localUs, e);
            throw new RuntimeException(e);
        }
    }
}
 
Example 6
Source File: ZookeeperCoordinatorTest.java    From skywalking with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private void validateServiceInstance(Address address, RemoteInstance instance) throws Exception {
    ArgumentCaptor<ServiceInstance> argumentCaptor = ArgumentCaptor.forClass(ServiceInstance.class);
    verify(serviceDiscovery).registerService(argumentCaptor.capture());

    ServiceInstance<RemoteInstance> serviceInstance = argumentCaptor.getValue();

    assertEquals("remote", serviceInstance.getName());
    assertTrue(!Strings.isNullOrEmpty(serviceInstance.getId()));
    assertEquals(address.getHost(), serviceInstance.getAddress());
    assertEquals(address.getPort(), serviceInstance.getPort().intValue());

    RemoteInstance payload = serviceInstance.getPayload();
    assertEquals(payload.getAddress(), instance.getAddress());

}
 
Example 7
Source File: ZooKeeperDiscovery.java    From soabase with Apache License 2.0 5 votes vote down vote up
private DiscoveryInstance toSoaInstance(ServiceInstance<Payload> instance)
{
    if ( instance == null )
    {
        return null;
    }

    Payload payload = instance.getPayload();
    int port = Objects.firstNonNull(instance.getPort(), Objects.firstNonNull(instance.getSslPort(), 0));
    return new DiscoveryInstanceImpl(instance.getId(), instance.getAddress(), port, instance.getSslPort() != null, payload);
}
 
Example 8
Source File: ZooKeeperUpdatingListener.java    From armeria with Apache License 2.0 5 votes vote down vote up
private static ZooKeeperRegistrationSpec curatorSpec(
        ServiceInstance<?> serviceInstance, Server server) {
    final CuratorRegistrationSpecBuilder builder =
            ZooKeeperRegistrationSpec.builderForCurator(serviceInstance.getName());
    builder.serviceId(serviceInstance.getId());
    final String address;
    if (serviceInstance.getAddress() != null) {
        address = serviceInstance.getAddress();
    } else {
        address = defaultAddress(server);
    }
    builder.serviceAddress(address);
    final int port = port(server, SessionProtocol.HTTP, serviceInstance.getPort());
    if (port > 0) {
        builder.port(port);
    }
    final int sslPort = port(server, SessionProtocol.HTTPS, serviceInstance.getSslPort());
    if (sslPort > 0) {
        builder.sslPort(sslPort);
    }
    builder.serviceType(serviceInstance.getServiceType());
    final Object payload = serviceInstance.getPayload();
    if (payload != null) {
        builder.payload(payload);
    }
    return builder.build();
}
 
Example 9
Source File: ZookeeperServiceRegistry.java    From spring-cloud-zookeeper with Apache License 2.0 5 votes vote down vote up
@Override
public void setStatus(ZookeeperRegistration registration, String status) {
	ServiceInstance<ZookeeperInstance> serviceInstance = registration
			.getServiceInstance();
	ZookeeperInstance instance = serviceInstance.getPayload();
	instance.getMetadata().put(INSTANCE_STATUS_KEY, status);
	try {
		getServiceDiscovery().updateService(serviceInstance);
	}
	catch (Exception e) {
		ReflectionUtils.rethrowRuntimeException(e);
	}
}
 
Example 10
Source File: JsonInstanceSerializer.java    From curator with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 11
Source File: ConsumerClient.java    From BigData-In-Practice with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    ServiceDiscovery<InstanceDetails> serviceDiscovery;
    try (CuratorFramework client = ZKUtils.getClient()) {
        client.start();
        client.blockUntilConnected();

        serviceDiscovery = ServiceDiscoveryBuilder.builder(InstanceDetails.class)
                .client(client)
                .basePath(InstanceDetails.ROOT_PATH)
                .serializer(new JsonInstanceSerializer<InstanceDetails>(InstanceDetails.class))
                .build();
    }
    serviceDiscovery.start();

    boolean flag = true;

    // 死循环来不停的获取服务列表,查看是否有新服务发布
    while (flag) {

        //根据名称获取服务
        Collection<ServiceInstance<InstanceDetails>> services = serviceDiscovery.queryForInstances("OrderService");
        if (services.size() == 0) {
            System.out.println("当前没有发现服务");
            Thread.sleep(10 * 1000);
            continue;
        }

        for (ServiceInstance<InstanceDetails> service : services) {

            //获取请求的scheme 例如:http://127.0.0.1:8080
            String uriSpec = service.buildUriSpec();
            //获取服务的其他信息
            InstanceDetails payload = service.getPayload();

            //服务描述
            String serviceDesc = payload.getServiceDesc();
            //获取该服务下的所有接口
            Map<String, InstanceDetails.Service> allService = payload.getServices();
            Set<Map.Entry<String, InstanceDetails.Service>> entries = allService.entrySet();

            for (Map.Entry<String, InstanceDetails.Service> entry : entries) {
                System.out.println(serviceDesc + uriSpec
                        + "/" + service.getName()
                        + "/" + entry.getKey()
                        + " 该方法需要的参数为:"
                        + entry.getValue().getParams().toString());
            }
        }
        System.out.println("---------------------");
        Thread.sleep(10 * 1000);
    }

}