Skip to content

Commit 6e45c1a

Browse files
author
haiyang.luo
committed
新增rocketmq实例以及kafka实例工程
1 parent a54ef64 commit 6e45c1a

File tree

23 files changed

+450
-9
lines changed

23 files changed

+450
-9
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,3 @@ springboot-cli主要用于能够快速搭建基于springboot的项目框架,
1717
- SpringBoot + RocketMQ
1818
- rocketmq-producer
1919
- rocketmq-consumer
20-
c

kafka-consumer/pom.xml

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>2.7.8</version>
9+
<relativePath/> <!-- lookup parent from repository -->
10+
</parent>
11+
<groupId>com.springcli</groupId>
12+
<artifactId>kafka-consumer</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<name>kafka-consumer</name>
15+
<description>kafka-consumer</description>
16+
<properties>
17+
<java.version>1.8</java.version>
18+
</properties>
19+
<dependencies>
20+
<dependency>
21+
<groupId>org.springframework.boot</groupId>
22+
<artifactId>spring-boot-starter-web</artifactId>
23+
</dependency>
24+
<dependency>
25+
<groupId>org.springframework.boot</groupId>
26+
<artifactId>spring-boot-starter</artifactId>
27+
</dependency>
28+
<dependency>
29+
<groupId>org.projectlombok</groupId>
30+
<artifactId>lombok</artifactId>
31+
<optional>true</optional>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.springframework.kafka</groupId>
35+
<artifactId>spring-kafka</artifactId>
36+
</dependency>
37+
38+
<dependency>
39+
<groupId>org.springframework.boot</groupId>
40+
<artifactId>spring-boot-starter-test</artifactId>
41+
<scope>test</scope>
42+
</dependency>
43+
<dependency>
44+
<groupId>org.springframework.kafka</groupId>
45+
<artifactId>spring-kafka-test</artifactId>
46+
<scope>test</scope>
47+
</dependency>
48+
</dependencies>
49+
50+
<build>
51+
<plugins>
52+
<plugin>
53+
<groupId>org.springframework.boot</groupId>
54+
<artifactId>spring-boot-maven-plugin</artifactId>
55+
</plugin>
56+
<plugin>
57+
<groupId>org.springframework.boot</groupId>
58+
<artifactId>spring-boot-maven-plugin</artifactId>
59+
<configuration>
60+
<excludes>
61+
<exclude>
62+
<groupId>org.projectlombok</groupId>
63+
<artifactId>lombok</artifactId>
64+
</exclude>
65+
</excludes>
66+
</configuration>
67+
</plugin>
68+
</plugins>
69+
</build>
70+
71+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.springcli.kafkaconsumer;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class KafkaConsumerApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(KafkaConsumerApplication.class, args);
11+
}
12+
13+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.springcli.kafkaconsumer.service;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.apache.kafka.clients.consumer.ConsumerRecord;
5+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
6+
import org.springframework.kafka.support.Acknowledgment;
7+
import org.springframework.kafka.annotation.KafkaListener;
8+
import org.springframework.stereotype.Service;
9+
10+
import java.util.Optional;
11+
12+
/**
13+
* @Author : haiyang.luo
14+
* @Date : 2023/2/9 18:16
15+
* @Description :
16+
*/
17+
@Service
18+
@Slf4j
19+
@ConditionalOnProperty(value = "spring.profiles.active", havingValue = "kafka")
20+
public class kafkaConsumer {
21+
22+
@KafkaListener(topics = {"kafka_test_topic"}, groupId = "${spring.kafka.consumer.group-id}")
23+
public void onMessage(ConsumerRecord<?, ?> consumerRecord, Acknowledgment ack) {
24+
//消费者必须手动调用ack.acknowledge();不然会重复消费 因为在yml中配置了
25+
//ack-mode: manual_immediate
26+
ack.acknowledge();
27+
Optional<?> optional = Optional.ofNullable(consumerRecord.value());
28+
if (optional.isPresent()) {
29+
Object msg = optional.get();
30+
log.info("消费者接受消息:{}", msg);
31+
}
32+
}
33+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
server.port=8082
2+
3+
spring.kafka.consumer.group-id=springboot-cli-consumer-group
4+
spring.kafka.consumer.enable-auto-commit=false
5+
spring.kafka.consumer.auto-offset-reset=earliest
6+
spring.kafka.consumer.key-deserializer=org.apache.kafka.common.serialization.StringDeserializer
7+
spring.kafka.consumer.value-deserializer=org.apache.kafka.common.serialization.StringDeserializer
8+
spring.kafka.listener.ack-mode=manual_immediate
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.springcli.kafkaconsumer;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.springframework.boot.test.context.SpringBootTest;
5+
6+
@SpringBootTest
7+
class KafkaConsumerApplicationTests {
8+
9+
@Test
10+
void contextLoads() {
11+
}
12+
13+
}

kafka-producer/pom.xml

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>2.7.8</version>
9+
<relativePath/> <!-- lookup parent from repository -->
10+
</parent>
11+
<groupId>com.springcli</groupId>
12+
<artifactId>kafka-producer</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<name>kafka-producer</name>
15+
<description>kafka-producer</description>
16+
<properties>
17+
<java.version>1.8</java.version>
18+
</properties>
19+
<dependencies>
20+
21+
<dependency>
22+
<groupId>org.springframework.kafka</groupId>
23+
<artifactId>spring-kafka</artifactId>
24+
</dependency>
25+
26+
<dependency>
27+
<groupId>org.springframework.boot</groupId>
28+
<artifactId>spring-boot-starter</artifactId>
29+
</dependency>
30+
31+
<dependency>
32+
<groupId>org.projectlombok</groupId>
33+
<artifactId>lombok</artifactId>
34+
<optional>true</optional>
35+
</dependency>
36+
37+
<dependency>
38+
<groupId>org.springframework.boot</groupId>
39+
<artifactId>spring-boot-starter-web</artifactId>
40+
</dependency>
41+
42+
<dependency>
43+
<groupId>org.springframework.boot</groupId>
44+
<artifactId>spring-boot-starter-test</artifactId>
45+
<scope>test</scope>
46+
</dependency>
47+
48+
<dependency>
49+
<groupId>org.springframework.kafka</groupId>
50+
<artifactId>spring-kafka-test</artifactId>
51+
<scope>test</scope>
52+
</dependency>
53+
</dependencies>
54+
55+
<build>
56+
<plugins>
57+
<plugin>
58+
<groupId>org.springframework.boot</groupId>
59+
<artifactId>spring-boot-maven-plugin</artifactId>
60+
</plugin>
61+
<plugin>
62+
<groupId>org.springframework.boot</groupId>
63+
<artifactId>spring-boot-maven-plugin</artifactId>
64+
<configuration>
65+
<excludes>
66+
<exclude>
67+
<groupId>org.projectlombok</groupId>
68+
<artifactId>lombok</artifactId>
69+
</exclude>
70+
</excludes>
71+
</configuration>
72+
</plugin>
73+
</plugins>
74+
</build>
75+
76+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.springcli.kafkaproducer;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class KafkaProducerApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(KafkaProducerApplication.class, args);
11+
}
12+
13+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.springcli.kafkaproducer.controller;
2+
3+
import com.springcli.kafkaproducer.model.MessageSendRequest;
4+
import com.springcli.kafkaproducer.service.ProducerService;
5+
import org.springframework.boot.autoconfigure.data.ConditionalOnRepositoryType;
6+
import org.springframework.stereotype.Component;
7+
import org.springframework.stereotype.Controller;
8+
import org.springframework.web.bind.annotation.PostMapping;
9+
import org.springframework.web.bind.annotation.RequestBody;
10+
11+
import javax.annotation.Resource;
12+
13+
/**
14+
* @Author : haiyang.luo
15+
* @Date : 2023/2/9 17:54
16+
* @Description :
17+
*/
18+
@Controller
19+
public class ProducerController {
20+
21+
@Resource
22+
private ProducerService producerService;
23+
24+
@PostMapping("sendMsg")
25+
public String sendMsg(@RequestBody MessageSendRequest messageSendRequest) {
26+
producerService.sendMsg(messageSendRequest.getTopic(), messageSendRequest.getMessage());
27+
return "ok";
28+
}
29+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.springcli.kafkaproducer.model;
2+
3+
import lombok.Data;
4+
5+
/**
6+
* @Author : haiyang.luo
7+
* @Date : 2023/2/9 18:02
8+
* @Description :
9+
*/
10+
@Data
11+
public class MessageSendRequest {
12+
/**
13+
* token
14+
*/
15+
private String token;
16+
17+
/**
18+
* 消息
19+
*/
20+
private String message;
21+
22+
/**
23+
* topic
24+
*/
25+
private String topic;
26+
}

0 commit comments

Comments
 (0)