介绍 Spring Cloud Stream - Spring Cloud Stream中文文档(三)

本文为Spring Cloud Stream文档的中文翻译,原文地址:
https://cloud.spring.io/spring-cloud-static/spring-cloud-stream/3.0.3.RELEASE/reference/html/spring-cloud-stream.html
另外,Spring Cloud 版本:Hoxton.SR3,支持的 Spring Boot 版本:2.2.5.RELEASE

Spring Cloud Stream 是一个构建消息驱动的微服务框架,基于 Spring Boot 来创建独立的生产级的 Spring 应用,并使用 Spring Integration 连接消息代理。为一些供应商的消息中间件产品提供了个性化的自动化配置实现,并引入了发布-订阅、消费组、分区这三个核心概念。

将 spring-cloud-stream 的依赖添加到应用程序中,你可以连接到由spring-cloud-stream-binder暴露的消息代理(稍后会详细介绍),并且你可以通过java.util.function.Function执行传入的消息,实现需求。

这是一个简单的示例:

@SpringBootApplication
public class SampleApplication {

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

    @Bean
	public Function<String, String> uppercase() {
	    return value -> value.toUpperCase();
	}
}

和相应的测试

@SpringBootTest(classes =  SampleApplication.class)
@Import({TestChannelBinderConfiguration.class})
class BootTestStreamApplicationTests {

	@Autowired
	private InputDestination input;

	@Autowired
	private OutputDestination output;

	@Test
	void contextLoads() {
		input.send(new GenericMessage<byte[]>("hello".getBytes()));
		assertThat(output.receive().getPayload()).isEqualTo("HELLO".getBytes());
	}
}

本章示例代码:https://github.com/dudiao/spring-cloud-stream-example

我的最新文章会先发到公众号【读钓的YY】上,欢迎关注!

发布了10 篇原创文章 · 获赞 7 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/youthsong/article/details/105616609
今日推荐