gpt4 book ai didi

spring-cloud 与ribbon/eureka/hystrix 使用restTemplate 无法设置连接/读取超时

转载 作者:行者123 更新时间:2023-12-04 03:59:52 26 4
gpt4 key购买 nike

我已经使用 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 用于功能区/ Eureka Spring 启动默认值。也许这还没有建成。

最佳答案

RestTemplate除了 RibbonInterceptor 之外,您正在注入(inject)的完全是普通的为您选择 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)。超时和其他属性在 RestTemplate 中控制。通过ClientHttpRequest .您可能应该只注入(inject) RibbonInterceptor变成你自己的RestTemplate并设置 ClientHttpRequestFactory做超时,例如

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

关于spring-cloud 与ribbon/eureka/hystrix 使用restTemplate 无法设置连接/读取超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26285414/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com