본문 바로가기

기타

Spring boot Rabbitmq test

728x90
반응형

exchange > test.exchange 추가 

queues > test.queue 추가 

 

 

 

exchange 에서 만든 exchange에 들어가면 Bindings에 추가할 수 있다.

test.queue, test.route.#

 

 


Spring Boot > rabbitmq consumer

#application.properties


server.port=8080
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

 

package com.example.sampleproject.rabbitmq;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class SimpleListener {

    private static final Logger log = LoggerFactory.getLogger(SimpleListener.class);

    @RabbitListener(queues = "test.queue")
    public void receiveMessage(final Message message) {
        log.info(message.toString());
    }

}

 

@RabbitListener 에 queue name을 쓴다. 

 

서버 올린 후 publish test 가 가능하다.

 


SpringBoot > Producer

@application.properties

server.port=8081
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
package com.example.sampleproject.rabbitmq;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;

public class SampleConfig {
    private static final String EXCHANGE_NAME = "test.exchange";
    private static final String QUEUE_NAME = "test.queue";
    private static final String ROUTING_KEY = "test.route.#";
    @Bean
    TopicExchange exchange() {
        return new TopicExchange(EXCHANGE_NAME);
    }
    @Bean
    Queue queue() {
        return new Queue(QUEUE_NAME);
    }
    @Bean
    Binding binding (Queue queue, TopicExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(ROUTING_KEY);
    }
    @Bean
    RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory, MessageConverter messageConverter) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
        return rabbitTemplate;
    }

}
package com.example.sampleproject.controller;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SampleController {

    private static final String EXCHANGE_NAME = "test.exchange";

    @Autowired
    RabbitTemplate rabbitTemplate;
    @GetMapping("/test/queue")
    public String samplePublish() {
        rabbitTemplate.convertAndSend(EXCHANGE_NAME, "test.route.#", "Message Send >>>>>>>>>>>>");
        return "message sending!";
    }

}

API 호출시 consumer log

2021-08-05 17:03:47.962  INFO 4864 --- [ntContainer#0-1] c.e.s.rabbitmq.SimpleListener            : (Body:'Message Send >>>>>>>>>>>>' MessageProperties [headers={}, contentType=text/plain, contentEncoding=UTF-8, contentLength=0, receivedDeliveryMode=PERSISTENT, priority=0, redelivered=false, receivedExchange=test.exchange, receivedRoutingKey=test.route.#, deliveryTag=1, consumerTag=amq.ctag-FuAIdVLIOM9sySZayseJGg, consumerQueue=test.queue])
728x90
반응형