spring-cloud / spring-cloud-zookeeper

Spring Cloud Zookeeper
http://cloud.spring.io/spring-cloud-zookeeper/
Apache License 2.0
555 stars 413 forks source link

Spring Cloud ZooKeeper Discovery Client Not Register on ZooKeeper when using SpringBootServletInitializer #158

Open xiangyq000 opened 6 years ago

xiangyq000 commented 6 years ago

I have a spring boot application that use spring-cloud-stater-zookeeper-discovery to register the service, but it doesn't work.

  1. the application is deployed as a WAR.

    @SpringBootConfiguration
    @EnableAutoConfiguration
    @EnableDiscoveryClient
    public class MyApplication extends SpringBootServletInitializer {
    
        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
            return application.sources(MyApplication.class)
                    .properties("spring.cloud.zookeeper.discovery.instance-host=" + getHostName())
                    .web(true);
        }
    }
    
  2. the application has connected to the zookeeper instance. Ping messages to zookeeper can be seen in the application's log and the output of STAT command shows a connected zookeeper client.

  3. the application does not register watchers to appointed zookeeper path, i.e., /services. This can be confirmed by sending WCHC command to the zookeeper.

Spring Boot version: 1.5.9

spring-cloud-stater-zookeeper-discovery: 1.2.0.RELEASE

web container: tomcat 8.5.23

spencergibb commented 6 years ago

There is a similar problem in consul

iurii-dziuban-onpex commented 6 years ago

Any workaround on this?

spencergibb commented 6 years ago

Maybe

    @EventListener(ApplicationStartedEvent.class)
    public void init(ApplicationStartedEvent event) {
        ConfigurableApplicationContext context = event.getApplicationContext();
        ZookeeperRegistration registration = context.getBean(ZookeeperRegistration.class);
        registration.setPort(9080);
        ZookeeperAutoServiceRegistration serviceRegistration = context.getBean(ZookeeperAutoServiceRegistration.class);
        serviceRegistration.start();
    }
rodoc81 commented 5 years ago

I got the same issue with newer version:

I used suggested solution above by @spencergibb . I'm using properties to set the port so don't need the first part.

@Autowired private ZookeeperAutoServiceRegistration serviceRegistration;

@EventListener(ApplicationStartedEvent.class)
public void init(final ApplicationStartedEvent event) {
    serviceRegistration.start();
}
nicqchen commented 5 years ago

I have same issues, any one could help on this?

nicqchen commented 5 years ago
2019-01-24 07:15:43.006 ERROR 1 --- [nStateManager-0] o.a.c.x.d.details.ServiceDiscoveryImpl   : Could not re-register instances after reconnection

java.lang.IllegalStateException: instance must be started before calling this method
        at org.apache.curator.shaded.com.google.common.base.Preconditions.checkState(Preconditions.java:444) ~[curator-client-4.0.1.jar!/:na]
        at org.apache.curator.framework.imps.CuratorFrameworkImpl.create(CuratorFrameworkImpl.java:416) ~[curator-framework-4.0.1.jar!/:4.0.1]
        at org.apache.curator.x.discovery.details.ServiceDiscoveryImpl.internalRegisterService(ServiceDiscoveryImpl.java:236) ~[curator-x-discovery-4.0.1.jar!/:na]
        at org.apache.curator.x.discovery.details.ServiceDiscoveryImpl.reRegisterServices(ServiceDiscoveryImpl.java:456) ~[curator-x-discovery-4.0.1.jar!/:na]
        at org.apache.curator.x.discovery.details.ServiceDiscoveryImpl.access$100(ServiceDiscoveryImpl.java:58) ~[curator-x-discovery-4.0.1.jar!/:na]
        at org.apache.curator.x.discovery.details.ServiceDiscoveryImpl$1.stateChanged(ServiceDiscoveryImpl.java:78) ~[curator-x-discovery-4.0.1.jar!/:na]
        at org.apache.curator.framework.state.ConnectionStateManager$2.apply(ConnectionStateManager.java:274) [curator-framework-4.0.1.jar!/:4.0.1]
        at org.apache.curator.framework.state.ConnectionStateManager$2.apply(ConnectionStateManager.java:270) [curator-framework-4.0.1.jar!/:4.0.1]
        at org.apache.curator.framework.listen.ListenerContainer$1.run(ListenerContainer.java:93) [curator-framework-4.0.1.jar!/:4.0.1]
        at org.apache.curator.shaded.com.google.common.util.concurrent.MoreExecutors$DirectExecutor.execute(MoreExecutors.java:435) [curator-client-4.0.1.jar!/:na]
        at org.apache.curator.framework.listen.ListenerContainer.forEach(ListenerContainer.java:85) [curator-framework-4.0.1.jar!/:4.0.1]
        at org.apache.curator.framework.state.ConnectionStateManager.processEvents(ConnectionStateManager.java:268) [curator-framework-4.0.1.jar!/:4.0.1]
        at org.apache.curator.framework.state.ConnectionStateManager.access$000(ConnectionStateManager.java:44) [curator-framework-4.0.1.jar!/:4.0.1]
        at org.apache.curator.framework.state.ConnectionStateManager$1.call(ConnectionStateManager.java:120) [curator-framework-4.0.1.jar!/:4.0.1]
        at java.util.concurrent.FutureTask.run(FutureTask.java:266) [na:1.8.0_111-internal]
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_111-internal]
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_111-internal]
        at java.lang.Thread.run(Thread.java:745) [na:1.8.0_111-internal]
zhuangjinjin commented 5 years ago

it seem that problem was here in EventListenerMethodProcessor, it does't work on SpringContainerClass who annotated @EventListner

private static boolean isSpringContainerClass(Class<?> clazz) {
    return (clazz.getName().startsWith("org.springframework.") && !AnnotatedElementUtils.isAnnotated(ClassUtils.getUserClass(clazz), Component.class));
}

private void processBean(final String beanName, final Class<?> targetType) {
    if (!this.nonAnnotatedClasses.contains(targetType) && !isSpringContainerClass(targetType)) {
        ...
        //context#addApplicationListener
    }
}
venkateshzifo commented 5 years ago

Hi,

Any update on this one ? I am facing the same issue when deploy the application as WAR.

marcingrzejszczak commented 5 years ago

If there are no comments then there are no updates

venkateshzifo commented 5 years ago

Is there any other workaround ?

spencergibb commented 5 years ago

Instead of @EventListener

class MyClass implements ApplicationListener<ApplicationStartedEvent> {
  public void onApplicationEvent(ApplicationStartedEvent event) {
    serviceRegistration.start();
  }
}
sqnczzl commented 4 years ago

I have the same issuse

weihubeats commented 2 years ago

I also encountered a similar problem, mainly caused by the inconsistency of the ZooKeeper server and java client versions. Changing to a consistent version can solve the problem