RabbitMQ客户端:指定的端点均不可达

时间:2019-12-23 12:03:05

标签: kubernetes rabbitmq azure-dev-spaces

我正在研究ASP.NET Core WebApi微服务。我将项目部署在Azure Dev Space上,并且除非我开始使用RabbitMQ,否则它运行良好。使用RabbitMQ设置,该项目可以在localhost上正常运行。但是我不知道如何在DevSpaces上部署RabbitMQ。我有以下 Docker File ,可用于通过“ azds up”运行。该项目在我的本地计算机上运行良好。

FROM microsoft/dotnet:2.2-aspnetcore-runtime-nanoserver-1803 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM microsoft/dotnet:2.2-sdk-nanoserver-1803 AS build
WORKDIR /src
COPY ["src/Customer.WebApi/Customer.WebApi.csproj", "src/Customer.WebApi/"]
COPY ["src/Customer.WebApi/nuget.config", "src/Customer.WebApi/"]
RUN dotnet restore "src/Customer.WebApi/Customer.WebApi.csproj"
COPY . .
WORKDIR "/src/src/Customer.WebApi"
RUN dotnet build "Customer.WebApi.csproj" -c Release -o /app

FROM build AS publish
RUN dotnet publish "Customer.WebApi.csproj" -c Release -o /app

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "Customer.WebApi.dll"]

我尝试使用以下命令在与其他WebApi微服务相同的名称空间中部署RabbitMQ:

kubectl --namespace=devspace-dev create -f rabbitmq.yml

使用以下 rabbitmq.yml

apiVersion: v1
kind: Service
metadata:
  creationTimestamp: null
  name: rabbitmq
spec:
  ports:
  - port: 15672
    protocol: TCP
    targetPort: 15672
  selector:
    run: rabbitmq
status:
  loadBalancer: {}
---
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: rabbitmq
  name: rabbitmq
spec:
  containers:
  - image: rabbitmq:management
    name: rabbitmq
    ports:
    - containerPort: 15672
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Never
status: {}

我获得了一个具有15672端口的外部端点,但是通过访问它,它只会显示连接超时。

我还在Kubernetes仪表板上获得了Rabbitmq服务的以下内部端点:

rabbitmq.devspace-dev:15672 TCP

然后我更改我的 appsettings.json

"AzureServiceBusEnabled": false,


"SubscriptionClientName": "Invoicing",
  "CheckUpdateTime": "30000",
  "ApplicationInsights": {
    "InstrumentationKey": ""
  },
  "EventBusRetryCount": 5,
  "OrderClientName": "Invoicing",
  "EventBusConnection": "rabbitmq.devspace-dev",
  "EventBusUserName": "guest",
  "EventBusPassword": "guest",
  "UseVault": false,
  "Vault": {
    "Name": "eshop",
    "ClientId": "your-clien-id",
    "ClientSecret": "your-client-secret"
  }

然后更新我的DevSpace API时,得到 RabbitMQ客户端:没有指定的端点可访问。错误,API崩溃了。

这是在Kubernetes上使用RabbitMQ的正确方法吗? Internet上的所有教程都没有解决如何将其与Azure Dev Space正确集成的问题。有人可以帮我解决吗?

编辑:当我运行azds时,该API成功部署,但是当执行并开始连接到Rabbitmq客户端时,它便崩溃了。

1 个答案:

答案 0 :(得分:0)

在Dockerfile中设置RabbitMQ用户名和密码

FROM rabbitmq:3-management

# Define environment variables.
ENV RABBITMQ_DEFAULT_USER user
ENV RABBITMQ_DEFAULT_PASS password
COPY init.sh .
EXPOSE 15672 


# Define default command
RUN ["chmod" , "+x", "init.sh"]
CMD ["/init.sh"]

这是init.sh脚本

#!/bin/sh


# Create Rabbitmq user
(sleep 20 && \
rabbitmqctl add_user user password ; \
rabbitmqctl set_user_tags user administrator ; \
echo "Added user" ; \
rabbitmqctl set_permissions -p / user  ".*" ".*" ".*" ; \
rabbitmq-plugins --offline enable rabbitmq_peer_discovery_k8s ; \
echo "*** User 'user' with password 'password' completed. ***" ; \
echo "*** Log in the WebUI at port 15672 (example: http:/localhost:15672) ***") &

# $@ is used to pass arguments to the rabbitmq-server command.
# For example if you use it like this: docker run -d rabbitmq arg1 arg2,
# it will be as you run in the container rabbitmq-server arg1 arg2

rabbitmq-server $@

您可以在docker.yaml中公开您的端口。签出this链接。

用户名和密码是必须的,因为来宾身份验证只能在localhost上运行。如果您通过IP地址访问Rabbitmq,则访客凭据将不起作用。让我知道怎么回事!