Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit6e45c1a

Browse files
author
haiyang.luo
committed
新增rocketmq实例以及kafka实例工程
1 parenta54ef64 commit6e45c1a

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+
<projectxmlns="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+
packagecom.springcli.kafkaconsumer;
2+
3+
importorg.springframework.boot.SpringApplication;
4+
importorg.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
publicclassKafkaConsumerApplication {
8+
9+
publicstaticvoidmain(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+
packagecom.springcli.kafkaconsumer.service;
2+
3+
importlombok.extern.slf4j.Slf4j;
4+
importorg.apache.kafka.clients.consumer.ConsumerRecord;
5+
importorg.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
6+
importorg.springframework.kafka.support.Acknowledgment;
7+
importorg.springframework.kafka.annotation.KafkaListener;
8+
importorg.springframework.stereotype.Service;
9+
10+
importjava.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+
publicclasskafkaConsumer {
21+
22+
@KafkaListener(topics = {"kafka_test_topic"},groupId ="${spring.kafka.consumer.group-id}")
23+
publicvoidonMessage(ConsumerRecord<?, ?>consumerRecord,Acknowledgmentack) {
24+
//消费者必须手动调用ack.acknowledge();不然会重复消费 因为在yml中配置了
25+
//ack-mode: manual_immediate
26+
ack.acknowledge();
27+
Optional<?>optional =Optional.ofNullable(consumerRecord.value());
28+
if (optional.isPresent()) {
29+
Objectmsg =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+
packagecom.springcli.kafkaconsumer;
2+
3+
importorg.junit.jupiter.api.Test;
4+
importorg.springframework.boot.test.context.SpringBootTest;
5+
6+
@SpringBootTest
7+
classKafkaConsumerApplicationTests {
8+
9+
@Test
10+
voidcontextLoads() {
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+
<projectxmlns="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+
packagecom.springcli.kafkaproducer;
2+
3+
importorg.springframework.boot.SpringApplication;
4+
importorg.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
publicclassKafkaProducerApplication {
8+
9+
publicstaticvoidmain(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+
packagecom.springcli.kafkaproducer.controller;
2+
3+
importcom.springcli.kafkaproducer.model.MessageSendRequest;
4+
importcom.springcli.kafkaproducer.service.ProducerService;
5+
importorg.springframework.boot.autoconfigure.data.ConditionalOnRepositoryType;
6+
importorg.springframework.stereotype.Component;
7+
importorg.springframework.stereotype.Controller;
8+
importorg.springframework.web.bind.annotation.PostMapping;
9+
importorg.springframework.web.bind.annotation.RequestBody;
10+
11+
importjavax.annotation.Resource;
12+
13+
/**
14+
* @Author : haiyang.luo
15+
* @Date : 2023/2/9 17:54
16+
* @Description :
17+
*/
18+
@Controller
19+
publicclassProducerController {
20+
21+
@Resource
22+
privateProducerServiceproducerService;
23+
24+
@PostMapping("sendMsg")
25+
publicStringsendMsg(@RequestBodyMessageSendRequestmessageSendRequest) {
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+
packagecom.springcli.kafkaproducer.model;
2+
3+
importlombok.Data;
4+
5+
/**
6+
* @Author : haiyang.luo
7+
* @Date : 2023/2/9 18:02
8+
* @Description :
9+
*/
10+
@Data
11+
publicclassMessageSendRequest {
12+
/**
13+
* token
14+
*/
15+
privateStringtoken;
16+
17+
/**
18+
* 消息
19+
*/
20+
privateStringmessage;
21+
22+
/**
23+
* topic
24+
*/
25+
privateStringtopic;
26+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp