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> <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>
#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
@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.
@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.
@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