pushpin 是一个很不错的将restapi 转换为reailtime api 的proxy,openresty 具有很强的nginx 控制能力
可以方便的用来进行api 的开发,默认其他语言pushpin 提供了sdk ,但是lua 的暂时还没有,所以基于
http 请求,搞一个简单的测试,后期可以参考其他语言,封装为lua 模块

环境准备

  • docker-compose 文件
version: "3"
services:
pushpin:
image: fanout/pushpin
environment:
- "target=api:8080"
- "LOGNAME=nobody"
volumes:
- "./routes:/etc/pushpin/routes"
ports:
- "5561:5561"
- "5562:5562"
- "5563:5563"
- "7999:7999"
api:
build: ./
ports:
- "8080:8080"
volumes:
- "./nginx_lua/:/opt/app/"
- "./nginx.conf:/usr/local/openresty/nginx/conf/nginx.conf"
  • openresty dockerfile
FROM openresty/openresty:alpine-fat
LABEL author="1141591465@qq.com"
RUN /usr/local/openresty/luajit/bin/luarocks install lua-resty-http
RUN opm get thibaultcha/lua-resty-jit-uuid
  • router 文件
    实际上官方提供的镜像对于router 基本是可选的,因为这个docker 镜像,默认参数太多。。。
  • nginx.conf
worker_processes 1;
user root;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
lua_code_cache off;
lua_need_request_body on;
gzip on;
resolver 127.0.0.11 ipv6=off;
real_ip_header X-Forwarded-For;
real_ip_recursive on;
lua_package_path '/opt/app/?.lua;;';
server {
listen 8080;
server_name app;
charset utf-8;
default_type text/html;
location / {
default_type text/plain;
index index.html index.htm;
}
location = /favicon.ico {
root /opt/app/static;
}
location /userinfo {
content_by_lua_block {
require("api/userinfo")()
}
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
} }
}

lua publish 代码

代码比较简单,基于http 模块,同时注意content 内容需要一个\n 参数,这个稍有点坑,,,
ngx_lua/api/userinfo.lua

local json = require("cjson")
local http = require("resty.http")
function init()
local uuid = require("resty.jit-uuid")
uuid.seed()
local method_name = ngx.req.get_method()
if method_name == "GET" then
ngx.header["Content-Type"] = "text/plain"
ngx.header["Grip-Hold"] = "stream"
ngx.header["Grip-Channel"] = "userinfo"
ngx.say("is stream open")
return
end
ngx.req.read_body()
local body = ngx.req.get_body_data()
if not body then
if ngx.req.get_body_file() then
return nil, "request body did not fit into client body buffer, consider raising 'client_body_buffer_size'"
else
return ""
end
end
local mode = json.decode(body)
local body_entity = {
items = {
{
channel = mode.channel,
-- id 必须唯一,为了方式使用了uuid lua 包
id = uuid(),
formats = {
["http-stream"] = {
-- \n 很重要
content = json.encode(mode.data)..'\n',
},
-- action = "send"
}
}
}
}
ngx.log(ngx.ERR, json.encode(body_entity))
local requestapiurl = "http://pushpin:5561/publish/"
local httpc = http.new()
local res, err =
httpc:request_uri(
requestapiurl,
{
method = "POST",
body = json.encode(body_entity),
headers = {
["Content-Type"] = "application/json"
}
}
)
if not res then
ngx.say("failed to request: ", err)
return
end
ngx.log(ngx.ERR, res.body)
ngx.say(res.body)
end
return init

启动&&测试

  • 启动
docker-compose up -d
  • 测试效果
    client (curl) stream 连接
curl http://localhost:7999/userinfo

发送消息,(openresty 端,而不是一般的pushpin 的rest 地址)

curl -X POST \
http://localhost:8080/userinfo \
-H 'Content-Type: application/json' \
-H 'Postman-Token: a401a816-2f51-4183-811b-b62a5c9c66a3' \
-H 'cache-control: no-cache' \
-d '{
"channel":"userinfo",
"data":{
"username":"demoapp\n,userinfo app \\n",
"userage":333
}
}'

内容

说明

pushpin 支持的模型还是比较多的,不只是stream 还有websocket, sse。。。

参考资料

https://pushpin.org/docs/usage/#subscribing
https://github.com/rongfengliang/pushpin-openresty-docker-compose

 
 
 
 

pushpin openresty 集成试用的更多相关文章

  1. pushpin Server-sent events && openresty 集成试用

    前边有写过一个简单pushpin 集成stream 的demo,这次测试下sse 的功能 备注: 环境依然使用的是docker-compose运行 环境准备 docker-compose 文件 ver ...

  2. gearman openresty 集成试用

    很简单使用了一个openresty 的lua 模块 环境准备 docker-compose 文件 详细配置可以参考 https://github.com/rongfengliang/gearmango ...

  3. openresty 集成 keycloak-oauth-oidc

    keycloak 是一个比较全,而且比较方便的sso 解决方案,同时为我们提供了灵活的扩展特性 备注: 测试使用docker-compose 运行,对于keycloak 使用pg 数据库做为后端存储 ...

  4. openresty 集成 sentry 异常系统

    sentry 是一个方便的错误异常追踪系统,同时社区也提供了openresty 的lua 包,使用docker-compose 进行测试 备注: sentry 部分的配置来自官方文档 环境准备 doc ...

  5. openresty 集成lua-resty-mail +smtp2http 扩展灵活的mail 服务

    lua-resty-mail 是一个不错的openresty mail 扩展,我们可以用来进行邮件发送,支持附件功能 smtp2http 是一个smtp 服务,可以将smtp 请求数据转换为http ...

  6. gogs wekan 集成试用

    wekan 官方提供了一个集成gogs 的扩展,不是完全的自动化,需要结合cli,但是官方的cli 写的...(不是很全) 备注: 测试环境使用docker-compose 环境准备 docker-c ...

  7. graphql elasticsearch 集成试用

    graphql 是很方便的api 查询语言,elasticsearch 可以方便的进行全文检索的应用开发 有一个方便的npm 包graphql-compose-elasticsearch 可以进行es ...

  8. benthos stream nats 集成试用

    测试demo 来自官方例子 使用docker-compose 进行运行 nats docker-compose file version: '3.3' services: nats: image: n ...

  9. 基于OpenResty和Node.js的微服务架构实践

    什么是微服务? 传统的单体服务架构是单独服务包,共享代码与数据,开发成本较高,可维护性.伸缩性较差,技术转型.跨语言配合相对困难.而微服务架构强调一个服务负责一项业务,服务可以单独部署,独立进行技术选 ...

随机推荐

  1. 网页设置下载apk

     APK文件其实是zip格式,但后缀名被修改为apk,通过UnZip解压后,可以看到Dex文件,Dex是Dalvik VM executes的全称,即Android Dalvik执行程序,并非Ja ...

  2. Reveal 破解及使用

    Reveal是一款很好的iOS调试应用,这在你的开发中起到了相当大的帮助. 破解包可去http://xclient.info/?_=118ba0724e7a6af91ba29a22ee4131da 下 ...

  3. 8.2 C++标准输出流对象

    参考:http://www.weixueyuan.net/view/6408.html 总结: iostream头文件,包含了该头文件后,我们就可以直接使用这些对象,包含标准的输出流对象cout.ce ...

  4. 玩转X-CTR100 | STM32F4 l X-Assistant串口助手控制功能

    我造轮子,你造车,创客一起造起来!塔克创新资讯[塔克社区 www.xtark.cn ][塔克博客 www.cnblogs.com/xtark/ ]      X-CTR100控制器配套的X-Assis ...

  5. android 自定义命名空间 http://schemas.android.com/apk/res-auto

    XML中用 xmlns="http://schemas.android.com/apk/res-auto" 获取自定义属性值: public static String NAMES ...

  6. 创建vue项目

    1.创建项目 vue creat 项目名 // 要提前进入目标目录(项目应该创建在哪个目录下) // 选择自定义方式创建项目,选取Router, Vuex插件 2.启动/停止项目 npm run se ...

  7. ionic的actionsheet安卓样式不正常的坑及解决之道

    这是actionsheet该有的样子,可是android下变成了这样: 百度后,发现修改lonic.css,注释这段代码就可以了:

  8. 分析无线遥控器信号并制作Hack硬件进行攻击

    无线遥控器(无线电遥控器)在我们生活中非常常见,应用于各种场景,方便着用户的使用.不过大多数还是用于安防方面的,比如: 遥控报警器.电动卷帘门.电动伸缩门.遥控电开关.无线遥控门铃…… 1.无线遥控器 ...

  9. ubantu 安装nginx HTTP反向代理服务器

    Nginx发音的“engine x”是一个免费的开源高性能HTTP和反向代理服务器,负责处理互联网上一些最大的网站的负载. 本教程将概述在Ubuntu 18.04机器上安装和管理Nginx的步骤. 安 ...

  10. Linux运维命令总结(-)

    Linux运维命令总结(-)  此次整理linux运维常用命令13个,常用linux运维命令大概有150个,约占百分之十,大牛见笑,本人菜鸟一枚不才整理如下,如有不正确之处,请多多指正. 1.创建目录 ...