为什么 Micronaut 会丢弃客户端 url 配置的主机名之后的所有内容?
Why does Micronaut discard everything after hostname for client url configurations?
在 application.yaml 中配置 Micronaut Http 客户端时,我不知道 url 只能是主机名和端口,如果包含主机名和端口以外的任何内容,那么它会被 Micronaut 丢弃当它选择要呼叫的主机时。 Micronaut Configuring Http Clients 文档没有明确警告 reader 关于此行为。
这种行为是预期的吗?如果是这样,那么对于处理长 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 页面未找到”并包含以下日志详细信息:
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
URL应该只是没有路径的URL。如果你想提供一个上下文路径,你可以通过 config:
micronaut:
http:
services:
bad-client:
urls:
- http://localhost:8080
path: /server2
在 application.yaml 中配置 Micronaut Http 客户端时,我不知道 url 只能是主机名和端口,如果包含主机名和端口以外的任何内容,那么它会被 Micronaut 丢弃当它选择要呼叫的主机时。 Micronaut Configuring Http Clients 文档没有明确警告 reader 关于此行为。
这种行为是预期的吗?如果是这样,那么对于处理长 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 页面未找到”并包含以下日志详细信息:
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
URL应该只是没有路径的URL。如果你想提供一个上下文路径,你可以通过 config:
micronaut:
http:
services:
bad-client:
urls:
- http://localhost:8080
path: /server2