简体   繁体   English

Zuul根据登录的用户代理到不同的URL

[英]Zuul proxy to different URL according to logged in user

I'm designing some bot farm where each user has its own bot, and I would like to achieve the following 我正在设计一些机器人场,每个用户都有自己的机器人,我希望实现以下目标

  1. User logs in to the web server 用户登录Web服务器
  2. Zuul server will then extract from the DB the IP of the specific user's bot 然后,Zuul服务器将从数据库中提取特定用户的机器人的IP。
  3. Zuul will act as a reverse proxy to the bot, with other users able to do the same thing in parallel of course Zuul将充当bot的反向代理,其他用户当然也可以并行执行相同的操作

So far I saw examples to redirect to specific service according to the path. 到目前为止,我已经看到了根据路径重定向到特定服务的示例。 but is it achievable to do this dynamically according to the logged in user's bot IP? 但是可以根据登录的用户的bot IP动态地执行此操作吗?

I was able to achieve it by writing the following filter: 我可以通过编写以下过滤器来实现:

  @Override
  public String filterType() {
    return PRE_TYPE;
  }

  @Override
  public int filterOrder() {
    return PRE_DECORATION_FILTER_ORDER + 1;
  }

  public Object run() {
    RequestContext ctx = getCurrentContext();
    ctx.put(REQUEST_URI_KEY, "/test/");
    try {
      if (new Random().nextInt(10) > 5) {
        ctx.setRouteHost(new URL("http://192.168.1.14:8088"));
      } else {
        ctx.setRouteHost(new URL("http://192.168.1.14:8089"));
      }
    } catch (MalformedURLException e) {
      log.error("", e);
    }
  return null;
  }

my application.properties looks like so: 我的application.properties看起来像这样:

spring.application.name=zuul-server
server.port=8080
management.endpoints.web.exposure.include=*
zuul.routes.test.path=/test/**

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM