Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
spring-cloud-netflix
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
openSource
spring-cloud-netflix
Commits
9e180b8b
Unverified
Commit
9e180b8b
authored
Jun 05, 2017
by
Spencer Gibb
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch '1.3.x'
parents
d914406c
e08a6e8a
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
61 additions
and
43 deletions
+61
-43
ZuulFilterInitializer.java
...ngframework/cloud/netflix/zuul/ZuulFilterInitializer.java
+9
-10
ZuulProperties.java
...gframework/cloud/netflix/zuul/filters/ZuulProperties.java
+8
-0
SimpleHostRoutingFilter.java
...d/netflix/zuul/filters/route/SimpleHostRoutingFilter.java
+17
-19
ZuulFilterInitializerTests.java
...mework/cloud/netflix/zuul/ZuulFilterInitializerTests.java
+4
-11
SimpleHostRoutingFilterTests.java
...flix/zuul/filters/route/SimpleHostRoutingFilterTests.java
+23
-3
No files found.
spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/zuul/ZuulFilterInitializer.java
View file @
9e180b8b
...
...
@@ -19,8 +19,8 @@ package org.springframework.cloud.netflix.zuul;
import
java.lang.reflect.Field
;
import
java.util.Map
;
import
javax.
servlet.ServletContextEven
t
;
import
javax.
servlet.ServletContextListener
;
import
javax.
annotation.PostConstruc
t
;
import
javax.
annotation.PreDestroy
;
import
org.springframework.util.ReflectionUtils
;
...
...
@@ -39,7 +39,7 @@ import lombok.extern.apachecommons.CommonsLog;
*
*/
@CommonsLog
public
class
ZuulFilterInitializer
implements
ServletContextListener
{
public
class
ZuulFilterInitializer
{
private
final
Map
<
String
,
ZuulFilter
>
filters
;
private
final
CounterFactory
counterFactory
;
...
...
@@ -59,10 +59,9 @@ public class ZuulFilterInitializer implements ServletContextListener {
this
.
filterRegistry
=
filterRegistry
;
}
@Override
public
void
contextInitialized
(
ServletContextEvent
sce
)
{
log
.
info
(
"Starting filter initializer context listener"
);
@PostConstruct
public
void
contextInitialized
()
{
log
.
info
(
"Starting filter initializer"
);
TracerFactory
.
initialize
(
tracerFactory
);
CounterFactory
.
initialize
(
counterFactory
);
...
...
@@ -72,9 +71,9 @@ public class ZuulFilterInitializer implements ServletContextListener {
}
}
@
Override
public
void
contextDestroyed
(
ServletContextEvent
sce
)
{
log
.
info
(
"Stopping filter initializer
context listener
"
);
@
PreDestroy
public
void
contextDestroyed
()
{
log
.
info
(
"Stopping filter initializer"
);
for
(
Map
.
Entry
<
String
,
ZuulFilter
>
entry
:
this
.
filters
.
entrySet
())
{
filterRegistry
.
remove
(
entry
.
getKey
());
}
...
...
spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/zuul/filters/ZuulProperties.java
View file @
9e180b8b
...
...
@@ -329,6 +329,14 @@ public class ZuulProperties {
*/
private
int
maxPerRouteConnections
=
20
;
/**
* The socket timeout in millis. Defaults to 10000.
*/
private
int
socketTimeoutMillis
=
10000
;
/**
* The connection timeout in millis. Defaults to 2000.
*/
private
int
connectTimeoutMillis
=
2000
;
/**
* The lifetime for the connection pool.
*/
private
long
timeToLive
=
-
1
;
...
...
spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/zuul/filters/route/SimpleHostRoutingFilter.java
View file @
9e180b8b
...
...
@@ -67,18 +67,17 @@ import org.apache.http.message.BasicHeader;
import
org.apache.http.message.BasicHttpEntityEnclosingRequest
;
import
org.apache.http.message.BasicHttpRequest
;
import
org.apache.http.protocol.HttpContext
;
import
org.springframework.cloud.context.environment.EnvironmentChangeEvent
;
import
org.springframework.cloud.netflix.zuul.filters.ProxyRequestHelper
;
import
org.springframework.cloud.netflix.zuul.filters.ZuulProperties
;
import
org.springframework.cloud.netflix.zuul.filters.ZuulProperties.Host
;
import
org.springframework.cloud.netflix.zuul.util.ZuulRuntimeException
;
import
org.springframework.context.event.EventListener
;
import
org.springframework.util.LinkedMultiValueMap
;
import
org.springframework.util.MultiValueMap
;
import
org.springframework.util.StringUtils
;
import
com.netflix.config.DynamicIntProperty
;
import
com.netflix.config.DynamicPropertyFactory
;
import
com.netflix.zuul.ZuulFilter
;
import
com.netflix.zuul.constants.ZuulConstants
;
import
com.netflix.zuul.context.RequestContext
;
import
static
org
.
springframework
.
cloud
.
netflix
.
zuul
.
filters
.
support
.
FilterConstants
.
HTTPS_SCHEME
;
...
...
@@ -98,14 +97,6 @@ public class SimpleHostRoutingFilter extends ZuulFilter {
private
static
final
Log
log
=
LogFactory
.
getLog
(
SimpleHostRoutingFilter
.
class
);
private
static
final
DynamicIntProperty
SOCKET_TIMEOUT
=
DynamicPropertyFactory
.
getInstance
()
.
getIntProperty
(
ZuulConstants
.
ZUUL_HOST_SOCKET_TIMEOUT_MILLIS
,
10000
);
private
static
final
DynamicIntProperty
CONNECTION_TIMEOUT
=
DynamicPropertyFactory
.
getInstance
()
.
getIntProperty
(
ZuulConstants
.
ZUUL_HOST_CONNECT_TIMEOUT_MILLIS
,
2000
);
private
final
Timer
connectionManagerTimer
=
new
Timer
(
"SimpleHostRoutingFilter.connectionManagerTimer"
,
true
);
...
...
@@ -117,9 +108,18 @@ public class SimpleHostRoutingFilter extends ZuulFilter {
private
PoolingHttpClientConnectionManager
connectionManager
;
private
CloseableHttpClient
httpClient
;
private
final
Runnable
clientloader
=
new
Runnable
()
{
@Override
public
void
run
()
{
@EventListener
public
void
onPropertyChange
(
EnvironmentChangeEvent
event
)
{
boolean
createNewClient
=
false
;
for
(
String
key
:
event
.
getKeys
())
{
if
(
key
.
startsWith
(
"zuul.host."
))
{
createNewClient
=
true
;
break
;
}
}
if
(
createNewClient
)
{
try
{
SimpleHostRoutingFilter
.
this
.
httpClient
.
close
();
}
...
...
@@ -128,7 +128,7 @@ public class SimpleHostRoutingFilter extends ZuulFilter {
}
SimpleHostRoutingFilter
.
this
.
httpClient
=
newClient
();
}
}
;
}
public
SimpleHostRoutingFilter
(
ProxyRequestHelper
helper
,
ZuulProperties
properties
)
{
this
.
helper
=
helper
;
...
...
@@ -141,8 +141,6 @@ public class SimpleHostRoutingFilter extends ZuulFilter {
@PostConstruct
private
void
initialize
()
{
this
.
httpClient
=
newClient
();
SOCKET_TIMEOUT
.
addCallback
(
this
.
clientloader
);
CONNECTION_TIMEOUT
.
addCallback
(
this
.
clientloader
);
this
.
connectionManagerTimer
.
schedule
(
new
TimerTask
()
{
@Override
public
void
run
()
{
...
...
@@ -251,8 +249,8 @@ public class SimpleHostRoutingFilter extends ZuulFilter {
protected
CloseableHttpClient
newClient
()
{
final
RequestConfig
requestConfig
=
RequestConfig
.
custom
()
.
setSocketTimeout
(
SOCKET_TIMEOUT
.
get
())
.
setConnectTimeout
(
CONNECTION_TIMEOUT
.
get
())
.
setSocketTimeout
(
this
.
hostProperties
.
getSocketTimeoutMillis
())
.
setConnectTimeout
(
this
.
hostProperties
.
getConnectTimeoutMillis
())
.
setCookieSpec
(
CookieSpecs
.
IGNORE_COOKIES
).
build
();
HttpClientBuilder
httpClientBuilder
=
HttpClients
.
custom
();
...
...
spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/ZuulFilterInitializerTests.java
View file @
9e180b8b
...
...
@@ -16,12 +16,12 @@
package
org
.
springframework
.
cloud
.
netflix
.
zuul
;
import
java.lang.reflect.Constructor
;
import
java.lang.reflect.Field
;
import
java.util.HashMap
;
import
java.util.Map
;
import
javax.servlet.ServletContextEvent
;
import
org.junit.Test
;
import
org.springframework.test.util.ReflectionTestUtils
;
import
org.springframework.util.ReflectionUtils
;
...
...
@@ -31,10 +31,6 @@ import com.netflix.zuul.filters.FilterRegistry;
import
com.netflix.zuul.monitoring.CounterFactory
;
import
com.netflix.zuul.monitoring.TracerFactory
;
import
org.junit.Test
;
import
java.lang.reflect.Constructor
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
...
...
@@ -42,9 +38,6 @@ import static org.mockito.Mockito.mock;
public
class
ZuulFilterInitializerTests
{
private
static
final
ServletContextEvent
DUMMY_SERVLET_CONTEXT_EVENT
=
mock
(
ServletContextEvent
.
class
);
private
Map
<
String
,
ZuulFilter
>
filters
=
getFilters
();
private
CounterFactory
counterFactory
=
mock
(
CounterFactory
.
class
);
private
TracerFactory
tracerFactory
=
mock
(
TracerFactory
.
class
);
...
...
@@ -56,7 +49,7 @@ public class ZuulFilterInitializerTests {
@Test
public
void
shouldSetupOnContextInitializedEvent
()
throws
Exception
{
initializer
.
contextInitialized
(
DUMMY_SERVLET_CONTEXT_EVENT
);
initializer
.
contextInitialized
();
assertEquals
(
tracerFactory
,
TracerFactory
.
instance
());
assertEquals
(
counterFactory
,
CounterFactory
.
instance
());
...
...
@@ -66,7 +59,7 @@ public class ZuulFilterInitializerTests {
@Test
public
void
shouldCleanupOnContextDestroyed
()
throws
Exception
{
initializer
.
contextDestroyed
(
DUMMY_SERVLET_CONTEXT_EVENT
);
initializer
.
contextDestroyed
();
assertEquals
(
null
,
ReflectionTestUtils
.
getField
(
TracerFactory
.
class
,
"INSTANCE"
));
assertEquals
(
null
,
...
...
spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/filters/route/SimpleHostRoutingFilterTests.java
View file @
9e180b8b
...
...
@@ -22,6 +22,7 @@ import java.io.IOException;
import
java.lang.reflect.Field
;
import
java.nio.charset.Charset
;
import
java.util.Arrays
;
import
java.util.Collections
;
import
java.util.concurrent.TimeUnit
;
import
java.util.zip.GZIPOutputStream
;
...
...
@@ -46,6 +47,7 @@ import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoCon
import
org.springframework.boot.context.embedded.LocalServerPort
;
import
org.springframework.boot.context.properties.EnableConfigurationProperties
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.cloud.context.environment.EnvironmentChangeEvent
;
import
org.springframework.cloud.netflix.zuul.filters.ProxyRequestHelper
;
import
org.springframework.cloud.netflix.zuul.filters.ZuulProperties
;
import
org.springframework.context.annotation.AnnotationConfigApplicationContext
;
...
...
@@ -54,6 +56,7 @@ import org.springframework.context.annotation.Configuration;
import
org.springframework.mock.web.MockHttpServletRequest
;
import
org.springframework.test.annotation.DirtiesContext
;
import
org.springframework.test.context.junit4.SpringRunner
;
import
org.springframework.test.util.ReflectionTestUtils
;
import
org.springframework.util.LinkedMultiValueMap
;
import
org.springframework.util.ReflectionUtils
;
import
org.springframework.web.bind.annotation.PathVariable
;
...
...
@@ -76,8 +79,7 @@ import static org.springframework.util.StreamUtils.copyToString;
@RunWith
(
SpringRunner
.
class
)
@SpringBootTest
(
classes
=
SampleApplication
.
class
,
webEnvironment
=
RANDOM_PORT
,
properties
=
{
"server.contextPath: /app"
,
"zuul.host.socket-timeout-millis=11000"
,
"zuul.host.connect-timeout-millis=2100"
})
properties
=
{
"server.contextPath: /app"
})
@DirtiesContext
public
class
SimpleHostRoutingFilterTests
{
...
...
@@ -95,6 +97,8 @@ public class SimpleHostRoutingFilterTests {
@Test
public
void
timeoutPropertiesAreApplied
()
{
addEnvironment
(
this
.
context
,
"zuul.host.socket-timeout-millis=11000"
,
"zuul.host.connect-timeout-millis=2100"
);
setupContext
();
CloseableHttpClient
httpClient
=
getFilter
().
newClient
();
Assertions
.
assertThat
(
httpClient
).
isInstanceOf
(
Configurable
.
class
);
...
...
@@ -188,6 +192,17 @@ public class SimpleHostRoutingFilterTests {
assertTrue
(
"Get 1"
.
equals
(
responseString
));
}
@Test
public
void
zuulHostKeysUpdateHttpClient
()
{
setupContext
();
SimpleHostRoutingFilter
filter
=
getFilter
();
CloseableHttpClient
httpClient
=
(
CloseableHttpClient
)
ReflectionTestUtils
.
getField
(
filter
,
"httpClient"
);
EnvironmentChangeEvent
event
=
new
EnvironmentChangeEvent
(
Collections
.
singleton
(
"zuul.host.mykey"
));
filter
.
onPropertyChange
(
event
);
CloseableHttpClient
newhttpClient
=
(
CloseableHttpClient
)
ReflectionTestUtils
.
getField
(
filter
,
"httpClient"
);
Assertions
.
assertThat
(
httpClient
).
isNotEqualTo
(
newhttpClient
);
}
private
void
setupContext
()
{
this
.
context
.
register
(
PropertyPlaceholderAutoConfiguration
.
class
,
TestConfiguration
.
class
);
...
...
@@ -199,9 +214,14 @@ public class SimpleHostRoutingFilterTests {
}
@Configuration
@EnableConfigurationProperties
(
ZuulProperties
.
class
)
@EnableConfigurationProperties
protected
static
class
TestConfiguration
{
@Bean
ZuulProperties
zuulProperties
()
{
return
new
ZuulProperties
();
}
@Bean
SimpleHostRoutingFilter
simpleHostRoutingFilter
(
ZuulProperties
zuulProperties
)
{
return
new
SimpleHostRoutingFilter
(
new
ProxyRequestHelper
(),
zuulProperties
);
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment