RabbitMQ 설치 & 기본 사용법

RabbitMQ 설치 & 기본 사용법

mac 에 RabbitMQ Server 를 설치하려다가.. M1 칩셋에 문제가 있는건지 메모리 참조 문제가 발생

알아보니 Mac 과 호환 안되는 문제가 있는 것 같다.

Rosetta3.10 on macOS segmentation fault 11

따라서 윈도우 컴퓨터로 설치를 진행합니다...

ErLang 설치

RabbitMQ 는 ErLang 을 필요로 한다.

Erlang Programming Language

RabbitMQ 설치하기

Downloading and Installing RabbitMQ

RabbitMQ 실행하기

Downloading and Installing RabbitMQ

RabbitMQ 실행하기

Downloading and Installing RabbitMQ

RabbitMQ 실행하기

RabbitMQ Service Start 를 실행합니다.

RabbitMQ Process 확인하기

명령창에서 아래와 같은 명령어를 입력합니다. 이는 RabbitMQ 의 기본 Port 가 5672 인 점을 이용해서 찾습니다.

netstat -ano | findstr 5672

관리자 콘솔창 확인하기

기본 RabbitMQ 가 설치된 경로에서 아래 명령어를 입력한다. 해당 경로는 RabbitMQ log 폴더가 위치한 곳에서 sbin을 찾았다.

경로는 다음과 같았다.

cd C:\\Program Files\\RabbitMQ Server\\rabbitmq_server-3.9.7\\sbin

명령어

rabbitmq-plugins enable rabbitmq_management

이제 아래 url로 브라우저를 띄우면 다음과 같은 화면을 만날 수 있다.

http://localhost:15672/

접속을 하면 위와 같은 로그인창이 나타나는데 guest / guest 로 접속할 수 있다

port 열기

윈도우 컴퓨터를 server 로 사용할 것이기 때문에 외부에서 접근하게 된다면 다음과 같이 포트를 열어준다.

제어판 - 시스템 및 보안 - Windows Defender 방화벽 - 고급 설정 - 새 규칙

규칙 종류(인바운드) : 포트

프로토콜 및 포트 : 5672

연결 허용

나머지 기본 설정

RabbitMQ 간단 시작하기

의존성

org.springframework.amqp spring-rabbit-test test

rabbitmq 의 클라이언트 관련 의존성이다.

Send (Producer)

public class Send { private final static String QUEUE_NAME = "hello"; public static void main(String[] args) { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); factory.setPort(5672); factory.setUsername("guest"); factory.setPassword("guest"); try (Connection connection = factory.newConnection(); Channel channel = connection.createChannel()) { for (int i = 0; i <= 1000; i++) { channel.queueDeclare(QUEUE_NAME, false, false, false, null); String message = "Hello World!" + (int) (Math.random() * 100); channel.basicPublish("", QUEUE_NAME, null, message.getBytes()); System.out.println(" [x] Set '" + message + "'"); Thread.sleep(10); } } catch (TimeoutException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } }

위 코드를 실행시키고 rabbitmq 관리자 콘솔을 보면 메세지가 쌓이는 것을 확인할 수 있다.

이렇게 쌓인 메세지를 Recv 를 실행시키면 모두 처리한다.

Recv (Consumer)

public class Recv { private final static String QUEUE_NAME = "hello"; public static void main(String[] argv) throws Exception { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); factory.setPort(5672); factory.setUsername("guest"); factory.setPassword("guest"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.queueDeclare(QUEUE_NAME, false, false, false, null); System.out.println(" [*] Waiting for messages. To exit press CTRL+C"); DeliverCallback deliverCallback = (consumerTag, delivery) -> { String message = new String(delivery.getBody(), "UTF-8"); System.out.println(" [x] Received '" + message + "'"); }; channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -> { }); } }

개념 정리

RabbitMQ 는 Message Broker 로 보통 요청과 처리 사이의 결합을 느슨하게 하게 위해 사용한다. 즉, 요청을 쌓아두고 나중에 처리하기 위해서 사용을 하는 느낌이라고 생각하면 된다.

기본적으로 요청을 생성하는 Producer 와 요청 메세지를 받는 서버인 Message Broker (RabbitMQ) 그리고 쌓인 요청을 처리할 Consumer 이 세가지로 이루어져 있다.

메세지는 기본적으로 큐에 쌓아두고, 해당 큐의 메세지를 처리할 수 있도록 등록된 Consumer 가 처리 가능한 상태가 되면 Message Broker 의 요청을 받아서 처리하는 것이 기본 형태이다.

RabbitMQ 튜토리얼에서 설명하고 있는 그림은 다음과 같다.

RabbitMQ Tutorials

from http://way-be-developer.tistory.com/275 by ccl(A) rewrite - 2021-12-08 18:01:23