spring-cloud / spring-cloud-consul

Spring Cloud Consul
http://cloud.spring.io/spring-cloud-consul/
Apache License 2.0
813 stars 541 forks source link

Fixed the problem of creating ConsulServiceInstance #773

Closed manzhizhen closed 1 year ago

manzhizhen commented 2 years ago

When I registered a "myApp" service on consumer, because I didn't set the port number, spring cloud consumer discovery threw a NullPointerException when reading the service and creating a DefaultServiceInstance. Later, I found that it was because the port of the DefaultServiceInstance constructor was of type "int" rather than "Integer", so when our port was null, the reading of the service would fail, Therefore, I think spring cloud consumer discovery should filter out such illegal instances and ensure that other instances of the service are still read successfully.

Here I attach the exception stack information:

java.lang.NullPointerException: null
    at org.springframework.cloud.consul.discovery.ConsulServiceInstance.<init>(ConsulServiceInstance.java:37) ~[spring-cloud-consul-discovery-3.0.4.jar:3.0.4]
    at org.springframework.cloud.consul.discovery.reactive.ConsulReactiveDiscoveryClient.lambda$getInstances$0(ConsulReactiveDiscoveryClient.java:69) ~[spring-cloud-consul-discovery-3.0.4.jar:3.0.4]
    at reactor.core.publisher.FluxDefer.subscribe(FluxDefer.java:46) ~[reactor-core-3.4.14.jar:3.4.14]

Here is also a screenshot of the Consul service registration: image

image

spencergibb commented 1 year ago

bb8e674acbc677409d19bba85c48db23df2b0a8e made it so port is never null so NPE is avoided.