RabbitMq安装和代码的基本实现

linux 安装
1. 安装依赖环境
yum install build-essential openssl openssl-devel unixODBC unixODBC-devel make gcc gcc-c++ kernel-devel m4 ncurses-devel tk tc xz
2. 安装Erlang
rpm -ivh erlang-18.3-1.el7.centos.x86_64.rpm
如果出现libc.so错误说明gblic 版本太低。我们可以查看当前机器的gblic 版本
strings /lib64/libc.so.6 | grep GLIBC
sudo yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make -y
sudo rpm -Uvh *-2.17-55.el6.x86_64.rpm --force --nodeps
3. 安装RabbitMQ
rpm -ivh socat-1.7.3.2-5.el7.lux.x86_64.rpm
rpm -ivh rabbitmq-server-3.6.5-1.noarch.rpm
# 开启管理界面
rabbitmq-plugins enable rabbitmq_management
# 修改默认配置信息
vim /usr/lib/rabbitmq/lib/rabbitmq_server-3.6.5/ebin/rabbit.app
# 比如修改密码、配置等等,例如:loopback_users 中的 <<"guest">>,只保留guest
4.启动服务
service rabbitmq-server start # 启动服务
service rabbitmq-server stop # 停止服务
service rabbitmq-server restart # 重启服务
5.移动配置文件
cd /usr/share/doc/rabbitmq-server-3.6.5/
cp rabbitmq.config.example /etc/rabbitmq/rabbitmq.config
6.(先开端口或者关闭防火墙) 访问登录 ip:15672
user:guest
password:guest
安装完成


基本代码实现
依赖
 <dependencies>
        <dependency>
            <groupId>com.rabbitmq</groupId>
            <artifactId>amqp-client</artifactId>
            <version>5.6.0</version>
        </dependency>
    </dependencies>


    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
    
生产者代码
public class Producer_HelloWord {
    public static void main(String[] args) throws IOException, TimeoutException {
        /*建立连接*/
        ConnectionFactory connectionFactory = new ConnectionFactory();
        /*参数*/
        connectionFactory.setHost("");
        connectionFactory.setPort(5672);
        connectionFactory.setVirtualHost("");
        connectionFactory.setUsername("");
        connectionFactory.setPassword("");
        /*连接*/
        Connection connection = connectionFactory.newConnection();
        /*创建channel*/
        /*String queue,  队列名称
         boolean durable, 是否持久化,mq重启还存在
          boolean exclusive, 是否独占 只能有一个消费者监听 当连接关闭时是否删除列队
          boolean autoDelete, 是否自动删除 当没有消费者时自动删除
           Map<String, Object> arguments   参数信息*/
        Channel channel = connection.createChannel();
        /*创建队列 如果没有这个名子的队列创建有则不创建*/
        channel.queueDeclare("hello_world", true, false, false, null);
        /*发送消息*/
        /*String exchange,  交换机名称
        String routingKey,  路由名称
         boolean mandatory,
          boolean immediate,
          BasicProperties props,  配置信息
          byte[] body  / body.getBytes()  发送消息的数据*/
        String body = "你好";
        channel.basicPublish("", "hello_world", null, body.getBytes());
        /*释放资源*/
        channel.close();
        connection.close();

    }

}

消费者代码
public class CousmerWorld {
    public static void main(String[] args) throws IOException, TimeoutException {
        /*建立连接*/
        ConnectionFactory connectionFactory = new ConnectionFactory();
        /*参数*/
        connectionFactory.setHost("");
        connectionFactory.setPort(5672);
        connectionFactory.setVirtualHost("");
        connectionFactory.setUsername("");
        connectionFactory.setPassword("");
        /*连接*/
        Connection connection = connectionFactory.newConnection();
        /*创建channel*/
        /*String queue,  队列名称
         boolean durable, 是否持久化,mq重启还存在
          boolean exclusive, 是否独占 只能有一个消费者监听 当连接关闭时是否删除列队
          boolean autoDelete, 是否自动删除 当没有消费者时自动删除
           Map<String, Object> arguments   参数信息*/
        Channel channel = connection.createChannel();
        /*创建队列 如果没有这个名子的队列创建有则不创建*/
        channel.queueDeclare("hello_world", true, false, false, null);
        /*发送消息*/
        /*String queue,  队列名称
        DeliverCallback deliverCallback,回调对象
        CancelCallback cancelCallback, 回调对象
        ConsumerShutdownSignalCallback shutdownSignalCallback*/
        DefaultConsumer defaultConsumer = new DefaultConsumer(channel){
            /*回调方法当收到消息时执行此方法*/
            /*String consumerTag,  标识
            Envelope envelope,获取一些信息 交换机 路由KEY
            AMQP.BasicProperties properties, 配置信息
             byte[] body   收到的信息*/
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("consumerTag:"+consumerTag);
                System.out.println("envelopeExchange:"+envelope.getExchange());
                System.out.println("properties:"+properties);
                System.out.println("body:"+new String(body));
            }
        };
        channel.basicConsume("hello_world",true,defaultConsumer);

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

20岁30年经验的码农

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值