Summary Message Queuing face questions

Summary Message Queuing face questions

1. Message Queuing application scenarios What?

A: Message Queuing application scenario as follows.

  • Application of decoupling, for example, after a user orders, the order requires notification system inventory system, if the inventory system is not accessible, orders minus inventories will fail, causing orders to fail. Order system coupled to the system inventory, this time if you use the message queue, the user can return to the success of the first message persistence, and so after inventory system recovery, you can subtract inventory of normal consumption.
  • Peak load shifting, for example, spike activity, usually because the amount of traffic, causing traffic surge, hang application, this time adding the message queue, the server receives a user request, first written message queue, if the message queue length exceeds the maximum number, the user request or discarded directly jump to the error page.
  • Log system, for example, the client is responsible for collecting logs, and the timing of writing the message queue, the message queue and then unified log data storage and forwarding.

What are the advantages 2.RabbitMQ there?

A: Advantages RabbitMQ as follows:

  • Reliability, RabbitMQ persistent support to ensure the stability of the message;
  • High concurrency, RabbitMQ using Erlang development language, Erlang is a language developed for the telephone exchange, natural aura that comes with high concurrency and high availability features;
  • Cluster deployment is simple, it is because of Erlang makes RabbitMQ cluster deployment becomes very simple;
  • Community active high, because RabbitMQ used widely, so the activity of the community is also very high;
  • Solve the problem of low costs, because more data, so the cost to solve the problem is very low;
  • Supports multiple languages, mainstream programming languages ​​support, such as Java, .NET, PHP, Python, JavaScript, Ruby, Go and so on;
  • Plug-and more convenient to use, such as web console message management plug-ins, message delay plug-ins.

What are the important role 3.RabbitMQ there?

A: RabbitMQ contains the following three important roles:

  • Manufacturer: creator of the message, is responsible for creating and push data to the message server;
  • Consumer: a message receiver for processing data and an acknowledgment message;
  • Agent: RabbitMQ is itself, for playing the role of "express" itself does not produce a message, just play the role of "Express" is.

What are the important components 4.RabbitMQ there? They do?

A: The critical components RabbitMQ contains are: the ConnectionFactory (connection manager), Channel (channel), the Exchange (switch), Queue (queue), RoutingKey (routing keys), bindingKey important components (bound key) or the like, which the role is as follows:

  • The ConnectionFactory (connection manager): application establishes a connection between the manager and RabbitMQ, using program code;
  • Channel (Channel): used in the feed channel message;
  • The Exchange (switch): for receiving, allocation message;
  • Queue (queue): storing a message producers;
  • RoutingKey (routing key): the data generator for assigning to the switch;
  • BindingKey (binding key): message for the exchanger bound to the queue.

5. What is the message persistence?

A: The message persistence is to save the message to the physical media, in order to prevent the loss of messages.

6.RabbitMQ to implement the message persistence, what conditions need to meet?

A: RabbitMQ message persistence to achieve, must meet the following four conditions:

  • The durable time delivery message set to true, the message persistence, Code: channel.queueDeclare (x, true, false, false, null), parameter 2 is set to true persistence;
  • DeliveryMode delivery mode setting is set to 2 (persistent), Code: channel.basicPublish (X, X, MessageProperties.PERSISTENT the TEXT PLAIN, X), the parameter is set to 3 stored plain text to disk;
  • Persistent message has arrived on the switch;
  • Message has arrived persistent queue.

7. What message persistence of shortcomings? How to ease?

A: The message is very persistent shortcomings consumption performance, because you want to write a lot less hard than the write memory performance, thereby reducing the throughput of the server. SSDs can be used to increase the access speed, in order to achieve remission message persistence disadvantages.

8. How to use Java code to connect to RabbitMQ?

A: Using Java code to connect to RabbitMQ following two ways:

method one:

public static Connection GetRabbitConnection() {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setUsername(Config.UserName);
    factory.setPassword(Config.Password);
    factory.setVirtualHost(Config.VHost);
    factory.setHost(Config.Host);
    factory.setPort(Config.Port);
    Connection conn = null;
    try {
        conn = factory.newConnection();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return conn;
}

Second way:

public static Connection GetRabbitConnection2() {
    ConnectionFactory factory = new ConnectionFactory();
    // 连接格式:amqp://userName:password@hostName:portNumber/virtualHost
    String uri = String.format("amqp://%s:%s@%s:%d%s", Config.UserName, Config.Password, Config.Host, Config.Port,
            Config.VHost);
    Connection conn = null;
    try {
        factory.setUri(uri);
        factory.setVirtualHost(Config.VHost);
        conn = factory.newConnection();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return conn;
}

9. Using a Java code example RabbitMQ consumption and production?

A: The code is as follows:

public static void main(String[] args) {
    publisher();     // 生产消息
    consumer();     // 消费消息
}

/\*\* \* 推送消息 \*/
public static void publisher() {
    // 创建一个连接
    Connection conn = ConnectionFactoryUtil.GetRabbitConnection();
    if (conn != null) {
        try {
            // 创建通道
            Channel channel = conn.createChannel();
            // 声明队列【参数说明:参数一:队列名称,参数二:是否持久化;参数三:是否独占模式;参数四:消费者断开连接时是否删除队列;参数五:消息其他参数】
            channel.queueDeclare(Config.QueueName, false, false, false, null);
            String content = String.format("当前时间:%s", new Date().getTime());
            // 发送内容【参数说明:参数一:交换机名称;参数二:队列名称,参数三:消息的其他属性-routing headers,此属性为MessageProperties.PERSISTENT\_TEXT\_PLAIN用于设置纯文本消息存储到硬盘;参数四:消息主体】
            channel.basicPublish("", Config.QueueName, null, content.getBytes("UTF-8"));
            System.out.println("已发送消息:" + content);
            // 关闭连接
            channel.close();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

/\*\* \* 消费消息 \*/
public static void consumer() {
    // 创建一个连接
    Connection conn = ConnectionFactoryUtil.GetRabbitConnection();
    if (conn != null) {
        try {
            // 创建通道
            Channel channel = conn.createChannel();
            // 声明队列【参数说明:参数一:队列名称,参数二:是否持久化;参数三:是否独占模式;参数四:消费者断开连接时是否删除队列;参数五:消息其他参数】
            channel.queueDeclare(Config.QueueName, false, false, false, null);

            // 创建订阅器,并接受消息
            channel.basicConsume(Config.QueueName, false, "", new DefaultConsumer(channel) {
                @Override
                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                    String routingKey = envelope.getRoutingKey(); // 队列名称
                    String contentType = properties.getContentType(); // 内容类型
                    String content = new String(body, "utf-8"); // 消息正文
                    System.out.println("消息正文:" + content);
                    channel.basicAck(envelope.getDeliveryTag(), false); // 手动确认消息【参数说明:参数一:该消息的index;参数二:是否批量应答,true批量确认小于index的消息】
                }
            });

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

10.RabbitMQ exchange types are there?

A: RabbitMQ consumption patterns is the switch (Exchange) There are four types:

  • direct: polling
  • headers: polling, rather than routing header allows the use of message keys match, poor performance, almost no
  • fanout: broadcast, sent to all subscribers
  • topic: matching mode, allowing the use of regular expression matching message

RabbitMQ default is direct way.

How to ensure that each message can be 11.RabbitMQ consumption?

A: RabbitMQ use ack message acknowledgments way to ensure that each message can be consumed, the developers according to their actual business, to select channel.basicAck () method manual confirmation message is consumed.

After receiving the message 12.RabbitMQ must consume it?

A: After receiving the message RabbitMQ can not consume, consume before the message confirmation, you can do the following two things:

  • Consumer rejection message, use channel.basicReject (message number, true) method, the message will be assigned to other subscribers;
  • Is set dead letter queue, dead letter queue for specialized storage rejected message queue.

Released a key route for the "com.mq.rabbit.error" message under 13.topic mode, you do not receive the following message is?

A cn.mq.rabbit. *
B: #. Mistake
100: cn.mq. *
500: # cn.mq.

Answer: C

Analytical Title: "*" to match a segment of the content, "#" and 0 for matching a plurality of characters (with the division ".").

14. The following can obtain historical message is?

A: topic switch
B: fanout switch
C: direct exchanger
D: None of the above

Answer: C

Topic parsing: fanout and are broadcast in the form of topic, history can not get the message, and may direct.

15.RabbitMQ contains transaction functionality? how to use?

A: RabbitMQ contain transaction function, mainly to set the channel (Channel), the following three main methods:

  • channel.txSelect () statement starts a transaction mode;
  • channel.txComment () commits the transaction;
  • channel.txRollback () rolls back the transaction.

16.RabbitMQ transaction under what circumstances is invalid?

A: RabbitMQ transaction in autoAck = true consumption is automatically recognized when the transaction is invalid. Because if consumption is automatically confirmed, RabbitMQ will direct the message removed from the queue, even if the transaction is rolled back behind not play any role.

17.Kafka from ZooKeeper can use it alone?

A: Kafka can not be divorced ZooKeeper alone, because Kafka Kafka management and coordination of the use ZooKeeper node server.

There are several strategies 18.Kafka data retention?

A: Kafka There are two data retention policy: retention and retention in accordance with the size of messages stored in accordance with the expiration time.

19.Kafka also set at 7 and 10G clear the data, the fifth day when the news reached 10G, this time will be how to deal with Kafka?

A: This time Kafka performs data cleanup, regardless of size and time to meet the conditions which will clear the data.

20. What would cause Kafka run slower?

A: The following cases can lead to Kafka run slower:

  • CPU performance bottlenecks
  • Disk read and write bottlenecks
  • Network bottlenecks

21. What is the use of cluster Kafka need to pay attention?

A: Kafka cluster using the following considerations:

  • Number of clusters is not possible, it is best not more than seven, because the more nodes, the longer the time required to copy the message, the lower the throughput of the entire group;
  • The best number of clusters is singular, because more than half failed cluster can not be used, set to the singular fault tolerance higher.

I welcome the attention of the public number, keyword reply " the Java ", there will be a gift both hands! ! ! I wish you a successful interview ! ! !

% 97% E5% 8F% B7 % E4% BA% 8C% E7% BB% B4% E7% A0% 81.png)

Guess you like

Origin www.cnblogs.com/dailyprogrammer/p/12272757.html