参考:http://www.rabbitmq.com/install-rpm.html
1.安装Erlang,参考上面Install monolithic Erlang from VMware,从VMware下载erlang-R15B-02.1.el6.x86_64.rpm,然后直接
rpm -ivh erlang-R15B-02.1.el6.x86_64.rpm
2.安装rabbitmq-server.
a.先下载rabbitmq-server:http://www.rabbitmq.com/releases/rabbitmq-server/v3.4.1/rabbitmq-server-3.4.1-1.noarch.rpm
b.再执行:rpm --import http://www.rabbitmq.com/rabbitmq-signing-key-public.asc
c.最后执行:yum install rabbitmq-server-3.4.1-1.noarch.rpm
3.根据需要配置(配置可默认,即此步可省略)
4.管理
a.开机启动:chkconfig rabbitmq-server on.
b.启动:/sbin/service rabbitmq-server start
使用guest远程连接rabbitmq-server.要开启权限,详细参考https://www.rabbitmq.com/access-control.html.
只需要rabbitmq.config添加[{rabbit, [{loopback_users, []}]}].
1.安装Erlang,参考上面Install monolithic Erlang from VMware,从VMware下载erlang-R15B-02.1.el6.x86_64.rpm,然后直接
rpm -ivh erlang-R15B-02.1.el6.x86_64.rpm
2.安装rabbitmq-server.
a.先下载rabbitmq-server:http://www.rabbitmq.com/releases/rabbitmq-server/v3.4.1/rabbitmq-server-3.4.1-1.noarch.rpm
b.再执行:rpm --import http://www.rabbitmq.com/rabbitmq-signing-key-public.asc
c.最后执行:yum install rabbitmq-server-3.4.1-1.noarch.rpm
3.根据需要配置(配置可默认,即此步可省略)
4.管理
a.开机启动:chkconfig rabbitmq-server on.
b.启动:/sbin/service rabbitmq-server start
c.停止:/sbin/service rabbitmq-server stop
spring AMQP入门测试:
1.pom.xml.主要依赖
<dependencies>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
<version>1.4.0.RC1</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>http://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
2.RabbitConfiguration.java.CachingConnectionFactory默认host是localhost,port是5672,username是guest,password是guest.
使用guest远程连接rabbitmq-server.要开启权限,详细参考https://www.rabbitmq.com/access-control.html.
只需要rabbitmq.config添加[{rabbit, [{loopback_users, []}]}].
package com.exam.config;
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* Created by xin on 14/11/3.
*/
@Configuration
public class RabbitConfiguration {
@Bean
public ConnectionFactory connectionFactory() {
CachingConnectionFactory connectionFactory = new CachingConnectionFactory("192.168.1.118");
return connectionFactory;
}
@Bean
public AmqpAdmin amqpAdmin() {
return new RabbitAdmin(connectionFactory());
}
@Bean
public RabbitTemplate rabbitTemplate() {
return new RabbitTemplate(connectionFactory());
}
@Bean
public Queue myQueue() {
return new Queue("myqueue");
}
}
3.RabbitConfigurationTest.java.运行main方法查看测试结果(不要忘了启动rabbitmq-server,也不要让防火墙困扰了).
package com.exam.config;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* Created by xin on 14/11/3.
*/
public class RabbitConfigurationTest {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(RabbitConfiguration.class);
AmqpTemplate template = context.getBean(AmqpTemplate.class);
template.convertAndSend("myqueue", "foo");
String foo = (String) template.receiveAndConvert("myqueue");
System.out.println(foo);
}
}