mirror of https://github.com/longtai-cn/hippo4j
Add spring Kafka sample project (#269)
parent
ca0ec6f338
commit
3014bf36f1
@ -0,0 +1,56 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>cn.hippo4j</groupId>
|
||||
<artifactId>hippo4j-example</artifactId>
|
||||
<version>${revision}</version>
|
||||
</parent>
|
||||
<artifactId>hippo4j-spring-boot-starter-adapter-kafka-example</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-json</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>cn.hippo4j</groupId>
|
||||
<artifactId>hippo4j-spring-boot-starter-adapter-kafka</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>cn.hippo4j</groupId>
|
||||
<artifactId>hippo4j-spring-boot-starter</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>cn.hippo4j</groupId>
|
||||
<artifactId>hippo4j-example-core</artifactId>
|
||||
<version>${revision}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.kafka</groupId>
|
||||
<artifactId>spring-kafka</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
@ -0,0 +1,14 @@
|
||||
package cn.hippo4j.springboot.starter.adapter.kafka.example;
|
||||
|
||||
import cn.hippo4j.core.enable.EnableDynamicThreadPool;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableDynamicThreadPool
|
||||
public class Hippo4jAdapterKafkaExampleApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Hippo4jAdapterKafkaExampleApplication.class, args);
|
||||
}
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
package cn.hippo4j.springboot.starter.adapter.kafka.example.consumer;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.kafka.clients.consumer.ConsumerRecord;
|
||||
import org.springframework.kafka.annotation.KafkaListener;
|
||||
import org.springframework.kafka.support.Acknowledgment;
|
||||
import org.springframework.kafka.support.KafkaHeaders;
|
||||
import org.springframework.messaging.handler.annotation.Header;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* Kafka message consumer.
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class KafkaMessageConsumer {
|
||||
|
||||
@KafkaListener(topics = "kafka_message_hippo4j", groupId = "hippo4j")
|
||||
public void onMessage(ConsumerRecord<?, ?> record, Acknowledgment ack, @Header(KafkaHeaders.RECEIVED_TOPIC) String topic) {
|
||||
Optional message = Optional.ofNullable(record.value());
|
||||
message.ifPresent(each -> log.info(each.toString()));
|
||||
ack.acknowledge();
|
||||
}
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
package cn.hippo4j.springboot.starter.adapter.kafka.example.produce;
|
||||
|
||||
import cn.hippo4j.common.toolkit.JSONUtil;
|
||||
import cn.hippo4j.example.core.dto.SendMessageDTO;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.kafka.core.KafkaTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* Kafka message produce.
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
@RestController
|
||||
@AllArgsConstructor
|
||||
public class KafkaMessageProduce {
|
||||
|
||||
private final KafkaTemplate kafkaTemplate;
|
||||
|
||||
private final String TOPIC = "kafka_message_hippo4j";
|
||||
|
||||
@GetMapping("/message/send")
|
||||
public String sendMessage(Integer count) {
|
||||
for (int i = 0; i < count; i++) {
|
||||
String keys = UUID.randomUUID().toString();
|
||||
SendMessageDTO payload = SendMessageDTO.builder()
|
||||
.receiver("156011xxx91")
|
||||
.uid(keys)
|
||||
.build();
|
||||
kafkaTemplate.send(TOPIC, JSONUtil.toJSONString(payload));
|
||||
}
|
||||
return "success";
|
||||
}
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
server.port=8092
|
||||
|
||||
spring.profiles.active=dev
|
||||
spring.dynamic.thread-pool.server-addr=http://localhost:6691
|
||||
spring.dynamic.thread-pool.namespace=prescription
|
||||
spring.dynamic.thread-pool.item-id=dynamic-threadpool-example
|
||||
spring.dynamic.thread-pool.username=admin
|
||||
spring.dynamic.thread-pool.password=123456
|
||||
|
||||
spring.kafka.bootstrap-servers=127.0.0.1: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.acks=1
|
||||
spring.kafka.consumer.auto-offset-reset=latest
|
||||
spring.kafka.consumer.enable-auto-commit=false
|
||||
spring.kafka.consumer.key-deserializer=org.apache.kafka.common.serialization.StringDeserializer
|
||||
spring.kafka.consumer.value-deserializer=org.apache.kafka.common.serialization.StringDeserializer
|
||||
spring.kafka.listener.concurrency=2
|
||||
spring.kafka.listener.ack-mode=manual_immediate
|
||||
spring.kafka.listener.missing-topics-fatal=false
|
Loading…
Reference in new issue