Rabbitmq distributed cluster environment construction

 1 、 SQM

 

       MQ stands for Message Queue, and Message Queue (MQ) is an application-to-application communication method. Applications communicate by reading and writing messages to and from the queue (data for the application) without requiring a dedicated connection to link them. Messaging refers to the communication between programs by sending data in messages, rather than by calling each other directly, which is typically used for techniques such as remote procedure calls.

2. Installation

http://www.rabbitmq.com/download.html Select Universal Binary to decompress and install

4. Start

sbin/rabbitmq-server -detached

 5. Enable web management interface

sbin/rabbitmq-plugins enable rabbitmq_management

 web access: http://IP:15672/

6. Set the account password

 

# delete guest account
sbin/rabbitmqctl delete_user guest
# New management account admin
sbin/rabbitmqctl add_user admin 123456
# Set admin as super administrator
sbin/rabbitmqctl set_user_tags admin administrator
# View the list of current user roles
 sbin/rabbitmqctl list_users

 7. Java client code

a. Add the Jar package

<dependency>
	        <groupId>com.rabbitmq</groupId>
	        <artifactId>amqp-client</artifactId>
	        <version>3.0.4</version>
	</dependency>

 

b. Generate code

import java.io.IOException;  
  
import com.rabbitmq.client.Channel;  
import com.rabbitmq.client.Connection;  
import com.rabbitmq.client.ConnectionFactory;  
  
public class Send {  
    private final static String QUEUE_NAME = "hello";  
  
    public static void main(String[] args) throws IOException {  
        ConnectionFactory factory = new ConnectionFactory();  
        factory.setHost("10.0.0.104");  
        factory.setUsername("rabbitmq");
        factory.setPassword("123456");
        factory.setPort( 5672);
        Connection connection = factory.newConnection();  
        Channel channel = connection.createChannel();  
        // The second parameter here refers to whether the message is persistent, and the persistent delivery is true, which is consistent with the consumer here
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        
        for(int x=0;x<20;x++){
	        String message = "Hello World!";  
	        channel.basicPublish("", QUEUE_NAME, null, message.getBytes());  
	        System.out.println(" [x] Sent '" + message + "'");  
        }
        channel.close();  
        connection.close();  
    }  
}  

 c. Consumer code

import com.rabbitmq.client.AMQP.BasicProperties;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;  
   
  
public class Reqv {  
    private final static String QUEUE_NAME = "hello";  
  
    public static void main(String[] argv) throws Exception {  
  
        ConnectionFactory factory = new ConnectionFactory();  
        factory.setHost("10.0.0.104");  
        factory.setUsername("rabbitmq");
        factory.setPassword("123456");
        Connection connection = factory.newConnection();  
        Channel channel = connection.createChannel();  
        // The second parameter here refers to whether the message is persistent, and the persistent delivery is true, which is consistent with the producer here
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);  
        System.out.println(" [*] Waiting for messages. To exit press CTRL+C");  
  
         DefaultConsumer consumer = new DefaultConsumer(channel){
        	
        	@Override
        	public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
        			throws IOException {
        		System.out.println(" [m] Received '" + new String(body, "UTF-8") + "'" );
        	}
        };
        boolean autoAck = true;
        
        channel.basicConsume(QUEUE_NAME, autoAck, consumer);
    }  
}  

8、spring boot amqp 

a. Introduce the Jar package

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

b. Configuration file:

spring:
    rabbitmq:
      host: 10.0.0.104
      port: 5672
      username: admin
      password: 123456

  

c. Producer code

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MessageQueueService {
	@Autowired
	private RabbitTemplate rabbitTemplate;
	public void send(String msg) {
		System.out.println("Sender : " + msg);
		rabbitTemplate.convertAndSend( "hello" , msg);
	}
	
}

d. Consumer code:

import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
@RabbitListener(queues = { "hello" } )
public class ReceiverQueue {
	@RabbitHandler
	public void process(String hello) {
		System.out.println("Receiver : " + hello);
	}
}

 

 

9. If you are prompted that you do not have permission, enter the web management interface, select Admin, and set the Can access virtual hosts column in the future. Of course, you can also control permissions according to the directory structure.



 

10. Rabbitmq distributed construction

a. Prepare two machines server1 and server2 or docker environment, install the above service rabbitmq on both machines, and then run the command

Both machines must start rabbitmq

#Stop rabbitmq application, not server
sbin/rabbitmqctl stop_app
#Link server1
sbin/rabbitmqctl join_cluster rabbit@server1
# Here may prompt the following error
Error: unable to connect to nodes [rabbit@rabbitmqmaster]: nodedown

DIAGNOSTICS
===========

attempted to contact: [rabbit@rabbitmqmaster]

rabbit@rabbitmqmaster:
  * connected to epmd (port 4369) on rabbitmqmaster
  * epmd reports node 'rabbit' running on port 25672
  * TCP connection succeeded but Erlang distribution failed

  * Authentication failed (rejected by the remote node), please check the Erlang cookie


current node details:
- node name: 'rabbitmq-cli-88@rabbitmqsalver2'
- home dir: /root
- cookie hash: 55WeQTsiv71JcEDq/JcE9Q==

Find the .erlang.cookie under the home dir, here is /root/.erlang.cookie to keep the contents of the file on the two machines consistent, and run the command again successfully.

# View cluster status
sbin/rabbitmqct cluster_status

 11. Command to set account permissions

#set up
sbin/rabbitmqctl set_permissions -p "/" admin ".*" ".*" ".*"
#View permissions
sbin/rabbitmqctl list_permissions

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326994530&siteId=291194637