Belajar RabbitMQ - Cara Membuat RabbitMQ Publisher
RabbitMQ Publisher merupakan aplikasi atau komponen yang mengirimkan pesan ke message broker. Pastikan kalian sudah membuat RabbitMQ Consumer untuk menerima pesan yang akan kalian kirimkan.
Berikut ini merupakan contoh source code untuk membuat RabbitMQ Publisher dengan menggunakan Spring Boot 3.3.2.
dependencies
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.amqp</groupId> <artifactId>spring-rabbit-test</artifactId> <scope>test</scope> </dependency> </dependencies>
properties
#Server server.port=8081 server.servlet.context-path=/api-v1 #Konfigurasi RabbitMQ spring.rabbitmq.host=localhost spring.rabbitmq.port=5672 spring.rabbitmq.username=guest spring.rabbitmq.password=guest #QUEQUE rabbit.queue-log=queue_log rabbit.exchange-log=exchange_log rabbit.routing-log=routing_log
PropertiesRabbit
import lombok.Getter; import lombok.Setter; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Getter @Setter @Component @ConfigurationProperties(prefix = "rabbit") public class PropertiesRabbit { private String queueLog; private String exchangeLog; private String routingLog; }
ConfigRabbit
Mendefinisikan konfigurasi RabbitMQ, termasuk pembuatan queue dan exchange.
ConfigRabbit
import org.springframework.amqp.core.*; 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.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class ConfigRabbit { @Autowired private PropertiesRabbit propertiesRabbit; @Bean public Queue queue(){ return new Queue(propertiesRabbit.getQueueLog()); } @Bean public TopicExchange exchange(){ return new TopicExchange(propertiesRabbit.getExchangeLog()); } @Bean public Binding binding(Queue queue, TopicExchange exchange){ return BindingBuilder .bind(queue) .to(exchange) .with(propertiesRabbit.getRoutingLog()); } @Bean public MessageConverter converter() { return new Jackson2JsonMessageConverter(); } @Bean public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) { RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory); rabbitTemplate.setMessageConverter(converter()); return rabbitTemplate; } }
ApiController
Controller untuk mengirim pesan ke RabbitMQ menggunakan RabbitTemplate untuk mengirim pesan ke queue yang telah ditentukan.
ApiController
import id.aban.rabbit.config.PropertiesRabbit; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; @RestController public class ApiController { @Autowired private PropertiesRabbit propertiesRabbit; @Autowired RabbitTemplate template; @PostMapping("/create") public ResponseEntity<String> create(@RequestBody String message) { template.convertAndSend( propertiesRabbit.getExchangeLog(), propertiesRabbit.getRoutingLog(), message); return ResponseEntity.ok("Pesan Berhasil Dikirim"); } }
Dengan mengikuti tutorial ini, Anda telah membuat aplikasi sederhana menggunakan RabbitMQ sebagai publisher.
0 Response to "Belajar RabbitMQ - Cara Membuat RabbitMQ Publisher"
Posting Komentar