整合Spring Cloud Stream Binder与GCP Pubsub进行消息发送与接收
我最新最全的文章都在南瓜慢说 www.pkslow.com,欢迎大家来喝茶!
1 前言
之前的文章《整合Spring Cloud Stream Binder与RabbitMQ进行消息发送与接收》讲解了Spring Cloud stream
与RabbitMQ
的整合,本文将简单介绍一下Spring Cloud Stream
与Google Cloud Pub/Sub
的整合。
2 通过Emulator启动Pub/Sub
因使用实际的GCP Pub/Sub相对麻烦,本文通过模拟器来运行。
关于Google Cloud SDK
的安装可参考:Mac安装Google Cloud SDK
安装必要的组件:
gcloud components install beta
gcloud components install pubsub-emulator
启动模拟器:
$ gcloud beta emulators pubsub start --project=pkslow-prj --host-port=0.0.0.0:8511
Executing: /google-cloud-sdk/platform/pubsub-emulator/bin/cloud-pubsub-emulator --host=0.0.0.0 --port=8511
[pubsub] This is the Google Pub/Sub fake.
[pubsub] Implementation may be incomplete or differ from the real system.
[pubsub] May 11, 2021 10:27:31 PM com.google.cloud.pubsub.testing.v1.Main main
[pubsub] INFO: IAM integration is disabled. IAM policy methods and ACL checks are not supported
[pubsub] SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
[pubsub] SLF4J: Defaulting to no-operation (NOP) logger implementation
[pubsub] SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
[pubsub] May 11, 2021 10:27:32 PM com.google.cloud.pubsub.testing.v1.Main main
[pubsub] INFO: Server started, listening on 8511
启动的时候设置了项目名和端口。
3 整合
引入依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-gcp-pubsub-stream-binder</artifactId>
</dependency>
实现简单的消息收发处理:
package com.pkslow.cloud.stream.binder.pubsub;
@SpringBootApplication
public class StreamBinderPubsub {
private static final Logger log = LoggerFactory.getLogger(StreamBinderPubsub.class);
public static void main(String[] args) {
SpringApplication.run(StreamBinderPubsub.class, args);
}
@Bean
public Supplier<String> pkslowSource() {
return () -> {
String message = "www.pkslow.com";
log.info("Sending value: " + message);
return message;
};
}
@Bean
public Consumer<String> pkslowSink() {
return message -> {
log.info("Received message " + message);
};
}
}
配置Pub/Sub属性与Cloud Stream属性:
spring:
cloud:
stream:
function:
definition: pkslowSource;pkslowSink
bindings:
pkslowSource-out-0:
destination: pkslow-topic
pkslowSink-in-0:
destination: pkslow-topic
poller:
fixed-delay: 500
gcp:
pubsub:
emulator-host: localhost:8511
project-id: pkslow-prj
如果是实际的GCP Pub/Sub
,指定key文件即可:
spring:
cloud:
gcp:
credentials:
location: file:/xxx.json
运行日志如下:
4 总结
代码请查看:https://github.com/LarryDpk/pkslow-samples
欢迎关注微信公众号<南瓜慢说>,将持续为你更新...
多读书,多分享;多写作,多整理。
整合Spring Cloud Stream Binder与GCP Pubsub进行消息发送与接收的更多相关文章
- 整合Spring Cloud Stream Binder与RabbitMQ进行消息发送与接收
我最新最全的文章都在南瓜慢说 www.pkslow.com,欢迎大家来喝茶! 1 前言 Spring Cloud Stream专门用于事件驱动的微服务系统,使用消息中间件来收发信息.使用Spring ...
- Spring Cloud Stream如何消费自己生产的消息?
在上一篇<Spring Cloud Stream如何处理消息重复消费>中,我们通过消费组的配置解决了多实例部署情况下消息重复消费这一入门时的常见问题.本文将继续说说在另外一个被经常问到的问 ...
- Spring Cloud Stream如何消费自己生产的消息
在上一篇<Spring Cloud Stream如何处理消息重复消费>中,我们通过消费组的配置解决了多实例部署情况下消息重复消费这一入门时的常见问题.本文将继续说说在另外一个被经常问到的问 ...
- 使用 Spring Cloud Stream 构建消息驱动微服务
相关源码: spring cloud demo 微服务的目的: 松耦合 事件驱动的优势:高度解耦 Spring Cloud Stream 的几个概念 Spring Cloud Stream is a ...
- Kafka及Spring Cloud Stream
安装 下载kafka http://mirrors.hust.edu.cn/apache/kafka/2.0.0/kafka_2.11-2.0.0.tgz kafka最为重要三个配置依次为:broke ...
- Spring Cloud Stream 知识点
发布-订阅模式 在Spring Cloud Stream中的消息通信方式遵循了发布-订阅模式,当一条消息被投递到消息中间件之后,它会通过共享的Topic主题进行广播,消息消费者在订阅的主题中收到它并触 ...
- Spring Cloud Alibaba学习笔记(14) - Spring Cloud Stream + RocketMQ实现分布式事务
发送消息 在Spring消息编程模型下,使用RocketMQ收发消息 一文中,发送消息使用的是RocketMQTemplate类. 在集成了Spring Cloud Stream之后,我们可以使用So ...
- Spring Cloud Stream教程(二)主要概念
Spring Cloud Stream提供了一些简化了消息驱动的微服务应用程序编写的抽象和原语.本节概述了以下内容: Spring Cloud Stream的应用模型 Binder抽象 持续的发布 - ...
- 【进阶技术】一篇文章搞掂:Spring Cloud Stream
本文总结自官方文档http://cloud.spring.io/spring-cloud-static/spring-cloud-stream/2.1.0.RC3/single/spring-clou ...
随机推荐
- Spring Boot 2.5.0 发布:支持Java16、Gradle 7、Datasource初始化机制调整
今年520的事情是真的多,娱乐圈的我们不管,就跟DD一起来看看 Spring Boot 2.5.0 的发布吧!看看都带来了哪些振奋人心的新特性和改动! 主要更新 支持 Java 16 支持 Gradl ...
- [bug] Hive:map.xml could only be replicated to 0 nodes instead of minReplication (=1). There are 0 datanode(s) running and no node(s) are excluded in this operation.
原因: datanode未运行,重启hdfs
- Linux PATH环境变量及作用(初学者必读)
Linux PATH环境变量及作用(初学者必读) < 什么是环境变量,Linux环境变量有哪些?Linux打包(归档)和压缩 > <Linux就该这么学>是一本基于最新Linu ...
- canal 环境搭建 canal 与kafka通信(三)
canal 占用了生产者 .net core端 使用消费者获取canal 消息 安装 Confluent.Kafka demo使用 1.3.0 public static void Consumer ...
- Linux_日志管理理论概述
一.日志系统 1.kernel -->物理终端(/dev/console) --> /var/log/dmesg(系统启动时信息(包括错误信息)记录到该文件) 或者:# dmesg 或 # ...
- Linux_控制服务与守护进程
一.systemd 1.systemd简介 1️⃣:systemd是用户空间的第一个应用程序,即/sbin/init 2️⃣:init程序的类型: SysV风格:init(centos5),实现系统初 ...
- QT学习笔记(一)——Helloworld
QT学习笔记(一)--Helloworld 一.调试的基本方法: Log调试法 --在代码中加入一定的打印语句 --打印程序状态和关键变量的值 断点调试法: --在开发环境中的对应代码行加上断点 -- ...
- Linux服务之Apache服务篇
apache httpd:提供http服务 http超文本协议 HTML超文本标记语言 URL(Uniform Resource Locator)统一资源定位符 http://www.sina.com ...
- centos7安装google-chrome
完整的安装步骤:https://www.tecmint.com/install-google-chrome-on-redhat-centos-fedora-linux/ 1.简单安装测试版:sudo ...
- 程序"三高"解决方案
0. 程序三高 1. 缓存 2. 预处理和延后处理 3. 池化 3.1 内存池 3.2 线程池 3.3 连接池 4. 异步(回调) 5. 消息队列 5.1 服务解耦 5.2 异步处理 5.3 流量削峰 ...