【问题标题】:Why does Micronaut discard everything after hostname for client url configurations?为什么 Micronaut 会丢弃客户端 url 配置的主机名之后的所有内容?
【发布时间】:2020-08-16 12:57:02
【问题描述】:

在 application.yaml 中配置 Micronaut Http 客户端时,我不知道 url 只能是主机名和端口,如果包含的不仅仅是主机名和端口,那么它会在 Micronaut 选择要调用的主机时被丢弃。 Micronaut Configuring Http Clients 文档没有明确警告读者这种行为。

这是预期的行为吗?如果是这样,那么对于处理长 URI 的用户有哪些选择,即: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

当使用好客户端时,它会按预期工作,但坏客户端会抛出“404 Page Not Found”,并带有以下日志详细信息:

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

【问题讨论】:

    标签: java microservices micronaut


    【解决方案1】:

    URL 应该只是没有路径的 URL。如果你想提供一个上下文路径,你可以通过 config 来实现:

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

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

    【讨论】:

    • 非常感谢!我似乎太快地浏览了配置参考太多次了,因为我想弄清楚我做错了什么,我错过了路径选项。
    猜你喜欢
    • 1970-01-01
    • 2011-01-01
    • 2016-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 2016-06-18
    相关资源
    最近更新 更多