Localstack SNS:无法将消息发送到ElasticMq

时间:2019-03-05 10:54:18

标签: atlassian-localstack

我有2个应用程序:

1个应用程序使用ElasticMq队列收听消息。 第二个应用程序在SNS主题上发布消息。

我能够订阅ElasticMq主题上的SNS队列。但是当我发布主题local stack时,即使订阅成功,也无法将消息发送到elasticmq

awslocal sns list-subscriptions-by-topic --topic-arn arn:aws:sns:us-east-1:123456789012:classification-details-topic
{
    "Subscriptions": [
        {
            "SubscriptionArn": "arn:aws:sns:us-east-1:123456789012:classification-details-topic:ea470c5a-c352-472e-9ae0-a1386044b750",
            "Owner": "",
            "Protocol": "sqs",
            "Endpoint": "http://elasticmq-service:9324/queue/test",
            "TopicArn": "arn:aws:sns:us-east-1:123456789012:classification-details-topic"
        }
    ]
}

以下是我收到的错误消息:

  

awslocal sns发布--topic-arn   arn:aws:sns:us-east-1:123456789012:classification-details-topic   --message“我的消息”

     

调用发布时发生错误(InvalidParameter)   操作:发生错误(AWS.SimpleQueueService.NonExistentQueue)   调用SendMessage操作时:   AWS.SimpleQueueService.NonExistentQueue;请参阅SQS文档。

我是否有elasticmq在本地堆栈上订阅的错误?

我正在使用docker-compose文件运行localstack

version: '2.1'

services:
  localstack:
    image: localstack/localstack
    ports:
      - "4567-4584:4567-4584"
      - "${PORT_WEB_UI-8001}:${PORT_WEB_UI-8080}"
    environment:
      - SERVICES=${SERVICES- }
      - DEBUG=${DEBUG- }
      - DATA_DIR=${DATA_DIR- }
      - PORT_WEB_UI=${PORT_WEB_UI- }
      - LAMBDA_EXECUTOR=${LAMBDA_EXECUTOR- }
      - KINESIS_ERROR_PROBABILITY=${KINESIS_ERROR_PROBABILITY- }
      - DOCKER_HOST=unix:///var/run/docker.sock
    volumes:
      - "${TMPDIR:-/tmp/localstack}:/tmp/localstack"
      - "/var/run/docker.sock:/var/run/docker.sock"
networks:
  default:
    external:
      name: my_network

我使用相同的docker网络“ my_network”将elasticmq和其他服务作为不同docker-compose的一部分

以下是完整的docker-compose。我尝试通过将条目合并到一个docker-compose文件中来重现它。

复制步骤

version: '3'
services:
  elasticmq:
    build: ./elasticmq
    ports:
      - '9324:9324'
    networks:
      - my_network
    dns:
      - 172.16.198.101
  localstack:
    image: localstack/localstack
    ports:
      - "4567-4584:4567-4584"
      - "${PORT_WEB_UI-8001}:${PORT_WEB_UI-8080}"
    environment:
      - SERVICES=${SERVICES- }
      - DEBUG=${DEBUG- }
      - DATA_DIR=${DATA_DIR- }
      - PORT_WEB_UI=${PORT_WEB_UI- }
      - LAMBDA_EXECUTOR=${LAMBDA_EXECUTOR- }
      - KINESIS_ERROR_PROBABILITY=${KINESIS_ERROR_PROBABILITY- }
      - DOCKER_HOST=unix:///var/run/docker.sock
    volumes:
      - "${TMPDIR:-/tmp/localstack}:/tmp/localstack"
      - "/var/run/docker.sock:/var/run/docker.sock"
    links:
      - elasticmq:elasticmq-service
    networks:
      - my_network
    dns:
      - 172.16.198.101
networks:
  my_network:
    driver: bridge
    ipam:
      config:
        - subnet: 172.16.198.0/24

此后可以运行以下命令

awslocal sqs create-queue --queue-name test --endpoint http://elasticmq:9324/

awslocal sns create-topic --name test-topic

awslocal sns subscribe --topic-arn arn:aws:sns:us-east-1:123456789012:test-topic --protocol sqs --notification-endpoin http://elasticmq-service:9324/queue/test

1 个答案:

答案 0 :(得分:0)

根据您的评论,我可能会猜测您的2 docker-compose个文件的网络设置不正确。

为简单起见,我将合并elasticmq中的docker-compose服务,然后重试(如果您发布第二个docker-compose和精确的aws命令来创建订阅有人可以在本地尝试)。

如果您确实要保留2个单独的docker-compose文件,那么如果上述方法可行,那么至少您可以指出问题所在。恐怕我不太熟悉此设置,但是this answer可能会有所帮助。

编辑:

感谢其他详细信息。我有一个适用于我的docker-compose的简化版本。首先according to this,您将需要创建一个配置文件来设置elasticmq实例的主机名,因为它不会从container_name中提取docker-compose(类似于您将在HOSTNAME中设置LocalStack环境变量,如下所示。名为elasticmq.conf的文件(位于名为config的文件夹中)的内容为:

include classpath("application.conf")

node-address {
  host = elasticmq
}

queues {
  test-queue {}
}

在适当的位置上,以下docker-compose发布消息时没有任何错误:

version: '3'

services:

  elasticmq:
    image: s12v/elasticmq
    container_name: elasticmq
    ports:
      - '9324:9324'
    volumes:
      - ./config/elasticmq.conf:/etc/elasticmq/elasticmq.conf

  localstack:
    image: localstack/localstack
    container_name: localstack
    environment:
      - SERVICES=sns
      - DEBUG=1
      - PORT_WEB_UI=${PORT_WEB_UI- }
      - HOSTNAME=localstack
    ports:
      - "4575:4575"
      - "8080:8080"

  awscli:
    image: garland/aws-cli-docker
    container_name: awscli
    depends_on:
      - localstack
      - elasticmq
    environment:
      - AWS_DEFAULT_REGION=eu-west-2
      - AWS_ACCESS_KEY_ID=xxx
      - AWS_SECRET_ACCESS_KEY=xxx
    command:
      - /bin/sh
      - -c
      - |
          sleep 20
          aws --endpoint-url=http://localstack:4575 sns create-topic --name test_topic
          aws --endpoint-url=http://localstack:4575 sns subscribe --topic-arn arn:aws:sns:eu-west-2:123456789012:test_topic --protocol http --notification-endpoint http://elasticmq:9324/queue/test-queue
          aws --endpoint-url=http://localstack:4575 sns publish --topic-arn arn:aws:sns:eu-west-2:123456789012:test_topic --message "My message"

输出:

enter image description here

在这一点上,公认的是,我没有检查elasticmq来查看邮件是否已被占用,但我将其留给您。