spring-cloud-netflix icon indicating copy to clipboard operation
spring-cloud-netflix copied to clipboard

getURI() in EurekaServiceInstance misses custom contextPath.

Open thomasdarimont opened this issue 10 years ago • 56 comments

I have several Spring Boot Apps with REST endpoints with custom server.contextPath's which use @EnableDiscoveryClient to register themselves in the eureka service registry.

The applications are registered with eureka at startup and later queried by a management application. I just found out that the URIs returned by

org.springframework.cloud.netflix.eureka.EurekaDiscoveryClient.EurekaServiceInstance.getUri()

only contain scheme,hostname and port but no custom context-path.

The URIs were returned by discoveryClient.getInstances(serviceId) where discoveryClient is an EurekaDiscoveryClient.

The URI one gets via

public URI getUri() {
  return DefaultServiceInstance.getUri(this);
}

looks like:

http://localhost:8761

The proper URI would be:

http://localhost:8761/mycontextpath/

I fixed tested this by locally shadowing the org.springframework.cloud.netflix.eureka.EurekaDiscoveryClient and replacing the body of the URI getUri() method with:

@Override
public URI getUri() {

  //HACK to get proper URI with context-path back
  //return DefaultServiceInstance.getUri(this);
  return URI.create(this.instance.getHomePageUrl());
}

With that adjustment the org.springframework.cloud.client.ServiceInstance that I got back returned the proper service URIs.

I think the op of the issue #500 has the same problem.

Another Problem was that I needed to explicitly declare a

@Bean
public EurekaInstanceConfig eurekaConfigBean() {
  return new EurekaInstanceConfigBean();
}

in order to get custom eureka instance settings in an applications.yml file taken into account at all but I guess that's for another issue ;-)

server:
  port: 9999
  context-path: /admin
...
eureka:
  instance:
    statusPageUrlPath: /admin/manage/info
    homePageUrlPath: /admin/
    healthCheckUrlPath: /admin/manage/health
    non-secure-port: ${server.port:9999}
    ...

thomasdarimont avatar Dec 18 '15 23:12 thomasdarimont

We've not extended eureka to know about an instances context path yet.

spencergibb avatar Mar 03 '16 01:03 spencergibb

Hi,

When will it be available to download?

cbelleza avatar Mar 14 '16 21:03 cbelleza

@ofbizbrazil it hasn't been done, until it has, it won't be available to download.

spencergibb avatar Mar 14 '16 22:03 spencergibb

@spencergibb,

My applications have context-path at uri, is there any workaround until this is done?

cbelleza avatar Mar 14 '16 22:03 cbelleza

https://github.com/spring-cloud/spring-cloud-netflix/issues/737#issue-123043546

spencergibb avatar Mar 14 '16 22:03 spencergibb

Hi @spencergibb, of course i saw that comment, but my doubt is how to apply this 'locally shadowing' mentioned in there.

I guess the spring package was patched to overwrite the class EurekaDiscoveryClient. Do you know how to do it?

cbelleza avatar Mar 15 '16 13:03 cbelleza

Hi @ofbizbrazil,

(as a quick hack) just copy the org.springframework.cloud.netflix.eureka.EurekaDiscoveryClient in your local source folder (e.g. src/main/java) with the same package structure and change the method in question as described. Your classes take precedence over classes from additional libraries.

Cheers, Thomas

thomasdarimont avatar Mar 15 '16 13:03 thomasdarimont

That's I was afraid of, doing a patch into my project to handle the class EurekaDiscoveryClient.

cbelleza avatar Mar 15 '16 13:03 cbelleza

Yep - same here - I just used this to verify what a potential solution might be... ;-)

thomasdarimont avatar Mar 15 '16 13:03 thomasdarimont

What's the use case? Why do you need this?

spencergibb avatar Mar 15 '16 13:03 spencergibb

Nowadays we are limiting an application to only use a root context, and if I want to change the context-path to a different name, EurekaDiscoveryClient simply does work and nobody will know about it.

So, in my opinion that's a critical issue, taking in mind Spring Netflix only works for a specific URI.

cbelleza avatar Mar 15 '16 13:03 cbelleza

I guess I think it's strange that you would change the context of a service and not expect clients to change. This has been marked as Help Wanted. Pull requests are welcome.

spencergibb avatar Mar 15 '16 14:03 spencergibb

My micro-service uses a custom context-path and the clients which uses it really works fine.

The issue here is EurekaDiscoveryClient does support context-path when the method getUri() builds the URI. There should be the methods getContextPath or getHomePageUrl in EurekaServiceInstance to allow this resource.

cbelleza avatar Mar 15 '16 14:03 cbelleza

@spencergibb I wanted to do a fix on this fella the other day When I modified the EurekaDiscoveryClient.getUri to what @thomasdarimont suggested, DiscoveryClientConfigServiceAutoConfigurationTests fails on onWhenRequested because DiscoveryClientConfigServiceBootstrapConfiguration sets the homePage as follows

private String getHomePage(ServiceInstance server) {
        return server.getUri().toString() + "/";
 }

So, I think if it is decided to change the EurekaDiscoveryClient.getUri, to modify in spring-cloud-config the homePage

sfat avatar Apr 20 '16 10:04 sfat

Hi, I ran into the same problem and looks like the current implementation assumes only one eureka client application per JVM (web)container. Is this a restriction being imposed ? If so , then it needs to be documented else it needs to be fixed to include a configured context-path as part of the look ups

kk-manh avatar Apr 20 '16 18:04 kk-manh

is ther any news about this matter?

cbelleza avatar Jun 15 '16 02:06 cbelleza

Is this still the case? I´m running in to this problem at the moment using the default ribbon configuration.

dilgerma avatar Oct 15 '16 06:10 dilgerma

I am curious. Has there been any update to this issue? Thanks!

cjemison avatar Jan 06 '17 21:01 cjemison

No update. It's not high on our priorities since it can be done with properties. Pull requests welcome.

spencergibb avatar Jan 09 '17 20:01 spencergibb

Use case for this issue: We have a java application deployed on Tomcat with a context root. The urls look like '/myapp/url_path_in_the_application. Now we have also deploy the application in on Docker. Now the urls are the same, but without the context root '/myapp', so only '/url_path_in_the_application'.

We are looking for a way that we can register the old version with context root '/myapp' and the new version with '/' as context root. For the client the invocation is easy base_url + '/url_path_in_the_application'. Base_url is created based on the registration in Eureka.

cbos avatar Jan 20 '17 12:01 cbos