java interview questions (middleware)

Table of contents

RabbitMQ

1. What is MQ? Why use MQ? Advantages of MQ

2. What is RabbitMQ? Usage scenarios

3. What are the advantages and disadvantages of RabbitMQ?

4. Working mode of RabbitMQ

5. What are the common problems of MQ?

6. How to ensure the order of RabbitMQ messages?

7. How to ensure that messages are not repeatedly consumed?

8. How to distribute the message?

9. How to ensure that the message is sent to RabbitMQ correctly? How to ensure that the receiver of the message consumes the message?

10. How to ensure the reliable transmission of RabbitMQ messages?

11. Why shouldn't the persistence mechanism be used for all messages?

12. How to ensure high availability?

13. How to ensure message consistency

14. How to implement the message retry mechanism?

15. Where is RabbitMQ used in the project

16. RabbitMQ transaction

17. What is a dead letter queue? How?

18. A large amount of data backlog, how to solve it

19. Delay consumption

20. The running process of RabbitMQ

Shiro

1. What is Shiro?

2. Why use Shiro

3. Three core components

4. What are the basic functions

5. Advantages of Shiro

6. Shiro's four-fold authority control method

7. Shiro's login and running process

8. How to implement self-fulfilling authorization

9. Shiro authorization process

10. What are coarse-grained and fine-grained permissions?

11. How to authorize coarse and fine particles?


RabbitMQ

1. What is MQ? Why use MQ? Advantages of MQ

        MQ: message queue; is a container for saving messages during message transmission.

mainly:

        Decoupling: Communication between systems through messages, no need to care about the processing of other systems

        Asynchronous: When multiple systems write to the library, there is no need for synchronous execution, which can reduce the response time

        Peak shaving: reduce the pressure on the server during peak hours

2. What is RabbitMQ? Usage scenarios

        RabbitMQ is an open source message middleware written in Erlang and based on the AMQP protocol;

        Usage scenarios: scheduled tasks, asynchronous communication between services, sequential consumption, request peak shaving

3. What are the advantages and disadvantages of RabbitMQ?

advantage:

        1. Application decoupling (MQ is equivalent to an intermediary, the producer interacts with the consumer through MQ, and it decouples the application)

        2. Task asynchronous processing (the message queue will notify the message receiver to perform asynchronous processing of operations that do not require synchronous processing and take a long time. Improve the response time of the application)

        3. Peak shaving and valley filling (when the amount of data is large, the database is too late to process, and it is stored in mq for storage, and processed according to consumption capacity)

shortcoming:

        1. Reduced system availability (the more external dependencies the system introduces, the worse the system stability)

        2. The complexity of the system has increased (the complexity of the system has been greatly increased. In the past, there were synchronous remote calls between systems, but now asynchronous calls are made through MQ)

        3. Consistency issues (how to ensure the consistency of message data processing)

4. Working mode of RabbitMQ

        1. Simple mode: generate a message, put the message into the queue, the consumer listens to the message queue, and consumes if there is a message in the queue

        2. Working mode: generate a message, put the message into the queue, multiple consumers listen at the same time, whoever gets it first consumes it

        3. Publish/subscribe mode: messages are routed and delivered to multiple queues, and one message is acquired by multiple consumers

        4. Routing mode: The message producer sends the message to the switch according to the routing judgment, and the switch can consume the message according to the message queue corresponding to the routing key.

        5. Topic mode: The message producer generates a message and sends the message to the switch, and the switch fuzzily matches to the corresponding queue according to the rules of the key, and the listening consumer of the queue receives the message consumption

5. What are the common problems of MQ?

        1. The sequence of messages

        2. Duplication of messages

6. How to ensure the order of RabbitMQ messages?

        1. Split into multiple queues, each with one consumer;

        2. A consumer corresponds to a queue, and then the consumer internally uses a memory queue for queuing, and then distributes it to different underlying workers for processing

7. How to ensure that messages are not repeatedly consumed?

Cause: Due to the sudden disconnection of the network between the consumer and the MQ server, etc., after the consumer consumes, an ack confirmation message is returned to the MQ server, but MQ does not receive it, resulting in repeated consumption.

The solution is: to ensure the uniqueness of the message, even if it is transmitted multiple times, do not let the multiple consumption of the message affect

        1. Make a unique mark on the data written into the message queue. When consuming a message, judge whether it has been consumed according to the unique mark

        2. Use a log table to record the ID of the message that has been successfully processed. If the newly arrived message ID is already in the log table, then the message will not be processed

8. How to distribute the message?

Distribution mechanism:

        1. Polling distribution (default): When multiple worker threads receive messages from the queue, they use polling, also known as fair distribution

        2. Unfair distribution: send tasks to idle consumers instead of waiting there all the time

        3. Pre-value distribution: It is to set the channel to prefetch 5 messages. After receiving 5 messages, it will no longer respond to subsequent messages, and the number of internal parameters is the number of pre-fetched values ​​(required to be greater than 1)

9. How to ensure that the message is sent to RabbitMQ correctly? How to ensure that the receiver of the message consumes the message?

Correct sending: If the channel is set to sender confirmation mode, all messages published on the channel will be assigned a unique ID. Once the message is delivered to the destination queue, or the message is written to disk, the channel will send an acknowledgment to the producer; if an internal error occurs in RabbitMQ and the message is lost, a nack message will be sent.

Receiving consumption: the receiver confirmation mechanism, the consumer must confirm after receiving each message. RabbitMQ can safely delete the message from the queue only if the consumer confirms the message.

10. How to ensure the reliable transmission of RabbitMQ messages?

        Unreliable messages may be due to message loss, hijacking, etc.; loss is divided into: producer lost message, message list lost message, consumer lost message;

        The producer loses the message: before sending the message, start the transaction, and then send the message. If any exception occurs during the sending process, the transaction will be rolled back. If the sending is successful, the transaction will be committed

        Missing messages in the message list: message persistence (enable the configuration of the persistent disk)

        Consumers lose messages: change to manual confirmation message mode; consumers will automatically reply that RabbitMQ has received the message after receiving the message and before processing the message; if the message processing fails at this time, the message will be lost; solution: processing After the message is successful, manually reply to the confirmation message

11. Why shouldn't the persistence mechanism be used for all messages?

        Lead to performance degradation; only key messages are persisted (according to business importance), and it should be ensured that the amount of key messages will not cause performance bottlenecks

12. How to ensure high availability?

        Stand-alone mode, common cluster mode, mirror cluster mode

13. How to ensure message consistency

        1. The producer first sends the message to MQ, and the message status is marked as pending;

        2. After MQ receives the message, it persists the message to the message storage, but does not deliver the message to the consumer;

        3. MQ returns the message persistence result (success or failure), and the producer judges how to process the business operation according to the returned result; (failure: abandon the business operation processing; success: execute the business operation processing)

        4. After the business operation is completed, send the business operation result (success/failure) to MQ;

        5. After MQ receives the result of the business operation, it processes it according to the result; (failure: delete the message in the message storage; success: update the status of the message in the message storage to be sent) and then execute message delivery;

        6. After the previous forward process is successful, deliver the message to the consumer;

14. How to implement the message retry mechanism?

        When a consumer consumes a message, an abnormal situation occurs, causing the message to be unconfirmed, and the message will be consumed repeatedly (by default, there is no number of repetitions, that is, infinite loop consumption). You can set the number of retries and process the message after the number of retries is reached

15. Where is RabbitMQ used in the project

        Chat room, seckill, points

16. RabbitMQ transaction

Transaction is a strategy in MQ to ensure that all messages are sent successfully and prevent loss of messages;

        Open transaction: txSelect() method

        Commit transaction: txCommit() method

        Rollback transaction: txRollback() method (rollback transaction must be closed before)

        Close: close()

17. What is a dead letter queue? How?

        When a message becomes a dead letter in a queue, it can be resent to another switch. This switch is a dead letter switch. The queue bound to a dead letter switch is called a dead letter queue.

There are several reasons for dead letters:

        1. Message rejected

        2. The queue is full and no more can be added

        3. The message lifetime expires

        If the dead letter queue information is configured, the message will be thrown into the dead letter queue; if not configured, the message will be discarded;

Dead letter queue application scenarios:

        It is generally used in more important business queues to ensure that messages that have not been correctly consumed are not discarded. Generally, the possible causes of consumption exceptions are mainly due to errors in the message itself that cause processing exceptions. By configuring the dead letter queue, it is possible to let incorrectly processed messages The message is temporarily stored in another queue, and after the subsequent investigation of the problem, write the corresponding processing code to process the dead letter message

18. A large amount of data backlog, how to solve it

When the number of messages is too large, it may cause a backlog of messages, which in turn affects system performance.

solve:

        1. Increase consumers: increasing the number of consumers can improve message processing speed

        2. Increase the queue: You can increase the number of queues to alleviate the message backlog

        3. Add MQ nodes: Adding MQ nodes can improve message processing capabilities

        4. Message retry mechanism: When the message processing fails, the message can be reposted to the queue

        5. Set message expiration time: You can set the message expiration time. When the message waits in the queue for more than the specified time, it will be automatically deleted, thereby reducing the message backlog

        6. Use the current limiting mechanism: You can use the current limiting mechanism to control the consumption speed of consumers, so as to avoid consumers being unable to process in time due to too many messages

19. Delay consumption

  1. Create a dead letter exchange and bind a dead letter queue

  2. Set the queue timeout for the target queue of the message and specify the dead letter switch and routing key

  3. Bind the message's target queue to a dead-letter exchange

  4. Consumers listen to the dead letter queue to get timed out messages

20. The running process of RabbitMQ

Producer sends message:

       1. The producer connects to MQ, establishes a connection, and opens a channel

        2. The producer declares an exchange and sets properties;

        3. The producer declares a queue and sets properties;

        4. The producer binds the exchange and the queue through the routing key

        5. The producer sends a message to MQ, which contains routing key, switch and other information

        6. The switch matches the queue through the routing key, finds it and stores it in the queue; if it does not find it, it returns to the producer

        7. Close the channel and close the connection

The consumer sends a message:

        1. The consumer connects to MQ, establishes a connection, and opens a channel

        2. The consumer requests the message in the consumption queue to MQ

        3. Wait for MQ to respond and deliver the message in the queue, and the consumer receives the message

        4. The consumer confirms receipt of the message

        5. Delete the confirmed message in MQ

        6. Close the channel and close the connection

Shiro

1. What is Shiro?

        is a powerful and easy-to-use Java security (permissions) framework. Shiro can complete: authentication, authorization, encryption, session management, integration with the Web, caching, etc.

2. Why use Shiro

        Ease of use: It is very simple to use Shiro to build a system security framework

        Comprehensive: Shiro includes the functions required by the system security framework and is a "one-stop service" that meets security requirements

        Flexible: Shiro can work in any application environment

        Strong web support: Shiro has excellent web application support

        Strong compatibility: Shiro's design patterns make it easy to integrate with other frameworks and applications

        Community support: Shiro is an open source project of the Apache Software Foundation, with complete community support and document support

3. Three core components

        Subject : the current user's operation

        SecurityManager: used to manage all Subjects

        Realm: used to verify permission information

4. What are the basic functions

        1. Identity authentication/login, verify whether the user has the corresponding identity

        2. Authorization, that is, authority verification, verifies whether an authenticated user has a certain authority

        3. Session management, that is, after the user logs in, it is a session. Before logging out, all its information is in the session

        4. Encryption to protect the security of data, such as password encryption stored in the database instead of plaintext storage

        5. Web support, can be easily integrated into the Web environment

        6. Cache, for example, after a user logs in, his user information, roles/permissions do not need to be checked every time

        7. Shiro supports concurrent verification of multi-threaded applications

        8. Provide testing support

        9. Allowing a user to pretend to be another user (if they allow it) to gain access

        10. Remember me, this is a very common function, that is, after logging in once, you don’t need to log in next time

5. Advantages of Shiro

        1. Simple identity authentication, supporting multiple data sources

        2. Simple authorization of roles

        3. Not bundled with any framework or container, it can run independently

6. Shiro's four-fold authority control method

        1. URL level permission control

        2. Method annotation permission control

        3. Code level permission control

        4. Page label permission control

7. Shiro's login and running process

        1. First call Subject.login(token) to log in.
        2. SecurityManager to perform identity verification logic.
        3. Pass the token into Realm and get the identity verification information from Realm.

8. How to implement self-fulfilling authorization

        In actual development, the implementation class of AuthorizingRealm is usually provided, and the specific implementation of the doGetAuthorizationInfo() method is provided.

9. Shiro authorization process

        1. The application calls the method of the Subject to pass the permission

        2.Subject calls the corresponding method of securityManager

        3.SecurityManager calls the corresponding method of the Authorizer interface

        4. Each configured Realm is checked to see if it implements the same Authorizer interface

10. What are coarse-grained and fine-grained permissions?

        Coarse-grained permissions: management of resource types; that is, only control to menus, buttons, and methods. Coarse-grained examples such as: the user has user management permissions, has the permission to export order details

        Fine-grained permissions: control over resource instances; that is, permissions down to the data level, for example: users are only allowed to modify employee information in their own department, and users are only allowed to export order details created by themselves

11. How to authorize coarse and fine particles?

        Coarse-grained permissions: You can use filters to block urls uniformly

        Fine-grained permissions: control in service, control at program level, personalized programming

Guess you like

Origin blog.csdn.net/qq_35056891/article/details/129833350