LOKI Documentation

website·Indexed 30 days ago

https://guoshiqiufeng.github.io/loki-doc/

A simplified enhancement tool for Message Queues (MQ) designed to improve development efficiency. LOKI provides a non-intrusive way to consume and send messages via annotations, supports multiple MQ types, and integrates with Spring Boot 2.x and 3.x based on JDK8. Key features include auto and non-auto decoding, LokiClient for sending messages, BaseMapper support, and interceptors for outgoing and listening messages using the PipelineProcess interface.

Tokens
11.1K
Snippets
79
Records
92
Agent score
74%

What's inside LOKI

  1. Overview of LOKI MQ simplification tool

    LOKI is a tool designed to simplify the use of Message Queues (MQ) and improve development efficiency. Its core value propositions include:

    • Non-intrusive integration: Uses annotations to handle MQ messages without polluting business logic.
    • Simplified development: Streamlines the development workflow.
    • Unified specification: Allows a single implementation to adapt to multiple MQ providers.
  2. Overview of LOKI MQ enhancement tool

    LOKI is a simplification and enhancement tool for Message Queues (MQ) designed to improve development efficiency. It provides a non-intrusive way to handle MQ messages using annotations, streamlining the development process and allowing developers to adapt multiple MQ middleware types within a single development cycle.
  3. Overview of Loki MQ Integration Library

    Loki is a library designed to simplify the development process and improve efficiency when working with Message Queues (MQ). It provides a unified specification that allows a single implementation to adapt to multiple types of MQ messages and supports rapid integration specifically for Spring Boot applications.
  4. Intercept outgoing messages using PipelineProcess

    To intercept outgoing messages in a Spring-managed instance, implement the PipelineProcess interface and specify the generic type as ProducerRecordModel. The process method is where the interception logic is implemented, receiving a PipelineContext containing the message model.
    @Slf4j
    @Component
    public class SendInterceptor implements PipelineProcess<ProducerRecordModel> {
    
        @Override
        public void process(PipelineContext<ProducerRecordModel> context) {
            log.debug("SendInterceptor context:{}", context);
        }
    }
  5. Implement custom business logic with Loki Interceptors

    Loki interceptors allow developers to implement unified business processing, such as encrypting outgoing messages and decrypting incoming messages. There are two supported types of interceptors: sending interceptors and listening interceptors. Custom filters can be implemented by creating a Spring Scanning Instance that implements the PipelineProcess interface.
  6. Filter intercepted messages with the support method

    To conditionally execute an interceptor based on the message content, override the support method in your PipelineProcess implementation. Return true if the interceptor should process the message, and false otherwise.
    @Slf4j
    @Component
    public class SendInterceptor implements PipelineProcess<ProducerRecordModel> {
    
        @Override
        public boolean support(PipelineContext<ProducerRecordModel> context) {
            // Only intercept messages for the 'loki' topic
            return "loki".equals(context.getModel().getTopic());
        }
        
        @Override
        public void process(PipelineContext<ProducerRecordModel> context) {
            log.debug("SendInterceptor context:{}", context);
        }
    }
  7. Intercept listening messages using PipelineProcess

    To intercept listening messages in a Spring application, create a component that implements the PipelineProcess interface and specify ConsumerRecordModel as the generic type. The process method is where the interception logic is implemented, receiving a PipelineContext containing the record model.
    @Slf4j
    @Component
    public class ListenerInterceptor implements PipelineProcess<ConsumerRecordModel> {
    
        @Override
        public void process(PipelineContext<ConsumerRecordModel> context) {
            log.debug("ListenerInterceptor context:{}", context);
        }
    }
  8. Install LOKI with RocketMQ Remoting support

    Use the loki-spring-boot-starter-rocketmq-remoting artifact for projects requiring support for RocketMQ 4.X and 5.X using the Remoting protocol.
    <dependency>
        <groupId>com.github.guoshiqiufeng</groupId>
        <artifactId>loki-spring-boot-starter-rocketmq-remoting</artifactId>
        <version>1.2.1</version>
    </dependency>
    implementation 'com.github.guoshiqiufeng:loki-spring-boot-starter-rocketmq-remoting:1.2.1'
  9. Implement automatic message decoding with Spring Listeners

    To enable automatic decoding of messages, create a Spring-managed component that implements the Listener<T> interface. The generic type T used in the Listener implementation must be annotated with @MessageName, which contains the subscription information required to route messages to the listener.
    @Slf4j
    @Component
    public class TestListener implements Listener<TestEntity> {
        @Override
        public void onMessage(MessageContent<TestEntity> entity) {
            log.info("entity:{}", entity);
        }
    }
  10. Install LOKI Spring Boot Starter

    Add the loki-spring-boot-starter dependency to your Spring Boot project (supports versions 2.x and 3.x).
    <!-- Maven -->
    <dependency>
        <groupId>com.github.guoshiqiufeng</groupId>
        <artifactId>loki-spring-boot-starter</artifactId>
        <version>1.2.1</version>
    </dependency>
    // Gradle
    implementation 'com.github.guoshiqiufeng:loki-spring-boot-starter:1.2.1'
  11. Install LOKI with RocketMQ gRPC support

    Use the loki-spring-boot-starter-rocketmq artifact for projects requiring support for RocketMQ 5.X via gRPC.
    <dependency>
        <groupId>com.github.guoshiqiufeng</groupId>
        <artifactId>loki-spring-boot-starter-rocketmq</artifactId>
        <version>1.2.1</version>
    </dependency>
    implementation 'com.github.guoshiqiufeng:loki-spring-boot-starter-rocketmq:1.2.1'
  12. Implement custom business logic with Loki Interceptors

    Loki interceptors allow developers to implement unified business processing, such as message encryption during sending or decryption during reception. The system supports two types of interception: sending interception and listening interception. Custom filtering is also supported. To implement an interceptor, create a Spring-scanned bean that implements the PipelineProcess interface.