kubernetes Node.js服务无法连接AKS上的RabbitMQ

juzqafwq  于 2023-08-03  发布在  Kubernetes
关注(0)|答案(1)|浏览(197)

我有一个Node.js应用程序,它是容器化的,部署在AKS上。
我还将RabbitMQ部署在同一集群上,queuename.rabbitmq.svc.cluster.local:5672上可用
当它用一个普通的Dockerfile容器化时,它工作得很好,像这样:

# Install Node.js
FROM node:20.2.0-alpine3.16

# stage 1 for dev
RUN mkdir -p /usr/src/app

RUN mkdir -p /usr/src/app/dist
WORKDIR /usr/src/app
COPY . /usr/src/app

RUN npm install

EXPOSE 80
ENTRYPOINT [ "npm", "start" ]

字符串
但是当我用一个非root用户和一个不同的端口优化它时,它无法连接到RabbitMQ并抛出这个错误:

/* 7 */
{
    "_id" : ObjectId("647db632e239b362ce8649ac"),
    "serviceName" : "nodejs-service",
    "controllerName" : "RabbitMQService",
    "methodName" : "connectToRabbitMQ",
    "message" : "Failed to connect to RabbitMQ.",
    "exception" : "Error: Expected amqp: or amqps: as the protocol; got \n    at connect (D:\\LyveCloud-11-04-23\\nodejs-service\\node_modules\\amqplib\\lib\\connect.js:171:11)\n
    "logLevel" : "Error",
    "severity" : "Critical",
    "request" : null,
    "response" : null,
    "statusCode" : "400",


除了Dockerfile中的更改外,没有任何更改,如下所示:

# Install Node.js
FROM node:20.2.0-alpine3.16

# stage 1 for dev
RUN mkdir -p /usr/src/app

RUN mkdir -p /usr/src/app/dist
WORKDIR /usr/src/app
COPY . /usr/src/app

RUN npm install

# Create a non-root user and group
RUN addgroup -S appgroup && adduser -S appuser -u 1001 -G appgroup
# Set ownership of necessary directories
RUN chown -R appuser:appgroup /usr/src/app
# Set user and group for the container
USER appuser:appgroup
EXPOSE 8080
ENTRYPOINT [ "npm", "start" ]


这可能是什么原因,我该如何解决它?

9wbgstp7

9wbgstp71#

错误指向amqp库中的这段代码:

"exception" : "Error: Expected amqp: or amqps: as the protocol; 
         got \n    at connect (D:\\LyveCloud-11-04-23\\nodejs- 
         service\\node_modules\\amqplib\\lib\\connect.js:171:11)\n

字符串
网址:https://github.com/amqp-node/amqplib/blob/main/lib/connect.js#L171:

if (protocol === 'amqp:') {
    sock = require('net').connect(sockopts, onConnect);
  }
  else if (protocol === 'amqps:') {
    sock = require('tls').connect(sockopts, onConnect);
  }
  else {
    throw new Error("Expected amqp: or amqps: as the protocol; got " + protocol);
  }


该错误似乎表明URL中收到了一个前缀而不是amqps://或ampq://。事实上,错误消息输出它拾取的protocol,但错误消息中got标记后没有出现任何内容,表明没有从指定的url拾取协议。

throw new Error("Expected amqp: or amqps: as the protocol; got " + ****protocol****);


也许你需要逃避一些字符或一个没有设置?

相关问题