我最新最全的文章都在南瓜慢说 www.pkslow.com,欢迎大家来喝茶!

1 前言

之前的文章《整合Spring Cloud Stream Binder与RabbitMQ进行消息发送与接收》讲解了Spring Cloud streamRabbitMQ的整合,本文将简单介绍一下Spring Cloud StreamGoogle 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进行消息发送与接收的更多相关文章

  1. 整合Spring Cloud Stream Binder与RabbitMQ进行消息发送与接收

    我最新最全的文章都在南瓜慢说 www.pkslow.com,欢迎大家来喝茶! 1 前言 Spring Cloud Stream专门用于事件驱动的微服务系统,使用消息中间件来收发信息.使用Spring ...

  2. Spring Cloud Stream如何消费自己生产的消息?

    在上一篇<Spring Cloud Stream如何处理消息重复消费>中,我们通过消费组的配置解决了多实例部署情况下消息重复消费这一入门时的常见问题.本文将继续说说在另外一个被经常问到的问 ...

  3. Spring Cloud Stream如何消费自己生产的消息

    在上一篇<Spring Cloud Stream如何处理消息重复消费>中,我们通过消费组的配置解决了多实例部署情况下消息重复消费这一入门时的常见问题.本文将继续说说在另外一个被经常问到的问 ...

  4. 使用 Spring Cloud Stream 构建消息驱动微服务

    相关源码: spring cloud demo 微服务的目的: 松耦合 事件驱动的优势:高度解耦 Spring Cloud Stream 的几个概念 Spring Cloud Stream is a ...

  5. Kafka及Spring Cloud Stream

    安装 下载kafka http://mirrors.hust.edu.cn/apache/kafka/2.0.0/kafka_2.11-2.0.0.tgz kafka最为重要三个配置依次为:broke ...

  6. Spring Cloud Stream 知识点

    发布-订阅模式 在Spring Cloud Stream中的消息通信方式遵循了发布-订阅模式,当一条消息被投递到消息中间件之后,它会通过共享的Topic主题进行广播,消息消费者在订阅的主题中收到它并触 ...

  7. Spring Cloud Alibaba学习笔记(14) - Spring Cloud Stream + RocketMQ实现分布式事务

    发送消息 在Spring消息编程模型下,使用RocketMQ收发消息 一文中,发送消息使用的是RocketMQTemplate类. 在集成了Spring Cloud Stream之后,我们可以使用So ...

  8. Spring Cloud Stream教程(二)主要概念

    Spring Cloud Stream提供了一些简化了消息驱动的微服务应用程序编写的抽象和原语.本节概述了以下内容: Spring Cloud Stream的应用模型 Binder抽象 持续的发布 - ...

  9. 【进阶技术】一篇文章搞掂:Spring Cloud Stream

    本文总结自官方文档http://cloud.spring.io/spring-cloud-static/spring-cloud-stream/2.1.0.RC3/single/spring-clou ...

随机推荐

  1. linux跨文件复制粘贴

    跨文件是这样的: 复制a.txt的第20行至第30行到b.txt中vi a.txt:2010yy:e b.txtp

  2. 加载usbserial驱动后,为什么adb不可用了?

    ​某设备提供了USB串口功能,上位机(Host端)可以通过USB串口与之通信.对于Linux上位机,比如Ubuntu,自带usbserial驱动,当安装usbserial驱动后,上位机就会生成ttyU ...

  3. docker运行mysql数据库

    1.搜索镜像源是否有mysql镜像 执行语句: sudo docker search mysql  2.拉取需要版本的mysql,执行语句: docker pull mysql:5.7  3.查看安装 ...

  4. 将top命令的输出,写入到文件中 top -b -n 1 -d 3 >>file.txt

    top -b -n 1 -d 3 >>file.txt 解析: -b :batch模式,可以重定向到文件中 -n 1:一共取1次top数据.后边加数字,表示次数 -d 3:每次top时间间 ...

  5. QT 资源链家暂存

    1.Qt右击菜单栏中文化 链接:https://blog.csdn.net/yangxiao_0203/article/details/7488967

  6. 使用 IPMI 远程为服务器安装操作系统教程

    使用 IPMI 远程为服务器安装操作系统教程 shida_csdn 2019-01-09 11:30:10 9588 收藏 16展开一.什么是 IPMI? IPMI 是智能平台管理接口(Intelli ...

  7. 021.Python的内置函数

    内置函数 1 abs 绝对值函数 res = abs(-9.9867) print(res) 执行 [root@node10 python]# python3 test.py 9.9867 2 rou ...

  8. C语言函数sscanf()的用法-(转自Walter L)

    在我的学习过程中,从文件读取数据是一件很麻烦的事,所幸有sscanf()函数. C语言函数sscanf()的用法sscanf() - 从一个字符串中读进与指定格式相符的数据. 函数原型: int ss ...

  9. [ Java面试题 ]Java 开发岗面试知识点解析

    如背景中介绍,作者在一年之内参加过多场面试,应聘岗位均为 Java 开发方向. 在不断的面试中,分类总结了 Java 开发岗位面试中的一些知识点. 主要包括以下几个部分: Java 基础知识点 Jav ...

  10. 26.Qt Quick QML-RotationAnimation、PathAnimation、SmoothedAnimation、Behavior、PauseAnimation、SequentialAnimation和ParallelAnimation

    1.RotationAnimationRotationAnimation也是继承于PropertyAnimation组件,但是它有点特殊,它只需要指定taget目标对象,并且不需要指定property ...