1. Introduction
1.绪论
Spring Cloud Feign Client is a handy declarative REST client, that we use to implement communication between microservices.
Spring Cloud Feign Client是一个方便的声明式REST客户端,我们用它来实现微服务之间的通信。
In this short tutorial, we’ll show how to set a custom Feign Client connection timeout, both globally and per client.
在这个简短的教程中,我们将展示如何设置一个自定义的Feign Client连接超时,包括全局和每个客户端。
2. Defaults
2.默认情况下
Feign Client is pretty configurable.
伪装客户是相当可配置的。
In terms of a timeout, it allows us to configure both read and connection timeouts. Connection timeout is the time needed for the TCP handshake, while the read timeout needed to read data from the socket.
就超时而言,它允许我们配置读取和连接超时。连接超时是TCP握手所需的时间,而读取超时则需要从套接字中读取数据。
Connection and read timeouts are by default 10 and 60 seconds, respectively.
连接和读取超时的时间默认分别为10秒和60秒。
3. Globally
3.在全球范围内
We can set the connection and read timeouts that apply to every Feign Client in the application via the feign.client.config.default property set in our application.yml file:
我们可以通过application.yml文件中设置的feign.client.config.default属性,设置适用于应用程序中每个Feign客户端的连接和读取超时。
feign:
client:
config:
default:
connectTimeout: 60000
readTimeout: 10000
The values represent the number of milliseconds before a timeout occurs.
这些数值代表超时发生前的毫秒数。
4. Per-client
4.每个客户
It’s also possible to set these timeouts per specific client by naming the client:
也可以通过命名客户,为每个特定客户设置这些超时:。
feign:
client:
config:
FooClient:
connectTimeout: 10000
readTimeout: 20000
And, we could, of course, list a global setting and also per-client overrides together without a problem.
当然,我们也可以把全局设置和每个客户的重写放在一起,不会有问题。
5. Conclusion
5.总结
In this tutorial, we explained how to tweak Feign Client’s timeouts and how to set custom values through the application.yml file. Feel free to try these out by following our main Feign introduction.
在本教程中,我们解释了如何调整Feign客户端的超时以及如何通过application.yml文件设置自定义值。请按照我们的主要Feign介绍,随时尝试这些。