简体   繁体   中英

RabbitMQ configuration problems

I am having a problem with how to use the following rabbit environment configuration.

rabbit_mq:
    image: rabbitmq:3-management-alpine
    container_name: 'rabbitmq'
    hostname: rabbitmq
    ports:
      - 5672:5672
      - 15672:15672
    volumes:
      - ./docker/rabbitmq/data/:/var/lib/rabbitmq/
      - ./docker/rabbitmq/log/:/var/log/rabbitmq
      - ./docker/rabbitmq/conf/:/var/conf/rabbitmq
    environment:
#(I comment this now but before I used this)
#      RABBITMQ_HOST: rabbitmq  
#      RABBITMQ_PORT: 15672
#      RABBITMQ_VHOST: /
      RABBITMQ_DEFAULT_USER: guest  
#(I also tried to use RABBITMQ_USER)
      RABBITMQ_DEFAULT_PASS: guest  
#(and I also tried to use RABBITMQ_PASS & RABBITMQ_PASSWORD)

I need to use environment configuration to connect to the server. There is my PHP code:

$connection = new AMQPStreamConnection($_ENV['RABBITMQ_HOST'], $_ENV['RABBITMQ_PORT'], $_ENV['RABBITMQ_DEFAULT_USER'], $_ENV['RABBITMQ_DEFAULT_PASS']);

I tried a lot of things, for example, I created a rabbitmq.env file, and write the config down there, but it's not working

Also, there is an error-

Fatal error: Uncaught PhpAmqpLib\Exception\AMQPIOException: stream_socket_client(): unable to connect to tcp://: (Failed to parse address ":") in /app/vendor/php-amqplib/php-amqplib/PhpAmqpLib/Wire/IO/StreamIO.php:108

How to resolve this issue?

Okey, there is the answer: i had to write this config in php container, not in rabbitmq container, so there is the docker-compose code:

    send:
        build:
          context: './docker'
          dockerfile: Dockerfile
        image: php:7.4.cli
        container_name: send
        ports:
          - 8000:8000
        volumes:
          - ./:/app
        environment:
          RABBITMQ_HOST: '${RABBIT_HOST}'
          RABBITMQ_PORT: '${RABBIT_PORT}'
          RABBITMQ_USERNAME: '${RABBIT_USERNAME}'
          RABBITMQ_PASSWORD: '${RABBIT_PASSWORD}'
        depends_on:
          - rabbit_mq
        command: ["php", "./send.php"]

And also add.env file in the root directoria:

RABBIT_HOST=rabbitmq
RABBIT_PORT=5672
RABBIT_USERNAME=guest
RABBIT_PASSWORD=guest

My PHP code:

 $connection = new AMQPStreamConnection($_ENV['RABBITMQ_HOST'], $_ENV['RABBITMQ_PORT'], $_ENV['RABBITMQ_USERNAME'], $_ENV['RABBITMQ_PASSWORD']);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM