SpringBoot——SpringBoot整合RabbitMQ,java编程题面试

SpringBoot——SpringBoot整合RabbitMQ,java编程题面试,第1张

SpringBoot——SpringBoot整合RabbitMQ,java编程题面试

spring.rabbitmq.addresses=192.168.31.199:32771

#用户名密码

spring.rabbitmq.username=guest

spring.rabbitmq.password=guest

spring.rabbitmq.virtual-host=/

spring.rabbitmq.connection-timeout=15000

3.创建实体对象

======================================================================

如果大家没有@Data注解,需要创建属性的get和set方法。

package com.youyou.entity;

import lombok.Data;

import java.io.Serializable;

@Data

public class Order implements Serializable {

private String id;

private String name;

private String messageId;

}

注意:一定要实现Serializa

《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》

【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 完整内容开源分享

ble接口

4.创建向rabbitMQ中发送数据的类(核心)

======================================================================================

package com.youyou.producer;

import com.youyou.entity.Order;

import org.springframework.amqp.rabbit.core.RabbitTemplate;

import org.springframework.amqp.rabbit.support.CorrelationData;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Component;

@Component

public class OrderSend {

@Autowired

private RabbitTemplate rabbitTemplate;

public void send (Order order) throws Exception{

CorrelationData correlationData = new CorrelationData();

correlationData.setId(order.getMessageId());

rabbitTemplate.convertAndSend(“order-exchange” //交换机

,“order.abc” //路由

,order //发送数据

,correlationData); //消息唯一id

}

}

5.配置rabbitMQ的交换机和路由键

==================================================================================

添加 一个rabbitMQ交换机

添加rabbitMQ队列

到目前为止rabbitMQ的配置整合已经完成。就是这么简单

6.测试

==================================================================

package com.youyou.worldmq;

import com.youyou.entity.Order;

import com.youyou.producer.OrderSend;

import org.junit.Test;

import org.junit.runner.RunWith;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.boot.test.context.SpringBootTest;

import org.springframework.test.context.junit4.SpringRunner;

import java.util.UUID;

@RunWith(SpringRunner.class)

@SpringBootTest

public class MQApplicationTests {

@Autowired

private OrderSend orderSend;

@Test

public void send() throws Exception {

Order order = new Order();

order.setId(“2018100100001”);

order.setName(“测试订单”);

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5676941.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存