1、先安装ELANG,再按照RabbitMQ
2、打开RabbitMQ控制台:rabbit command prompt
1、设置elang的路径:set ERLANG_HOME=D:\work_program_file\rabbitmq\elang\erl9.1
设置elang根目录,重要,否则一直提示找不到elang。
2、在控制台输入:rabbitmqctl start_app等待启动,出现:Starting node rabbit@...等字样表示启动成功了,这里示例:Starting node rabbit@MS-20170624IVFG
3、安装管理器:rabbitmq-plugins enable rabbitmq_management,用来访问web管理页面:如下示例
如果安装成功,控制台如下提示:
Enabling plugins on node rabbit@MS-
rabbitmq_management
The following plugins have been con
rabbitmq_management
rabbitmq_management_agent
rabbitmq_web_dispatch
Applying plugin configuration to ra
The following plugins have been ena
rabbitmq_management
rabbitmq_management_agent
rabbitmq_web_dispatch
started 3 plugins.
4、通过地址访问web管理控制台,默认端口15672,http://localhost:15672/
5、登录账户通过在控制台创建
创建管理员用户,负责整个MQ的运维
rabbitmqctl add_user kyoxue kyoxue
赋予administrator角色
rabbitmqctl set_user_tags kyoxue administrator
创建RabbitMQ监控用户,负责整个MQ的监控
rabbitmqctl add_user kyoxue1 kyoxue1
赋予monitoring角色:
rabbitmqctl set_user_tags kyoxue1 monitoring
创建某个项目的专用用户,只能访问项目自己的virtual hosts
rabbitmqctl add_user user_proj passwd_proj
赋予其management角色:
rabbitmqctl set_user_tags user_proj management
查看用户
rabbitmqctl list_users
使用户kyoxue具有/这个virtual host中所有资源的配置、写、读权限以便管理其中的资源
rabbitmqctl set_permissions -p / kyoxue '.*' '.*' '.*'
PS:这里设置了权限,在添加队列时可能会提示:Access refused. 表示账户权限设置有问题,去页面控制台:Admin\ kyoxue(用户)\Set permission下面点Set permission按钮重新设置下。
6、停止RabbitMQ:rabbitmqctl stop
7、在Virtual Host:(\)下添加一个测试队列test,如下图:
8、接下来,可以通过连接信息进行发布,订阅了
在springboot配置文件添加:
spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=kyoxue
spring.rabbitmq.password=kyoxue
spring.rabbitmq.virtual-host=/
9、引入rabbit mq依赖
这里用的springboot 1.4.3.RELEASE版本
10、生成一个队列bean单例,队列名称刚才配置的test
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class RabbitConfig { @Bean public org.springframework.amqp.core.Queue Queue() { return new org.springframework.amqp.core.Queue("test"); } }
11、建一个接收队列消息的客户端监听端,监听的队列名称要一致,这里是test,通过content接收字符文本内容,这里也可以是对象接收,springboot会自动序列,反序列化。
import org.springframework.amqp.rabbit.annotation.RabbitHandler; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Component; @Component @RabbitListener(queues="test") public class TestReceiver { @RabbitHandler public void process(String content) { System.out.println("accept:"+content); } }
12、建一个发布端,测试队列
import java.io.PrintWriter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; 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.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import com.chobits81.cc.component.util.DirectCommUtil; @Controller @RequestMapping(value = "/services/rabbit/test") public class RabbitTestController{ public static final Logger LOG = LoggerFactory.getLogger(RabbitTestController.class); @Autowired private AmqpTemplate rabbitTemplate; @RequestMapping(value = "/send", method = { RequestMethod.POST, RequestMethod.GET }) public void send(HttpServletRequest request,HttpServletResponse response) { PrintWriter printWriter = null; String string = ""; try { printWriter = response.getWriter(); response.setContentType("application/json;charset=" + DirectCommUtil.encoding_utf8); String content = request.getParameter("content"); rabbitTemplate.convertAndSend("test",content); string = "ok"; } catch (Exception e) { // TODO: handle exception LOG.error("异常",e); }finally { if (null != printWriter) { printWriter.print(string); printWriter.flush(); printWriter.close(); } } } }
13、客户端监听
@RabbitListener要结合@EnableRabbit一起使用,所以在springboot启动类上加入@EnableRabbit注解
import org.springframework.amqp.rabbit.annotation.EnableRabbit; @EnableRabbit //此处省略了其它注解配置... public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
14、启动springboot,通过地址测试效果:
后台客户端接收打印: