【问题标题】:spring-cloud with ribbon/eureka/hystrix using restTemplate unable to set connect/read timeoutsspring-cloud 与ribbon/eureka/hystrix 使用restTemplate 无法设置连接/读取超时
【发布时间】:2014-12-04 19:06:59
【问题描述】:

我已经使用 spring-cloud 构建了一个 Spring Boot 应用程序,并希望在我的客户端应用程序(这也是一个微服务)中使用 RestTemplate,以便我可以继续使用 mockMvc 进行集成测试。我正在使用默认的ribbon/eureka/hystrix 客户端设置和我正在调用的服务中的客户端微服务和eureka 客户端。这是有效的(一旦我发现 serviceIds 是在 restTemplate 中标识服务端点的东西)。我的问题是我似乎无法从默认的 300 毫秒更改 restTemplate 读取或连接超时。

通话详情:

`@Configuration
 @EnableAutoConfiguration
 @ComponentScan
 @EnableConfigurationProperties
 @EnableHystrix
 @EnableEurekaClient
public class Application { ... public static void main(String[] args) {} ... }

@Component
class EricComponentToDoHystrix {   // apparently this has to be a component for hystrix to work btw
    @Autowired
    RestTemplate restTemplate;
    ..
    @HystrixCommand(fallbackMethod="defaultRestTemplateCall")
    public void doRestTemplateCall() 
        ResponseEntity<String> result = restTemplate.getForEntity("http://someservice/doSomething", String.class);  // actually make a call
    ..
    }
}`

application.properties 包含:

spring:
  cloud:
    client:
      serviceIds:
        - someservice

someservice:
  ribbon:
    #listOfServers: localhost:7080
    #NIWSServerListClassName: com.netflix.niws.loadbalancer.DiscoveryEnabledNIWSServerList

    # the eureka vipAddress of the target service (Disabled)
    DeploymentContextBasedVipAddresses: someservice

    # Interval to refresh the server list from the source
    ServerListRefreshInterval: 1000

    # Connect timeout used by Apache HttpClient.. apparently not by restTemplate 
    ConnectTimeout: 30000

    # Read timeout used by Apache HttpClient.. apparently not by restTemplate
    ReadTimeout: 30000

eureka:
  client:
    #Region where eureka is deployed -For AWS specify one of the AWS regions, for other datacenters specify a arbitrary string
    #indicating the region.This is normally specified as a -D option (eg) -Deureka.region=us-east-1
    region: default

    #For eureka clients running in eureka server, it needs to connect to servers in other zones
    preferSameZone: false

    us-east-1:
      availabilityZones: default

  instance:
    #Virtual host name by which the clients identifies this service
    virtualHostName: ${spring.application.name}
    appGroupName: ericGroup


# disable Ribbon's cicruit breaker and rely soley on Hystrix.
# this helps to avoid confusion.
# see https://github.com/Netflix/ribbon/issues/15
niws:
  loadbalancer:
    availabilityFilteringRule:
      filterCircuitTripped: false

有人知道我需要什么属性来修改restTemplate的默认超时吗?文档在这个主题上非常简单,似乎最近的代码甚至允许将 restTemplate 用于功能区/尤里卡弹簧启动默认值。也许这还没有建成。

【问题讨论】:

    标签: spring-cloud


    【解决方案1】:

    您注入的RestTemplate 完全是普通的,除了RibbonInterceptor 为您选择URI 中的物理主机(请参阅https://github.com/spring-cloud/spring-cloud-netflix/blob/master/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/ribbon/RibbonAutoConfiguration.java)。通过ClientHttpRequestRestTemplate 中控制超时和其他属性。您可能应该将RibbonInterceptor 注入您自己的RestTemplate 并设置ClientHttpRequestFactory 来执行超时,例如

    @Component
    class EricComponentToDoHystrix {
        private RestTemplate restTemplate;
        @Autowired
        public EricComponentToDoHystrix(RibbonInterceptor interceptor) {
             restTemplate = new RestTemplate();
             restTemplate.setInterceptors(Arrays.asList(interceptor));
             restTemplate.setRequestFactory(...);
        }
    }
    

    【讨论】:

    • 感谢你们两位如此迅速地回答!这和@spencergibb 的回答都对我有帮助。知道 Ribbon 的配置与 RestTemplate 无关,并且除了 RibbonInterceptor 之外它的 vanilla 也将有助于测试。我会按照上面列出的方式设置自己。顺便说一句,这里还有另一个超时,那就是 hystrix 的 execution.isolation.thread.timeoutInMilliseconds。所以现在有 3 次超时,Ribbon 的、RestTemplate 的和 Hystrix 的。
    • @DaveSyer 看起来RibbonInterceptor 在 RELEASE 版本中消失了。您能指出我现在如何配置 RestTemplate 的正确方向吗?
    • 试试LoadBalancerInterceptor
    • @DirkLachowski:你能提供你必须工作的配置示例吗?
    • @brainstorm 当然,看看gist.github.com/DirkLachowski/288edb8d28ed3dfd7120,但也请注意,如果将其余模板的超时时间推到很远,则必须调整 hystrix 的超时时间(要点中的第二类示例) .见github.com/Netflix/Hystrix/wiki/…
    【解决方案2】:

    由于无法发表评论,我会回答。 RestTemplate 集成仅使用 Ribbon LoadBalancer,而不是 RestClient 或 NFHttpClient。

    您不再需要 spring.cloud.client.serviceIds,顺便说一句。如果它在文档中,我会删除它。

    【讨论】:

    • 为什么不再需要 serviceIds?相反,建议是什么?我发现的示例使用 serviceID。你能指出相关文件吗?另外,请尽可能提供您对这篇文章的见解。 stackoverflow.com/questions/31901054/…
    • serviceIds 是一个不再需要的早期实现。哪些样品还有呢?见github.com/spring-cloud-samples/customers-stores。据我所知,这个 SO question 是唯一引用它的东西。你想做什么?
    • 我只是浏览了这些项目,并没有看到它被使用。
    • 如果您查看https://github.com/joshlong/service-registration-and-discovery/blob/master/passport-service/src/main/java/passport/Application.java,您会注意到bookmark-servicephoto-service 是服务ID。为了从微服务中调用其他微服务,需要正确的 serviceID?
    猜你喜欢
    • 2016-12-30
    • 2015-02-12
    • 1970-01-01
    • 2016-07-05
    • 1970-01-01
    • 2015-12-12
    • 2019-07-26
    • 2018-09-22
    • 2018-11-10
    相关资源
    最近更新 更多