Search code examples
javamicroservicesmicronaut

Why does Micronaut discard everything after hostname for client url configurations?


When configuring Micronaut Http Clients in the application.yaml I was unaware the url must only be the hostname and port, if anything more than the hostname and port is included then it is discarded by Micronaut when it chooses the host to call. The Micronaut Configuring Http Clients documentation doesn't explicitly warn the reader about this behavior.

Is this behavior expected? If so then what options are there for users who are dealing with long URIs, i.e.: http://localhost:8080/application-name/v1.0/controller-name/endpoint

Demo Code

BadClient.java

@Client("bad-client")
public interface BadClient {
    @Get("/hello")
    Maybe<String> getHello();
}

GoodClient.java

@Client("good-client")
public interface GoodClient {
    @Get("/server2/hello")
    Maybe<String> getHello();
}

application.yaml

micronaut:
  application:
    name: micronaut-client-issues
  http:
    services:
      bad-client:
        urls:
          - http://localhost:8080/server2
      good-client:
        urls:
          - http://localhost:8080
  server:
    port: 8080
    cors:
      enabled: true

When the good-client is used it works as expected, but the bad-client throws a '404 Page Not Found' with the following log details:

18:35:05.927 [nioEventLoopGroup-1-2] DEBUG io.micronaut.context.DefaultBeanContext - Registering singleton bean io.micronaut.http.client.DefaultHttpClient@2b80242 for type [@Named('bad-client') io.micronaut.http.client.HttpClient] using bean key @Named('bad-client') io.micronaut.http.client.DefaultHttpClient
18:35:05.966 [nioEventLoopGroup-1-3] DEBUG io.micronaut.http.client.DefaultHttpClient - Sending HTTP Request: GET /hello
18:35:05.966 [nioEventLoopGroup-1-3] DEBUG io.micronaut.http.client.DefaultHttpClient - Chosen Server: localhost(8080)
18:35:05.985 [nioEventLoopGroup-1-4] DEBUG io.micronaut.http.server.netty.NettyHttpServer - Server localhost:8080 Received Request: GET /hello
18:35:05.985 [nioEventLoopGroup-1-4] DEBUG io.micronaut.http.server.netty.RoutingInBoundHandler - Matching route GET - /hello
18:35:05.985 [nioEventLoopGroup-1-4] DEBUG io.micronaut.http.server.netty.RoutingInBoundHandler - No matching route found for URI /hello and method GET
18:35:05.989 [nioEventLoopGroup-1-4] DEBUG io.micronaut.web.router.RouteMatchUtils - Route match attribute for request (/hello) not found

Solution

  • The URLs are supposed to be just the URL without a path. If you want to supply a context path, you can do so via config:

    micronaut:
      http:
        services:
          bad-client:
            urls:
              - http://localhost:8080
            path: /server2
    

    See https://docs.micronaut.io/latest/guide/configurationreference.html#io.micronaut.http.client.ServiceHttpClientConfiguration