ZookeeperAutoServiceRegistration throws an exception when ServiceInstanceRegistration method getServiceInstance() is called before ZookeeperAutoServiceRegistration method getPort()
It happens because method getServiceInstance() calls build() and when getPort() will be called it will return null instead 0.
|
public ServiceInstance<ZookeeperInstance> getServiceInstance() { |
|
if (this.serviceInstance == null) { |
|
build(); |
|
} |
|
return this.serviceInstance; |
|
} |
|
protected void build() { |
|
this.serviceInstance = this.builder.build(); |
|
} |
|
public int getPort() { |
|
if (this.serviceInstance == null) { |
|
return 0; |
|
} |
|
return this.serviceInstance.getPort(); |
|
} |
|
protected void register() { |
|
if (!this.properties.isRegister()) { |
|
log.debug("Registration disabled."); |
|
return; |
|
} |
|
if (this.registration.getPort() == 0) { |
|
this.registration.setPort(getPort().get()); |
|
} |
|
super.register(); |
|
} |
Also getInstanceId() method returns null.
Use case when we need to call getServiceInstance for example to create leader candidate and associate with registration id for further understanding which node is a leader.
@Bean
public DefaultCandidate bean(ServiceInstanceRegistration registration) {
return return new DefaultCandidate(registration.getServiceInstance().getId(), "default");
}
ZookeeperAutoServiceRegistration throws an exception when ServiceInstanceRegistration method getServiceInstance() is called before ZookeeperAutoServiceRegistration method getPort()
It happens because method
getServiceInstance()callsbuild()and whengetPort()will be called it will return null instead 0.spring-cloud-zookeeper/spring-cloud-zookeeper-discovery/src/main/java/org/springframework/cloud/zookeeper/serviceregistry/ServiceInstanceRegistration.java
Lines 64 to 69 in ec845a4
spring-cloud-zookeeper/spring-cloud-zookeeper-discovery/src/main/java/org/springframework/cloud/zookeeper/serviceregistry/ServiceInstanceRegistration.java
Lines 71 to 73 in ec845a4
spring-cloud-zookeeper/spring-cloud-zookeeper-discovery/src/main/java/org/springframework/cloud/zookeeper/serviceregistry/ServiceInstanceRegistration.java
Lines 83 to 88 in ec845a4
spring-cloud-zookeeper/spring-cloud-zookeeper-discovery/src/main/java/org/springframework/cloud/zookeeper/serviceregistry/ZookeeperAutoServiceRegistration.java
Lines 70 to 79 in ec845a4
Also
getInstanceId()method returns null.Use case when we need to call
getServiceInstancefor example to create leader candidate and associate with registration id for further understanding which node is a leader.