繁体   English   中英

Fastify-http-proxy 无法获取动态路由

[英]Fastify-http-proxy unable to get dynamic routes

我有一个指向两个“后端” fastify 服务器的 fastify 服务器,我想生成一个随机数,然后用于决定路由到哪个后端。

目前它只在第一次工作,之后我无法覆盖上游。

server.register(require('fastify-http-proxy'), {
  upstream: '',
  replyOptions: { 
    getUpstream: (originalReq, base) => {
      if (Math.random() < 0.5) {
        console.log(`setting base to backend1`)
        return `http://localhost:${backend1Port}`
      } else {
        console.log(`setting base to backend2`)
        return `http://localhost:${backend2Port}`
      }
    },
    disableCache: true,
    cacheURLs: 0,
  },
})

我怎样才能做到这一点,以便在刷新时它可能会改变它的路由到哪里?

弄清楚了。 disableCache 需要在replyOptions 之外。

server.register(require('fastify-http-proxy'), {
  upstream: '',
  replyOptions: { 
    getUpstream: (originalReq, base) => {
      if (Math.random() < 0.5) {
        console.log(`setting base to backend1`)
        return `http://localhost:${backend1Port}`
      } else {
        console.log(`setting base to backend2`)
        return `http://localhost:${backend2Port}`
      }
    },
    disableCache: true,
    cacheURLs: 0,
  },
})

暂无
暂无

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

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