Merge remote-tracking branch 'origin/1.4.x'
This commit is contained in:
@@ -137,6 +137,7 @@ public class EurekaClientAutoConfiguration {
|
||||
ManagementMetadataProvider managementMetadataProvider) {
|
||||
String hostname = getProperty("eureka.instance.hostname");
|
||||
boolean preferIpAddress = Boolean.parseBoolean(getProperty("eureka.instance.prefer-ip-address"));
|
||||
String ipAddress = getProperty("eureka.instance.ipAddress");
|
||||
boolean isSecurePortEnabled = Boolean.parseBoolean(getProperty("eureka.instance.secure-port-enabled"));
|
||||
|
||||
String serverContextPath = env.getProperty("server.context-path", "/");
|
||||
@@ -150,6 +151,9 @@ public class EurekaClientAutoConfiguration {
|
||||
instance.setNonSecurePort(serverPort);
|
||||
instance.setInstanceId(getDefaultInstanceId(env));
|
||||
instance.setPreferIpAddress(preferIpAddress);
|
||||
if (StringUtils.hasText(ipAddress)) {
|
||||
instance.setIpAddress(ipAddress);
|
||||
}
|
||||
|
||||
if(isSecurePortEnabled) {
|
||||
instance.setSecurePort(serverPort);
|
||||
|
||||
@@ -300,6 +300,25 @@ public class EurekaClientAutoConfigurationTests {
|
||||
|
||||
assertEquals("statusPageUrl is wrong", "http://" + instance.getIpAddress() + ":9999/info",
|
||||
instance.getStatusPageUrl());
|
||||
assertEquals("healthCheckUrl is wrong", "http://" + instance.getIpAddress() + ":9999/health",
|
||||
instance.getHealthCheckUrl());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void statusPageAndHealthCheckUrlsShouldSetUserDefinedIpAddress() {
|
||||
addEnvironment(this.context, "server.port=8989",
|
||||
"management.port=9999", "eureka.instance.hostname=foo",
|
||||
"eureka.instance.ipAddress:192.168.13.90",
|
||||
"eureka.instance.preferIpAddress:true");
|
||||
|
||||
setupContext(RefreshAutoConfiguration.class);
|
||||
EurekaInstanceConfigBean instance = this.context
|
||||
.getBean(EurekaInstanceConfigBean.class);
|
||||
|
||||
assertEquals("statusPageUrl is wrong", "http://192.168.13.90:9999/info",
|
||||
instance.getStatusPageUrl());
|
||||
assertEquals("healthCheckUrl is wrong", "http://192.168.13.90:9999/health",
|
||||
instance.getHealthCheckUrl());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -179,12 +179,24 @@ public class SendResponseFilter extends ZuulFilter {
|
||||
}
|
||||
finally {
|
||||
/**
|
||||
* Closing the wrapping InputStream itself has no effect on closing the underlying tcp connection since it's a wrapped stream. I guess for http
|
||||
* keep-alive. When closing the wrapping stream it tries to reach the end of the current request, which is impossible for infinite http streams. So
|
||||
* instead of closing the InputStream we close the HTTP response.
|
||||
* We must ensure that the InputStream provided by our upstream pooling mechanism is ALWAYS closed
|
||||
* even in the case of wrapped streams, which are supplied by pooled sources such as Apache's
|
||||
* PoolingHttpClientConnectionManager. In that particular case, the underlying HTTP connection will
|
||||
* be returned back to the connection pool iif either close() is explicitly called, a read
|
||||
* error occurs, or the end of the underlying stream is reached. If, however a write error occurs, we will
|
||||
* end up leaking a connection from the pool without an explicit close()
|
||||
*
|
||||
* @author Johannes Edmeier
|
||||
*/
|
||||
if (is != null) {
|
||||
try {
|
||||
is.close();
|
||||
}
|
||||
catch (Exception ex) {
|
||||
log.warn("Error while closing upstream input stream", ex);
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
Object zuulResponse = RequestContext.getCurrentContext()
|
||||
.get("zuulResponse");
|
||||
@@ -195,7 +207,7 @@ public class SendResponseFilter extends ZuulFilter {
|
||||
// The container will close the stream for us
|
||||
}
|
||||
catch (IOException ex) {
|
||||
log.warn("Error while sending response to client: " + ex.getMessage());
|
||||
log.warn("Error while sending response to client: " + ex.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -222,7 +222,7 @@ public class SimpleHostRoutingFilter extends ZuulFilter {
|
||||
.setCookieSpec(CookieSpecs.IGNORE_COOKIES).build();
|
||||
return httpClientFactory.createBuilder().
|
||||
setDefaultRequestConfig(requestConfig).
|
||||
setConnectionManager(this.connectionManager).build();
|
||||
setConnectionManager(this.connectionManager).disableRedirectHandling().build();
|
||||
}
|
||||
|
||||
private CloseableHttpResponse forward(CloseableHttpClient httpclient, String verb,
|
||||
|
||||
@@ -19,6 +19,7 @@ package org.springframework.cloud.netflix.zuul.filters.post;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.lang.reflect.UndeclaredThrowableException;
|
||||
|
||||
import javax.servlet.ServletOutputStream;
|
||||
@@ -47,6 +48,7 @@ import static org.mockito.Matchers.anyInt;
|
||||
import static org.mockito.Matchers.isA;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.X_ZUUL_DEBUG_HEADER;
|
||||
@@ -116,13 +118,14 @@ public class SendResponseFilterTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void closeResponseOutpusStreamError() throws Exception {
|
||||
public void closeResponseOutputStreamError() throws Exception {
|
||||
HttpServletResponse response = mock(HttpServletResponse.class);
|
||||
InputStream mockStream = spy(new ByteArrayInputStream("Hello\n".getBytes("UTF-8")));
|
||||
|
||||
RequestContext context = new RequestContext();
|
||||
context.setRequest(new MockHttpServletRequest());
|
||||
context.setResponse(response);
|
||||
context.setResponseDataStream(new ByteArrayInputStream("Hello\n".getBytes("UTF-8")));
|
||||
context.setResponseDataStream(mockStream);
|
||||
Closeable zuulResponse = mock(Closeable.class);
|
||||
context.set("zuulResponse", zuulResponse);
|
||||
RequestContext.testSetCurrentContext(context);
|
||||
@@ -141,6 +144,29 @@ public class SendResponseFilterTests {
|
||||
}
|
||||
|
||||
verify(zuulResponse).close();
|
||||
verify(mockStream).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCloseResponseDataStream() throws Exception {
|
||||
HttpServletResponse response = mock(HttpServletResponse.class);
|
||||
InputStream mockStream = spy(new ByteArrayInputStream("Hello\n".getBytes("UTF-8")));
|
||||
|
||||
RequestContext context = new RequestContext();
|
||||
context.setRequest(new MockHttpServletRequest());
|
||||
context.setResponse(response);
|
||||
context.setResponseDataStream(mockStream);
|
||||
Closeable zuulResponse = mock(Closeable.class);
|
||||
context.set("zuulResponse", zuulResponse);
|
||||
RequestContext.testSetCurrentContext(context);
|
||||
|
||||
when(response.getOutputStream()).thenReturn(mock(ServletOutputStream.class));
|
||||
|
||||
SendResponseFilter filter = new SendResponseFilter();
|
||||
|
||||
filter.run();
|
||||
|
||||
verify(mockStream).close();
|
||||
}
|
||||
|
||||
private void runFilter(String characterEncoding, String content, boolean streamContent) throws Exception {
|
||||
|
||||
@@ -212,6 +212,20 @@ public class SimpleHostRoutingFilterTests {
|
||||
assertTrue("Get 1".equals(responseString));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void redirectTest() throws Exception {
|
||||
setupContext();
|
||||
InputStreamEntity inputStreamEntity = new InputStreamEntity(new ByteArrayInputStream(new byte[]{}));
|
||||
HttpRequest httpRequest = getFilter().buildHttpRequest("GET", "/app/redirect", inputStreamEntity,
|
||||
new LinkedMultiValueMap<String, String>(), new LinkedMultiValueMap<String, String>(), new MockHttpServletRequest());
|
||||
|
||||
CloseableHttpResponse response = getFilter().newClient().execute(new HttpHost("localhost", this.port), httpRequest);
|
||||
assertEquals(302, response.getStatusLine().getStatusCode());
|
||||
String responseString = copyToString(response.getEntity().getContent(), Charset.forName("UTF-8"));
|
||||
assertTrue(response.getLastHeader("Location").getValue().contains("/app/get/5"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void zuulHostKeysUpdateHttpClient() {
|
||||
setupContext();
|
||||
@@ -357,6 +371,12 @@ class SampleApplication {
|
||||
public String getString(@PathVariable String id, HttpServletResponse response) throws IOException {
|
||||
return "Get " + id;
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/redirect", method = RequestMethod.GET)
|
||||
public String redirect(HttpServletResponse response) throws IOException {
|
||||
response.sendRedirect("/app/get/5");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
class GZIPCompression {
|
||||
|
||||
Reference in New Issue
Block a user