I encountered a strange behaviour, when I set management and server port to 0 so I can easily start a few local instances, then the eureka client registers the service with the management port.
My Setup: Spring Boot 1.5.10 + Edgeware.SR2
- BookmarksService with server+management port = 0
- Eureka Server
- Zuul: Forwards all requests to BookmarksService instances
Using a fixed IP list for ribbon works. Enabling DiscoveryClient in the bookmarksservice and Zuul leads to JSON 404 error messages produced by the BookmarksService. After a long while of searching I found that the service name seems to be resolved with the management port instead of the correct one.
I have verified that using the correct port without zuul/ribbon the correct result is returned and using the management port I see the same JSON error message that I see when I am using json.
This can be fixed by not using a management port. But that requires additional access control so no one accesses the actuator stuff via zuul.
This is the relevant part of the application.yaml with a management port set
server.port: 0
management:
port: 10000
context-path: /internal
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
instance:
instance-id: ${spring.application.name}:${random.value}
I also tried to set the management port to a fixed 10000 and this is the result in eureka:
<application>
<name>BOOKMARKS</name>
<instance>
<instanceId>bookmarks:96a084044a99b3ea5ce02b05f0c19619</instanceId>
<hostName>192.168.2.20</hostName>
<app>BOOKMARKS</app>
<ipAddr>192.168.2.20</ipAddr>
<status>UP</status>
<overriddenstatus>UNKNOWN</overriddenstatus>
<port enabled="true">10000</port>
<securePort enabled="false">443</securePort>
<countryId>1</countryId>
<dataCenterInfo class="com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo">
<name>MyOwn</name>
</dataCenterInfo>
<leaseInfo>
<renewalIntervalInSecs>30</renewalIntervalInSecs>
<durationInSecs>90</durationInSecs>
<registrationTimestamp>1518881421504</registrationTimestamp>
<lastRenewalTimestamp>1518881421504</lastRenewalTimestamp>
<evictionTimestamp>0</evictionTimestamp>
<serviceUpTimestamp>1518881420997</serviceUpTimestamp>
</leaseInfo>
<metadata>
<management.port>10000</management.port>
<jmx.port>60862</jmx.port>
</metadata>
<homePageUrl>http://192.168.2.20:10000/</homePageUrl>
<statusPageUrl>http://192.168.2.20:10000/internal/info</statusPageUrl>
<healthCheckUrl>http://192.168.2.20:10000/internal/health</healthCheckUrl>
<vipAddress>bookmarks</vipAddress>
<secureVipAddress>bookmarks</secureVipAddress>
<isCoordinatingDiscoveryServer>false</isCoordinatingDiscoveryServer>
<lastUpdatedTimestamp>1518881421504</lastUpdatedTimestamp>
<lastDirtyTimestamp>1518881420965</lastDirtyTimestamp>
<actionType>ADDED</actionType>
</instance>
When I set the server port to a fixed port the problem goes away:
<application>
<name>BOOKMARKS</name>
<instance>
<instanceId>bookmarks:1a0adf92cef53c159e443686a76bc19c</instanceId>
<hostName>192.168.2.20</hostName>
<app>BOOKMARKS</app>
<ipAddr>192.168.2.20</ipAddr>
<status>UP</status>
<overriddenstatus>UNKNOWN</overriddenstatus>
<port enabled="true">9000</port>
<securePort enabled="false">443</securePort>
<countryId>1</countryId>
<dataCenterInfo class="com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo">
<name>MyOwn</name>
</dataCenterInfo>
<leaseInfo>
<renewalIntervalInSecs>30</renewalIntervalInSecs>
<durationInSecs>90</durationInSecs>
<registrationTimestamp>1518881585038</registrationTimestamp>
<lastRenewalTimestamp>1518881585038</lastRenewalTimestamp>
<evictionTimestamp>0</evictionTimestamp>
<serviceUpTimestamp>1518881585038</serviceUpTimestamp>
</leaseInfo>
<metadata>
<management.port>10000</management.port>
<jmx.port>61698</jmx.port>
</metadata>
<homePageUrl>http://192.168.2.20:9000/</homePageUrl>
<statusPageUrl>http://192.168.2.20:10000/internal/info</statusPageUrl>
<healthCheckUrl>http://192.168.2.20:10000/internal/health</healthCheckUrl>
<vipAddress>bookmarks</vipAddress>
<secureVipAddress>bookmarks</secureVipAddress>
<isCoordinatingDiscoveryServer>false</isCoordinatingDiscoveryServer>
<lastUpdatedTimestamp>1518881585038</lastUpdatedTimestamp>
<lastDirtyTimestamp>1518881585009</lastDirtyTimestamp>
<actionType>ADDED</actionType>
</instance>
Maybe the documentation should state somewhere that eureka discovery doesn't work with randomized ports. This would save a lot of people a lot of Time, I think ;-) And as contrast to #812, this time I don't have the config server (yet)
I was hoping that the problem was solved already. Turns out https://stackoverflow.com/questions/29637324/spring-cloud-random-port-not-registered-to-eureka is still partially valid. (Although it works with a random port when no management port is set)
Comment From: ryanjbaxter
The problem comes down to this code https://github.com/spring-cloud/spring-cloud-netflix/blob/1.4.x/spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/serviceregistry/EurekaAutoServiceRegistration.java#L122
This event is fired twice when both the management and server port are set. However we dont distinguish the events so the first one to fire wins. It seems that it is always the management port but IDK if that is always the case.
@spencergibb @dsyer do one of you know another way of accessing the port reliably?
Comment From: spencergibb
AFAIK, besides having the port set via server.port, that is the most reliable way to do it. We may need to differentiate between the management context and the normal context.
Comment From: SImgrund
I've got the same behaviour using Spring Boot 2.0.5.RELEASE Spring Cloud Finchley.SR1
#spring actuator config
management:
server:
port: 9001
security.enabled: false # whether security should be enabled or disabled altogether
endpoints.beans.enabled: false # disable beans endpoint
endpoints.web.exposure.include: "*" # expose all web endpoints
spring:
application:
name: xxx-service
server:
port: 0
eureka:
client:
serviceUrl:
defaultZone: ${EUREKA_URI:http://localhost:8761/eureka}
instance:
preferIpAddress: true
2018-10-12 12:10:30.360 INFO 3348 --- [ restartedMain] com.netflix.discovery.DiscoveryClient : Disable delta property : false
2018-10-12 12:10:30.360 INFO 3348 --- [ restartedMain] com.netflix.discovery.DiscoveryClient : Single vip registry refresh property : null
2018-10-12 12:10:30.361 INFO 3348 --- [ restartedMain] com.netflix.discovery.DiscoveryClient : Force full registry fetch : false
2018-10-12 12:10:30.361 INFO 3348 --- [ restartedMain] com.netflix.discovery.DiscoveryClient : Application is null : false
2018-10-12 12:10:30.364 INFO 3348 --- [ restartedMain] com.netflix.discovery.DiscoveryClient : Registered Applications size is zero : true
2018-10-12 12:10:30.367 INFO 3348 --- [ restartedMain] com.netflix.discovery.DiscoveryClient : Application version is -1: true
2018-10-12 12:10:30.373 INFO 3348 --- [ restartedMain] com.netflix.discovery.DiscoveryClient : Getting all instance registry info from the eureka server
2018-10-12 12:10:30.840 INFO 3348 --- [ restartedMain] com.netflix.discovery.DiscoveryClient : The response status is 200
2018-10-12 12:10:30.847 INFO 3348 --- [ restartedMain] com.netflix.discovery.DiscoveryClient : Starting heartbeat executor: renew interval is: 30
2018-10-12 12:10:30.859 INFO 3348 --- [ restartedMain] c.n.discovery.InstanceInfoReplicator : InstanceInfoReplicator onDemand update allowed rate per min is 4
2018-10-12 12:10:30.873 INFO 3348 --- [ restartedMain] com.netflix.discovery.DiscoveryClient : Discovery Client initialized at timestamp 1539346230869 with initial instances count: 2
2018-10-12 12:10:30.886 INFO 3348 --- [ restartedMain] o.s.c.n.e.s.EurekaServiceRegistry : Registering application xxx-service with eureka with status UP
2018-10-12 12:10:30.894 INFO 3348 --- [ restartedMain] com.netflix.discovery.DiscoveryClient : Saw local status change event StatusChangeEvent [timestamp=1539346230894, current=UP, previous=STARTING]
2018-10-12 12:10:30.921 INFO 3348 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_XXX-SERVICE/10.0.75.1:xxx-service:0: registering service...
2018-10-12 12:10:30.926 INFO 3348 --- [ restartedMain] o.s.c.support.DefaultLifecycleProcessor : Starting beans in phase 0
2018-10-12 12:10:31.009 INFO 3348 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 62248 (http) with context path ''
2018-10-12 12:10:31.018 INFO 3348 --- [ restartedMain] d.l.u.Application : Started Application in 32.691 seconds (JVM running for 34.399)
2018-10-12 12:10:31.127 INFO 3348 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_XXX-SERVICE/10.0.75.1:xxx-service:0 - registration status: 204
2018-10-12 12:15:30.379 INFO 3348 --- [trap-executor-0] c.n.d.s.r.aws.ConfigClusterResolver : Resolving eureka endpoints via configuration
<application>
<name>XXX-SERVICE</name>
<instance>
<instanceId>10.0.75.1:xxx-service:0</instanceId>
<hostName>10.0.75.1</hostName>
<app>XXX-SERVICE</app>
<ipAddr>10.0.75.1</ipAddr>
<status>UP</status>
<overriddenstatus>UNKNOWN</overriddenstatus>
<port enabled="true">9001</port>
<securePort enabled="false">443</securePort>
<countryId>1</countryId>
<dataCenterInfo class="com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo">
<name>MyOwn</name>
</dataCenterInfo>
<leaseInfo>
<renewalIntervalInSecs>30</renewalIntervalInSecs>
<durationInSecs>90</durationInSecs>
<registrationTimestamp>1539346231641</registrationTimestamp>
<lastRenewalTimestamp>1539346231641</lastRenewalTimestamp>
<evictionTimestamp>0</evictionTimestamp>
<serviceUpTimestamp>1539340245318</serviceUpTimestamp>
</leaseInfo>
<metadata>
<management.port>9001</management.port>
</metadata>
<homePageUrl>http://10.0.75.1:9001/</homePageUrl>
<statusPageUrl>http://10.0.75.1:9001/actuator/info</statusPageUrl>
<healthCheckUrl>http://10.0.75.1:9001/actuator/health</healthCheckUrl>
<vipAddress>xxx-service</vipAddress>
<secureVipAddress>xxx-service</secureVipAddress>
<isCoordinatingDiscoveryServer>false</isCoordinatingDiscoveryServer>
<lastUpdatedTimestamp>1539346231641</lastUpdatedTimestamp>
<lastDirtyTimestamp>1539346230894</lastDirtyTimestamp>
<actionType>ADDED</actionType>
</instance>
</application>
Is there any news about the issue?
Comment From: ryanjbaxter
Nope nothing yet
Comment From: pcornelissen
FYI: @SImgrund you use deprecated config keys, for example the security enabled thing which does nothing anymore.
Comment From: arittner
The solution would be very simple:
org.springframework.cloud.netflix.eureka.serviceregistry.EurekaAutoServiceRegistration
@EventListener(WebServerInitializedEvent.class)
public void onApplicationEvent(WebServerInitializedEvent event) {
// TODO: take SSL into account
String contextName = event.getApplicationContext ().getNamespace ();
if ( StringUtils.isEmpty ( contextName ) ) {
int localPort = event.getWebServer().getPort();
if (this.port.get() == 0) {
log.info("Updating port to " + localPort);
this.port.compareAndSet(0, localPort);
start();
}
}
}
A workaround is to disable the auto-registration and implement a fixed EurekaAutoServiceRegistration. But this is not very nice, because we have also to register EurekaRegistration.
It would be helpful to add spring boot conditionals for missing beans for EurekaRegistration and EurekaAutoServiceRegistration. This makes a patch a little bit easier.
Comment From: spencergibb
PRs welcome
Comment From: chriswhite199
The following AOP patches the issue while we wait for the PR to be accepted:
@Configuration
@Aspect
public class EurekaIssue2732ClientConfig {
@Around("execution(void org.springframework.cloud.netflix.eureka.serviceregistry.EurekaAutoServiceRegistration.onApplicationEvent(*))")
public void aroundAppEvent(ProceedingJoinPoint joinPoint) throws Throwable {
ApplicationEvent event = (ApplicationEvent) joinPoint.getArgs()[0];
if (event instanceof WebServerInitializedEvent) {
WebServerInitializedEvent webEvent = (WebServerInitializedEvent) event;
if (StringUtils.isEmpty(webEvent.getApplicationContext().getServerNamespace())) {
joinPoint.proceed();
}
} else {
joinPoint.proceed();
}
}
}
Comment From: erwannleroy
Hi, I know it's closed, but it's not working for me : - spring-boot-starter-parent 2.2.5.RELEASE - spring cloud : Hoxton.SR3 - eureka 2.2.2.RELEASE
Eureka is still registering another randomized port.
Comment From: erwannleroy
Hi, I know it's closed, but it's not working for me :
- spring-boot-starter-parent 2.2.5.RELEASE
- spring cloud : Hoxton.SR3
- eureka 2.2.2.RELEASE
Eureka is still registering another randomized port.
Ok I got it. I didn't use 'server.port: 0" but "server.port: ${random.int(20002,29999)}"
The fix is not working for this kind of expression.