如何为Spring Boot配置服务器设置超时值

3
我有一个简单的设置,使用Spring Boot ConfigServer和客户端服务,该服务调用ConfigServer从GIT获取配置prop文件的详细信息。
我的Config服务器正常工作,并且我能够从GIT获取prop文件。但是当我尝试运行使用ConfigServer获取详细信息的消费者服务器时,我遇到了一个错误... 错误如下...
Connect Timeout Exception on Url - http://localhost:8888. Will be trying the next url if available

localhost:8888是我的configServer的URL,我可以直接从浏览器调用它,但由于我有一个大的prop文件,在从GIT中检索它时需要一些时间。

configServer上的配置(application.properties)

spring.application.name=config-server
server.port=8888
spring.cloud.config.server.git.uri=https://github.com/shibajiJava/MicroServiceDemo
spring.cloud.config.server.bootstrap=true

消费者服务的配置(bootstrap.properties)

spring.application.name=configuration-service
spring.cloud.config.uri=http://localhost:8888
spring.cloud.config.server.bootstrap=true

在消费者端有没有什么可以指定超时值的东西?谢谢。

2个回答

3

服务器端的配置:

  • 尝试将 spring.cloud.config.server.git.timeout 设置为所需的值。
  • 尝试将 server.connection-timeout 设置为所需的值。

客户端的配置:

我不知道是否有任何属性可以完成此任务。您可能需要覆盖执行请求的默认RestTemplate。为此,请创建一个具有所需超时时间的RestTemplate并将其注入而不是默认模板(我的最佳猜测是在正确的@Qualifier@Primary的基础上进行注入,但您应该检查源代码并确认这确实是默认模板如何注入的方式)。

@Configuration
public class ConsumerConfig {

    @Bean
    @Primary
    @Qualifier("rightQualifierHere")
    public RestTemplate configRestTemplate() {
        return new RestTemplateBuilder()
               .setReadTimeout(readTimeout)
               .setConnectTimeout(connectionTimeout)
               .build();
    }

}

Documentation:


嗨@alexrolea,我在“spring.cloud.config.uri=http://localhost:8888”处遇到连接超时问题,但在从github读取文件时没有出现此问题。我猜想在Consumer Service bootstrap.properties中有些问题。请告诉我您的看法...如果您有任何解决方案,请告诉我。 - Biswajit
已更新答案,我不知道有更简洁的方法来做这件事。 - user10367961

0
作为Spring Config客户端文档的一部分,有两个属性可用于配置超时。
If you want to configure timeout thresholds:

Read timeouts can be configured by using the property spring.cloud.config.request-read-timeout.

Connection timeouts can be configured by using the property spring.cloud.config.request-connect-timeout.

源代码


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接