如何使用 stubby4j 存根一些请求并为其他人调用真正的服务

How to stub some requests and call real service for others using stubby4j

我正在使用 stubby4j 存根一些服务端点。我目前正在对那些非常繁重且模拟起来不那么复杂的那些进行存根,但我想为其余端点调用真正的服务。

像这样:

/heavy-call-1 => stub service
/heavy-call-2 => stub service
/lightweight-call-1 => real service
/lightweight-call-2 => real service

有什么方法可以使用此工具实现此目的,还是我应该考虑使用其他工具?

实际上你可以让stubby调用真正的服务并记录第一次响应,所以下一次请求将使用这个记录的响应。 您可以这样做的方法是在您的 yaml 文件中的存根响应正文中指定一个 URL,如下所示:

-  request:
      url: /1.1/direct_messages.json
      query:
         since_id: 240136858829479935
         count: 1
   response:
      headers:
         content-type: application/json
      body: https://api.twitter.com/1.1/direct_messages.json?since_id=240136858829479935&count=1

您可以在存根 github 文档中找到更多信息:https://stubby4j.com/#key-features and https://stubby4j.com/docs/http_endpoint_configuration_howto.html#record-and-replay

希望对您有所帮助!

你在使用 webpack 吗?如果是这样,您可以匹配不同的域。例如:

const config = merge(common, {
  devtool: 'inline-source-map',
  mode: 'development',
  devServer: {
    historyApiFallback: true,
    port: 3000,
    hot: true,
    proxy: [
      { path: '/heavy-all-1 ', target: 'http://localhost:8882' }, //stubby
    ],
  },
  plugins: [
    new webpack.HotModuleReplacementPlugin(),
    new webpack.DefinePlugin({
      'process.env.NODE_ENV': JSON.stringify('development'),
    }),
  ],
});

没有所述前缀的 URL 将不会被存根。