【问题标题】:How to communicate between microservices using event bus?如何使用事件总线在微服务之间进行通信?
【发布时间】:2021-08-26 08:56:21
【问题描述】:

我正在尝试通过事件总线将数据从一个微服务发送到另一个微服务。但结果仍然是我得到一个空数据,我不明白我做错了什么,请帮忙。

尝试发送数据:

app.get ('/products', async (req , res) => {
    let db = await connect();

    let cursor = await db.collection('posts').find({});
    let doc = await cursor.toArray();
    
    res.json(doc);
    if (doc.insertedCount == 1) {
        res.send({
            status: 'success',
            id: results.insertedId,
        });
    } 
    else {
        res.send({
            status: 'fail',
        });
    }
axios.get('http://localhost:4205/events', {
            type: 'Success',
            data: {
                _id: mongo.ObjectID(id),
                doc,
                postId: req.params.id,
            }
    
        })
});

事件总线:

app.get('/events', async (req, res) => {
    const event = req.body;
    res.json(event);
    axios.get('http://localhost:4202/events', event)
    res.send({status:'OK'})
})

我要获取数据的微服务:

app.get('/events', async (req, res) => {
    
    res.send(req.body)
});

【问题讨论】:

  • axios.get 的第二个参数是带有以下选项的配置:params 和 headers,什么意思 在您的情况下键入和数据?
  • 如果我没记错的话,EventBus 是关于 emmiting 事件和 listening 它,在你的情况下就像一个简单的 REST API
  • 是的,没错,我遵循了一些示例,我正在尝试从相同的 get 方法发送一些数据,我更新了我的问题,因为这个 axios.get 函数在 app.get 内,所以可以你试图纠正什么是错的?
  • 第一个在 4200 端口上运行,第二个在 4205(事件总线)上运行,并试图将它传递给在 4202 上运行的第三个。
  • 我将创建一个示例作为答案。

标签: vue.js event-handling microservices event-bus


【解决方案1】:

第一:

app.get('/products', async (req, res) => {
    try {
        const db = await connect();
        const cursor = await db.collection('posts').find({});
        const doc = await cursor.toArray();

    /*  Below are weird code
      res.json(doc);
      if (doc.insertedCount == 1) {
        res.send({
          status: 'success',
          id: results.insertedId,
        });
      } else {
        res.send({
          status: 'fail',
        });
      }
    */

        axios.post('http://localhost:4205/events', {
            type: 'Success',
            data: {
                id: mongo.ObjectID(id),
                doc,
                postId: req.params.id,
            },
        });

        return res.status(200).send();
  } catch (error) {
    // Here catch and do something with errors;
    console.log(error);
  }
});

第二:

app.post('/events', async (req, res) => {
  try {
    const event = req.body;
    console.log(event);

    // What's mean next line ?
    // res.json(event);

    const response = await axios.post('http://localhost:4202/events', event);
    console.log(response);
    
    return res.status(200).json({ status: 'OK' });
  } catch (error) {
    console.log(error);
  }
});

最后

app.post('/events', async (req, res) => {
  try {
    console.log(req.body);
    
    return res.status(200).json(req.body);
  } catch (error) {
    console.log(error);
  }
});

【讨论】:

  • 当我在最后一个微服务的postman 中检查该帖子时,它对我不起作用,它仍然是空的,我希望它们被写为响应,而不是请求。我的req.body 在事件总线中是空的。但是微服务和事件总线之间的通信是有效的。您能帮我正确发送数据吗?
  • 你评论的那个奇怪的代码,我的错,它代表doc而不是results,它允许显示至少一个帖子,否则它不会工作。
猜你喜欢
  • 2021-10-21
  • 1970-01-01
  • 2018-11-03
  • 1970-01-01
  • 2018-09-10
  • 1970-01-01
  • 2020-07-15
  • 2021-07-17
  • 2016-06-10
相关资源
最近更新 更多