在我的应用程序中,我必须从列表中知道一个已启动的服务器地址。我发现的解决方案是从Spring-Boot Actuator中为每个调用健康端点。 JSon的响应是:
{
"status": "UP"
}
在应用程序的其他部分,我使用来自Spring-Cloud的Feign客户端,该客户端通过@FeignClient
注释定义,效果很好:
@FeignClient(
name = "tokenProxy",
url = "${host}:${port}"
)
遗憾的是,这种配置不允许重复使用同一客户端在不同地址上调用同一端点。因此,我必须定义自己的自定义客户端(如果还有其他解决方案,请不要犹豫告诉我!):
@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;
}
}
当我调用端点/servers
时,出现以下错误,表明我的自定义Feign客户端未与适当的解码器混淆:
feign.codec.DecodeException: class com.xxx.web.Server is not a type supported by this decoder.
at feign.codec.StringDecoder.decode(StringDecoder.java:34) ~[feign-core-10.10.1.jar:na]
at feign.codec.Decoder$Default.decode(Decoder.java:92) ~[feign-core-10.10.1.jar:na]
at feign.AsyncResponseHandler.decode(AsyncResponseHandler.java:115) ~[feign-core-10.10.1.jar:na]
at feign.AsyncResponseHandler.handleResponse(AsyncResponseHandler.java:87) ~[feign-core-10.10.1.jar:na]
at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:138) ~[feign-core-10.10.1.jar:na]
我想我应该使用JacksonDecoder,但是我在Spring-Cloud Hoxton.SR5
的依赖项中找不到它:
<dependencies>
...
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
...
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencyManagement>
有人可以通过我的需要提供更好的解决方案,或者提供有关如何正确配置自定义Feign客户端的说明来帮助我吗?
预先感谢
实际上,使用spring-cloud依赖关系时,默认情况下未加载包含Jackson解码器和编码器的库。要解决此问题,我只需将以下内容添加到我的pom.xml
文件中:
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-jackson</artifactId>
</dependency>