Merge branch '2.0.x'
This commit is contained in:
@@ -1046,9 +1046,16 @@ spring:
|
||||
- Path=/service/**
|
||||
----
|
||||
|
||||
NOTE By default when a service instance cannot be found in the `LoadBalancer` a `503` will be returned.
|
||||
NOTE: By default when a service instance cannot be found in the `LoadBalancer` a `503` will be returned.
|
||||
You can configure the Gateway to return a `404` by setting `spring.cloud.gateway.loadbalancer.use404=true`.
|
||||
|
||||
NOTE: The `isSecure` value of the `ServiceInstance` returned from the `LoadBalancer` will override
|
||||
the scheme specified in the request made to the Gateway. For example, if the request comes into the Gateway over `HTTPS`
|
||||
but the `ServiceInstance` indicates it is not secure, then the downstream request will be made over
|
||||
`HTTP`. The opposite situation can also apply. However if `GATEWAY_SCHEME_PREFIX_ATTR` is specified for the
|
||||
route in the Gateway configuration, the prefix will be stripped and the resulting scheme from the
|
||||
route URL will override the `ServiceInstance` configuration.
|
||||
|
||||
=== Netty Routing Filter
|
||||
|
||||
The Netty Routing Filter runs if the url located in the `ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR` exchange attribute has a `http` or `https` scheme. It uses the Netty `HttpClient` to make the downstream proxy request. The response is put in the `ServerWebExchangeUtils.CLIENT_RESPONSE_ATTR` exchange attribute for use in a later filter. (There is an experimental `WebClientHttpRoutingFilter` that performs the same function, but does not require netty)
|
||||
|
||||
@@ -87,7 +87,7 @@ public class LoadBalancerClientFilter implements GlobalFilter, Ordered {
|
||||
|
||||
// if the `lb:<scheme>` mechanism was used, use `<scheme>` as the default,
|
||||
// if the loadbalancer doesn't provide one.
|
||||
String overrideScheme = null;
|
||||
String overrideScheme = instance.isSecure() ? "https" : "http";
|
||||
if (schemePrefix != null) {
|
||||
overrideScheme = url.getScheme();
|
||||
}
|
||||
|
||||
@@ -129,6 +129,17 @@ public class NettyRoutingFilter implements GlobalFilter, Ordered {
|
||||
exchange.getAttributes().put(ORIGINAL_RESPONSE_CONTENT_TYPE_ATTR, contentTypeValue);
|
||||
}
|
||||
|
||||
HttpStatus status = HttpStatus.resolve(res.status().code());
|
||||
if (status != null) {
|
||||
response.setStatusCode(status);
|
||||
} else if (response instanceof AbstractServerHttpResponse) {
|
||||
// https://jira.spring.io/browse/SPR-16748
|
||||
((AbstractServerHttpResponse) response).setStatusCodeValue(res.status().code());
|
||||
} else {
|
||||
throw new IllegalStateException("Unable to set status code on response: " + res.status().code() + ", " + response.getClass());
|
||||
}
|
||||
|
||||
// make sure headers filters run after setting status so it is available in response
|
||||
HttpHeaders filteredResponseHeaders = HttpHeadersFilter.filter(
|
||||
this.headersFilters.getIfAvailable(), headers, exchange, Type.RESPONSE);
|
||||
|
||||
@@ -139,15 +150,6 @@ public class NettyRoutingFilter implements GlobalFilter, Ordered {
|
||||
response.getHeaders().remove(HttpHeaders.TRANSFER_ENCODING);
|
||||
}
|
||||
response.getHeaders().putAll(filteredResponseHeaders);
|
||||
HttpStatus status = HttpStatus.resolve(res.status().code());
|
||||
if (status != null) {
|
||||
response.setStatusCode(status);
|
||||
} else if (response instanceof AbstractServerHttpResponse) {
|
||||
// https://jira.spring.io/browse/SPR-16748
|
||||
((AbstractServerHttpResponse) response).setStatusCodeValue(res.status().code());
|
||||
} else {
|
||||
throw new IllegalStateException("Unable to set status code on response: " + res.status().code() + ", " + response.getClass());
|
||||
}
|
||||
|
||||
// Defer committing the response until all route filters have run
|
||||
// Put client response as ServerWebExchange attribute and write response later NettyWriteResponseFilter
|
||||
|
||||
@@ -137,6 +137,59 @@ public class LoadBalancerClientFilterTests {
|
||||
verifyNoMoreInteractions(chain);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void instanceOverrideNonSecureScheme() {
|
||||
MockServerHttpRequest request = MockServerHttpRequest
|
||||
.get("https://localhost")
|
||||
.build();
|
||||
|
||||
URI lbUri = URI.create("lb://service1");
|
||||
ServerWebExchange webExchange = testFilter(request, lbUri);
|
||||
URI uri = webExchange.getRequiredAttribute(GATEWAY_REQUEST_URL_ATTR);
|
||||
assertThat(uri).hasScheme("http").hasHost("service1-host1");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void instanceOverrideSecureScheme() {
|
||||
MockServerHttpRequest request = MockServerHttpRequest
|
||||
.get("http://localhost")
|
||||
.build();
|
||||
|
||||
URI lbUri = URI.create("lb://service1");
|
||||
ServerWebExchange webExchange = testFilter(request, lbUri, 443);
|
||||
URI uri = webExchange.getRequiredAttribute(GATEWAY_REQUEST_URL_ATTR);
|
||||
assertThat(uri).hasScheme("https").hasHost("service1-host1");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void instanceOverrideSecureSchemePrefix() {
|
||||
MockServerHttpRequest request = MockServerHttpRequest
|
||||
.get("https://localhost")
|
||||
.build();
|
||||
|
||||
URI lbUri = URI.create("http://service1");
|
||||
ServerWebExchange exchange = MockServerWebExchange.from(request);
|
||||
exchange.getAttributes().put(GATEWAY_SCHEME_PREFIX_ATTR, "lb");
|
||||
ServerWebExchange webExchange = testFilter(exchange, lbUri, 443);
|
||||
URI uri = webExchange.getRequiredAttribute(GATEWAY_REQUEST_URL_ATTR);
|
||||
assertThat(uri).hasScheme("http").hasHost("service1-host1");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void instanceOverrideNonSecureSchemePrefix() {
|
||||
MockServerHttpRequest request = MockServerHttpRequest
|
||||
.get("http://localhost")
|
||||
.build();
|
||||
|
||||
URI lbUri = URI.create("https://service1");
|
||||
ServerWebExchange exchange = MockServerWebExchange.from(request);
|
||||
exchange.getAttributes().put(GATEWAY_SCHEME_PREFIX_ATTR, "lb");
|
||||
ServerWebExchange webExchange = testFilter(exchange, lbUri, 8081);
|
||||
URI uri = webExchange.getRequiredAttribute(GATEWAY_REQUEST_URL_ATTR);
|
||||
assertThat(uri).hasScheme("https").hasHost("service1-host1");
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void happyPath() {
|
||||
MockServerHttpRequest request = MockServerHttpRequest
|
||||
@@ -221,7 +274,7 @@ public class LoadBalancerClientFilterTests {
|
||||
exchange = MockServerWebExchange.from(request);
|
||||
exchange.getAttributes().put(GATEWAY_SCHEME_PREFIX_ATTR, "lb");
|
||||
|
||||
ServerWebExchange webExchange = testFilter(exchange, lbUri);
|
||||
ServerWebExchange webExchange = testFilter(exchange, lbUri, 8081);
|
||||
URI uri = webExchange.getRequiredAttribute(GATEWAY_REQUEST_URL_ATTR);
|
||||
assertThat(uri).hasScheme("ws").hasHost("service1-host1")
|
||||
.hasParameter("a", "b");
|
||||
@@ -287,10 +340,14 @@ public class LoadBalancerClientFilterTests {
|
||||
}
|
||||
|
||||
private ServerWebExchange testFilter(MockServerHttpRequest request, URI uri) {
|
||||
return testFilter(MockServerWebExchange.from(request), uri);
|
||||
return testFilter(MockServerWebExchange.from(request), uri, 8081);
|
||||
}
|
||||
|
||||
private ServerWebExchange testFilter(ServerWebExchange exchange, URI uri) {
|
||||
private ServerWebExchange testFilter(MockServerHttpRequest request, URI uri, int port) {
|
||||
return testFilter(MockServerWebExchange.from(request), uri, port);
|
||||
}
|
||||
|
||||
private ServerWebExchange testFilter(ServerWebExchange exchange, URI uri, int port) {
|
||||
exchange.getAttributes().put(GATEWAY_REQUEST_URL_ATTR, uri);
|
||||
|
||||
ArgumentCaptor<ServerWebExchange> captor = ArgumentCaptor.forClass(ServerWebExchange.class);
|
||||
@@ -301,7 +358,7 @@ public class LoadBalancerClientFilterTests {
|
||||
|
||||
when(clientFactory.getLoadBalancerContext("service1")).thenReturn(new RibbonLoadBalancerContext(loadBalancer));
|
||||
when(clientFactory.getLoadBalancer("service1")).thenReturn(loadBalancer);
|
||||
when(loadBalancer.chooseServer(any())).thenReturn(new Server("service1-host1", 8081));
|
||||
when(loadBalancer.chooseServer(any())).thenReturn(new Server("service1-host1", port));
|
||||
|
||||
RibbonLoadBalancerClient client = new RibbonLoadBalancerClient(clientFactory);
|
||||
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
* Copyright 2013-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.gateway.filter.headers;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.SpringBootConfiguration;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.cloud.gateway.test.BaseWebClientTests;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
|
||||
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(webEnvironment = RANDOM_PORT)
|
||||
@DirtiesContext
|
||||
public class HttpStatusInResponseHeadersFilterTests extends BaseWebClientTests {
|
||||
|
||||
@Test
|
||||
public void statusCodeAvailableInResponseHttpHeadersFilter() {
|
||||
testClient.get()
|
||||
.uri("/get")
|
||||
.exchange()
|
||||
.expectStatus().isOk();
|
||||
}
|
||||
|
||||
@EnableAutoConfiguration
|
||||
@SpringBootConfiguration
|
||||
@Import(DefaultTestConfig.class)
|
||||
public static class TestConfig {
|
||||
|
||||
@Bean
|
||||
public HttpHeadersFilter checkStatusFilter() {
|
||||
return new HttpHeadersFilter() {
|
||||
@Override
|
||||
public HttpHeaders filter(HttpHeaders input, ServerWebExchange exchange) {
|
||||
HttpStatus statusCode = exchange.getResponse().getStatusCode();
|
||||
assertThat(statusCode).isEqualTo(HttpStatus.OK);
|
||||
return input;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supports(Type type) {
|
||||
return type == Type.RESPONSE;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
test:
|
||||
hostport: httpbin.org:80
|
||||
uri: lb://testservice
|
||||
uri: lb:https://testservice
|
||||
|
||||
server:
|
||||
ssl:
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
test:
|
||||
hostport: httpbin.org:80
|
||||
uri: lb://testservice
|
||||
uri: lb:https://testservice
|
||||
|
||||
server:
|
||||
ssl:
|
||||
|
||||
Reference in New Issue
Block a user