rabbit安装配置

  1. 先安装支持库
  2. wget http://www.erlang.org/download/otp_src_R14B03.tar.gz
      692  ls
      693  tar xvzf otp_src_R14B03.tar.gz
      694   cd otp_src_R14B03
      695  ./configure
      696  make
      697  sudo make install
      698  yum -y install ncurses-devel
      699  make
      700  sudo make install
      701  ls
      702  ./configure –enable-shared
      703  apt-cache search ncurses
      704  yum install ncurses-devel
      705  ./config --prefix=/opt/erlang    --without-javac
      706  ./config
      707  make
      708  yum install gcc-c++
      709  yum install erlang-doc   
      710  yum search libtool
      711  yum search libtool-ltdl-devel
      712  yum install libtool
      713  yum install libtool-ltdl-devel
      714  ls
      715  make
      716  make install
      717  yum install erlang-doc
      718  ./config
      719  ls
      720  ./configure
      721  make
      722  make install

     

  3. 启动登录http://192.168.2.114:15672/ 密码和用户是guest
    建议是先下载rabbit 在用tar版本的.之后再找依赖库即可3.0以后版本没有sbin ,操作都在script下面,并且自带web 管理界面
    (1) 新增一个用户
    rabbitmqctl  add_user  Username  Password
    ./rabbitmqctl  add_user  moon moon
    (2) 删除一个用户
    rabbitmqctl  delete_user  Username
    (3) 修改用户的密码
    rabbitmqctl  change_password  Username  Newpassword
    (4) 查看当前用户列表
    rabbitmqctl  list_users
  4. 如果新加了用户不进去去是因为没有给权限,执行如下权限就行了
    ./rabbitmqctl set_user_tags moon administrator
    参考地址http://blog.csdn.net/zyz511919766/article/details/42292655
  5. 编写java生产者
    import com.rabbitmq.client.Channel;
    import com.rabbitmq.client.Connection;
    import com.rabbitmq.client.ConnectionFactory;
     
    import java.io.IOException;
    import java.util.concurrent.TimeoutException;
     
    /**
     * Created by wendongshan on 2016/5/25.
     */
    public class Tsst {
        private final static String QUEUE_NAME = "hello";
     
        public static void main(String[] argv) throws Exception {
     
            ConnectionFactory factory = new ConnectionFactory();
            factory.setHost("192.168.2.114");
            factory.setUsername("moon");
            factory.setPassword("moon");
            Connection connection = factory.newConnection();
            Channel channel = connection.createChannel();
     
            channel.queueDeclare(QUEUE_NAME, false, false, false, null);
            String message = "Hello World1!";
            channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
            System.out.println(" [x] Sent '" + message + "'");
     
            channel.close();
            connection.close();
        }
    }
  6. 消费者代码:
    import com.rabbitmq.client.Channel;
    import com.rabbitmq.client.Connection;
    import com.rabbitmq.client.ConnectionFactory;
    import com.rabbitmq.client.QueueingConsumer;
     
    public class Reqv {
        private final static String QUEUE_NAME = "hello";
     
        public static void main(String[] argv) throws Exception {
     
            ConnectionFactory factory = new ConnectionFactory();
            factory.setHost("192.168.2.114");
            factory.setPort(5672);
            Connection connection = factory.newConnection();
            Channel channel = connection.createChannel();
     
            channel.queueDeclare(QUEUE_NAME, false, false, false, null);
            System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
     
            QueueingConsumer consumer = new QueueingConsumer(channel);
            channel.basicConsume(QUEUE_NAME, true, consumer);
     
            while (true) {
                QueueingConsumer.Delivery delivery = consumer.nextDelivery();
                String message = new String(delivery.getBody());
                System.out.println(" [x] Received '" + message + "'");
            }
        }
    }
  7. 以上实现是简单实用,当然rabbit的使用还有很多种格式;如广播,主题;根据不同的业务现状不同的场景;
posted @ 2016-06-28 15:21  靖远小和尚  阅读(297)  评论(0)    收藏  举报