简单反向代理服务器
使用不带Ribbon,Eureka或Hystrix的Spring Boot设置简单的反向代理很容易。
只需使用注释主应用程序类,
@EnableZuulProxy并在配置中设置以下属性:
ribbon.eureka.enabled=false
然后在您的配置中定义路由,如下所示:
zuul.routes.<route_name>.path=<route_path> zuul.routes.<route_name>.url=http://<url_to_host>/
where
<route_name>是您的路线的任意名称,并且
<route_path>是使用Ant样式的路径匹配的路径。
所以一个具体的例子就是这样
zuul.routes.userservice.path=users/**zuul.routes.userservice.url=http://localhost:9999/
自定义过滤器
您还可以通过扩展和实现
ZuulFilter类并将其作为类添加
@Bean到您的
@Configuration类中,以实现自定义身份验证和任何其他标头。
再举一个具体的例子:
public class MyFilter extends ZuulFilter { @Override public String filterType() { // can be pre, route, post, and error return "pre"; } @Override public int filterOrder() { return 0; } @Override public boolean shouldFilter() { return true; } @Override public Object run() { // RequestContext is shared by all ZuulFilters RequestContext ctx = RequestContext.getCurrentContext(); HttpServletRequest request = ctx.getRequest(); // add custom headers ctx.addZuulRequestHeader("x-custom-header", "foobar"); // additional custom logic goes here // return isn't used in current impl, null is fine return null; }}然后
@Configurationpublic class GatewayApplication { @Bean public MyFilter myFilter() { return new myFilter(); }}


