SpringBoot整合SpringCloudStream3.1+版本Kafka

SpringBoot整合SpringCloudStream3.1+版本Kafka

下一节直通车

SpringBoot整合SpringCloudStream3.1+版本的Kafka死信队列

为什么用SpringCloudStream3.1

  1. Springcloud架构提供,基于spring生态
  2. 能够快速切换市面上常见的MQ产品
  3. 3.1后使用配置文件的形式定义channel,不再需要3.1前的硬编码

Jar

xml 复制代码
<!--SpringCloudStream Kafka-->
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-stream-kafka</artifactId>
	<version>3.2.4</version>
</dependency>
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-stream</artifactId>
	<version>3.2.4</version>
</dependency>
<!--Kafka相关-->
<dependency>
	<groupId>org.springframework.kafka</groupId>
	<artifactId>spring-kafka</artifactId>
	<version>2.8.6</version>
</dependency>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
	<version>2.6.8</version>
</dependency>
<dependency>
	<groupId>org.projectlombok</groupId>
	<artifactId>lombok</artifactId>
	<version>1.18.24</version>
</dependency>

配置文件yml

kafka基本配置(application-mq.yml)

yml 复制代码
server:
  port: 7105
spring:
  application:
	name: betrice-message-queue
  config:
	import:
	- classpath:application-bindings.yml
  cloud:
	stream:
	  kafka:
		binder:
		  brokers: localhost:9092
		  configuration:
			key-serializer: org.apache.kafka.common.serialization.StringSerializer
			value-serializer: org.apache.kafka.common.serialization.StringSerializer
		  consumer-properties:
			enable.auto.commit: false
	  binders:
		betrice-kafka:
		  type: kafka
		  environment:
			spring.kafka:
			  bootstrap-servers: ${spring.cloud.stream.kafka.binder.brokers}

classpath:application-bindings.yml 引入通道绑定配置文件,即消息生产、消费者的关系。

通道绑定配置(application-bindings.yml)

参数含义:
spring.cloud.stream.function.definition:定义channel名字,每个channel又可以作为生产者(in)与消费者(out)
spring.cloud.stream.bindings: 是一个map形式(具体看源码的Properties定义)

  1. destination(生产/消费者通向的topic);

  2. group:消费者组名;

  3. binder:绑定当前使用的MQ类型(见betrice-kafka);

  4. content-type:消息序列/反序列化的类型(见源码的支持的类型)

yml 复制代码
spring:
  cloud:
	stream:
	  betrice-default-binder: betrice-kafka
	  function:
		# 声明两个channel,transfer接收生产者的消息,处理完后给sink
		definition: transfer;sink;gather;gatherEcho
	  bindings:
		# 添加生产者binding,输出到destination对应的topic
		Evad05:
		  destination: Evad07
		  binder: ${spring.cloud.stream.betrice-default-binder}
		transfer-in-0:
		  destination: Evad07
		  binder: ${spring.cloud.stream.betrice-default-binder}
		transfer-out-0:
		  destination: Evad08
		  binder: ${spring.cloud.stream.betrice-default-binder}
		  content-type: text/plain
		sink-in-0:
		  destination: Evad08
		  binder: ${spring.cloud.stream.betrice-default-binder}
		  content-type: text/plain

Controller

java 复制代码
/**
 * @author Evad.Wu
 * @Description 消息队列 控制类
 * @date 2023-02-10
 */
@Slf4j
@RestController
@RequestMapping(value = "betriceMqController")
public class BetriceMqController {
	@Resource(name = "betriceKafkaProducer")
	private Producer<String, String> producer;
	@Resource(name = "streamBridgeUtils")
	private StreamBridge streamBridge;

	@PostMapping("send")
	public void send(String topic, String key, String message) {
		try {
			producer.send(new ProducerRecord<>(topic, key, message));
			log.info("发送消息:" + message);
		} catch (Exception e) {
			log.error("异常消息:" + e);
		}
	}

	@PostMapping("streamSend")
	public void streamSend(String topic, String message) {
		try {
			streamBridge.send(topic, message);
			log.info("发送消息:" + message);
		} catch (Exception e) {
			log.error("异常消息:" + e);
		}
	}
}

Channel(生产、消费者通道)

java 复制代码
/**
 * @author Evad.Wu
 * @Description mq消息通道 配置类
 * @date 2023-02-11
 */
@Configuration
public class BetriceMqSubChannel {
	/**
	 * Function方式声明binding,相当于同时声明了一个Producer的Bindng和一个Consumer的Binding。
	 * transfer-in-0 表示消费者  transfer-out-0 表示生产者
	 * 其作用就在于,echo-in-0收到的消息,立即就会通过echo-out-0发送出去。
	 */
	@Bean
	public Function<String, String> transfer() {
		return message -> {
			System.out.println("transfer: " + message);
			throw new RuntimeException("死信队列测试!");
//            return "transfer:" + message;
		};
	}

	/**
	 * Consumer声明一个消息消费者,sink1就对应sink-in-0
	 */
	@Bean
	public Consumer<String> sink() {
		return message -> {
			System.out.println("******************");
			System.out.println("At Sink1");
			System.out.println("******************");
			System.out.println("Received message " + message);
		};
	}
}

结果

参考网址

SpringCloudStream实战拆解以及3.1后新版本特性分析
@EnableBinding @deprecated 自 3.1 起支持函数式编程模型

相关推荐
K***728420 分钟前
springBoot 和springCloud 版本对应关系
spring boot·后端·spring cloud
z***948420 分钟前
解决SpringBoot项目启动错误:找不到或无法加载主类
java·spring boot·后端
sheji341627 分钟前
【开题答辩全过程】以 基于Spring Boot的驾校预约练车系统的设计与实现为例,包含答辩的问题和答案
java·spring boot·后端
p***q7827 分钟前
springboot整合libreoffice(两种方式,使用本地和远程的libreoffice);docker中同时部署应用和libreoffice
spring boot·后端·docker
w***H6501 小时前
SpringBoot项目如何导入外部jar包:详细指南
spring boot·后端·jar
i***13241 小时前
Spring Boot 整合 log4j2 日志配置教程
spring boot·单元测试·log4j
v***5651 小时前
【wiki知识库】07.用户管理后端SpringBoot部分
spring boot·后端·状态模式
3***C7441 小时前
Spring Boot 集成 MyBatis 全面讲解
spring boot·后端·mybatis
r***R2891 小时前
Spring Boot3.3.X整合Mybatis-Plus
spring boot·后端·mybatis
f***01931 小时前
SpringBoot中整合RabbitMQ(测试+部署上线 最完整)
spring boot·rabbitmq·java-rabbitmq