SPRING BOOT + achieve KAFKA message sending and receiving information

Transfer:   https://blog.csdn.net/lin000_0/article/details/91884684

 

A note similar to the rabbitmq support functions, through access to information, kafka communication mechanism must know that I do not write, you can directly see the figure below to understand the kafka profile:

In particular surgeon in the project as follows:

1. MAVEN increase in transmission and reception of the following dependencies PROJECT

2. The following configuration information received in the system configuration file PROJECT, mainly in order to connect consumer group and topic kafka server

3. increase in the reception prodject following components, primarily for receiving information, wherein KafkaListener to listen for the topic information zll

import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

@Component
public class ListenerMsg {
    
    @KafkaListener(topics = "zll")
    public void processMessage(String content) {
        // ...
        System.out.println("recieve msg:" + content);
    }

  }

4. Add the following in the project's transmission profile springboot

5. Add the following in the transmission component in the project

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.SendResult;
import org.springframework.stereotype.Component;
import org.springframework.util.concurrent.ListenableFuture;


@Component
public class SendMsg {
    
    private Log log = LogFactory.getLog(SendMsg.class);

    private final KafkaTemplate kafkaTemplate;
    
    @Value("${spring.kafka.topic}")
    private String topic;

    @Autowired
    public SendMsg(KafkaTemplate kafkaTemplate) {
        this.kafkaTemplate = kafkaTemplate;
        
        
        for(int i=0;i<100;i++) {
            this.SendMessge("hello "+i);
        }
    }
    
    public void SendMessge(String msg) {
        
        
        log.info("send send msg :"+msg);
        
        this.kafkaTemplate.send("zll", msg);
         
    }

}

7. The first project will be able to see both sides turned to receive a log in the development of project delivery

The sender logs:

The recipient logs:


--------------------- 
Author: lin000_0 
Source: CSDN 
Original: https: //blog.csdn.net/lin000_0/article/details/91884684 
Disclaimer: This article as a blogger original article, reproduced, please attach Bowen link!

Guess you like

Origin blog.csdn.net/qq_36688928/article/details/93337798