整合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 ...
随机推荐
- 6.注册CRT 以及SecureCRT访问
1.什么是 SecureCRT SecureCRT是一款支持SSH(SSH1和SSH2)的终端仿真程序,简单地说是Windows下登录Unix或Linux服务器主 机的软件. 1)准备工作:安装好Se ...
- jQuery 实现 全选/全不选/反选
<button id="getall" class="btn btn-warning">全选</button><button id ...
- alpine安装网络工具
telnet:busybox-extras net-tools: net-tools tcpdump: tcpdump wget: wget dig nslookup: bind-tools curl ...
- Rsync忽略文件夹或目录
使用Rsync同步的时候往往会要求对某个文件夹或者文件进行忽略,客户端可以使用--exclude参数来实现对,目录或者文件的忽略 rsync -rltvz --port=873 --exclude & ...
- dpkg -S {file} #ubuntu 14.04 rpm -qf {file} #centos 7
Linux查找命令或组件对应安装包的方法原创FJEagle 最后发布于2017-12-15 19:10:06 阅读数 4603 收藏展开Linux查找命令或组件对应安装包的方法当新搭建服务器或者维护不 ...
- Linux_源码安装包管理理论概述
一.源码包基本概述 1️⃣:源码包的编译用到了linux系统里的编译器,通常源码包都是用C语言开发的,这也是因为C语言为linux上最标准的程序语言 2️⃣:Linux上的C语言编译器叫做gcc,利用 ...
- mysql基础之数据库变量(参数)管理
数据库的数据存放路径:[root@ren7 mysql]# pwd /var/lib/mysql [root@ren7 mysql]# ls aria_log.00000001 ibdata1 mul ...
- 06丨MongoDB基本操作
使用 insert 完成插入操作 操作格式: db.<集合>.insertOne(<JSON对象>) db.<集合>.insertMany([<JSON 1& ...
- 基于 IntersectionObserver 实现一个组件的曝光监控
我们在产品推广过程中,经常需要判断用户是否对某个模块感兴趣.那么就需要获取该模块的曝光量和用户对该模块的点击量,若点击量/曝光量越高,说明该模块越有吸引力. 那么如何知道模块对用户是否曝光了呢?之前我 ...
- opencv——自适应阈值Canny边缘检测
前言 Canny边缘检测速度很快,OpenCV中经常会用到Canny边缘检测,以前的Demo中使用Canny边缘检测都是自己手动修改高低阈值参数,最近正好要研究点小东西时,就想能不能做个自适应的阈值, ...