您的位置:首页 > 编程语言 > Java开发

Springboot之集成RabbitMQ

2017-11-06 16:48 471 查看
在集成RabbitMQ之前,首先需要安装RabbitMQ,我选择的是在centos上安装,具体的安装步骤请移步在Centos7上安装RabbitMQ

首先开始安装的第一步:

1、修改pom.xml文件

springboot对rabbitmq有很好的支持,提供了starter,支持自动配置

<!-- RabbitMQ -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>


2、修改application.properties文件,添加rabbitmq的参数

#===============================rabbitmq配置===============================
spring.rabbitmq.host=192.168.184.100
spring.rabbitmq.port=5672
spring.rabbitmq.username=root
spring.rabbitmq.password=0.0.0.
spring.rabbitmq.publisher-confirms=true
spring.rabbitmq.virtual-host=/


3、添加RabbitConfig.java,声明队列

@Configuration
public class RabbitConfig {

public static String QUEUE_LOGINRECORD = "loginRecord";

/*
* 登录记录队列
*/
@Bean
public Queue Queue() {
return new Queue(RabbitConfig.QUEUE_LOGINRECORD);
}

}


4、编写发送者,我这里是发送的是对象,其实发送对象和String是一样的操作,我这个示例的消息队列主要是完成登录成功时,用户的登录记录的保存操作

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.cy.example.config.RabbitConfig;
import com.cy.example.entity.LoginRecordEntity;

@Component
public class RabbitSender {

private static final Logger logger = LoggerFactory
.getLogger(RabbitSender.class);

@Autowired
private AmqpTemplate rabbitTemplate;

public void send(LoginRecordEntity loginRecord) {
logger.info("发送对象信息: " + loginRecord.toString());
this.rabbitTemplate.convertAndSend(RabbitConfig.QUEUE_LOGINRECORD, loginRecord);
}

}


5、编写监听者

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import com.cy.example.entity.LoginRecordEntity;
import com.cy.example.service.ILoginRecordService;

@Configuration
public class RabbitReceiver {

private static final Logger logger = LoggerFactory
.getLogger(RabbitReceiver.class);

@Autowired
private ILoginRecordService loginRecordService;

@RabbitListener(queues = "loginRecord")
public void process(LoginRecordEntity loginRecord) {
logger.info("接收到object : " + loginRecord);
loginRecordService.insert(loginRecord);
}

}


在这里我踩了不少的坑,一开始我编写的是

@Configuration
@RabbitListener(queues = "loginRecord")
public class RabbitReceiver {

private static final Logger logger = LoggerFactory
.getLogger(RabbitReceiver.class);

@Autowired
private ILoginRecordService loginRecordService;

@RabbitHandle//不知道为啥的这样写会一直报错监听失败,重复启动。网上查了一下,应该是springboot的starter写的有问题,
public void process(LoginRecordEntity loginRecord) {
logger.info("接收到object : " + loginRecord);
loginRecordService.insert(loginRecord);
}

}


6、调用。

@Autowired
private RabbitSender sender;

@SuppressWarnings("finally")
@RequestMapping("/validate")
@ResponseBody
public Map<String, Object> validate(String username, String password) {
Map<String, Object> map = new HashMap<String, Object>();
password = MD5Util.GetMD5Code(password);
UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(
username, password);
boolean flag = true;
String msg = "";
Subject subject = SecurityUtils.getSubject();
try {
subject.login(usernamePasswordToken); // 完成登录
UserEntity user = (UserEntity) subject.getPrincipal();
subject.getSession().setAttribute(WebConfig.LOGIN_USER, user);
LoginRecordEntity loginRecord = new LoginRecordEntity();
super.add(loginRecord);
loginRecord.setC_loginIp(super.getIP(getRequest()));
loginRecord.setC_username(user.getC_username());
//          loginRecordService.insert(loginRecord);
//采用消息中心的通知添加
sender.send(loginRecord);
msg = "登陆成功!";
map.put("flag", flag);
} catch (Exception exception) {
if (exception instanceof UnknownAccountException) {
logger.info("账号不存在: -- > UnknownAccountException");
msg = "登录失败,用户账号不存在!";
} else if (exception instanceof IncorrectCredentialsException) {
logger.info(" 密码不正确: -- >IncorrectCredentialsException");
msg = "登录失败,用户密码不正确!";
} else if (exception instanceof LockedAccountException) {
logger.info(" 用户被锁定: -- >LockedAccountException");
msg = "登录失败,用户被锁定!";
} else {
logger.info("else -- >" + exception);
msg = "登录失败,发生未知错误:" + exception;
}
map.put("flag", false);
} finally {
map.put("msg", msg);
return map;
}
}


最后提一下,在rabbitmq的web管理界面应该新建一个loginRecord的queue

下面附上我的项目地址,想看源码 的可以去下载,对你有帮助请star我的github地址
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  RabbitMQ springboot