Skip to content

Commit f5a6a97

Browse files
committed
Merge branch '4.2.x'
2 parents abd126f + 6d6fe09 commit f5a6a97

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/filter/NettyRoutingFilter.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2020 the original author or authors.
2+
* Copyright 2013-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -254,7 +254,8 @@ protected Mono<HttpClient> getHttpClientMono(Route route, ServerWebExchange exch
254254
* @return the configured HttpClient.
255255
*/
256256
protected HttpClient getHttpClient(Route route, ServerWebExchange exchange) {
257-
Object connectTimeoutAttr = route.getMetadata().get(CONNECT_TIMEOUT_ATTR);
257+
Object connectTimeoutAttr = route.getMetadata().get(CONNECT_TIMEOUT_ATTR) != null
258+
? route.getMetadata().get(CONNECT_TIMEOUT_ATTR) : properties.getConnectTimeout();
258259
if (connectTimeoutAttr != null) {
259260
Integer connectTimeout = getInteger(connectTimeoutAttr);
260261
return this.httpClient.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, connectTimeout);

spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/filter/NettyRoutingFilterTests.java

+42-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2020 the original author or authors.
2+
* Copyright 2013-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,25 +16,35 @@
1616

1717
package org.springframework.cloud.gateway.filter;
1818

19+
import java.util.Collections;
20+
import java.util.HashMap;
21+
22+
import io.netty.channel.ChannelOption;
1923
import org.hamcrest.Matchers;
2024
import org.junit.jupiter.api.BeforeAll;
2125
import org.junit.jupiter.api.Disabled;
2226
import org.junit.jupiter.api.Test;
2327
import reactor.core.publisher.Mono;
2428
import reactor.netty.DisposableServer;
29+
import reactor.netty.http.client.HttpClient;
30+
import reactor.netty.http.client.HttpClientConfig;
2531
import reactor.netty.http.server.HttpServer;
2632

2733
import org.springframework.beans.factory.annotation.Autowired;
2834
import org.springframework.boot.SpringBootConfiguration;
2935
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
3036
import org.springframework.boot.test.context.SpringBootTest;
37+
import org.springframework.cloud.context.environment.EnvironmentChangeEvent;
38+
import org.springframework.cloud.gateway.route.Route;
3139
import org.springframework.cloud.gateway.route.RouteLocator;
3240
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
3341
import org.springframework.cloud.gateway.test.BaseWebClientTests;
3442
import org.springframework.cloud.gateway.test.PermitAllSecurityConfiguration;
3543
import org.springframework.context.ApplicationContext;
3644
import org.springframework.context.annotation.Bean;
3745
import org.springframework.context.annotation.Import;
46+
import org.springframework.core.env.ConfigurableEnvironment;
47+
import org.springframework.core.env.MapPropertySource;
3848
import org.springframework.test.util.TestSocketUtils;
3949
import org.springframework.test.web.reactive.server.WebTestClient;
4050

@@ -48,6 +58,12 @@ class NettyRoutingFilterTests extends BaseWebClientTests {
4858
@Autowired
4959
private ApplicationContext context;
5060

61+
@Autowired
62+
private NettyRoutingFilter nettyRoutingFilter;
63+
64+
@Autowired
65+
private RouteLocator routeLocator;
66+
5167
@BeforeAll
5268
public static void beforeAll() {
5369
port = TestSocketUtils.findAvailableTcpPort();
@@ -88,6 +104,30 @@ void testCaseInsensitiveScheme() {
88104
}
89105
}
90106

107+
@Test
108+
void testConnectTimeoutConfigurationReloadWorks() {
109+
ConfigurableEnvironment environment = (ConfigurableEnvironment) this.context.getEnvironment();
110+
HashMap<String, Object> map = new HashMap<>();
111+
environment.getPropertySources().addFirst(new MapPropertySource("mock", map));
112+
map.put("spring.cloud.gateway.httpclient.connect-timeout", "1000");
113+
this.context.publishEvent(new EnvironmentChangeEvent(this.context,
114+
Collections.singleton("spring.cloud.gateway.httpclient.connect-timeout")));
115+
Route route = routeLocator.getRoutes()
116+
.filter(r -> r.getId().equals("refreshable_configuration_test")).blockLast();
117+
assertThat(route).isNotNull();
118+
HttpClient httpClient = nettyRoutingFilter.getHttpClient(route, null);
119+
HttpClientConfig configuration = httpClient.configuration();
120+
assertThat(configuration.options().get(ChannelOption.CONNECT_TIMEOUT_MILLIS)).isEqualTo(1000);
121+
122+
map.put("spring.cloud.gateway.httpclient.connect-timeout", "5000");
123+
this.context.publishEvent(new EnvironmentChangeEvent(this.context,
124+
Collections.singleton("spring.cloud.gateway.httpclient.connect-timeout")));
125+
httpClient = nettyRoutingFilter.getHttpClient(route, null);
126+
configuration = httpClient.configuration();
127+
assertThat(configuration.options().get(ChannelOption.CONNECT_TIMEOUT_MILLIS)).isEqualTo(5000);
128+
environment.getPropertySources().remove("mock");
129+
}
130+
91131
@SpringBootConfiguration
92132
@EnableAutoConfiguration
93133
@Import(PermitAllSecurityConfiguration.class)
@@ -98,6 +138,7 @@ public RouteLocator routes(RouteLocatorBuilder builder) {
98138
return builder.routes()
99139
.route(p -> p.path("/mockexample").filters(f -> f.prefixPath("/httpbin")).uri("http://example.com"))
100140
.route(p -> p.path("/issue").uri("HTTP://127.0.0.1:" + port))
141+
.route("refreshable_configuration_test", p -> p.path("/refresh").uri("http://example.com"))
101142
.build();
102143
}
103144

0 commit comments

Comments
 (0)