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 起支持函数式编程模型

相关推荐
RuoyiOffice9 小时前
SpringBoot+Vue3 凭证生命周期设计:审核、过账、冲销与反过账怎样保持账目一致
java·spring boot·事务·springboot3·财务系统·会计凭证·ruoyi office
RuoyiOffice10 小时前
KubeSphere 容器云实践:从管理 K8s 到建设企业 PaaS,还需要哪些能力
spring boot·kubernetes·kubesphere·jenkins·paas·微服务部署·ruoyi office
我不会起名字32214 小时前
消费回调固定 return nil:错误传播与 Kafka 位点提交的区别
后端·mongodb·kafka·go·im·故障排查
软件技术NINI16 小时前
基于SpringBoot和Vue的新能源汽车销售推荐系统
java·vue.js·spring boot·后端·汽车·毕业设计·毕设
Flynn Liang17 小时前
LangChain4j 生产级工程骨架:12 个模块 + Docker 一键启动
java·spring boot·langchain·rag·langchain4j
RuoyiOffice18 小时前
固定资产维修管理:从报修、审批到办结,原资产卡怎么回查?
spring boot·固定资产·ruoyi office·资产维修·维修审批·维修办结·资产履历
招风的黑耳18 小时前
SpringBoot+Vue+MySQL 民宿在线预定平台 完整可运行版(源码+文档)
vue.js·spring boot·mysql
Java码农也是农19 小时前
Spring 事务详解:@Transactional 如何生效、何时回滚、传播行为怎么选
java·spring boot·spring·事务
学长毕设20 小时前
基于SpringBoot的游乐园管理系统-附源码
java·spring boot·后端
杨凯凡20 小时前
Spring Boot 文件上传怎么做才安全?流式落盘、路径校验与数据库一致性实战
java·spring boot·文件上传·后端安全·数据库一致性