Explorar o código

增加Kafka逻辑

hmy hai 1 mes
pai
achega
58b097887f

+ 98 - 94
xdz-module-vehicle/xdz-module-vehicle-server/pom.xml

@@ -1,97 +1,101 @@
 <?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
-    <parent>
-        <groupId>com.xindazhou</groupId>
-        <artifactId>xdz-module-vehicle</artifactId>
-        <version>${revision}</version>
-    </parent>
-    <modelVersion>4.0.0</modelVersion>
-    <artifactId>xdz-module-vehicle-server</artifactId>
-    <packaging>jar</packaging>
-    <name>${project.artifactId}</name>
-    <description>vehicle 服务实现</description>
-    <dependencies>
-        <dependency>
-            <groupId>com.xindazhou</groupId>
-            <artifactId>xdz-spring-boot-starter-env</artifactId>
-        </dependency>
-        <dependency>
-            <groupId>com.xindazhou</groupId>
-            <artifactId>xdz-module-vehicle-api</artifactId>
-            <version>${revision}</version>
-        </dependency>
-        <dependency>
-            <groupId>com.xindazhou</groupId>
-            <artifactId>xdz-spring-boot-starter-web</artifactId>
-        </dependency>
-        <dependency>
-            <groupId>com.xindazhou</groupId>
-            <artifactId>xdz-spring-boot-starter-security</artifactId>
-        </dependency>
-        <dependency>
-            <groupId>com.xindazhou</groupId>
-            <artifactId>xdz-spring-boot-starter-mybatis</artifactId>
-        </dependency>
-        <dependency>
-            <groupId>com.xindazhou</groupId>
-            <artifactId>xdz-spring-boot-starter-redis</artifactId>
-        </dependency>
-        <dependency>
-            <groupId>com.xindazhou</groupId>
-            <artifactId>xdz-spring-boot-starter-rpc</artifactId>
-        </dependency>
-        <dependency>
-            <groupId>com.alibaba.cloud</groupId>
-            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
-        </dependency>
-        <dependency>
-            <groupId>com.alibaba.cloud</groupId>
-            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
-        </dependency>
-        <dependency>
-            <groupId>com.xindazhou</groupId>
-            <artifactId>xdz-spring-boot-starter-mq</artifactId>
-        </dependency>
-        <!-- 用户服务 API -->
-        <dependency>
-            <groupId>com.xindazhou</groupId>
-            <artifactId>xdz-module-user-api</artifactId>
-            <version>${revision}</version>
-        </dependency>
-        <!-- 消息服务 API -->
-        <dependency>
-            <groupId>com.xindazhou</groupId>
-            <artifactId>xdz-module-message-api</artifactId>
-            <version>${revision}</version>
-        </dependency>
-        <!-- 业务服务 API(用于权限校验) -->
-        <dependency>
-            <groupId>com.xindazhou</groupId>
-            <artifactId>xdz-module-business-api</artifactId>
-            <version>${revision}</version>
-        </dependency>
-        <dependency>
-            <groupId>com.xindazhou</groupId>
-            <artifactId>xdz-spring-boot-starter-monitor</artifactId>
-        </dependency>
-    </dependencies>
-    <build>
-        <finalName>${project.artifactId}</finalName>
-        <plugins>
-            <plugin>
-                <groupId>org.springframework.boot</groupId>
-                <artifactId>spring-boot-maven-plugin</artifactId>
-                <version>${spring.boot.version}</version>
-                <executions>
-                    <execution>
-                        <goals>
-                            <goal>repackage</goal>
-                        </goals>
-                    </execution>
-                </executions>
-            </plugin>
-        </plugins>
-    </build>
+	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+	<parent>
+		<groupId>com.xindazhou</groupId>
+		<artifactId>xdz-module-vehicle</artifactId>
+		<version>${revision}</version>
+	</parent>
+	<modelVersion>4.0.0</modelVersion>
+	<artifactId>xdz-module-vehicle-server</artifactId>
+	<packaging>jar</packaging>
+	<name>${project.artifactId}</name>
+	<description>vehicle 服务实现</description>
+	<dependencies>
+		<dependency>
+			<groupId>com.xindazhou</groupId>
+			<artifactId>xdz-spring-boot-starter-env</artifactId>
+		</dependency>
+		<dependency>
+			<groupId>com.xindazhou</groupId>
+			<artifactId>xdz-module-vehicle-api</artifactId>
+			<version>${revision}</version>
+		</dependency>
+		<dependency>
+			<groupId>com.xindazhou</groupId>
+			<artifactId>xdz-spring-boot-starter-web</artifactId>
+		</dependency>
+		<dependency>
+			<groupId>com.xindazhou</groupId>
+			<artifactId>xdz-spring-boot-starter-security</artifactId>
+		</dependency>
+		<dependency>
+			<groupId>com.xindazhou</groupId>
+			<artifactId>xdz-spring-boot-starter-mybatis</artifactId>
+		</dependency>
+		<dependency>
+			<groupId>com.xindazhou</groupId>
+			<artifactId>xdz-spring-boot-starter-redis</artifactId>
+		</dependency>
+		<dependency>
+			<groupId>com.xindazhou</groupId>
+			<artifactId>xdz-spring-boot-starter-rpc</artifactId>
+		</dependency>
+		<dependency>
+			<groupId>com.alibaba.cloud</groupId>
+			<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
+		</dependency>
+		<dependency>
+			<groupId>com.alibaba.cloud</groupId>
+			<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
+		</dependency>
+		<dependency>
+			<groupId>com.xindazhou</groupId>
+			<artifactId>xdz-spring-boot-starter-mq</artifactId>
+		</dependency>
+		<!-- 用户服务 API -->
+		<dependency>
+			<groupId>com.xindazhou</groupId>
+			<artifactId>xdz-module-user-api</artifactId>
+			<version>${revision}</version>
+		</dependency>
+		<!-- 消息服务 API -->
+		<dependency>
+			<groupId>com.xindazhou</groupId>
+			<artifactId>xdz-module-message-api</artifactId>
+			<version>${revision}</version>
+		</dependency>
+		<!-- 业务服务 API(用于权限校验) -->
+		<dependency>
+			<groupId>com.xindazhou</groupId>
+			<artifactId>xdz-module-business-api</artifactId>
+			<version>${revision}</version>
+		</dependency>
+		<dependency>
+			<groupId>com.xindazhou</groupId>
+			<artifactId>xdz-spring-boot-starter-monitor</artifactId>
+		</dependency>
+		<dependency>
+			<groupId>org.springframework.kafka</groupId>
+			<artifactId>spring-kafka</artifactId>
+		</dependency>
+	</dependencies>
+	<build>
+		<finalName>${project.artifactId}</finalName>
+		<plugins>
+			<plugin>
+				<groupId>org.springframework.boot</groupId>
+				<artifactId>spring-boot-maven-plugin</artifactId>
+				<version>${spring.boot.version}</version>
+				<executions>
+					<execution>
+						<goals>
+							<goal>repackage</goal>
+						</goals>
+					</execution>
+				</executions>
+			</plugin>
+		</plugins>
+	</build>
 </project>

+ 3 - 4
xdz-module-vehicle/xdz-module-vehicle-server/src/main/java/com/xindazhou/vehicle/VehicleServerApplication.java

@@ -11,9 +11,8 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
 @SpringBootApplication
 public class VehicleServerApplication {
 
-    public static void main(String[] args) {
-        SpringApplication.run(VehicleServerApplication.class, args);
-    }
+	public static void main(String[] args) {
+		SpringApplication.run(VehicleServerApplication.class, args);
+	}
 
 }
-

+ 49 - 0
xdz-module-vehicle/xdz-module-vehicle-server/src/main/java/com/xindazhou/vehicle/controller/admin/vehicle/KafkaDemoController.java

@@ -0,0 +1,49 @@
+package com.xindazhou.vehicle.controller.admin.vehicle;
+
+import com.xindazhou.framework.common.pojo.CommonResult;
+import io.swagger.v3.oas.annotations.Operation;
+import io.swagger.v3.oas.annotations.tags.Tag;
+import jakarta.annotation.Resource;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.kafka.annotation.KafkaListener;
+import org.springframework.kafka.core.KafkaTemplate;
+import org.springframework.security.access.prepost.PreAuthorize;
+import org.springframework.validation.annotation.Validated;
+import org.springframework.web.bind.annotation.*;
+
+import static com.xindazhou.framework.common.pojo.CommonResult.success;
+
+/**
+ * Kafka Demo Controller - 演示发送和接收消息(参数写死,消息输出到控制台)
+ *
+ * @author xindazhou
+ */
+@Tag(name = "管理后台 - Kafka Demo")
+@RestController
+@RequestMapping("/admin/vehicle/kafka-demo")
+@Validated
+public class KafkaDemoController {
+
+    private static final Logger log = LoggerFactory.getLogger(KafkaDemoController.class);
+
+    private static final String TOPIC = "vehicle-demo-topic";
+    private static final String MESSAGE = "Hello Kafka Demo";
+
+    @Resource
+    private KafkaTemplate<String, String> kafkaTemplate;
+
+    @PostMapping("/send")
+    @Operation(summary = "发送 Kafka 消息")
+    @PreAuthorize("permitAll()")
+    public CommonResult<String> sendMessage() {
+        kafkaTemplate.send(TOPIC, MESSAGE);
+        log.info("[Kafka Demo] 发送消息: topic={}, message={}", TOPIC, MESSAGE);
+        return success("消息已发送");
+    }
+
+    @KafkaListener(topics = TOPIC, groupId = "vehicle-demo-consumer")
+    public void listen(String message) {
+        log.info("[Kafka Demo] 接收消息: {}", message);
+    }
+}

+ 14 - 1
xdz-module-vehicle/xdz-module-vehicle-server/src/main/resources/application-my.yaml

@@ -61,7 +61,20 @@ spring:
           max-idle: 8
           max-active: 8
           max-wait: -1ms
-
+  kafka:
+    bootstrap-servers: 119.45.95.228:10042
+    properties:
+      security.protocol: SASL_PLAINTEXT
+      sasl.mechanism: PLAIN
+      sasl.jaas.config: 'org.apache.kafka.common.security.plain.PlainLoginModule required username="admin" password="xdz@123";'
+    producer:
+      key-serializer: org.apache.kafka.common.serialization.StringSerializer
+      value-serializer: org.apache.kafka.common.serialization.StringSerializer
+      acks: all
+    consumer:
+      key-deserializer: org.apache.kafka.common.serialization.StringDeserializer
+      value-deserializer: org.apache.kafka.common.serialization.StringDeserializer
+      auto-offset-reset: earliest
 --- #################### SpringDoc/Swagger 配置 ####################
 
 # 禁用 SpringDoc/Swagger(vehicle-server 不需要 API 文档)