具有Spring Boot和Netflix Zuul的简单反向代理 [英] Simple Reverse Proxy with Spring Boot and Netflix Zuul

查看:227
本文介绍了具有Spring Boot和Netflix Zuul的简单反向代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望通过Spring Boot实现一个简单的反向代理,即:

I'm looking to implement a simple reverse proxy with Spring Boot that is:

  • 易于添加路线
  • 能够基于每个路由添加自定义身份验证
  • 根据需要添加其他标题

我已经看过@EnableZuulProxy注释提供的功能,但是它似乎太重了,因为我不想使用Eureka,Ribbon或Hystrix.但是,@EnableZuulServer在配置上有点不足.

I've looked at the facilities provided by the @EnableZuulProxy annotation but it seems too heavyweight as I don't have a desire to use Eureka, Ribbon, or Hystrix. However, @EnableZuulServer is a bit light on configuration.

任何人都可以提供我所追求的例子吗? Netflix Zuul是这个的正确选择,还是我应该看的另一个图书馆?

Would anyone be able to provide an example of what I'm after? Is Netflix Zuul the right choice for this or is there another library I should be looking at?

谢谢!

推荐答案

简单反向代理服务器

使用不带Ribbon,Eureka或Hystrix的Spring Boot设置简单的反向代理很容易.

Simple Reverse Proxy Server

It's easy to set up a simple proxy reverse using Spring Boot without Ribbon, Eureka, or Hystrix.

@EnableZuulProxy注释您的主应用程序类,并在配置中设置以下属性:

Simply annotate your main application class with @EnableZuulProxy and set the following property in your configuration:

ribbon.eureka.enabled=false

然后在您的配置中定义路由,如下所示:

Then define your routes in your configuration like such:

zuul.routes.<route_name>.path=<route_path>    
zuul.routes.<route_name>.url=http://<url_to_host>/

其中,<route_name>是您的路线的任意名称,而<route_path>是使用Ant样式的路径匹配的路径.

where <route_name> is an arbitrary name for your route and <route_path> is a path using Ant-style path matching.

所以一个具体的例子就是这样

So a concrete example would be something like this

zuul.routes.userservice.path=users/**
zuul.routes.userservice.url=http://localhost:9999/

自定义过滤器

您还可以通过扩展和实现ZuulFilter类并将其作为@Bean添加到您的@Configuration类中来实现自定义身份验证和任何其他标头.

Custom Filters

You can also implement your custom authentication and any additional headers by extending and implementing the ZuulFilter class and adding it as an @Bean to your @Configuration class.

还有另一个具体示例:

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

}

然后

@Configuration
public class GatewayApplication {

  @Bean
  public MyFilter myFilter() {
    return new myFilter();
  }

}

这篇关于具有Spring Boot和Netflix Zuul的简单反向代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆