pom文件
2、基本配置
#kafka config
spring.kafka.bootstrap-servers = 10.154.7.181:9092
spring.kafka.producer.retries=0
spring.kafka.producer.batch-size = 16384
spring.kafka.producer.buffer-memory = 33554432
spring.kafka.producer.key-serializer = org.apache.kafka.common.serialization.StringSerializer
spring.kafka.producer.value-serializer= org.apache.kafka.common.serialization.StringSerializer
spring.kafka.producer.properties.linger.ms = 1
spring.kafka.consumer.enable-auto-commit= false
spring.kafka.consumer.auto-commit-interval = 100ms
spring.kafka.consumer.key-deserializer = org.apache.kafka.common.serialization.StringDeserializer
spring.kafka.consumer.value-deserializer = org.apache.kafka.common.serialization.StringDeserializer
spring.kafka.consumer.properties.session.timeout.ms = 15000
kafka.topic.group-id = topicGroupId
kafka.topic.topic-name = wftest
3、实体类
package com.example.demo.model;
import java.util.Date;
public class Messages {
private Long id;
private String msg;
private Date sendTime;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Date getSendTime() {
return sendTime;
}
public void setSendTime(Date sendTime) {
this.sendTime = sendTime;
}
}
4、生产者端
package com.chinamobile.cmss.eshub.kafka;
import java.util.Date;
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.Service;
import org.springframework.util.concurrent.ListenableFuture;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
@Service
public class KafkaSender {
@Autowired
private KafkaTemplate
@Value("${kafka.topic.topic-name}")
private String topic;
private Gson gson = new GsonBuilder().create();
public void send() {
Messages message = new Messages();
message.setId(System.currentTimeMillis());
message.setMsg("222");
message.setSendTime(new Date());
ListenableFuture
}
}
5消费者
package com.chinamobile.cmss.eshub.kafka;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Map;
import java.util.Optional;
import javax.annotation.PostConstruct;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Service;
@Service
public class KafkaReceiver {
@Value("${kafka.topic.topic-name}")
private String topic;
@KafkaListener
public void listen(ConsumerRecord, ?> record) {
Optional> kafkaMessage = Optional.ofNullable(record.value());
if (kafkaMessage.isPresent()) {
Object message = kafkaMessage.get();
System.out.println("record =" + record);
System.out.println("message =" + message);
}
}
@PostConstruct
public void initKafkaHandler() throws NoSuchMethodException, SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
String topics =topic;
String[] topicArray = topics.split(",");
//反射,listen是方法名,ConsumerRecord.class是参数的类,找到这个监听方法修改topics的值
Method listen = KafkaReceiver.class.getDeclaredMethod("listen", ConsumerRecord.class);
KafkaListener kafkaListener = listen.getAnnotation(KafkaListener.class);
InvocationHandler invocationHandler = Proxy.getInvocationHandler(kafkaListener);
Field hField = invocationHandler.getClass().getDeclaredField("memberValues");
//默认的访问权限是不行的,要修改成true才能修改属性的值
hField.setAccessible(true);
Map memberValues = (Map) hField.get(invocationHandler);
memberValues.put("topics", topicArray);
}
}
6测试
package com.example.demo;
import com.example.demo.service.KafkaSender;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class KafkademoApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(KafkademoApplication.class, args);
KafkaSender sender = context.getBean(KafkaSender.class);
for (int i = 0; i <1000; i++) {
sender.send();
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}