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
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
101 additions
and
82 deletions
+101
-82
FilterInitializer.java
...springframework/cloud/netflix/zuul/FilterInitializer.java
+5
-3
RouteLocator.java
.../org/springframework/cloud/netflix/zuul/RouteLocator.java
+28
-15
ZuulConfiguration.java
...springframework/cloud/netflix/zuul/ZuulConfiguration.java
+17
-8
ZuulHandlerMapping.java
...pringframework/cloud/netflix/zuul/ZuulHandlerMapping.java
+11
-17
RouteLocatorTests.java
...springframework/cloud/netflix/zuul/RouteLocatorTests.java
+40
-39
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;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
com.netflix.zuul.ZuulFilter
;
import
com.netflix.zuul.filters.FilterRegistry
;
...
...
@@ -23,10 +22,13 @@ public class FilterInitializer implements ServletContextListener {
private
static
final
Logger
LOGGER
=
LoggerFactory
.
getLogger
(
FilterInitializer
.
class
);
@Autowired
private
Map
<
String
,
ZuulFilter
>
filters
;
@Override
public
FilterInitializer
(
Map
<
String
,
ZuulFilter
>
filters
)
{
this
.
filters
=
filters
;
}
@Override
public
void
contextInitialized
(
ServletContextEvent
sce
)
{
LOGGER
.
info
(
"Starting filter initializer context listener"
);
...
...
spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/zuul/RouteLocator.java
View file @
05fe1752
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
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.bind.PropertySourceUtils
;
import
org.springframework.cloud.client.discovery.DiscoveryClient
;
import
org.springframework.cloud.context.environment.EnvironmentChangeEvent
;
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
java.lang.reflect.Field
;
import
java.util.*
;
import
java.util.concurrent.atomic.AtomicReference
;
/**
* @author Spencer Gibb
*/
@Slf4j
public
class
RouteLocator
implements
ApplicationListener
<
EnvironmentChangeEvent
>
{
public
class
RouteLocator
implements
ApplicationListener
<
EnvironmentChangeEvent
>
,
EnvironmentAware
{
public
static
final
String
DEFAULT_ROUTE
=
"/"
;
@Autowired
protected
ConfigurableEnvironment
env
;
private
ConfigurableEnvironment
env
=
new
StandardEnvironment
();
@Autowired
protected
DiscoveryClient
discovery
;
private
DiscoveryClient
discovery
;
@Autowired
protected
ZuulProperties
properties
;
private
ZuulProperties
properties
;
private
Field
propertySourcesField
;
private
AtomicReference
<
LinkedHashMap
<
String
,
String
>>
routes
=
new
AtomicReference
<>();
@Override
public
void
setEnvironment
(
Environment
environment
)
{
env
=
(
ConfigurableEnvironment
)
environment
;
}
public
RouteLocator
()
{
initField
();
public
RouteLocator
(
DiscoveryClient
discovery
,
ZuulProperties
properties
)
{
this
.
discovery
=
discovery
;
this
.
properties
=
properties
;
initField
();
}
private
void
initField
()
{
...
...
spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/zuul/ZuulConfiguration.java
View file @
05fe1752
package
org
.
springframework
.
cloud
.
netflix
.
zuul
;
import
java.util.Map
;
import
com.netflix.zuul.ZuulFilter
;
import
com.netflix.zuul.http.ZuulServlet
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.actuate.trace.TraceRepository
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnClass
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnExpression
;
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.SendResponseFilter
;
import
org.springframework.cloud.netflix.zuul.filters.pre.DebugFilter
;
...
...
@@ -19,7 +24,7 @@ import org.springframework.context.annotation.Configuration;
* @author Spencer Gibb
*/
@Configuration
@EnableConfigurationProperties
()
@EnableConfigurationProperties
(
ZuulProperties
.
class
)
@ConditionalOnClass
(
ZuulServlet
.
class
)
@ConditionalOnExpression
(
"${zuul.enabled:true}"
)
public
class
ZuulConfiguration
{
...
...
@@ -27,14 +32,18 @@ public class ZuulConfiguration {
@Autowired
(
required
=
false
)
private
TraceRepository
traces
;
@Bean
public
ZuulProperties
zuulProperties
()
{
return
new
ZuulProperties
();
}
@Autowired
private
DiscoveryClient
discovery
;
@Autowired
private
ZuulProperties
zuulProperties
;
@Autowired
private
Map
<
String
,
ZuulFilter
>
filters
;
@Bean
public
RouteLocator
routes
(){
return
new
RouteLocator
();
return
new
RouteLocator
(
discovery
,
zuulProperties
);
}
@Bean
...
...
@@ -44,12 +53,12 @@ public class ZuulConfiguration {
@Bean
public
ZuulHandlerMapping
zuulHandlerMapping
()
{
return
new
ZuulHandlerMapping
();
return
new
ZuulHandlerMapping
(
routes
(),
zuulController
(),
zuulProperties
);
}
@Bean
public
FilterInitializer
zuulFilterInitializer
()
{
return
new
FilterInitializer
();
return
new
FilterInitializer
(
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
;
import
lombok.extern.slf4j.Slf4j
;
import
java.util.Map
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.cloud.client.discovery.InstanceRegisteredEvent
;
import
org.springframework.context.ApplicationListener
;
import
org.springframework.util.StringUtils
;
import
org.springframework.web.servlet.handler.AbstractUrlHandlerMapping
;
import
javax.annotation.PostConstruct
;
import
java.util.Map
;
/**
* @author Spencer Gibb
*/
@Slf4j
public
class
ZuulHandlerMapping
extends
AbstractUrlHandlerMapping
implements
ApplicationListener
<
InstanceRegisteredEvent
>
{
@Autowired
protected
RouteLocator
routeLocator
;
@Autowired
protected
ZuulController
zuul
;
private
RouteLocator
routeLocator
;
@Autowired
protected
ZuulProperties
properties
;
private
ZuulController
zuul
;
public
ZuulHandlerMapping
()
{
setOrder
(-
200
);
}
private
ZuulProperties
properties
;
@PostConstruct
public
void
init
()
{
@Autowired
public
ZuulHandlerMapping
(
RouteLocator
routeLocator
,
ZuulController
zuul
,
ZuulProperties
properties
)
{
this
.
routeLocator
=
routeLocator
;
this
.
zuul
=
zuul
;
this
.
properties
=
properties
;
setOrder
(-
200
);
}
@Override
...
...
spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/RouteLocatorTests.java
View file @
05fe1752
...
...
@@ -20,52 +20,53 @@ import static org.mockito.MockitoAnnotations.initMocks;
*/
public
class
RouteLocatorTests
{
public
static
final
String
IGNOREDSERVICE
=
"ignoredservice"
;
public
static
final
String
ASERVICE
=
"aservice"
;
public
static
final
String
MYSERVICE
=
"myservice"
;
@Mock
ConfigurableEnvironment
env
;
public
static
final
String
IGNOREDSERVICE
=
"ignoredservice"
;
public
static
final
String
ASERVICE
=
"aservice"
;
public
static
final
String
MYSERVICE
=
"myservice"
;
@Mock
ConfigurableEnvironment
env
;
@Mock
DiscoveryClient
discovery
;
@Mock
DiscoveryClient
discovery
;
@Before
public
void
init
()
{
initMocks
(
this
);
}
@Before
public
void
init
()
{
initMocks
(
this
);
}
@Test
public
void
testGetRoutes
()
{
ZuulProperties
properties
=
new
ZuulProperties
();
RouteLocator
routeLocator
=
new
RouteLocator
(
this
.
discovery
,
properties
);
properties
.
setIgnoredServices
(
Lists
.
newArrayList
(
IGNOREDSERVICE
));
routeLocator
.
setEnvironment
(
this
.
env
);
@Test
public
void
testGetRoutes
()
{
RouteLocator
routeLocator
=
new
RouteLocator
();
routeLocator
.
properties
=
new
ZuulProperties
();
routeLocator
.
properties
.
setIgnoredServices
(
Lists
.
newArrayList
(
IGNOREDSERVICE
));
routeLocator
.
discovery
=
this
.
discovery
;
routeLocator
.
env
=
this
.
env
;
MutablePropertySources
propertySources
=
new
MutablePropertySources
();
propertySources
.
addFirst
(
new
MockPropertySource
().
withProperty
(
"zuul.route."
+
ASERVICE
,
getMapping
(
ASERVICE
)));
when
(
env
.
getPropertySources
()).
thenReturn
(
propertySources
);
when
(
discovery
.
getServices
()).
thenReturn
(
Lists
.
newArrayList
(
MYSERVICE
,
IGNOREDSERVICE
));
MutablePropertySources
propertySources
=
new
MutablePropertySources
();
propertySources
.
addFirst
(
new
MockPropertySource
().
withProperty
(
"zuul.route."
+
ASERVICE
,
getMapping
(
ASERVICE
)));
when
(
env
.
getPropertySources
()).
thenReturn
(
propertySources
);
when
(
discovery
.
getServices
()).
thenReturn
(
Lists
.
newArrayList
(
MYSERVICE
,
IGNOREDSERVICE
));
Map
<
String
,
String
>
routesMap
=
routeLocator
.
getRoutes
();
Map
<
String
,
String
>
routesMap
=
routeLocator
.
getRoutes
();
assertNotNull
(
"routesMap was null"
,
routesMap
);
assertFalse
(
"routesMap was empty"
,
routesMap
.
isEmpty
());
assertMapping
(
routesMap
,
MYSERVICE
);
assertMapping
(
routesMap
,
ASERVICE
);
assertNotNull
(
"routesMap was null"
,
routesMap
);
assertFalse
(
"routesMap was empty"
,
routesMap
.
isEmpty
());
assertMapping
(
routesMap
,
MYSERVICE
);
assertMapping
(
routesMap
,
ASERVICE
);
String
serviceId
=
routesMap
.
get
(
getMapping
(
IGNOREDSERVICE
));
assertNull
(
"routes did not ignore "
+
IGNOREDSERVICE
,
serviceId
);
}
String
serviceId
=
routesMap
.
get
(
getMapping
(
IGNOREDSERVICE
));
assertNull
(
"routes did not ignore "
+
IGNOREDSERVICE
,
serviceId
);
}
protected
void
assertMapping
(
Map
<
String
,
String
>
routesMap
,
String
expectedServiceId
)
{
String
mapping
=
getMapping
(
expectedServiceId
);
String
serviceId
=
routesMap
.
get
(
mapping
);
assertEquals
(
"routesMap had wrong value for "
+
mapping
,
expectedServiceId
,
serviceId
);
}
protected
void
assertMapping
(
Map
<
String
,
String
>
routesMap
,
String
expectedServiceId
)
{
String
mapping
=
getMapping
(
expectedServiceId
);
String
serviceId
=
routesMap
.
get
(
mapping
);
assertEquals
(
"routesMap had wrong value for "
+
mapping
,
expectedServiceId
,
serviceId
);
}
private
String
getMapping
(
String
serviceId
)
{
return
"/"
+
serviceId
+
"/**"
;
}
private
String
getMapping
(
String
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