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
05fe1752
Commit
05fe1752
authored
Nov 26, 2014
by
Dave Syer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Move field injection into @Configuration
parent
cee619d4
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
70 additions
and
51 deletions
+70
-51
FilterInitializer.java
...springframework/cloud/netflix/zuul/FilterInitializer.java
+4
-2
RouteLocator.java
.../org/springframework/cloud/netflix/zuul/RouteLocator.java
+27
-14
ZuulConfiguration.java
...springframework/cloud/netflix/zuul/ZuulConfiguration.java
+17
-8
ZuulHandlerMapping.java
...pringframework/cloud/netflix/zuul/ZuulHandlerMapping.java
+10
-16
RouteLocatorTests.java
...springframework/cloud/netflix/zuul/RouteLocatorTests.java
+12
-11
No files found.
spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/zuul/FilterInitializer.java
View file @
05fe1752
...
@@ -7,7 +7,6 @@ import javax.servlet.ServletContextListener;
...
@@ -7,7 +7,6 @@ import javax.servlet.ServletContextListener;
import
org.slf4j.Logger
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
com.netflix.zuul.ZuulFilter
;
import
com.netflix.zuul.ZuulFilter
;
import
com.netflix.zuul.filters.FilterRegistry
;
import
com.netflix.zuul.filters.FilterRegistry
;
...
@@ -23,9 +22,12 @@ public class FilterInitializer implements ServletContextListener {
...
@@ -23,9 +22,12 @@ public class FilterInitializer implements ServletContextListener {
private
static
final
Logger
LOGGER
=
LoggerFactory
.
getLogger
(
FilterInitializer
.
class
);
private
static
final
Logger
LOGGER
=
LoggerFactory
.
getLogger
(
FilterInitializer
.
class
);
@Autowired
private
Map
<
String
,
ZuulFilter
>
filters
;
private
Map
<
String
,
ZuulFilter
>
filters
;
public
FilterInitializer
(
Map
<
String
,
ZuulFilter
>
filters
)
{
this
.
filters
=
filters
;
}
@Override
@Override
public
void
contextInitialized
(
ServletContextEvent
sce
)
{
public
void
contextInitialized
(
ServletContextEvent
sce
)
{
...
...
spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/zuul/RouteLocator.java
View file @
05fe1752
package
org
.
springframework
.
cloud
.
netflix
.
zuul
;
package
org
.
springframework
.
cloud
.
netflix
.
zuul
;
import
java.lang.reflect.Field
;
import
java.util.LinkedHashMap
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Set
;
import
java.util.concurrent.atomic.AtomicReference
;
import
lombok.extern.slf4j.Slf4j
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.bind.PropertySourceUtils
;
import
org.springframework.boot.bind.PropertySourceUtils
;
import
org.springframework.cloud.client.discovery.DiscoveryClient
;
import
org.springframework.cloud.client.discovery.DiscoveryClient
;
import
org.springframework.cloud.context.environment.EnvironmentChangeEvent
;
import
org.springframework.cloud.context.environment.EnvironmentChangeEvent
;
import
org.springframework.context.ApplicationListener
;
import
org.springframework.context.ApplicationListener
;
import
org.springframework.core.env.*
;
import
org.springframework.context.EnvironmentAware
;
import
org.springframework.core.env.CompositePropertySource
;
import
org.springframework.core.env.ConfigurableEnvironment
;
import
org.springframework.core.env.Environment
;
import
org.springframework.core.env.MutablePropertySources
;
import
org.springframework.core.env.PropertySource
;
import
org.springframework.core.env.StandardEnvironment
;
import
org.springframework.util.ReflectionUtils
;
import
org.springframework.util.ReflectionUtils
;
import
java.lang.reflect.Field
;
import
java.util.*
;
import
java.util.concurrent.atomic.AtomicReference
;
/**
/**
* @author Spencer Gibb
* @author Spencer Gibb
*/
*/
@Slf4j
@Slf4j
public
class
RouteLocator
implements
ApplicationListener
<
EnvironmentChangeEvent
>
{
public
class
RouteLocator
implements
ApplicationListener
<
EnvironmentChangeEvent
>
,
EnvironmentAware
{
public
static
final
String
DEFAULT_ROUTE
=
"/"
;
public
static
final
String
DEFAULT_ROUTE
=
"/"
;
@Autowired
private
ConfigurableEnvironment
env
=
new
StandardEnvironment
();
protected
ConfigurableEnvironment
env
;
@Autowired
private
DiscoveryClient
discovery
;
protected
DiscoveryClient
discovery
;
@Autowired
private
ZuulProperties
properties
;
protected
ZuulProperties
properties
;
private
Field
propertySourcesField
;
private
Field
propertySourcesField
;
private
AtomicReference
<
LinkedHashMap
<
String
,
String
>>
routes
=
new
AtomicReference
<>();
private
AtomicReference
<
LinkedHashMap
<
String
,
String
>>
routes
=
new
AtomicReference
<>();
public
RouteLocator
()
{
@Override
public
void
setEnvironment
(
Environment
environment
)
{
env
=
(
ConfigurableEnvironment
)
environment
;
}
public
RouteLocator
(
DiscoveryClient
discovery
,
ZuulProperties
properties
)
{
this
.
discovery
=
discovery
;
this
.
properties
=
properties
;
initField
();
initField
();
}
}
...
...
spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/zuul/ZuulConfiguration.java
View file @
05fe1752
package
org
.
springframework
.
cloud
.
netflix
.
zuul
;
package
org
.
springframework
.
cloud
.
netflix
.
zuul
;
import
java.util.Map
;
import
com.netflix.zuul.ZuulFilter
;
import
com.netflix.zuul.http.ZuulServlet
;
import
com.netflix.zuul.http.ZuulServlet
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.actuate.trace.TraceRepository
;
import
org.springframework.boot.actuate.trace.TraceRepository
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnClass
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnClass
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnExpression
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnExpression
;
import
org.springframework.boot.context.properties.EnableConfigurationProperties
;
import
org.springframework.boot.context.properties.EnableConfigurationProperties
;
import
org.springframework.cloud.client.discovery.DiscoveryClient
;
import
org.springframework.cloud.netflix.zuul.filters.post.SendErrorFilter
;
import
org.springframework.cloud.netflix.zuul.filters.post.SendErrorFilter
;
import
org.springframework.cloud.netflix.zuul.filters.post.SendResponseFilter
;
import
org.springframework.cloud.netflix.zuul.filters.post.SendResponseFilter
;
import
org.springframework.cloud.netflix.zuul.filters.pre.DebugFilter
;
import
org.springframework.cloud.netflix.zuul.filters.pre.DebugFilter
;
...
@@ -19,7 +24,7 @@ import org.springframework.context.annotation.Configuration;
...
@@ -19,7 +24,7 @@ import org.springframework.context.annotation.Configuration;
* @author Spencer Gibb
* @author Spencer Gibb
*/
*/
@Configuration
@Configuration
@EnableConfigurationProperties
()
@EnableConfigurationProperties
(
ZuulProperties
.
class
)
@ConditionalOnClass
(
ZuulServlet
.
class
)
@ConditionalOnClass
(
ZuulServlet
.
class
)
@ConditionalOnExpression
(
"${zuul.enabled:true}"
)
@ConditionalOnExpression
(
"${zuul.enabled:true}"
)
public
class
ZuulConfiguration
{
public
class
ZuulConfiguration
{
...
@@ -27,14 +32,18 @@ public class ZuulConfiguration {
...
@@ -27,14 +32,18 @@ public class ZuulConfiguration {
@Autowired
(
required
=
false
)
@Autowired
(
required
=
false
)
private
TraceRepository
traces
;
private
TraceRepository
traces
;
@Bean
@Autowired
public
ZuulProperties
zuulProperties
()
{
private
DiscoveryClient
discovery
;
return
new
ZuulProperties
();
}
@Autowired
private
ZuulProperties
zuulProperties
;
@Autowired
private
Map
<
String
,
ZuulFilter
>
filters
;
@Bean
@Bean
public
RouteLocator
routes
(){
public
RouteLocator
routes
(){
return
new
RouteLocator
();
return
new
RouteLocator
(
discovery
,
zuulProperties
);
}
}
@Bean
@Bean
...
@@ -44,12 +53,12 @@ public class ZuulConfiguration {
...
@@ -44,12 +53,12 @@ public class ZuulConfiguration {
@Bean
@Bean
public
ZuulHandlerMapping
zuulHandlerMapping
()
{
public
ZuulHandlerMapping
zuulHandlerMapping
()
{
return
new
ZuulHandlerMapping
();
return
new
ZuulHandlerMapping
(
routes
(),
zuulController
(),
zuulProperties
);
}
}
@Bean
@Bean
public
FilterInitializer
zuulFilterInitializer
()
{
public
FilterInitializer
zuulFilterInitializer
()
{
return
new
FilterInitializer
();
return
new
FilterInitializer
(
filters
);
}
}
// pre filters
// pre filters
...
...
spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/zuul/ZuulHandlerMapping.java
View file @
05fe1752
package
org
.
springframework
.
cloud
.
netflix
.
zuul
;
package
org
.
springframework
.
cloud
.
netflix
.
zuul
;
import
lombok.extern.slf4j.Slf4j
;
import
java.util.Map
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.cloud.client.discovery.InstanceRegisteredEvent
;
import
org.springframework.cloud.client.discovery.InstanceRegisteredEvent
;
import
org.springframework.context.ApplicationListener
;
import
org.springframework.context.ApplicationListener
;
import
org.springframework.util.StringUtils
;
import
org.springframework.util.StringUtils
;
import
org.springframework.web.servlet.handler.AbstractUrlHandlerMapping
;
import
org.springframework.web.servlet.handler.AbstractUrlHandlerMapping
;
import
javax.annotation.PostConstruct
;
import
java.util.Map
;
/**
/**
* @author Spencer Gibb
* @author Spencer Gibb
*/
*/
@Slf4j
public
class
ZuulHandlerMapping
extends
AbstractUrlHandlerMapping
implements
ApplicationListener
<
InstanceRegisteredEvent
>
{
public
class
ZuulHandlerMapping
extends
AbstractUrlHandlerMapping
implements
ApplicationListener
<
InstanceRegisteredEvent
>
{
@Autowired
private
RouteLocator
routeLocator
;
protected
RouteLocator
routeLocator
;
@Autowired
private
ZuulController
zuul
;
protected
ZuulController
zuul
;
@Autowired
private
ZuulProperties
properties
;
protected
ZuulProperties
properties
;
public
ZuulHandlerMapping
()
{
@Autowired
public
ZuulHandlerMapping
(
RouteLocator
routeLocator
,
ZuulController
zuul
,
ZuulProperties
properties
)
{
this
.
routeLocator
=
routeLocator
;
this
.
zuul
=
zuul
;
this
.
properties
=
properties
;
setOrder
(-
200
);
setOrder
(-
200
);
}
}
@PostConstruct
public
void
init
()
{
}
@Override
@Override
public
void
onApplicationEvent
(
InstanceRegisteredEvent
event
)
{
public
void
onApplicationEvent
(
InstanceRegisteredEvent
event
)
{
registerHandlers
(
routeLocator
.
getRoutes
());
registerHandlers
(
routeLocator
.
getRoutes
());
...
...
spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/RouteLocatorTests.java
View file @
05fe1752
...
@@ -29,7 +29,6 @@ public class RouteLocatorTests {
...
@@ -29,7 +29,6 @@ public class RouteLocatorTests {
@Mock
@Mock
DiscoveryClient
discovery
;
DiscoveryClient
discovery
;
@Before
@Before
public
void
init
()
{
public
void
init
()
{
initMocks
(
this
);
initMocks
(
this
);
...
@@ -37,16 +36,17 @@ public class RouteLocatorTests {
...
@@ -37,16 +36,17 @@ public class RouteLocatorTests {
@Test
@Test
public
void
testGetRoutes
()
{
public
void
testGetRoutes
()
{
RouteLocator
routeLocator
=
new
RouteLocator
();
ZuulProperties
properties
=
new
ZuulProperties
();
routeLocator
.
properties
=
new
ZuulProperties
();
RouteLocator
routeLocator
=
new
RouteLocator
(
this
.
discovery
,
properties
);
routeLocator
.
properties
.
setIgnoredServices
(
Lists
.
newArrayList
(
IGNOREDSERVICE
));
properties
.
setIgnoredServices
(
Lists
.
newArrayList
(
IGNOREDSERVICE
));
routeLocator
.
discovery
=
this
.
discovery
;
routeLocator
.
setEnvironment
(
this
.
env
);
routeLocator
.
env
=
this
.
env
;
MutablePropertySources
propertySources
=
new
MutablePropertySources
();
MutablePropertySources
propertySources
=
new
MutablePropertySources
();
propertySources
.
addFirst
(
new
MockPropertySource
().
withProperty
(
"zuul.route."
+
ASERVICE
,
getMapping
(
ASERVICE
)));
propertySources
.
addFirst
(
new
MockPropertySource
().
withProperty
(
"zuul.route."
+
ASERVICE
,
getMapping
(
ASERVICE
)));
when
(
env
.
getPropertySources
()).
thenReturn
(
propertySources
);
when
(
env
.
getPropertySources
()).
thenReturn
(
propertySources
);
when
(
discovery
.
getServices
()).
thenReturn
(
Lists
.
newArrayList
(
MYSERVICE
,
IGNOREDSERVICE
));
when
(
discovery
.
getServices
()).
thenReturn
(
Lists
.
newArrayList
(
MYSERVICE
,
IGNOREDSERVICE
));
Map
<
String
,
String
>
routesMap
=
routeLocator
.
getRoutes
();
Map
<
String
,
String
>
routesMap
=
routeLocator
.
getRoutes
();
...
@@ -56,16 +56,17 @@ public class RouteLocatorTests {
...
@@ -56,16 +56,17 @@ public class RouteLocatorTests {
assertMapping
(
routesMap
,
ASERVICE
);
assertMapping
(
routesMap
,
ASERVICE
);
String
serviceId
=
routesMap
.
get
(
getMapping
(
IGNOREDSERVICE
));
String
serviceId
=
routesMap
.
get
(
getMapping
(
IGNOREDSERVICE
));
assertNull
(
"routes did not ignore "
+
IGNOREDSERVICE
,
serviceId
);
assertNull
(
"routes did not ignore "
+
IGNOREDSERVICE
,
serviceId
);
}
}
protected
void
assertMapping
(
Map
<
String
,
String
>
routesMap
,
String
expectedServiceId
)
{
protected
void
assertMapping
(
Map
<
String
,
String
>
routesMap
,
String
expectedServiceId
)
{
String
mapping
=
getMapping
(
expectedServiceId
);
String
mapping
=
getMapping
(
expectedServiceId
);
String
serviceId
=
routesMap
.
get
(
mapping
);
String
serviceId
=
routesMap
.
get
(
mapping
);
assertEquals
(
"routesMap had wrong value for "
+
mapping
,
expectedServiceId
,
serviceId
);
assertEquals
(
"routesMap had wrong value for "
+
mapping
,
expectedServiceId
,
serviceId
);
}
}
private
String
getMapping
(
String
serviceId
)
{
private
String
getMapping
(
String
serviceId
)
{
return
"/"
+
serviceId
+
"/**"
;
return
"/"
+
serviceId
+
"/**"
;
}
}
}
}
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