Commit 05fe1752 by Dave Syer

Move field injection into @Configuration

parent cee619d4
...@@ -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,10 +22,13 @@ public class FilterInitializer implements ServletContextListener { ...@@ -23,10 +22,13 @@ 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;
@Override public FilterInitializer(Map<String, ZuulFilter> filters) {
this.filters = filters;
}
@Override
public void contextInitialized(ServletContextEvent sce) { public void contextInitialized(ServletContextEvent sce) {
LOGGER.info("Starting filter initializer context listener"); LOGGER.info("Starting filter initializer context listener");
......
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<>();
@Override
public void setEnvironment(Environment environment) {
env = (ConfigurableEnvironment) environment;
}
public RouteLocator() { public RouteLocator(DiscoveryClient discovery, ZuulProperties properties) {
initField(); this.discovery = discovery;
this.properties = properties;
initField();
} }
private void initField() { private void initField() {
......
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
......
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
protected ZuulController zuul;
@Autowired private ZuulController zuul;
protected ZuulProperties properties;
public ZuulHandlerMapping() { private ZuulProperties properties;
setOrder(-200);
}
@PostConstruct @Autowired
public void init() { public ZuulHandlerMapping(RouteLocator routeLocator, ZuulController zuul, ZuulProperties properties) {
this.routeLocator = routeLocator;
this.zuul = zuul;
this.properties = properties;
setOrder(-200);
} }
@Override @Override
......
...@@ -20,52 +20,53 @@ import static org.mockito.MockitoAnnotations.initMocks; ...@@ -20,52 +20,53 @@ import static org.mockito.MockitoAnnotations.initMocks;
*/ */
public class RouteLocatorTests { public class RouteLocatorTests {
public static final String IGNOREDSERVICE = "ignoredservice"; public static final String IGNOREDSERVICE = "ignoredservice";
public static final String ASERVICE = "aservice"; public static final String ASERVICE = "aservice";
public static final String MYSERVICE = "myservice"; public static final String MYSERVICE = "myservice";
@Mock @Mock
ConfigurableEnvironment env; ConfigurableEnvironment env;
@Mock @Mock
DiscoveryClient discovery; DiscoveryClient discovery;
@Before
public void init() {
initMocks(this);
}
@Before @Test
public void init() { public void testGetRoutes() {
initMocks(this); ZuulProperties properties = new ZuulProperties();
} RouteLocator routeLocator = new RouteLocator(this.discovery, properties);
properties.setIgnoredServices(Lists.newArrayList(IGNOREDSERVICE));
routeLocator.setEnvironment(this.env);
@Test MutablePropertySources propertySources = new MutablePropertySources();
public void testGetRoutes() { propertySources.addFirst(new MockPropertySource().withProperty("zuul.route."
RouteLocator routeLocator = new RouteLocator(); + ASERVICE, getMapping(ASERVICE)));
routeLocator.properties = new ZuulProperties(); when(env.getPropertySources()).thenReturn(propertySources);
routeLocator.properties.setIgnoredServices(Lists.newArrayList(IGNOREDSERVICE)); when(discovery.getServices()).thenReturn(
routeLocator.discovery = this.discovery; Lists.newArrayList(MYSERVICE, IGNOREDSERVICE));
routeLocator.env = this.env;
MutablePropertySources propertySources = new MutablePropertySources(); Map<String, String> routesMap = routeLocator.getRoutes();
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(); assertNotNull("routesMap was null", routesMap);
assertFalse("routesMap was empty", routesMap.isEmpty());
assertMapping(routesMap, MYSERVICE);
assertMapping(routesMap, ASERVICE);
assertNotNull("routesMap was null", routesMap); String serviceId = routesMap.get(getMapping(IGNOREDSERVICE));
assertFalse("routesMap was empty", routesMap.isEmpty()); assertNull("routes did not ignore " + IGNOREDSERVICE, serviceId);
assertMapping(routesMap, MYSERVICE); }
assertMapping(routesMap, ASERVICE);
String serviceId = routesMap.get(getMapping(IGNOREDSERVICE)); protected void assertMapping(Map<String, String> routesMap, String expectedServiceId) {
assertNull("routes did not ignore "+IGNOREDSERVICE, serviceId); 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) { private String getMapping(String serviceId) {
String mapping = getMapping(expectedServiceId); return "/" + serviceId + "/**";
String serviceId = routesMap.get(mapping); }
assertEquals("routesMap had wrong value for "+mapping, expectedServiceId, serviceId);
}
private String getMapping(String serviceId) {
return "/"+ serviceId +"/**";
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment