apis = new HashSet<>();
+ for (HttpRequestMessageApiMatcher matcher : ZuulGatewayApiMatcherManager.getApiMatcherMap().values()) {
+ if (matcher.test(message)) {
+ apis.add(matcher.getApiName());
+ }
+ }
+ return apis;
+ }
+
+ @Override
+ public boolean shouldFilter(HttpRequestMessage msg) {
+ return true;
+ }
+}
diff --git a/sentinel-adapter/sentinel-zuul2-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/gateway/zuul2/filters/outbound/SentinelZuulOutboundFilter.java b/sentinel-adapter/sentinel-zuul2-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/gateway/zuul2/filters/outbound/SentinelZuulOutboundFilter.java
new file mode 100644
index 00000000..75f6a179
--- /dev/null
+++ b/sentinel-adapter/sentinel-zuul2-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/gateway/zuul2/filters/outbound/SentinelZuulOutboundFilter.java
@@ -0,0 +1,90 @@
+/*
+ * Copyright 1999-2018 Alibaba Group Holding Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.alibaba.csp.sentinel.adapter.gateway.zuul2.filters.outbound;
+
+import java.util.Deque;
+import java.util.List;
+import java.util.stream.Collectors;
+
+import com.alibaba.csp.sentinel.AsyncEntry;
+import com.alibaba.csp.sentinel.Tracer;
+import com.alibaba.csp.sentinel.adapter.gateway.zuul2.constants.ZuulConstant;
+import com.alibaba.csp.sentinel.adapter.gateway.zuul2.filters.EntryHolder;
+import com.alibaba.csp.sentinel.context.ContextUtil;
+import com.alibaba.csp.sentinel.slots.block.BlockException;
+import com.netflix.zuul.context.SessionContext;
+import com.netflix.zuul.filters.FilterError;
+import com.netflix.zuul.filters.http.HttpOutboundFilter;
+import com.netflix.zuul.message.http.HttpResponseMessage;
+import org.apache.commons.collections.CollectionUtils;
+import rx.Observable;
+
+/**
+ * Zuul2 outboundFilter for Sentinel.
+ *
+ * The filter will complete the entries and trace the exception that happen in previous filters.
+ *
+ * @author wavesZh
+ */
+public class SentinelZuulOutboundFilter extends HttpOutboundFilter {
+
+ private final int order;
+
+ public SentinelZuulOutboundFilter(int order) {
+ this.order = order;
+ }
+
+ @Override
+ public int filterOrder() {
+ return order;
+ }
+
+ @Override
+ public Observable applyAsync(HttpResponseMessage input) {
+ return Observable.just(apply(input));
+ }
+
+ public HttpResponseMessage apply(HttpResponseMessage response) {
+ SessionContext context = response.getContext();
+
+ if (context.get(ZuulConstant.ZUUL_CTX_SENTINEL_ENTRIES_KEY) == null) {
+ return response;
+ }
+ List errors = context.getFilterErrors().stream()
+ .filter(e -> BlockException.isBlockException(e.getException()))
+ .collect(Collectors.toList());
+ boolean notBlocked = true;
+ if (CollectionUtils.isEmpty(errors)) {
+ notBlocked = false;
+ }
+ Deque holders = (Deque) context.get(ZuulConstant.ZUUL_CTX_SENTINEL_ENTRIES_KEY);
+ while (!holders.isEmpty()) {
+ EntryHolder holder = holders.pop();
+ if (notBlocked) {
+ Tracer.traceEntry(context.getError(), holder.getEntry());
+ }
+ holder.getEntry().exit(1, holder.getParams());
+ }
+ return response;
+ }
+
+
+ @Override
+ public boolean shouldFilter(HttpResponseMessage msg) {
+ return true;
+ }
+}
diff --git a/sentinel-adapter/sentinel-zuul2-adapter/src/main/resources/META-INF/services/com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinitionChangeObserver b/sentinel-adapter/sentinel-zuul2-adapter/src/main/resources/META-INF/services/com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinitionChangeObserver
new file mode 100644
index 00000000..6c27b158
--- /dev/null
+++ b/sentinel-adapter/sentinel-zuul2-adapter/src/main/resources/META-INF/services/com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinitionChangeObserver
@@ -0,0 +1 @@
+com.alibaba.csp.sentinel.adapter.gateway.zuul2.api.ZuulApiDefinitionChangeObserver
\ No newline at end of file
diff --git a/sentinel-adapter/sentinel-zuul2-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/gateway/zuul2/fallback/ZuulBlockFallbackManagerTest.java b/sentinel-adapter/sentinel-zuul2-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/gateway/zuul2/fallback/ZuulBlockFallbackManagerTest.java
new file mode 100644
index 00000000..1b0416d5
--- /dev/null
+++ b/sentinel-adapter/sentinel-zuul2-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/gateway/zuul2/fallback/ZuulBlockFallbackManagerTest.java
@@ -0,0 +1,61 @@
+/*
+ * Copyright 1999-2018 Alibaba Group Holding Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.alibaba.csp.sentinel.adapter.gateway.zuul2.fallback;
+
+import com.alibaba.csp.sentinel.slots.block.flow.FlowException;
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * @author tiger
+ */
+public class ZuulBlockFallbackManagerTest {
+
+ private String ROUTE = "/test";
+
+ private String DEFAULT_ROUTE = "*";
+
+ class MyNullResponseFallBackProvider implements ZuulBlockFallbackProvider {
+ @Override
+ public String getRoute() {
+ return ROUTE;
+ }
+
+ @Override
+ public BlockResponse fallbackResponse(String route, Throwable cause) {
+ return null;
+ }
+ }
+
+ @Test
+ public void testRegisterProvider() throws Exception {
+ MyNullResponseFallBackProvider myNullResponseFallBackProvider = new MyNullResponseFallBackProvider();
+ ZuulBlockFallbackManager.registerProvider(myNullResponseFallBackProvider);
+ Assert.assertEquals(myNullResponseFallBackProvider.getRoute(), ROUTE);
+ Assert.assertNull(myNullResponseFallBackProvider.fallbackResponse(ROUTE, new FlowException("flow ex")));
+ }
+
+ @Test
+ public void clear() {
+ MyNullResponseFallBackProvider myNullResponseFallBackProvider = new MyNullResponseFallBackProvider();
+ ZuulBlockFallbackManager.registerProvider(myNullResponseFallBackProvider);
+ Assert.assertEquals(myNullResponseFallBackProvider.getRoute(), ROUTE);
+ ZuulBlockFallbackManager.clear();
+ Assert.assertEquals(ZuulBlockFallbackManager.getFallbackProvider(ROUTE).getRoute(), DEFAULT_ROUTE);
+ }
+
+}
\ No newline at end of file
diff --git a/sentinel-adapter/sentinel-zuul2-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/gateway/zuul2/fallback/ZuulBlockFallbackProviderTest.java b/sentinel-adapter/sentinel-zuul2-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/gateway/zuul2/fallback/ZuulBlockFallbackProviderTest.java
new file mode 100644
index 00000000..59f6d74a
--- /dev/null
+++ b/sentinel-adapter/sentinel-zuul2-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/gateway/zuul2/fallback/ZuulBlockFallbackProviderTest.java
@@ -0,0 +1,62 @@
+/*
+ * Copyright 1999-2018 Alibaba Group Holding Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.alibaba.csp.sentinel.adapter.gateway.zuul2.fallback;
+
+import com.alibaba.csp.sentinel.slots.block.flow.FlowException;
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * @author tiger
+ */
+public class ZuulBlockFallbackProviderTest {
+
+ private String ALL_ROUTE = "*";
+
+ @Test
+ public void testGetNullRoute() throws Exception {
+ ZuulBlockFallbackProvider fallbackProvider = ZuulBlockFallbackManager.getFallbackProvider(null);
+ Assert.assertEquals(fallbackProvider.getRoute(), ALL_ROUTE);
+ }
+
+ @Test
+ public void testGetDefaultRoute() throws Exception {
+ ZuulBlockFallbackProvider fallbackProvider = ZuulBlockFallbackManager.getFallbackProvider(ALL_ROUTE);
+ Assert.assertEquals(fallbackProvider.getRoute(), ALL_ROUTE);
+ }
+
+ @Test
+ public void testGetNotInCacheRoute() throws Exception {
+ ZuulBlockFallbackProvider fallbackProvider = ZuulBlockFallbackManager.getFallbackProvider("/not/in");
+ Assert.assertEquals(fallbackProvider.getRoute(), ALL_ROUTE);
+ }
+
+ @Test
+ public void testFlowControlFallbackResponse() throws Exception {
+ ZuulBlockFallbackProvider fallbackProvider = ZuulBlockFallbackManager.getFallbackProvider(ALL_ROUTE);
+ BlockResponse clientHttpResponse = fallbackProvider.fallbackResponse(ALL_ROUTE,
+ new FlowException("flow exception"));
+ Assert.assertEquals(clientHttpResponse.getCode(), 429);
+ }
+
+ @Test
+ public void testRuntimeExceptionFallbackResponse() throws Exception {
+ ZuulBlockFallbackProvider fallbackProvider = ZuulBlockFallbackManager.getFallbackProvider(ALL_ROUTE);
+ BlockResponse clientHttpResponse = fallbackProvider.fallbackResponse(ALL_ROUTE, new RuntimeException());
+ Assert.assertEquals(clientHttpResponse.getCode(), 500);
+ }
+}
\ No newline at end of file
diff --git a/sentinel-demo/pom.xml b/sentinel-demo/pom.xml
index 2e03da0e..dfb5980f 100755
--- a/sentinel-demo/pom.xml
+++ b/sentinel-demo/pom.xml
@@ -37,6 +37,7 @@
sentinel-demo-zuul-gateway
sentinel-demo-etcd-datasource
sentinel-demo-spring-webmvc
+ sentinel-demo-zuul2-gateway
sentinel-demo-log-logback
diff --git a/sentinel-demo/sentinel-demo-zuul2-gateway/pom.xml b/sentinel-demo/sentinel-demo-zuul2-gateway/pom.xml
new file mode 100644
index 00000000..01e562a7
--- /dev/null
+++ b/sentinel-demo/sentinel-demo-zuul2-gateway/pom.xml
@@ -0,0 +1,48 @@
+
+
+
+ sentinel-demo
+ com.alibaba.csp
+ 1.7.2-SNAPSHOT
+
+ 4.0.0
+
+ sentinel-demo-zuul2-gateway
+
+
+
+ com.alibaba.csp
+ sentinel-zuul2-adapter
+ ${project.version}
+
+
+ com.alibaba.csp
+ sentinel-transport-simple-http
+
+
+ com.netflix.zuul
+ zuul-core
+ 2.1.5
+
+
+
+ org.slf4j
+ slf4j-api
+ 1.7.28
+
+
+ org.slf4j
+ slf4j-log4j12
+ 1.7.28
+
+
+
+ org.apache.httpcomponents
+ httpclient
+ 4.5.8
+
+
+
+
\ No newline at end of file
diff --git a/sentinel-demo/sentinel-demo-zuul2-gateway/src/main/java/com/alibaba/csp/sentinel/demo/zuul2/gateway/Bootstrap.java b/sentinel-demo/sentinel-demo-zuul2-gateway/src/main/java/com/alibaba/csp/sentinel/demo/zuul2/gateway/Bootstrap.java
new file mode 100644
index 00000000..335c3505
--- /dev/null
+++ b/sentinel-demo/sentinel-demo-zuul2-gateway/src/main/java/com/alibaba/csp/sentinel/demo/zuul2/gateway/Bootstrap.java
@@ -0,0 +1,96 @@
+package com.alibaba.csp.sentinel.demo.zuul2.gateway;
+
+import java.io.IOException;
+import java.util.HashSet;
+import java.util.Set;
+
+import com.alibaba.csp.sentinel.adapter.gateway.common.SentinelGatewayConstants;
+import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinition;
+import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPathPredicateItem;
+import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPredicateItem;
+import com.alibaba.csp.sentinel.adapter.gateway.common.api.GatewayApiDefinitionManager;
+import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule;
+import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayParamFlowItem;
+import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayRuleManager;
+import com.google.inject.Injector;
+import com.google.inject.Scopes;
+import com.netflix.appinfo.EurekaInstanceConfig;
+import com.netflix.appinfo.providers.MyDataCenterInstanceConfigProvider;
+import com.netflix.config.ConfigurationManager;
+import com.netflix.governator.InjectorBuilder;
+import com.netflix.zuul.netty.server.BaseServerStartup;
+import com.netflix.zuul.netty.server.Server;
+
+public class Bootstrap {
+ public static void main(String[] args) {
+ new Bootstrap().start();
+ }
+
+ public void start() {
+ Server server;
+ try {
+ new GatewayRuleConfig().doInit();
+
+ ConfigurationManager.loadCascadedPropertiesFromResources("application");
+ Injector injector = InjectorBuilder.fromModule(new ZuulModule()).createInjector();
+ injector.getInstance(FiltersRegisteringService.class);
+ BaseServerStartup serverStartup = injector.getInstance(BaseServerStartup.class);
+ server = serverStartup.server();
+ server.start(true);
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+
+ private void initGatewayRules() {
+ Set rules = new HashSet<>();
+ rules.add(new GatewayFlowRule("another_customized_api")
+ .setResourceMode(SentinelGatewayConstants.RESOURCE_MODE_CUSTOM_API_NAME)
+ .setCount(1)
+ .setIntervalSec(1)
+ .setParamItem(new GatewayParamFlowItem()
+ .setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_URL_PARAM)
+ .setFieldName("pa")
+ )
+ );
+ rules.add(new GatewayFlowRule("some_customized_api")
+ .setResourceMode(SentinelGatewayConstants.RESOURCE_MODE_CUSTOM_API_NAME)
+ .setCount(5)
+ .setIntervalSec(1)
+ .setParamItem(new GatewayParamFlowItem()
+ .setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_URL_PARAM)
+ .setFieldName("pn")
+ )
+ );
+ GatewayRuleManager.loadRules(rules);
+ }
+
+ private void initCustomizedApis() {
+ Set definitions = new HashSet<>();
+ ApiDefinition api1 = new ApiDefinition("some_customized_api")
+ .setPredicateItems(new HashSet() {{
+ add(new ApiPathPredicateItem().setPattern("/ahas"));
+ add(new ApiPathPredicateItem().setPattern("/aliyun/**")
+ .setMatchStrategy(SentinelGatewayConstants.URL_MATCH_STRATEGY_PREFIX));
+ }});
+ ApiDefinition api2 = new ApiDefinition("another_customized_api")
+ .setPredicateItems(new HashSet() {{
+ add(new ApiPathPredicateItem().setPattern("/**")
+ .setMatchStrategy(SentinelGatewayConstants.URL_MATCH_STRATEGY_PREFIX));
+ }});
+ definitions.add(api1);
+ definitions.add(api2);
+ GatewayApiDefinitionManager.loadApiDefinitions(definitions);
+ }
+
+ public static class ZuulModule extends ZuulSampleModule {
+ @Override
+ protected void configure() {
+ //DataCenterInfo
+ bind(EurekaInstanceConfig.class)
+ .toProvider(MyDataCenterInstanceConfigProvider.class)
+ .in(Scopes.SINGLETON);
+ super.configure();
+ }
+ }
+}
diff --git a/sentinel-demo/sentinel-demo-zuul2-gateway/src/main/java/com/alibaba/csp/sentinel/demo/zuul2/gateway/FiltersRegisteringService.java b/sentinel-demo/sentinel-demo-zuul2-gateway/src/main/java/com/alibaba/csp/sentinel/demo/zuul2/gateway/FiltersRegisteringService.java
new file mode 100755
index 00000000..c4d1b3c7
--- /dev/null
+++ b/sentinel-demo/sentinel-demo-zuul2-gateway/src/main/java/com/alibaba/csp/sentinel/demo/zuul2/gateway/FiltersRegisteringService.java
@@ -0,0 +1,34 @@
+package com.alibaba.csp.sentinel.demo.zuul2.gateway;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Set;
+
+import javax.annotation.PostConstruct;
+import javax.inject.Inject;
+
+import com.netflix.zuul.filters.FilterRegistry;
+import com.netflix.zuul.filters.ZuulFilter;
+
+public class FiltersRegisteringService {
+
+ private final List filters;
+ private final FilterRegistry filterRegistry;
+
+ @Inject
+ public FiltersRegisteringService(FilterRegistry filterRegistry, Set filters) {
+ this.filters = new ArrayList<>(filters);
+ this.filterRegistry = filterRegistry;
+ }
+
+ public List getFilters() {
+ return filters;
+ }
+
+ @PostConstruct
+ public void initialize() {
+ for (ZuulFilter filter: filters) {
+ this.filterRegistry.put(filter.filterName(), filter);
+ }
+ }
+}
diff --git a/sentinel-demo/sentinel-demo-zuul2-gateway/src/main/java/com/alibaba/csp/sentinel/demo/zuul2/gateway/GatewayRuleConfig.java b/sentinel-demo/sentinel-demo-zuul2-gateway/src/main/java/com/alibaba/csp/sentinel/demo/zuul2/gateway/GatewayRuleConfig.java
new file mode 100644
index 00000000..33bea390
--- /dev/null
+++ b/sentinel-demo/sentinel-demo-zuul2-gateway/src/main/java/com/alibaba/csp/sentinel/demo/zuul2/gateway/GatewayRuleConfig.java
@@ -0,0 +1,87 @@
+package com.alibaba.csp.sentinel.demo.zuul2.gateway;
+
+import com.alibaba.csp.sentinel.adapter.gateway.common.SentinelGatewayConstants;
+import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinition;
+import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPathPredicateItem;
+import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPredicateItem;
+import com.alibaba.csp.sentinel.adapter.gateway.common.api.GatewayApiDefinitionManager;
+import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule;
+import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayParamFlowItem;
+import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayRuleManager;
+import com.alibaba.csp.sentinel.slots.block.RuleConstant;
+
+import java.util.HashSet;
+import java.util.Set;
+
+public class GatewayRuleConfig {
+
+ public void doInit() {
+ // Prepare some gateway rules and API definitions (only for demo).
+ // It's recommended to leverage dynamic data source or the Sentinel dashboard to push the rules.
+ initCustomizedApis();
+ initGatewayRules();
+ }
+
+ private void initCustomizedApis() {
+ Set definitions = new HashSet<>();
+ ApiDefinition api1 = new ApiDefinition("some_customized_api")
+ .setPredicateItems(new HashSet() {{
+ add(new ApiPathPredicateItem().setPattern("/images"));
+ add(new ApiPathPredicateItem().setPattern("/comments")
+ .setMatchStrategy(SentinelGatewayConstants.URL_MATCH_STRATEGY_PREFIX));
+ }});
+ ApiDefinition api2 = new ApiDefinition("another_customized_api")
+ .setPredicateItems(new HashSet() {{
+ add(new ApiPathPredicateItem().setPattern("/**")
+ .setMatchStrategy(SentinelGatewayConstants.URL_MATCH_STRATEGY_PREFIX));
+ }});
+ definitions.add(api1);
+ definitions.add(api2);
+ GatewayApiDefinitionManager.loadApiDefinitions(definitions);
+ }
+
+ private void initGatewayRules() {
+ Set rules = new HashSet<>();
+ rules.add(new GatewayFlowRule("images")
+ .setCount(10)
+ .setIntervalSec(1)
+ );
+ rules.add(new GatewayFlowRule("images")
+ .setCount(2)
+ .setIntervalSec(2)
+ .setBurst(2)
+ .setParamItem(new GatewayParamFlowItem()
+ .setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_CLIENT_IP)
+ )
+ );
+ rules.add(new GatewayFlowRule("comments")
+ .setCount(3)
+ .setIntervalSec(1)
+ .setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_RATE_LIMITER)
+ .setMaxQueueingTimeoutMs(6000)
+ .setParamItem(new GatewayParamFlowItem()
+ .setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_HEADER)
+ .setFieldName("X-Sentinel-Flag")
+ )
+ );
+ rules.add(new GatewayFlowRule("comments")
+ .setCount(1)
+ .setIntervalSec(1)
+ .setParamItem(new GatewayParamFlowItem()
+ .setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_URL_PARAM)
+ .setFieldName("pa")
+ )
+ );
+
+ rules.add(new GatewayFlowRule("some_customized_api")
+ .setResourceMode(SentinelGatewayConstants.RESOURCE_MODE_CUSTOM_API_NAME)
+ .setCount(5)
+ .setIntervalSec(1)
+ .setParamItem(new GatewayParamFlowItem()
+ .setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_URL_PARAM)
+ .setFieldName("pn")
+ )
+ );
+ GatewayRuleManager.loadRules(rules);
+ }
+}
\ No newline at end of file
diff --git a/sentinel-demo/sentinel-demo-zuul2-gateway/src/main/java/com/alibaba/csp/sentinel/demo/zuul2/gateway/SampleServerStartup.java b/sentinel-demo/sentinel-demo-zuul2-gateway/src/main/java/com/alibaba/csp/sentinel/demo/zuul2/gateway/SampleServerStartup.java
new file mode 100644
index 00000000..cbf86450
--- /dev/null
+++ b/sentinel-demo/sentinel-demo-zuul2-gateway/src/main/java/com/alibaba/csp/sentinel/demo/zuul2/gateway/SampleServerStartup.java
@@ -0,0 +1,61 @@
+package com.alibaba.csp.sentinel.demo.zuul2.gateway;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.inject.Inject;
+import javax.inject.Singleton;
+
+import com.netflix.appinfo.ApplicationInfoManager;
+import com.netflix.config.DynamicIntProperty;
+import com.netflix.discovery.EurekaClient;
+import com.netflix.netty.common.accesslog.AccessLogPublisher;
+import com.netflix.netty.common.channel.config.ChannelConfig;
+import com.netflix.netty.common.channel.config.CommonChannelConfigKeys;
+import com.netflix.netty.common.metrics.EventLoopGroupMetrics;
+import com.netflix.netty.common.proxyprotocol.StripUntrustedProxyHeadersHandler;
+import com.netflix.netty.common.ssl.ServerSslConfig;
+import com.netflix.netty.common.status.ServerStatusManager;
+import com.netflix.spectator.api.Registry;
+import com.netflix.zuul.FilterLoader;
+import com.netflix.zuul.FilterUsageNotifier;
+import com.netflix.zuul.RequestCompleteHandler;
+import com.netflix.zuul.context.SessionContextDecorator;
+import com.netflix.zuul.netty.server.BaseServerStartup;
+import com.netflix.zuul.netty.server.DirectMemoryMonitor;
+import com.netflix.zuul.netty.server.ZuulServerChannelInitializer;
+import io.netty.channel.ChannelInitializer;
+import io.netty.channel.group.ChannelGroup;
+
+@Singleton
+public class SampleServerStartup extends BaseServerStartup {
+
+ @Inject
+ public SampleServerStartup(ServerStatusManager serverStatusManager, FilterLoader filterLoader, SessionContextDecorator sessionCtxDecorator, FilterUsageNotifier usageNotifier, RequestCompleteHandler reqCompleteHandler, Registry registry, DirectMemoryMonitor directMemoryMonitor, EventLoopGroupMetrics eventLoopGroupMetrics, EurekaClient discoveryClient, ApplicationInfoManager applicationInfoManager, AccessLogPublisher accessLogPublisher) {
+ super(serverStatusManager, filterLoader, sessionCtxDecorator, usageNotifier, reqCompleteHandler, registry, directMemoryMonitor, eventLoopGroupMetrics, discoveryClient, applicationInfoManager, accessLogPublisher);
+ }
+
+ @Override
+ protected Map choosePortsAndChannels(ChannelGroup clientChannels) {
+ Map portsToChannels = new HashMap<>();
+ int port = new DynamicIntProperty("zuul.server.port.main", 8085).get();
+
+ String mainPortName = "main";
+ ChannelConfig channelConfig = BaseServerStartup.defaultChannelConfig(mainPortName);
+ ServerSslConfig sslConfig;
+ /* These settings may need to be tweaked depending if you're running behind an ELB HTTP listener, TCP listener,
+ * or directly on the internet.
+ */
+ ChannelConfig channelDependencies = defaultChannelDependencies(mainPortName);
+
+ channelConfig.set(CommonChannelConfigKeys.allowProxyHeadersWhen, StripUntrustedProxyHeadersHandler.AllowWhen.ALWAYS);
+ channelConfig.set(CommonChannelConfigKeys.preferProxyProtocolForClientIp, false);
+ channelConfig.set(CommonChannelConfigKeys.isSSlFromIntermediary, false);
+ channelConfig.set(CommonChannelConfigKeys.withProxyProtocol, false);
+
+ portsToChannels.put(port, new ZuulServerChannelInitializer(port, channelConfig, channelDependencies, clientChannels));
+ logPortConfigured(port, null);
+
+ return portsToChannels;
+ }
+}
diff --git a/sentinel-demo/sentinel-demo-zuul2-gateway/src/main/java/com/alibaba/csp/sentinel/demo/zuul2/gateway/ZuulClasspathFiltersModule.java b/sentinel-demo/sentinel-demo-zuul2-gateway/src/main/java/com/alibaba/csp/sentinel/demo/zuul2/gateway/ZuulClasspathFiltersModule.java
new file mode 100755
index 00000000..d3d9f505
--- /dev/null
+++ b/sentinel-demo/sentinel-demo-zuul2-gateway/src/main/java/com/alibaba/csp/sentinel/demo/zuul2/gateway/ZuulClasspathFiltersModule.java
@@ -0,0 +1,32 @@
+package com.alibaba.csp.sentinel.demo.zuul2.gateway;
+
+import com.alibaba.csp.sentinel.adapter.gateway.zuul2.filters.endpoint.SentinelZuulEndpoint;
+import com.alibaba.csp.sentinel.adapter.gateway.zuul2.filters.inbound.SentinelZuulInboundFilter;
+import com.alibaba.csp.sentinel.adapter.gateway.zuul2.filters.outbound.SentinelZuulOutboundFilter;
+import com.alibaba.csp.sentinel.demo.zuul2.gateway.filters.Route;
+import com.google.inject.AbstractModule;
+import com.google.inject.multibindings.Multibinder;
+import com.netflix.zuul.BasicFilterUsageNotifier;
+import com.netflix.zuul.DynamicCodeCompiler;
+import com.netflix.zuul.FilterFactory;
+import com.netflix.zuul.FilterUsageNotifier;
+import com.netflix.zuul.filters.ZuulFilter;
+import com.netflix.zuul.groovy.GroovyCompiler;
+import com.netflix.zuul.guice.GuiceFilterFactory;
+
+
+public class ZuulClasspathFiltersModule extends AbstractModule {
+ @Override
+ protected void configure() {
+ bind(DynamicCodeCompiler.class).to(GroovyCompiler.class);
+ bind(FilterFactory.class).to(GuiceFilterFactory.class);
+
+ bind(FilterUsageNotifier.class).to(BasicFilterUsageNotifier.class);
+
+ Multibinder filterMultibinder = Multibinder.newSetBinder(binder(), ZuulFilter.class);
+ filterMultibinder.addBinding().toInstance(new SentinelZuulInboundFilter(500));
+ filterMultibinder.addBinding().toInstance(new SentinelZuulOutboundFilter(500));
+ filterMultibinder.addBinding().toInstance(new SentinelZuulEndpoint());
+ filterMultibinder.addBinding().toInstance(new Route());
+ }
+}
diff --git a/sentinel-demo/sentinel-demo-zuul2-gateway/src/main/java/com/alibaba/csp/sentinel/demo/zuul2/gateway/ZuulSampleModule.java b/sentinel-demo/sentinel-demo-zuul2-gateway/src/main/java/com/alibaba/csp/sentinel/demo/zuul2/gateway/ZuulSampleModule.java
new file mode 100644
index 00000000..a52d62e9
--- /dev/null
+++ b/sentinel-demo/sentinel-demo-zuul2-gateway/src/main/java/com/alibaba/csp/sentinel/demo/zuul2/gateway/ZuulSampleModule.java
@@ -0,0 +1,61 @@
+package com.alibaba.csp.sentinel.demo.zuul2.gateway;
+
+import com.google.inject.AbstractModule;
+import com.netflix.discovery.AbstractDiscoveryClientOptionalArgs;
+import com.netflix.discovery.DiscoveryClient;
+import com.netflix.netty.common.accesslog.AccessLogPublisher;
+import com.netflix.netty.common.status.ServerStatusManager;
+import com.netflix.spectator.api.DefaultRegistry;
+import com.netflix.spectator.api.Registry;
+import com.netflix.zuul.BasicRequestCompleteHandler;
+import com.netflix.zuul.FilterFileManager;
+import com.netflix.zuul.RequestCompleteHandler;
+import com.netflix.zuul.context.SessionContextDecorator;
+import com.netflix.zuul.context.ZuulSessionContextDecorator;
+import com.netflix.zuul.init.ZuulFiltersModule;
+import com.netflix.zuul.netty.server.BaseServerStartup;
+import com.netflix.zuul.netty.server.ClientRequestReceiver;
+import com.netflix.zuul.origins.BasicNettyOriginManager;
+import com.netflix.zuul.origins.OriginManager;
+import com.netflix.zuul.stats.BasicRequestMetricsPublisher;
+import com.netflix.zuul.stats.RequestMetricsPublisher;
+
+/**
+ * Zuul Sample Module
+ *
+ * Author: Arthur Gonigberg
+ * Date: November 20, 2017
+ */
+public class ZuulSampleModule extends AbstractModule {
+ @Override
+ protected void configure() {
+ // sample specific bindings
+ bind(BaseServerStartup.class).to(SampleServerStartup.class);
+
+ // use provided basic netty origin manager
+ bind(OriginManager.class).to(BasicNettyOriginManager.class);
+
+ // zuul filter loading
+ install(new ZuulFiltersModule());
+ bind(FilterFileManager.class).asEagerSingleton();
+
+ install(new ZuulClasspathFiltersModule());
+ // general server bindings
+ // health/discovery status
+ bind(ServerStatusManager.class);
+ // decorate new sessions when requests come in
+ bind(SessionContextDecorator.class).to(ZuulSessionContextDecorator.class);
+ // atlas metrics registry
+ bind(Registry.class).to(DefaultRegistry.class);
+ // metrics post-request completion
+ bind(RequestCompleteHandler.class).to(BasicRequestCompleteHandler.class);
+ // discovery client
+ bind(AbstractDiscoveryClientOptionalArgs.class).to(DiscoveryClient.DiscoveryClientOptionalArgs.class);
+ // timings publisher
+ bind(RequestMetricsPublisher.class).to(BasicRequestMetricsPublisher.class);
+
+ // access logger, including request ID generator
+ bind(AccessLogPublisher.class).toInstance(new AccessLogPublisher("ACCESS",
+ (channel, httpRequest) -> ClientRequestReceiver.getRequestFromChannel(channel).getContext().getUUID()));
+ }
+}
diff --git a/sentinel-demo/sentinel-demo-zuul2-gateway/src/main/java/com/alibaba/csp/sentinel/demo/zuul2/gateway/filters/NotFoundEndpoint.java b/sentinel-demo/sentinel-demo-zuul2-gateway/src/main/java/com/alibaba/csp/sentinel/demo/zuul2/gateway/filters/NotFoundEndpoint.java
new file mode 100644
index 00000000..c50b9746
--- /dev/null
+++ b/sentinel-demo/sentinel-demo-zuul2-gateway/src/main/java/com/alibaba/csp/sentinel/demo/zuul2/gateway/filters/NotFoundEndpoint.java
@@ -0,0 +1,17 @@
+package com.alibaba.csp.sentinel.demo.zuul2.gateway.filters;
+
+import com.netflix.zuul.filters.http.HttpSyncEndpoint;
+import com.netflix.zuul.message.http.HttpRequestMessage;
+import com.netflix.zuul.message.http.HttpResponseMessage;
+import com.netflix.zuul.message.http.HttpResponseMessageImpl;
+import org.apache.http.HttpStatus;
+
+public class NotFoundEndpoint extends HttpSyncEndpoint {
+
+ @Override
+ public HttpResponseMessage apply(HttpRequestMessage request) {
+ HttpResponseMessage response = new HttpResponseMessageImpl(request.getContext(), request, HttpStatus.SC_NOT_FOUND);
+ response.finishBufferedBodyIfIncomplete();
+ return response;
+ }
+}
\ No newline at end of file
diff --git a/sentinel-demo/sentinel-demo-zuul2-gateway/src/main/java/com/alibaba/csp/sentinel/demo/zuul2/gateway/filters/Route.java b/sentinel-demo/sentinel-demo-zuul2-gateway/src/main/java/com/alibaba/csp/sentinel/demo/zuul2/gateway/filters/Route.java
new file mode 100644
index 00000000..6b0233b2
--- /dev/null
+++ b/sentinel-demo/sentinel-demo-zuul2-gateway/src/main/java/com/alibaba/csp/sentinel/demo/zuul2/gateway/filters/Route.java
@@ -0,0 +1,36 @@
+package com.alibaba.csp.sentinel.demo.zuul2.gateway.filters;
+
+import com.netflix.zuul.context.SessionContext;
+import com.netflix.zuul.filters.http.HttpInboundSyncFilter;
+import com.netflix.zuul.message.http.HttpRequestMessage;
+import com.netflix.zuul.netty.filter.ZuulEndPointRunner;
+
+public class Route extends HttpInboundSyncFilter {
+ @Override
+ public HttpRequestMessage apply(HttpRequestMessage request) {
+ SessionContext context = request.getContext();
+ switch (request.getPath()) {
+ case "/images":
+ context.setEndpoint(ZuulEndPointRunner.PROXY_ENDPOINT_FILTER_NAME);
+ context.setRouteVIP("images");
+ break;
+ case "/comments":
+ context.setEndpoint(ZuulEndPointRunner.PROXY_ENDPOINT_FILTER_NAME);
+ context.setRouteVIP("comments");
+ break;
+ default:
+ context.setEndpoint(NotFoundEndpoint.class.getCanonicalName());
+ }
+ return request;
+ }
+
+ @Override
+ public int filterOrder() {
+ return 0;
+ }
+
+ @Override
+ public boolean shouldFilter(HttpRequestMessage msg) {
+ return true;
+ }
+}
diff --git a/sentinel-demo/sentinel-demo-zuul2-gateway/src/main/resources/application.properties b/sentinel-demo/sentinel-demo-zuul2-gateway/src/main/resources/application.properties
new file mode 100644
index 00000000..7c04c2ff
--- /dev/null
+++ b/sentinel-demo/sentinel-demo-zuul2-gateway/src/main/resources/application.properties
@@ -0,0 +1,16 @@
+zuul.server.port.main=8887
+
+# Deactivate Eureka
+eureka.registration.enabled = false
+eureka.preferSameZone = false
+eureka.shouldUseDns = false
+eureka.shouldFetchRegistry=false
+
+# Loading Filters
+zuul.filters.packages = com.netflix.zuul.filters.common,com.alibaba.csp.sentinel.adapter.gateway.zuul2.filters.endpoint,com.alibaba.csp.sentinel.demo.zuul2.gateway.filters
+
+# Routing to proxied back-end services
+comments.ribbon.listOfServers=localhost:8081
+comments.ribbon.client.NIWSServerListClassName=com.netflix.loadbalancer.ConfigurationBasedServerList
+images.ribbon.listOfServers=localhost:8082
+images.ribbon.client.NIWSServerListClassName=com.netflix.loadbalancer.ConfigurationBasedServerList
\ No newline at end of file
diff --git a/sentinel-demo/sentinel-demo-zuul2-gateway/src/main/resources/log4j.properties b/sentinel-demo/sentinel-demo-zuul2-gateway/src/main/resources/log4j.properties
new file mode 100644
index 00000000..15b98e6b
--- /dev/null
+++ b/sentinel-demo/sentinel-demo-zuul2-gateway/src/main/resources/log4j.properties
@@ -0,0 +1,6 @@
+log4j.rootLogger=INFO,stdout
+
+# stdout
+log4j.appender.stdout=org.apache.log4j.ConsoleAppender
+log4j.appender.stdout.layout=com.netflix.zuul.logging.FilteredPatternLayout
+log4j.appender.stdout.layout.ConversionPattern=%d %-5p %c [%t] %m%n
\ No newline at end of file