在我的应用程序中,我必须从列表中知道哪些服务器地址已启动。我找到的解决方案是从 Spring-Boot Actuator 调用每个健康端点。JSon 响应是:
{
"status": "UP"
}
Run Code Online (Sandbox Code Playgroud)
在应用程序的其他部分,我使用 Spring-Cloud 中通过@FeignClient注释定义的 Feign 客户端,效果非常好:
@FeignClient(
name = "tokenProxy",
url = "${host}:${port}"
)
Run Code Online (Sandbox Code Playgroud)
不幸的是,这种配置不允许重复使用相同的客户端来调用不同地址上的相同端点。所以我必须定义我自己的自定义客户端(如果有其他解决方案,请随时告诉我!):
@GetMapping(
value = "/servers"
)
public Server discover() {
MyClient myClient = Feign.builder()
.target(
Target.EmptyTarget.create(
MyClient.class
)
);
return myClient.internalPing(URI.create("http://localhost:8090"));
}
interface MyClient {
@RequestLine("GET /actuator/health")
Server internalPing(URI baseUrl);
}
class Server {
private final String status;
@JsonCreator
public Server(@JsonProperty("status") String status) {
this.status = status;
}
public String getStatus() {
return status;
}
} …Run Code Online (Sandbox Code Playgroud)