api-gateway-netflix-zuul
DerekYRC 2 years ago
parent 168056ce1f
commit ee2b555c76

@ -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 {
}

@ -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 {
}

@ -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
* ZuulServlethttp
*/
@Bean
public ServletRegistrationBean zuulServlet() {
return new ServletRegistrationBean<>(new ZuulServlet(), zuulProperties.getServletPath());
}
/**
* preRouteLocator
*/
@Bean
public ZuulFilter preDecorationFilter() {
return new PreDecorationFilter();
}
/**
* route使ribbonhttp
*/
@Bean
ZuulFilter ribbonRoutingFilter() {
return new RibbonRoutingFilter();
}
/**
* post
*/
@Bean
ZuulFilter sendResponseFilter() {
return new SendResponseFilter();
}
@Bean
public FilterRegistry filterRegistry(Map<String, ZuulFilter> 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();
}
}

@ -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<String, ZuulRoute> 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<String, ZuulRoute> getRoutes() {
return routes;
}
public void setRoutes(Map<String, ZuulRoute> 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;
}
}
}

@ -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;
}
}

@ -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;
/**
* preRouteLocator
*
* @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;
}
}

@ -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使ribbonhttp
*
* @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;
}
}

@ -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";
}

@ -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) {
}
}

@ -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;
}
}
Loading…
Cancel
Save