springboot + springcloud + Google pubsub+ firebase
1.pom依赖
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-gcp-starter</artifactId><version>1.2.6.RELEASE</version></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-gcp-starter-pubsub</artifactId><version>1.2.6.RELEASE</version></dependency><!-- Firebase Admin SDK --><dependency><groupId>com.google.firebase</groupId><artifactId>firebase-admin</artifactId><version>8.1.0</version></dependency><!--pubsub--><dependency><groupId>com.google.guava</groupId><artifactId>guava</artifactId><version>30.1-jre</version></dependency><dependency><groupId>com.google.protobuf</groupId><artifactId>protobuf-java</artifactId><version>3.14.0</version></dependency>
2.yml配置
spring:cloud:gcp:credentials:location: file:/data/reco-data/********.jsonproject-id: *****pubsub:enabled: true
google:auth-file-path: /data/reco-data/*****.jsonproject-num-id: *****androidApps: *****firebase: topic: ***
3.代码
package com.****.service.impl;import java.io.FileInputStream;
import java.util.Arrays;
import java.util.List;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cloud.gcp.pubsub.core.publisher.PubSubPublisherTemplate;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import com.google.protobuf.ByteString;
import com.google.pubsub.v1.PubsubMessage;
import com.****.service.DataRelayService;
import com.google.auth.oauth2.AccessToken;
import com.google.auth.oauth2.GoogleCredentials;import lombok.extern.slf4j.Slf4j;@Service
@Slf4j
public class DataRelayServiceImp implements DataRelayService {private final RestTemplate restTemplate;public DataRelayServiceImp(RestTemplateBuilder restTemplateBuilder) {this.restTemplate = restTemplateBuilder.build();}@Autowiredprivate PubSubPublisherTemplate pubSubTemplate;@Value("${google.project-num-id}")private String projectNumId;@Value("${google.firebase.topic}")private String firebaseTopic;@Value("${google.auth-file-path}")private String filePath;@Value("#{'${google.androidApps}'.split(',')}")private List<String> androidApps;@Overridepublic void getApiData() {// 加载服务账号密钥try {List<String> scopes = Arrays.asList("https://www.googleapis.com/auth/firebase.readonly","https://www.googleapis.com/auth/cloud-platform");GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream(filePath)).createScoped(scopes);// 强制刷新以获取新的访问令牌credentials.refresh();AccessToken token = credentials.getAccessToken();// 获取令牌值String accessToken = token.getTokenValue();log.info("token----" + token);for (String androidApp : androidApps) {String url = new StringBuilder("https://fcmdata.googleapis.com/v1beta1/projects/").append(projectNumId).append("/androidApps/").append(androidApp).append("/deliveryData").toString();HttpHeaders headers = new HttpHeaders();headers.add("Authorization", "Bearer " + accessToken);// 创建 HttpEntityHttpEntity<String> entity = new HttpEntity<>("parameters", headers);// 发送 GET 请求ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, entity, String.class);log.info("==============" + response.getBody());ByteString data = ByteString.copyFromUtf8(response.getBody());PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).build();String res = pubSubTemplate.publish(firebaseTopic, pubsubMessage).get();log.info("pubsub 发布结果 = {}", res);}} catch (Exception e) {log.error("firebase异常=========" + e);}}
}
相关文章:
springboot + springcloud + Google pubsub+ firebase
1.pom依赖 <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-gcp-starter</artifactId><version>1.2.6.RELEASE</version></dependency><dependency><groupId>org.springframe…...
时序数据库TDengine和QuestDB对比
QuestDB和TDengine都是高性能的时序数据库(Time Series Database, TSDB),但它们在设计、功能、适用场景以及性能表现上各有特色。 以下是对两者的详细对比: 一、设计与架构 QuestDB 是一个开源的高性能SQL时序数据库࿰…...
Neuralink的进展与马斯克的技术愿景——从脑机接口到AI融合的未来
引言 Neuralink,这个由埃隆马斯克(Elon Musk)创立的公司,一直是科技界的焦点。自从其发布以来,Neuralink的脑机接口技术便吸引了全球的目光。最近,马斯克再次向公众展示了Neuralink的突破性进展࿰…...
大数据技术——实战项目:广告数仓(第四部分)
目录 第7章 数据仓库环境准备 7.1 数据仓库运行环境 7.1.1 Hive环境搭建 7.1.2 Yarn环境配置 7.2 数据仓库开发环境 第8章 广告数仓ODS层 8.1 广告信息表 8.2 推广平台表 8.3 产品表 8.4 广告投放表 8.5 日志服务器列表 8.6 广告监测日志表 8.7 数据装载脚本 第7章…...
cmake+ninja交叉编译android下的静态库
文章目录 cmakeninja案例背景重新安装ninja编译通过 参考 想整理一个库的cmake工程,他用 cmakeninja 简单了解了一下,是可以不依赖Android studio编译的cmake的,搜到了一个cmakeninja,参考[1] 案例 参考[1]中的代码 背景 cm…...
Vue项目-Table添加Form表单校验
一、HTML <template><div class"taskInfo"><el-form:model"generateParams":rules"formRules"ref"formRef"class"taskInfoForm"label-width"100px"><ul class"taskInfoSearch"&g…...
【iOS】—— 事件传递链和响应者链总结
事件传递链和响应者链总结 1. 事件传递链:事件传递链:传递流程:总结第一响应者: 2. 响应者链响应者链传递流程总结响应者链流程 总结: 之前也学习过这个内容这次在复习的时候,就想着写一下总结:…...
【多线程】初识进程和线程
💐个人主页:初晴~ 📚相关专栏:多线程 / javaEE初阶 前言 在我们之前编写的所有代码,都只能用上一个核心。众所周知,现在大多数CPU都有多个核心,但此时,无论如法优化程序,…...
1DCNN-2DResNet并行故障诊断模型
往期精彩内容: Python-凯斯西储大学(CWRU)轴承数据解读与分类处理 Python轴承故障诊断入门教学-CSDN博客 Python轴承故障诊断 (13)基于故障信号特征提取的超强机器学习识别模型-CSDN博客 Python轴承故障诊断 (14)高创新故障识别模型-CSDN…...
Java设计模式(原型模式)
定义 使用原型实例指定待创建对象的类型,并且通过复制这个原型来创建新的对象。 角色 Prototype(抽象原型角色) ConcretePrototype(具体原型角色) Client(客户端角色 优点 简化对象的创建过程,…...
C/C++ 知识点:typedef 关键字
文章目录 一、typedef 关键字1、 基本用法2、常见用法2.1、为基本数据类型定义别名2.2、为结构体或联合体定义别名2.3、为指针类型定义别名2.4、为复杂模板类型定义别名 3、注意事项4、总结 前言: 在C(以及C语言)中,typedef 关键字…...
【Linux学习】进程间通信之 匿名管道 与 基于管道的进程池
🍑个人主页:Jupiter. 🚀 所属专栏:Linux从入门到进阶 欢迎大家点赞收藏评论😊 目录 🍑进程间通信🐬进程间通信目的 📚管道 📕管道的原理🐧用fork来共享管道原…...
小团队如何选需求管理软件?8款顶级推荐
本文将分享8款适合小团队的需求管理软件:PingCode、Worktile、Tapd、Teambition、禅道、Asana、Jama Connect、Aha!。 在小团队中管理需求时,寻找合适的软件工具常常让人头疼,不同的需求管理软件提供各种功能,但哪些功能真正适合…...
docker操作入门
1.创建镜像,使用当前文件 docker build -t experience . 2.运行容器 docker run -d -p 8501:8501 --name my-running-app my-python-api docker run -p 8508:8508 experience docker run -p 8508:8508 -p 8509:8509 experience 3.查看容器状态 docker ps docker p…...
简单的射箭小游戏网页源码
简单的射箭小游戏网页源码,对准靶心开启你的射击之旅吧 微信扫码免费获取源码...
Python | Leetcode Python题解之第331题验证二叉树的前序序列化
题目: 题解: class Solution:def isValidSerialization(self, preorder: str) -> bool:pre 1for i in preorder.split(,):if i.isdigit():if pre 0:return Falsepre 1else:if pre 0:return Falsepre - 1return pre 0...
0x3 “护网行动”守之道
一、护网防守目标系统 二、护网防守之利器 通过安全流程控制、安全技术保障、安全工具支撑、安全能力提升四个层次全面构成安全防御体系。 安全技术名称解释 IPS(入侵防御系统)WAF(Web应用防火墙)IDS(入侵检测系统&a…...
白骑士的Matlab教学高级篇 3.1 高级编程技术
系列目录 上一篇:白骑士的Matlab教学进阶篇 2.5 Simulink 高级编程技术在MATLAB中扮演着至关重要的角色,帮助用户更高效地编写复杂程序、提高代码的可维护性和可读性。本节将介绍面向对象编程、函数句柄与回调函数、错误处理与调试的相关内容。 面向对…...
haproxy简介与用法
一、负载均衡 1.1、概念: 负载均衡SLB(Server Load Balancer)是一种对流量进行按需分发的服务,通过将流量分发到不同的后端服务来扩展应用系统的服务吞吐能力,并且可以消除系统中的单点故障,提升应用系统…...
Geoscene Pro的三维
一、场景设置 1.3D视图分为全局场景和局部场景。在Geoscene Pro中,两个场景可以自由切换。 (1)全局场景有固定的坐标系GCS(WGS84、CGCS2000),并在全球比例尺下展示(全球范围)。可以…...
利用最小二乘法找圆心和半径
#include <iostream> #include <vector> #include <cmath> #include <Eigen/Dense> // 需安装Eigen库用于矩阵运算 // 定义点结构 struct Point { double x, y; Point(double x_, double y_) : x(x_), y(y_) {} }; // 最小二乘法求圆心和半径 …...
SciencePlots——绘制论文中的图片
文章目录 安装一、风格二、1 资源 安装 # 安装最新版 pip install githttps://github.com/garrettj403/SciencePlots.git# 安装稳定版 pip install SciencePlots一、风格 简单好用的深度学习论文绘图专用工具包–Science Plot 二、 1 资源 论文绘图神器来了:一行…...
前端倒计时误差!
提示:记录工作中遇到的需求及解决办法 文章目录 前言一、误差从何而来?二、五大解决方案1. 动态校准法(基础版)2. Web Worker 计时3. 服务器时间同步4. Performance API 高精度计时5. 页面可见性API优化三、生产环境最佳实践四、终极解决方案架构前言 前几天听说公司某个项…...
Go 语言接口详解
Go 语言接口详解 核心概念 接口定义 在 Go 语言中,接口是一种抽象类型,它定义了一组方法的集合: // 定义接口 type Shape interface {Area() float64Perimeter() float64 } 接口实现 Go 接口的实现是隐式的: // 矩形结构体…...
React19源码系列之 事件插件系统
事件类别 事件类型 定义 文档 Event Event 接口表示在 EventTarget 上出现的事件。 Event - Web API | MDN UIEvent UIEvent 接口表示简单的用户界面事件。 UIEvent - Web API | MDN KeyboardEvent KeyboardEvent 对象描述了用户与键盘的交互。 KeyboardEvent - Web…...
Rust 异步编程
Rust 异步编程 引言 Rust 是一种系统编程语言,以其高性能、安全性以及零成本抽象而著称。在多核处理器成为主流的今天,异步编程成为了一种提高应用性能、优化资源利用的有效手段。本文将深入探讨 Rust 异步编程的核心概念、常用库以及最佳实践。 异步编程基础 什么是异步…...
LLM基础1_语言模型如何处理文本
基于GitHub项目:https://github.com/datawhalechina/llms-from-scratch-cn 工具介绍 tiktoken:OpenAI开发的专业"分词器" torch:Facebook开发的强力计算引擎,相当于超级计算器 理解词嵌入:给词语画"…...
数据库分批入库
今天在工作中,遇到一个问题,就是分批查询的时候,由于批次过大导致出现了一些问题,一下是问题描述和解决方案: 示例: // 假设已有数据列表 dataList 和 PreparedStatement pstmt int batchSize 1000; // …...
[Java恶补day16] 238.除自身以外数组的乘积
给你一个整数数组 nums,返回 数组 answer ,其中 answer[i] 等于 nums 中除 nums[i] 之外其余各元素的乘积 。 题目数据 保证 数组 nums之中任意元素的全部前缀元素和后缀的乘积都在 32 位 整数范围内。 请 不要使用除法,且在 O(n) 时间复杂度…...
蓝桥杯3498 01串的熵
问题描述 对于一个长度为 23333333的 01 串, 如果其信息熵为 11625907.5798, 且 0 出现次数比 1 少, 那么这个 01 串中 0 出现了多少次? #include<iostream> #include<cmath> using namespace std;int n 23333333;int main() {//枚举 0 出现的次数//因…...
