org.springframework.cloud.zookeeper.discovery.ZookeeperInstance Java Examples

The following examples show how to use org.springframework.cloud.zookeeper.discovery.ZookeeperInstance. 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: ZookeeperAutoServiceRegistrationTests.java    From spring-cloud-zookeeper with Apache License 2.0 6 votes vote down vote up
@Test
public void contextLoads() throws Exception {
	Collection<ServiceInstance<ZookeeperInstance>> instances = serviceDiscovery
			.queryForInstances("myTestService1-F");
	assertThat(instances).hasSize(1);

	ServiceInstance<ZookeeperInstance> instance = instances.iterator().next();
	assertThat(instance).isNotNull();
	assertThat(instance.getName()).isEqualTo("myTestService1-F");
	/*
	 * Response<Map<String, Service>> response = consul.getAgentServices();
	 * Map<String, Service> services = response.getValue(); Service service =
	 * services.get(registration.getServiceId()); assertNotNull("service was null",
	 * service); assertNotEquals("service port is 0", 0,
	 * service.getPort().intValue());
	 * assertFalse("service id contained invalid character: " + service.getId(),
	 * service.getId().contains(":")); assertEquals("service id was wrong",
	 * registration.getServiceId(), service.getId());
	 * assertEquals("service name was wrong", "myTestService1-FF-something",
	 * service.getService()); assertFalse("service address must not be empty",
	 * StringUtils.isEmpty(service.getAddress()));
	 * assertEquals("service address must equals hostname from discovery properties",
	 * discoveryProperties.getHostname(), service.getAddress());
	 */
}
 
Example #2
Source File: ServiceDefinitionToZookeeperRegistration.java    From camel-spring-boot with Apache License 2.0 6 votes vote down vote up
@Override
public ZookeeperRegistration convert(ServiceDefinition source) {
    ZookeeperInstance instance = new ZookeeperInstance(
        source.getId(),
        source.getName(),
        source.getMetadata()
    );

    return ServiceInstanceRegistration.builder()
        .address(properties.getServiceRegistry().getServiceHost())
        .port(source.getPort())
        .name(source.getName())
        .payload(instance)
        .uriSpec(ZookeeperDiscoveryProperties.DEFAULT_URI_SPEC)
        .build();
}
 
Example #3
Source File: DependencyWatcherAutoConfiguration.java    From spring-cloud-zookeeper with Apache License 2.0 5 votes vote down vote up
@Bean(destroyMethod = "clearDependencyRegistrationHooks")
@ConditionalOnMissingBean
public DependencyRegistrationHookProvider dependencyWatcher(
		ServiceDiscovery<ZookeeperInstance> serviceDiscovery,
		DependencyPresenceOnStartupVerifier dependencyPresenceOnStartupVerifier,
		ZookeeperDependencies zookeeperDependencies) {
	return new DefaultDependencyWatcher(serviceDiscovery,
			dependencyPresenceOnStartupVerifier, this.dependencyWatcherListeners,
			zookeeperDependencies);
}
 
Example #4
Source File: DefaultDependencyWatcher.java    From spring-cloud-zookeeper with Apache License 2.0 5 votes vote down vote up
public DefaultDependencyWatcher(ServiceDiscovery<ZookeeperInstance> serviceDiscovery,
		DependencyPresenceOnStartupVerifier dependencyPresenceOnStartupVerifier,
		List<DependencyWatcherListener> dependencyWatcherListeners,
		ZookeeperDependencies zookeeperDependencies) {
	this.serviceDiscovery = serviceDiscovery;
	this.dependencyPresenceOnStartupVerifier = dependencyPresenceOnStartupVerifier;
	this.listeners = dependencyWatcherListeners;
	this.zookeeperDependencies = zookeeperDependencies;
}
 
Example #5
Source File: ZookeeperReactiveDiscoveryClient.java    From spring-cloud-zookeeper with Apache License 2.0 5 votes vote down vote up
private Function<String, Publisher<org.apache.curator.x.discovery.ServiceInstance<ZookeeperInstance>>> getInstancesFromZookeeper() {
	return service -> {
		try {
			return Flux.fromIterable(serviceDiscovery.queryForInstances(service));
		}
		catch (Exception e) {
			logger.error("Error getting instances from zookeeper. Possibly, no service has registered.", e);
			return Flux.empty();
		}
	};
}
 
Example #6
Source File: ServiceInstanceRegistration.java    From spring-cloud-zookeeper with Apache License 2.0 5 votes vote down vote up
public static RegistrationBuilder builder() {
	try {
		return new RegistrationBuilder(ServiceInstance.<ZookeeperInstance>builder());
	}
	catch (Exception e) {
		throw new RuntimeException("Error creating ServiceInstanceBuilder", e);
	}
}
 
Example #7
Source File: ZookeeperServiceRegistry.java    From spring-cloud-zookeeper with Apache License 2.0 5 votes vote down vote up
@Override
public Object getStatus(ZookeeperRegistration registration) {
	ZookeeperInstance instance = registration.getServiceInstance().getPayload();
	String instanceStatus = instance.getMetadata().get(INSTANCE_STATUS_KEY);

	if (!StringUtils.hasText(instanceStatus)) {
		instanceStatus = STATUS_UP;
	}
	return instanceStatus;
}
 
Example #8
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 #9
Source File: CuratorServiceDiscoveryAutoConfiguration.java    From spring-cloud-zookeeper with Apache License 2.0 5 votes vote down vote up
@Bean
@ConditionalOnMissingBean(ServiceDiscoveryCustomizer.class)
public DefaultServiceDiscoveryCustomizer defaultServiceDiscoveryCustomizer(
		CuratorFramework curator, ZookeeperDiscoveryProperties properties,
		InstanceSerializer<ZookeeperInstance> serializer) {
	return new DefaultServiceDiscoveryCustomizer(curator, properties, serializer);
}
 
Example #10
Source File: ZookeeperAutoServiceRegistrationAutoConfiguration.java    From spring-cloud-zookeeper with Apache License 2.0 5 votes vote down vote up
@Bean
@ConditionalOnMissingBean(ZookeeperRegistration.class)
public ServiceInstanceRegistration serviceInstanceRegistration(
		ApplicationContext context, ZookeeperDiscoveryProperties properties) {
	String appName = context.getEnvironment().getProperty("spring.application.name",
			"application");
	String host = properties.getInstanceHost();
	if (!StringUtils.hasText(host)) {
		throw new IllegalStateException("instanceHost must not be empty");
	}

	ZookeeperInstance zookeeperInstance = new ZookeeperInstance(context.getId(),
			appName, properties.getMetadata());
	RegistrationBuilder builder = ServiceInstanceRegistration.builder().address(host)
			.name(appName).payload(zookeeperInstance)
			.uriSpec(properties.getUriSpec());

	if (properties.getInstanceSslPort() != null) {
		builder.sslPort(properties.getInstanceSslPort());
	}
	if (properties.getInstanceId() != null) {
		builder.id(properties.getInstanceId());
	}

	// TODO add customizer?

	return builder.build();
}
 
Example #11
Source File: CuratorServiceDiscoveryAutoConfiguration.java    From spring-cloud-zookeeper with Apache License 2.0 5 votes vote down vote up
@Bean
@ConditionalOnMissingBean
public ServiceDiscovery<ZookeeperInstance> curatorServiceDiscovery(
		ServiceDiscoveryCustomizer customizer) {
	return customizer
			.customize(ServiceDiscoveryBuilder.builder(ZookeeperInstance.class));
}
 
Example #12
Source File: DefaultServiceDiscoveryCustomizer.java    From spring-cloud-zookeeper with Apache License 2.0 5 votes vote down vote up
public DefaultServiceDiscoveryCustomizer(CuratorFramework curator,
		ZookeeperDiscoveryProperties properties,
		InstanceSerializer<ZookeeperInstance> instanceSerializer) {
	this.curator = curator;
	this.properties = properties;
	this.instanceSerializer = instanceSerializer;
}
 
Example #13
Source File: DefaultServiceDiscoveryCustomizer.java    From spring-cloud-zookeeper with Apache License 2.0 5 votes vote down vote up
@Override
public ServiceDiscovery<ZookeeperInstance> customize(
		ServiceDiscoveryBuilder<ZookeeperInstance> builder) {
	// @formatter:off
	return builder
			.client(this.curator)
			.basePath(this.properties.getRoot())
			.serializer(this.instanceSerializer)
			.build();
	// @formatter:on
}
 
Example #14
Source File: ServiceDiscoveryCustomizer.java    From spring-cloud-zookeeper with Apache License 2.0 4 votes vote down vote up
ServiceDiscovery<ZookeeperInstance> customize(
ServiceDiscoveryBuilder<ZookeeperInstance> builder);
 
Example #15
Source File: ZookeeperReactiveDiscoveryClientConfiguration.java    From spring-cloud-zookeeper with Apache License 2.0 4 votes vote down vote up
@Bean
@ConditionalOnMissingBean
public ZookeeperReactiveDiscoveryClient zookeeperReactiveDiscoveryClient(ServiceDiscovery<ZookeeperInstance> serviceDiscovery,
		ZookeeperDiscoveryProperties zookeeperDiscoveryProperties) {
	return new ZookeeperReactiveDiscoveryClient(serviceDiscovery, zookeeperDependencies, zookeeperDiscoveryProperties);
}
 
Example #16
Source File: CuratorServiceDiscoveryAutoConfiguration.java    From spring-cloud-zookeeper with Apache License 2.0 4 votes vote down vote up
@Bean
@ConditionalOnMissingBean
public InstanceSerializer<ZookeeperInstance> deprecatedInstanceSerializer() {
	return new JsonInstanceSerializer<>(ZookeeperInstance.class);
}
 
Example #17
Source File: ZookeeperReactiveDiscoveryClient.java    From spring-cloud-zookeeper with Apache License 2.0 4 votes vote down vote up
private ZookeeperServiceInstance toZookeeperServiceInstance(String serviceId,
		org.apache.curator.x.discovery.ServiceInstance<ZookeeperInstance> zkInstanceServiceInstance) {
	return new ZookeeperServiceInstance(serviceId, zkInstanceServiceInstance);
}
 
Example #18
Source File: ZookeeperReactiveDiscoveryClient.java    From spring-cloud-zookeeper with Apache License 2.0 4 votes vote down vote up
public ZookeeperReactiveDiscoveryClient(ServiceDiscovery<ZookeeperInstance> serviceDiscovery,
		ZookeeperDependencies zookeeperDependencies, ZookeeperDiscoveryProperties zookeeperDiscoveryProperties) {
	this.serviceDiscovery = serviceDiscovery;
	this.zookeeperDependencies = zookeeperDependencies;
	this.zookeeperDiscoveryProperties = zookeeperDiscoveryProperties;
}
 
Example #19
Source File: ServiceInstanceRegistration.java    From spring-cloud-zookeeper with Apache License 2.0 4 votes vote down vote up
public RegistrationBuilder(ServiceInstanceBuilder<ZookeeperInstance> builder) {
	this.builder = builder;
}
 
Example #20
Source File: DefaultDependencyWatcher.java    From spring-cloud-zookeeper with Apache License 2.0 4 votes vote down vote up
private ServiceDiscovery<ZookeeperInstance> getServiceDiscovery() {
	return this.serviceDiscovery;
}
 
Example #21
Source File: ServiceInstanceRegistration.java    From spring-cloud-zookeeper with Apache License 2.0 4 votes vote down vote up
public ServiceInstance<ZookeeperInstance> getServiceInstance() {
	if (this.serviceInstance == null) {
		build();
	}
	return this.serviceInstance;
}
 
Example #22
Source File: ServiceInstanceRegistration.java    From spring-cloud-zookeeper with Apache License 2.0 4 votes vote down vote up
public ServiceInstanceRegistration(
		ServiceInstanceBuilder<ZookeeperInstance> builder) {
	this.builder = builder;
}
 
Example #23
Source File: ZookeeperServiceRegistryAutoConfiguration.java    From spring-cloud-zookeeper with Apache License 2.0 4 votes vote down vote up
@Bean
@ConditionalOnMissingBean
public InstanceSerializer<ZookeeperInstance> instanceSerializer() {
	return new JsonInstanceSerializer<>(ZookeeperInstance.class);
}
 
Example #24
Source File: ZookeeperServiceRegistry.java    From spring-cloud-zookeeper with Apache License 2.0 4 votes vote down vote up
private ServiceDiscovery<ZookeeperInstance> getServiceDiscovery() {
	return this.serviceDiscovery;
}
 
Example #25
Source File: ZookeeperServiceRegistry.java    From spring-cloud-zookeeper with Apache License 2.0 4 votes vote down vote up
public ZookeeperServiceRegistry(
		ServiceDiscovery<ZookeeperInstance> serviceDiscovery) {
	this.serviceDiscovery = serviceDiscovery;
}
 
Example #26
Source File: ZooKeeperClientAutoConfiguartion.java    From Moss with Apache License 2.0 4 votes vote down vote up
@Bean
public ZooKeeperAutoRegistrationCustomizer zooKeeperAutoRegistrationCustomizer(CuratorFramework curator,
                                                                               ZookeeperDiscoveryProperties properties,
                                                                               InstanceSerializer<ZookeeperInstance> serializer) {
    return new ZooKeeperAutoRegistrationCustomizer(curator, properties, serializer);
}
 
Example #27
Source File: ZooKeeperAutoRegistrationCustomizer.java    From Moss with Apache License 2.0 4 votes vote down vote up
public ZooKeeperAutoRegistrationCustomizer(CuratorFramework curator, ZookeeperDiscoveryProperties properties, InstanceSerializer<ZookeeperInstance> serializer) {
    super(curator, properties, serializer);
}
 
Example #28
Source File: ZooKeeperClientAutoConfiguartion.java    From Moss with Apache License 2.0 4 votes vote down vote up
@Bean
public ZooKeeperAutoRegistrationCustomizer zooKeeperAutoRegistrationCustomizer(CuratorFramework curator,
                                                                               ZookeeperDiscoveryProperties properties,
                                                                               InstanceSerializer<ZookeeperInstance> serializer) {
    return new ZooKeeperAutoRegistrationCustomizer(curator, properties, serializer);
}
 
Example #29
Source File: ZooKeeperAutoRegistrationCustomizer.java    From Moss with Apache License 2.0 4 votes vote down vote up
public ZooKeeperAutoRegistrationCustomizer(CuratorFramework curator, ZookeeperDiscoveryProperties properties, InstanceSerializer<ZookeeperInstance> serializer) {
    super(curator, properties, serializer);
}
 
Example #30
Source File: CamelCloudZookeeperServiceRegistryTest.java    From camel-spring-boot with Apache License 2.0 4 votes vote down vote up
@Test
public void testServiceRegistry() throws Exception {
    final ZookeeperServer server = new ZookeeperServer(temporaryFolder.newFolder(testName.getMethodName()));

    ConfigurableApplicationContext context = new SpringApplicationBuilder(TestConfiguration.class)
        .web(WebApplicationType.NONE)
        .run(
            "--debug=false",
            "--spring.main.banner-mode=OFF",
            "--spring.application.name=" + UUID.randomUUID().toString(),
            "--ribbon.enabled=false",
            "--ribbon.eureka.enabled=false",
            "--management.endpoint.enabled=false",
            "--spring.cloud.zookeeper.enabled=true",
            "--spring.cloud.zookeeper.connect-string=" + server.connectString(),
            "--spring.cloud.zookeeper.config.enabled=false",
            "--spring.cloud.zookeeper.discovery.enabled=true",
            "--spring.cloud.service-registry.auto-registration.enabled=false",
            "--camel.cloud.service-registry.service-host=" + SERVICE_HOST
        );

    try {
        final ServiceDiscovery client = context.getBean(ServiceDiscovery.class);
        final ServiceRegistry registry = context.getBean(ServiceRegistry.class);

        registry.register(
            DefaultServiceDefinition.builder()
                .withHost(SERVICE_HOST)
                .withPort(SERVICE_PORT)
                .withName(SERVICE_NAME)
                .withId(SERVICE_ID)
                .build()
        );

        Collection<ServiceInstance<ZookeeperInstance>> services = client.queryForInstances(SERVICE_NAME);
        
        assertThat(services).hasSize(1);
        assertThat(services).first().hasFieldOrPropertyWithValue("address", SERVICE_HOST);
        assertThat(services).first().hasFieldOrPropertyWithValue("port", SERVICE_PORT);
        assertThat(services).first().extracting("payload").hasFieldOrPropertyWithValue("id", SERVICE_ID);
        assertThat(services).first().extracting("payload").hasFieldOrPropertyWithValue("name", SERVICE_NAME);

    } finally {
        // shutdown spring context
        context.close();

        // shutdown zookeeper
        server.shutdown();
    }
}