首页 > 解决方案 > 在 minikube 上运行的微服务应用程序正在运行,但未显示评论部分

问题描述

我在 win 10 home 并使用带有 minikube 的 docker 工具箱。这是一个基于微服务架构的应用程序,因此文件很多,但以下是我认为有问题的文件。整个应用程序正在运行,刷新时会显示帖子,但没有出现评论。我没有使用任何数据库。所有数据都保存在内存中。

这在将 ingress-nginx 初始化到应用程序后开始发生

预期行为:

  1. 添加帖子并刷新页面以查看新帖子
  2. 向帖子添加评论并刷新以查看新评论

脚步:

1. 在前端/commentCreate.jsx

const onSubmit = async (event) => {
    event.preventDefault();
 
    await axios.post(`http://posts.com/posts/${postId}/comments/create`, {
      content,
    });

 
    setContent("");
  }; 

2.在comments/index.js中

app.post('/posts/:id/comments/create', async (req, res) => {
    ...
    commentsByPost[req.params.id] = comments
 
    await axios.post('http://event-bus-int-serv:5000/events',{
        type: 'CommentCreated',
        data: {
            id: commentId,
            content,
            postId: req.params.id,
            status: 'pending'
        }
    })
    res.status(201).json(comments)
})

3. 在 event-bus/index.js

app.post('/events', (req,res) => {
    ...
    axios.post('http://posts-int-serv:4000/events', event).catch(err => console.log(err.message))
    axios.post('http://comments-int-serv:4100/events', event).catch(err => console.log(err.message))
    axios.post('http://query-int-serv:4200/events', event).catch(err => console.log(err.message))
    axios.post('http://moderation-int-serv:4300/events', event).catch(err => console.log(err.message))
    res.send({status: 'OK'})
})

4.在query/index.js中

const posts={}

const handleEvent = (type, data) => {
    ...     
    if(type==="CommentCreated"){
    
    const {id,content,postId, status} = data

    const post = posts[postId]

    
    post.comments.push({
        id, content, status
    })
    console.log("post",post)
    }
}

 
app.get('/posts', (req,res) => {
    console.log("posts query get: ",posts)
    res.send(posts)
})

app.post('/events', (req,res) => {
    const {type, data} = req.body
    handleEvent(type, data)

    res.send({})
})

可能的根本问题:

commentCreated 块中的 console.log("post",post) 显示了整个帖子及其评论,但似乎 app.get('/posts', (req,res) 不起作用,因为它的控制台日志没有显示

我检查了入口中的路由,我认为它是正确的。要到达 app.get('/posts', (req,res),我需要使用端口 4200,这是我为查询服务提供的端口号

入口服务 yml 配置的一部分

- path: /posts
  pathType: Prefix
  backend:
    service:
      name: query-serv
      port:
        number: 4200

为了进一步参考,这是我的仓库中的全部代码:

https://github.com/JohnLawliet/microservices

任何帮助是极大的赞赏。

标签: kubernetesmicroserviceskubernetes-ingressnginx-ingress

解决方案


推荐阅读