栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

kafka使用(二)--- springboot整合

kafka使用(二)--- springboot整合

pom.xml


	org.springframework.kafka
	spring-kafka
	2.1.5.RELEASE

 application.properties

# kafka相关配置
spring.kafka.bootstrap-servers=192.168.137.100:9092
spring.kafka.producer.retries=0
spring.kafka.producer.acks=1
spring.kafka.producer.batch-size=16384
spring.kafka.producer.properties.linger.ms=0
spring.kafka.producer.buffer-memory = 33554432
spring.kafka.producer.key-serializer=org.apache.kafka.common.serialization.StringSerializer
spring.kafka.producer.value-serializer=com.hurricane.kafka.utils.EncodeingKafka
spring.kafka.consumer.enable-auto-commit=true
spring.kafka.consumer.auto.commit.interval.ms=1000
spring.kafka.consumer.auto-offset-reset=earliest #latest
spring.kafka.consumer.properties.session.timeout.ms=120000
spring.kafka.consumer.properties.request.timeout.ms=180000
spring.kafka.consumer.key-deserializer=org.apache.kafka.common.serialization.StringDeserializer
spring.kafka.consumer.value-deserializer=com.hurricane.kafka.utils.DecodeingKafka
spring.kafka.listener.missing-topics-fatal=false

消费者:

package com.hurricane.kafka.consumer;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

@Component
public class KafkaConsumer {
    @KafkaListener(topics = {"testtopic"},groupId = "defaultConsumerGroup")
    public void onMessage1(ConsumerRecord record){
        System.out.println("简单消费:"+record.topic()+"-"+record.partition()+"-"+record.value());
    }
	
}

生产者:

package com.hurricane.kafka.producer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.SendResult;
import org.springframework.util.concurrent.ListenableFutureCallback;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class KafkaProducer {
	
	@Autowired
    private KafkaTemplate kafkaTemplate;
	
    // 发送消息
    @GetMapping("/kafka/callback/{message}")
    public void sendMessage3(@PathVariable("message") String callbackMessage) {
    	System.out.println(callbackMessage);
    	kafkaTemplate.send("testtopic", callbackMessage).addCallback(new ListenableFutureCallback>() {
            public void onFailure(Throwable ex) {
                System.out.println("发送消息失败:"+ex.getMessage());
            }
            public void onSuccess(SendResult result) {
                System.out.println("发送消息成功:" + result.getRecordmetadata().topic() + "-" + result.getRecordmetadata().partition() + "-" + result.getRecordmetadata().offset());
            }
        });
    }
}

为了能够将json类型作为消息成功发送,创建消息编解码类:

编码类:

package com.hurricane.kafka.utils;

import java.util.Map;
import org.apache.kafka.common.serialization.Serializer;
public class EncodeingKafka implements Serializer {
	@Override
	public void configure(Map configs, boolean isKey) {}
	@Override
	public byte[] serialize(String topic, Object data) {
		return BeanUtils.bean2Byte(data);
	}
	@Override
	public void close() {
		System.out.println("EncodeingKafka is close");
	}
}
 

解码类: 

package com.hurricane.kafka.utils;

import java.util.Map;
import org.apache.kafka.common.serialization.Deserializer;
 
public class DecodeingKafka implements Deserializer {
 
	@Override
	public void configure(Map configs, boolean isKey) {}
 
	@Override
	public Object deserialize(String topic, byte[] data) {
		return BeanUtils.byte2Obj(data);
	}
 
	@Override
	public void close() {}
} 

工具类: 

package com.hurricane.kafka.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
 
public class BeanUtils {
	private BeanUtils() {}
	
	public static byte[] bean2Byte(Object obj) {
		byte[] bb = null;
		try (ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
			 ObjectOutputStream outputStream = new ObjectOutputStream(byteArray)){
			outputStream.writeObject(obj);
			outputStream.flush();
			bb = byteArray.toByteArray();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return bb;
	}
	
	public static Object byte2Obj(byte[] bytes) {
		Object readObject = null;
		try (ByteArrayInputStream in = new ByteArrayInputStream(bytes);
			 ObjectInputStream inputStream = new ObjectInputStream(in)){
			 readObject = inputStream.readObject();
		} catch (Exception e) {
			e.printStackTrace();
		} 
		return readObject;
	}
}

转载请注明:文章转载自 www.mshxw.com
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号