From ee2b555c76b529414dcccc89812b33f0e4e9fcbf Mon Sep 17 00:00:00 2001 From: DerekYRC <15521077528@163.com> Date: Mon, 27 Jun 2022 20:40:40 +0800 Subject: [PATCH] =?UTF-8?q?API=E7=BD=91=E5=85=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/resources/application.yml | 5 +- .../cloud/netflix/zuul/EnableZuulProxy.java | 2 +- .../zuul/ZuulProxyAutoConfiguration.java | 11 ---- .../zuul/ZuulServerAutoConfiguration.java | 61 ++++++++++++++++++- .../netflix/zuul/filters/ZuulProperties.java | 36 +++++++++++ .../zuul/filters/post/SendResponseFilter.java | 35 +++++++++++ .../zuul/filters/pre/PreDecorationFilter.java | 35 +++++++++++ .../filters/route/RibbonRoutingFilter.java | 35 +++++++++++ .../zuul/filters/support/FilterConstants.java | 20 ++++++ .../zuul/metrics/EmptyCounterFactory.java | 14 +++++ .../zuul/metrics/EmptyTracerFactory.java | 15 +++++ 11 files changed, 254 insertions(+), 15 deletions(-) delete mode 100644 mini-spring-cloud-netflix-zuul/src/main/java/com/github/cloud/netflix/zuul/ZuulProxyAutoConfiguration.java create mode 100644 mini-spring-cloud-netflix-zuul/src/main/java/com/github/cloud/netflix/zuul/filters/post/SendResponseFilter.java create mode 100644 mini-spring-cloud-netflix-zuul/src/main/java/com/github/cloud/netflix/zuul/filters/pre/PreDecorationFilter.java create mode 100644 mini-spring-cloud-netflix-zuul/src/main/java/com/github/cloud/netflix/zuul/filters/route/RibbonRoutingFilter.java create mode 100644 mini-spring-cloud-netflix-zuul/src/main/java/com/github/cloud/netflix/zuul/filters/support/FilterConstants.java create mode 100644 mini-spring-cloud-netflix-zuul/src/main/java/com/github/cloud/netflix/zuul/metrics/EmptyCounterFactory.java create mode 100644 mini-spring-cloud-netflix-zuul/src/main/java/com/github/cloud/netflix/zuul/metrics/EmptyTracerFactory.java diff --git a/mini-spring-cloud-examples/mini-spring-cloud-api-gateway-example/src/main/resources/application.yml b/mini-spring-cloud-examples/mini-spring-cloud-api-gateway-example/src/main/resources/application.yml index 84c6fb1..51c9791 100644 --- a/mini-spring-cloud-examples/mini-spring-cloud-api-gateway-example/src/main/resources/application.yml +++ b/mini-spring-cloud-examples/mini-spring-cloud-api-gateway-example/src/main/resources/application.yml @@ -1,2 +1,5 @@ server: - port: 8888 \ No newline at end of file + port: 8888 + +zuul: + servlet-path: /* \ No newline at end of file diff --git a/mini-spring-cloud-netflix-zuul/src/main/java/com/github/cloud/netflix/zuul/EnableZuulProxy.java b/mini-spring-cloud-netflix-zuul/src/main/java/com/github/cloud/netflix/zuul/EnableZuulProxy.java index c579530..603555d 100644 --- a/mini-spring-cloud-netflix-zuul/src/main/java/com/github/cloud/netflix/zuul/EnableZuulProxy.java +++ b/mini-spring-cloud-netflix-zuul/src/main/java/com/github/cloud/netflix/zuul/EnableZuulProxy.java @@ -14,6 +14,6 @@ import org.springframework.context.annotation.Import; */ @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) -@Import(ZuulProxyAutoConfiguration.class) +@Import(ZuulServerAutoConfiguration.class) public @interface EnableZuulProxy { } diff --git a/mini-spring-cloud-netflix-zuul/src/main/java/com/github/cloud/netflix/zuul/ZuulProxyAutoConfiguration.java b/mini-spring-cloud-netflix-zuul/src/main/java/com/github/cloud/netflix/zuul/ZuulProxyAutoConfiguration.java deleted file mode 100644 index 2975afd..0000000 --- a/mini-spring-cloud-netflix-zuul/src/main/java/com/github/cloud/netflix/zuul/ZuulProxyAutoConfiguration.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.github.cloud.netflix.zuul; - -import org.springframework.context.annotation.Configuration; - -/** - * @author derek(易仁川) - * @date 2022/6/23 - */ -@Configuration -public class ZuulProxyAutoConfiguration extends ZuulServerAutoConfiguration { -} diff --git a/mini-spring-cloud-netflix-zuul/src/main/java/com/github/cloud/netflix/zuul/ZuulServerAutoConfiguration.java b/mini-spring-cloud-netflix-zuul/src/main/java/com/github/cloud/netflix/zuul/ZuulServerAutoConfiguration.java index 4f44311..1669f0f 100644 --- a/mini-spring-cloud-netflix-zuul/src/main/java/com/github/cloud/netflix/zuul/ZuulServerAutoConfiguration.java +++ b/mini-spring-cloud-netflix-zuul/src/main/java/com/github/cloud/netflix/zuul/ZuulServerAutoConfiguration.java @@ -1,7 +1,18 @@ package com.github.cloud.netflix.zuul; +import java.util.Map; + import com.github.cloud.netflix.zuul.filters.ZuulProperties; +import com.github.cloud.netflix.zuul.filters.post.SendResponseFilter; +import com.github.cloud.netflix.zuul.filters.pre.PreDecorationFilter; +import com.github.cloud.netflix.zuul.filters.route.RibbonRoutingFilter; +import com.github.cloud.netflix.zuul.metrics.EmptyCounterFactory; +import com.github.cloud.netflix.zuul.metrics.EmptyTracerFactory; +import com.netflix.zuul.ZuulFilter; +import com.netflix.zuul.filters.FilterRegistry; import com.netflix.zuul.http.ZuulServlet; +import com.netflix.zuul.monitoring.CounterFactory; +import com.netflix.zuul.monitoring.TracerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.context.properties.EnableConfigurationProperties; @@ -21,11 +32,57 @@ public class ZuulServerAutoConfiguration { protected ZuulProperties zuulProperties; /** - * 注册ZuulServlet - * @return + * 注册ZuulServlet,用于拦截处理http请求 */ @Bean public ServletRegistrationBean zuulServlet() { return new ServletRegistrationBean<>(new ZuulServlet(), zuulProperties.getServletPath()); } + + /** + * pre类型过滤器,根据RouteLocator来进行路由规则的匹配 + */ + @Bean + public ZuulFilter preDecorationFilter() { + return new PreDecorationFilter(); + } + + /** + * route类型过滤器,使用ribbon负载均衡器进行http请求 + */ + @Bean + ZuulFilter ribbonRoutingFilter() { + return new RibbonRoutingFilter(); + } + + /** + * post类型过滤器,向客户端输出响应报文 + */ + @Bean + ZuulFilter sendResponseFilter() { + return new SendResponseFilter(); + } + + @Bean + public FilterRegistry filterRegistry(Map filterMap) { + FilterRegistry filterRegistry = FilterRegistry.instance(); + filterMap.forEach((name, filter) -> { + filterRegistry.put(name, filter); + }); + return filterRegistry; + } + + //监控相关类,不必关注------------------------------- + + @Bean + public CounterFactory emptyCounterFactory() { + CounterFactory.initialize(new EmptyCounterFactory()); + return CounterFactory.instance(); + } + + @Bean + public TracerFactory emptyTracerFactory() { + TracerFactory.initialize(new EmptyTracerFactory()); + return TracerFactory.instance(); + } } diff --git a/mini-spring-cloud-netflix-zuul/src/main/java/com/github/cloud/netflix/zuul/filters/ZuulProperties.java b/mini-spring-cloud-netflix-zuul/src/main/java/com/github/cloud/netflix/zuul/filters/ZuulProperties.java index 5676571..fdc7c75 100644 --- a/mini-spring-cloud-netflix-zuul/src/main/java/com/github/cloud/netflix/zuul/filters/ZuulProperties.java +++ b/mini-spring-cloud-netflix-zuul/src/main/java/com/github/cloud/netflix/zuul/filters/ZuulProperties.java @@ -1,5 +1,8 @@ package com.github.cloud.netflix.zuul.filters; +import java.util.LinkedHashMap; +import java.util.Map; + import org.springframework.boot.context.properties.ConfigurationProperties; /** @@ -11,6 +14,8 @@ public class ZuulProperties { private String servletPath = "/zuul/*"; + private Map routes = new LinkedHashMap<>(); + public String getServletPath() { return servletPath; } @@ -18,4 +23,35 @@ public class ZuulProperties { public void setServletPath(String servletPath) { this.servletPath = servletPath; } + + public Map getRoutes() { + return routes; + } + + public void setRoutes(Map routes) { + this.routes = routes; + } + + public static class ZuulRoute { + + private String path; + + private String serviceId; + + public String getPath() { + return path; + } + + public void setPath(String path) { + this.path = path; + } + + public String getServiceId() { + return serviceId; + } + + public void setServiceId(String serviceId) { + this.serviceId = serviceId; + } + } } diff --git a/mini-spring-cloud-netflix-zuul/src/main/java/com/github/cloud/netflix/zuul/filters/post/SendResponseFilter.java b/mini-spring-cloud-netflix-zuul/src/main/java/com/github/cloud/netflix/zuul/filters/post/SendResponseFilter.java new file mode 100644 index 0000000..f107d93 --- /dev/null +++ b/mini-spring-cloud-netflix-zuul/src/main/java/com/github/cloud/netflix/zuul/filters/post/SendResponseFilter.java @@ -0,0 +1,35 @@ +package com.github.cloud.netflix.zuul.filters.post; + +import com.netflix.zuul.ZuulFilter; +import com.netflix.zuul.exception.ZuulException; + +import static com.github.cloud.netflix.zuul.filters.support.FilterConstants.POST_TYPE; + +/** + * post类型过滤器,向客户端输出响应报文 + * + * @author derek(易仁川) + * @date 2022/6/27 + */ +public class SendResponseFilter extends ZuulFilter { + + @Override + public String filterType() { + return POST_TYPE; + } + + @Override + public int filterOrder() { + return 1000; + } + + @Override + public boolean shouldFilter() { + return true; + } + + @Override + public Object run() throws ZuulException { + return null; + } +} diff --git a/mini-spring-cloud-netflix-zuul/src/main/java/com/github/cloud/netflix/zuul/filters/pre/PreDecorationFilter.java b/mini-spring-cloud-netflix-zuul/src/main/java/com/github/cloud/netflix/zuul/filters/pre/PreDecorationFilter.java new file mode 100644 index 0000000..672c115 --- /dev/null +++ b/mini-spring-cloud-netflix-zuul/src/main/java/com/github/cloud/netflix/zuul/filters/pre/PreDecorationFilter.java @@ -0,0 +1,35 @@ +package com.github.cloud.netflix.zuul.filters.pre; + +import com.netflix.zuul.ZuulFilter; +import com.netflix.zuul.exception.ZuulException; + +import static com.github.cloud.netflix.zuul.filters.support.FilterConstants.PRE_TYPE; + +/** + * pre类型过滤器,根据RouteLocator来进行路由规则的匹配 + * + * @author derek(易仁川) + * @date 2022/6/27 + */ +public class PreDecorationFilter extends ZuulFilter { + + @Override + public String filterType() { + return PRE_TYPE; + } + + @Override + public int filterOrder() { + return 5; + } + + @Override + public boolean shouldFilter() { + return true; + } + + @Override + public Object run() throws ZuulException { + return null; + } +} diff --git a/mini-spring-cloud-netflix-zuul/src/main/java/com/github/cloud/netflix/zuul/filters/route/RibbonRoutingFilter.java b/mini-spring-cloud-netflix-zuul/src/main/java/com/github/cloud/netflix/zuul/filters/route/RibbonRoutingFilter.java new file mode 100644 index 0000000..64a05df --- /dev/null +++ b/mini-spring-cloud-netflix-zuul/src/main/java/com/github/cloud/netflix/zuul/filters/route/RibbonRoutingFilter.java @@ -0,0 +1,35 @@ +package com.github.cloud.netflix.zuul.filters.route; + +import com.netflix.zuul.ZuulFilter; +import com.netflix.zuul.exception.ZuulException; + +import static com.github.cloud.netflix.zuul.filters.support.FilterConstants.ROUTE_TYPE; + +/** + * route类型过滤器,使用ribbon负载均衡器进行http请求 + * + * @author derek(易仁川) + * @date 2022/6/27 + */ +public class RibbonRoutingFilter extends ZuulFilter { + + @Override + public String filterType() { + return ROUTE_TYPE; + } + + @Override + public int filterOrder() { + return 10; + } + + @Override + public boolean shouldFilter() { + return true; + } + + @Override + public Object run() throws ZuulException { + return null; + } +} diff --git a/mini-spring-cloud-netflix-zuul/src/main/java/com/github/cloud/netflix/zuul/filters/support/FilterConstants.java b/mini-spring-cloud-netflix-zuul/src/main/java/com/github/cloud/netflix/zuul/filters/support/FilterConstants.java new file mode 100644 index 0000000..0af4f80 --- /dev/null +++ b/mini-spring-cloud-netflix-zuul/src/main/java/com/github/cloud/netflix/zuul/filters/support/FilterConstants.java @@ -0,0 +1,20 @@ +package com.github.cloud.netflix.zuul.filters.support; + +/** + * 过滤器常量类 + * + * @author derek(易仁川) + * @date 2022/6/27 + */ +public interface FilterConstants { + + + //过滤器类型常量----------------------------------- + String PRE_TYPE = "pre"; + + String ROUTE_TYPE = "route"; + + String POST_TYPE = "post"; + + String ERROR_TYPE = "error"; +} diff --git a/mini-spring-cloud-netflix-zuul/src/main/java/com/github/cloud/netflix/zuul/metrics/EmptyCounterFactory.java b/mini-spring-cloud-netflix-zuul/src/main/java/com/github/cloud/netflix/zuul/metrics/EmptyCounterFactory.java new file mode 100644 index 0000000..8f4263b --- /dev/null +++ b/mini-spring-cloud-netflix-zuul/src/main/java/com/github/cloud/netflix/zuul/metrics/EmptyCounterFactory.java @@ -0,0 +1,14 @@ +package com.github.cloud.netflix.zuul.metrics; + +import com.netflix.zuul.monitoring.CounterFactory; + +/** + * @author derek(易仁川) + * @date 2022/6/27 + */ +public class EmptyCounterFactory extends CounterFactory { + @Override + public void increment(String name) { + + } +} diff --git a/mini-spring-cloud-netflix-zuul/src/main/java/com/github/cloud/netflix/zuul/metrics/EmptyTracerFactory.java b/mini-spring-cloud-netflix-zuul/src/main/java/com/github/cloud/netflix/zuul/metrics/EmptyTracerFactory.java new file mode 100644 index 0000000..958e258 --- /dev/null +++ b/mini-spring-cloud-netflix-zuul/src/main/java/com/github/cloud/netflix/zuul/metrics/EmptyTracerFactory.java @@ -0,0 +1,15 @@ +package com.github.cloud.netflix.zuul.metrics; + +import com.netflix.zuul.monitoring.Tracer; +import com.netflix.zuul.monitoring.TracerFactory; + +/** + * @author derek(易仁川) + * @date 2022/6/27 + */ +public class EmptyTracerFactory extends TracerFactory { + @Override + public Tracer startMicroTracer(String name) { + return null; + } +}