Spring系列学习之Spring Cloud Stream流式消息传递

英文原文:https://cloud.spring.io/spring-cloud-stream/

目录

Spring Cloud Stream

绑定器实现

快速开始

Apache Kafka客户端兼容性

发布版本

示例项目

相关项目


Spring Cloud Stream

Spring Cloud Stream是一个用于构建与共享消息传递系统相连的高度可扩展的事件驱动微服务的框架。

该框架提供了基于已经建立和熟悉的Spring习语和最佳实践的灵活编程模型,包括对持久性Pub/sub(发布/订阅),使用者组和有状态分区的支持。

Spring Cloud Stream的核心构建块是:

  • 目标绑定器:负责提供与外部消息传递系统集成的组件。
  • 目标绑定:外部消息传递系统和应用程序之间的桥接消息的生产者和消费者(由目标绑定器创建)。
  • 消息:生产者和使用者使用的规范数据结构与目标绑定器(以及通过外部消息传递系统的其他应用程序)进行通信。

绑定器实现

Spring Cloud Stream支持各种binder实现,下表包含GitHub项目的链接。

RabbitMQ

Apache Kafka

Amazon Kinesis

Google PubSub (partner maintained)

Solace PubSub+ (partner maintained)

Azure Event Hubs (partner maintained)

快速开始

推荐使用spring-cloud-stream的方法是使用Spring Initializr。 只需按照这些说明操作 您还可以使用依赖关系管理系统在项目中开始使用它 - 下面的代码段可以复制并粘贴到您的构建中。 需要帮忙? 请参阅我们的MavenGradle构建入门指南。(导航到英文原文可以选择版本和依赖方式)

Maven

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-stream-dependencies</artifactId>
            <version>Fishtown.RC4</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-stream</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-stream-rabbit</artifactId> <!-- or '*-stream-kafka' -->
    </dependency>
</dependencies><repositories>
    <repository>
        <id>spring-milestones</id>
        <name>Spring Milestones</name>
        <url>https://repo.spring.io/libs-milestone</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
</repositories>

Gradle

buildscript {
    dependencies {
        classpath "io.spring.gradle:dependency-management-plugin:0.5.2.RELEASE"
    }
}

apply plugin: "io.spring.dependency-management"

dependencyManagement {
     imports {
          mavenBom 'org.springframework.cloud:spring-cloud-stream-dependencies:Fishtown.RC4'
     }
}

dependencies {
    compile 'org.springframework.cloud:spring-cloud-stream'
    compile 'org.springframework.cloud:spring-cloud-starter-stream-kafka'
}repositories {
    maven {
        url 'https://repo.spring.io/libs-milestone'
    }
}

只要Spring Cloud Stream和Spring Cloud Stream Binder依赖项在类路径上,任何带有@EnableBinding的Spring Boot应用程序都会绑定到外部消息传递系统(例如Rabbit MQApache Kafka,具体取决于选择的binder实现)。

有关更全面的逐步入门体验,请参阅用户指南的“快速入门”部分。

以下是功能齐全的Spring Cloud Stream应用程序,它接收数据并将数据记录到控制台。

@SpringBootApplication
@EnableBinding(Sink.class)
public class LoggingConsumerApplication {

	public static void main(String[] args) {
		SpringApplication.run(LoggingConsumerApplication.class, args);
	}

	@StreamListener(Sink.INPUT)
	public void handle(Person person) {
		System.out.println("Received: " + person);
	}

	public static class Person {
		private String name;
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		public String toString() {
			return this.name;
		}
	}
}

从前面的例子可以看出:

  • 我们使用@EnableBinding(Sink.class)启用了Sink绑定(input-no-output)。 这样做是向框架发出信号以启动与消息传递系统的绑定,它自动创建绑定到Sink.INPUT通道的目标(即队列,主题和其他)。
  • 我们添加了一个处理程序方法来接收Person类型的传入消息。 这样做可以让您看到框架的核心功能之一:它尝试自动将传入的消息有效负载转换为Person类型。

您只需运行main(…)方法即可启动应用程序。

Apache Kafka客户端兼容性

Spring Cloud Stream支持各种Apache Kafka Client版本。 要了解有关它的更多信息,请参阅Wiki页面中的Apache Kafka客户端兼容性部分。

发布版本

Spring Cloud Stream

Release

Documentation

Fishtown RC4

Reference API

Fishtown

Reference API

Elmhurst SR2

Reference API

Ditmars SR4

Reference API

Ditmars

Reference API

Chelsea SR2

Reference API

Chelsea

Reference API

Brooklyn SR3

Reference API

示例项目

相关项目

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值