bloom-server 基于 rust 编写的 rest api cache 中间件
bloom-server 基于 rust 编写的 rest api cache 中间件,他位于lb 与api worker 之间,使用redis 作为缓存内容存储,
我们需要做的就是配置proxy,同时他使用基于share 的概念,进行cache 的分布存储,包含了请求端口(proxy,访问数据)
以及cache 控制端口(api 方便cache 策略的控制)
测试环境使用openresty+ docker + docker-compose 运行
一张参考图
环境准备
- docker-compose 文件
version: "3"
services:
redis:
image: redis
ports:
- "6379:6379"
lb:
image: openresty/openresty:alpine-fat
volumes:
- "./nginx/nginx-lb.conf:/usr/local/openresty/nginx/conf/nginx.conf"
ports:
- "9000:80"
webapi:
image: openresty/openresty:alpine-fat
volumes:
- "./nginx/nginx.conf:/usr/local/openresty/nginx/conf/nginx.conf"
ports:
- "8090:80"
webapi2:
image: openresty/openresty:alpine-fat
volumes:
- "./nginx/nginx2.conf:/usr/local/openresty/nginx/conf/nginx.conf"
ports:
- "8091:80"
bloom:
image: valeriansaliou/bloom:v1.25.0
environment:
- "RUST_BACKTRACE=1"
volumes:
- "./bloom/config.cfg:/etc/bloom.cfg"
ports:
- "8080:8080"
- "8811:8811"
bloom2:
image: valeriansaliou/bloom:v1.25.0
environment:
- "RUST_BACKTRACE=1"
volumes:
- "./bloom/config2.cfg:/etc/bloom.cfg"
ports:
- "8081:8080"
- "8812:8811"
- webapi 配置
主要是基于openresty 的 lua 脚本编写
webapi1 nginx/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;
server {
listen 80;
server_name app;
charset utf-8;
default_type text/html;
location / {
default_type text/plain;
index index.html index.htm;
}
location /userinfo {
default_type application/json;
content_by_lua_block {
local cjson = require("cjson");
local user = {
name ="dalongdempo",
age =333,
}
ngx.say(cjson.encode(user))
}
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
webapi2 nginx/nginx2.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;
server {
listen 80;
server_name app;
charset utf-8;
default_type text/html;
location / {
default_type text/plain;
index index.html index.htm;
}
location /userinfo2 {
default_type application/json;
content_by_lua_block {
local cjson = require("cjson");
local user = {
name ="dalongdempo222",
age =333,
}
ngx.say(cjson.encode(user))
}
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
- bloom 配置
bloom 类似一个sidecar,官方建议的方式是部署在每个服务的节点,但是实际上我们可以部署在
其他地方,进行分离部署
bloom1 配置 bloom/config.cfg 注意我将log级别修改为了debug,方便测试
# Bloom
# HTTP REST API caching middleware
# Configuration file
# Example: https://github.com/valeriansaliou/bloom/blob/master/config.cfg
[server]
log_level = "debug"
inet = "0.0.0.0:8080"
[control]
inet = "0.0.0.0:8811"
tcp_timeout = 600
[proxy]
[[proxy.shard]]
shard = 0
host = "webapi"
port = 80
[cache]
ttl_default = 600
executor_pool = 64
disable_read = false
disable_write = false
compress_body = true
[redis]
host = "redis"
port = 6379
database = 0
pool_size = 80
max_lifetime_seconds = 60
idle_timeout_seconds = 600
connection_timeout_seconds = 1
max_key_size = 256000
max_key_expiration = 2592000
bloom2 配置 bloom/config2.cfg
# Bloom
# HTTP REST API caching middleware
# Configuration file
# Example: https://github.com/valeriansaliou/bloom/blob/master/config.cfg
[server]
log_level = "debug"
inet = "0.0.0.0:8080"
[control]
inet = "0.0.0.0:8811"
tcp_timeout = 600
[proxy]
[[proxy.shard]]
shard = 1
host = "webapi2"
port = 80
[cache]
ttl_default = 600
executor_pool = 64
disable_read = false
disable_write = false
compress_body = true
[redis]
host = "redis"
port = 6379
database = 0
pool_size = 80
max_lifetime_seconds = 60
idle_timeout_seconds = 600
connection_timeout_seconds = 1
max_key_size = 256000
max_key_expiration = 2592000
启动&&测试
- 启动
docker-compose up -d
- 测试效果
从bloom 访问,注意需要Bloom-Request-Shard
curl -X GET \
http://localhost:8081/userinfo2 \
-H 'Bloom-Request-Shard: 1' \
-H 'Content-Type: application/json' \
-H 'Postman-Token: d13543ca-a031-47e3-b47a-996a6faaad53' \
-H 'cache-control: no-cache'
{"age":333,"name":"dalongdempo222"}
curl -X GET \
http://localhost:8080/userinfo \
-H 'Bloom-Request-Shard: 0' \
-H 'Content-Type: application/json' \
-H 'Postman-Token: bc168c7c-3b8b-471d-aa01-6eb6cb0d421c' \
-H 'cache-control: no-cache'
redis key
KEYS *
1) "bloom:0:a:dc56d17a"
2) "bloom:1:a:dc56d17a"
openresty(lb) 端访问: 不用添加header,因为nginx端已经配置了
curl http://localhost:9000/userinfo
{"age":333,"name":"dalongdempo"}
说明
bloom-server 还是一个不错的rest cache proxy,同时openresty 端也是可以做的,作者在项目中也介绍为什么不那么
干的原因,同时bloom-server 基于rust 编写,具有类型安全,以及很不错的性能,同时也包含了好多可选的控制,但是
如果你在运行的时候可能会发现部分提示并不是很友好,尤其对于在排查问题的时候,但是总的来说设计还是挺好的
参考资料
https://github.com/valeriansaliou/bloom
https://crates.io/crates/bloom-server
https://github.com/rongfengliang/bloom-server-docker-compose
bloom-server 基于 rust 编写的 rest api cache 中间件的更多相关文章
- 基于nginx+lua+redis高性能api应用实践
基于nginx+lua+redis高性能api应用实践 前言 比较传统的服务端程序(PHP.FAST CGI等),大多都是通过每产生一个请求,都会有一个进程与之相对应,请求处理完毕后相关进程自动释放. ...
- 在云平台上基于Go语言+Google图表API提供二维码生成应用
二维码能够说已经深深的融入了我们的生活其中.到处可见它的身影:但通常我们都是去扫二维码, 曾经我们分享给朋友一个网址直接把Url发过去,如今我们能够把自己的信息生成二维码再分享给他人. 这里就分享一下 ...
- 使用 node-odata 轻松创建基于 OData 协议的 RESTful API
前言 OData, 相信身为.NET程序员应该不为陌生, 对于他的实现, 之前也有童鞋进行过介绍(见:这里1,这里2). 微软的WCF Data Service即采用的该协议来进行通信, ASP.NE ...
- 基于gulp编写的一个简单实用的前端开发环境好了,安装完Gulp后,接下来是你大展身手的时候了,在你自己的电脑上面随便哪个地方建一个目录,打开命令行,然后进入创建好的目录里面,开始撸代码,关于生成的json文件请点击这里https://docs.npmjs.com/files/package.json,打开的速度看你的网速了注意:以下是为了演示 ,我建的一个目录结构,你自己可以根据项目需求自己建目
自从Node.js出现以来,基于其的前端开发的工具框架也越来越多了,从Grunt到Gulp再到现在很火的WebPack,所有的这些新的东西的出现都极大的解放了我们在前端领域的开发,作为一个在前端领域里 ...
- 基于 Markdown 编写接口文档
最近公司开发项目需要前后端分离,这样话就设计到后端接口设计.复杂功能需要提供各种各样的接口供前端调用,因此编写API文档非常有必要了 网上查了很多资料,发现基于Markdown编写文档是一种比较流行而 ...
- 基于gulp编写的一个简单实用的前端开发环境
自从Node.js出现以来,基于其的前端开发的工具框架也越来越多了,从Grunt到Gulp再到现在很火的WebPack,所有的这些新的东西的出现都极大的解放了我们在前端领域的开发,作为一个在前端领域里 ...
- 使用CEF(二)— 基于VS2019编写一个简单CEF样例
使用CEF(二)- 基于VS2019编写一个简单CEF样例 在这一节中,本人将会在Windows下使用VS2019创建一个空白的C++Windows Desktop Application项目,逐步进 ...
- 【转】发布一个基于NGUI编写的UI框架
发布一个基于NGUI编写的UI框架 1.加载,显示,隐藏,关闭页面,根据标示获得相应界面实例 2.提供界面显示隐藏动画接口 3.单独界面层级,Collider,背景管理 4.根据存储的导航信息完成界面 ...
- artDialog是一个基于javascript编写的对话框组件,它拥有精致的界面与友好的接口
artDialog是一个基于javascript编写的对话框组件,它拥有精致的界面与友好的接口 自适应内容 artDialog的特殊UI框架能够适应内容变化,甚至连外部程序动态插入的内容它仍然能自适应 ...
随机推荐
- 理解JavaScript的运行
JavaScript可以运行在head和body标签中! HTML的脚本必须放在<script></script>标签中间! 浏览器会解释并执行位于script标签中的脚本! ...
- CodeForces - 631C (截取法)
C. Report time limit per test 2 seconds memory limit per test 256 megabytes input standard input out ...
- python 爬虫时间数据-时间格式转换
1 import time,datetime 2 3 time_original = '17/Sep/2012:11:40:00' 4 time_format = datetime.datetime. ...
- Day13作业及默写
1. 整理今天的博客,写课上代码,整理流程图. 博客链接--博客园 2. 写一个函数完成三次登陆功能: 用户的用户名密码从一个文件register中取出. register文件包含多个用户名,密码,用 ...
- hadoop day 4
1.自定义的一种数据类型,要在hadoop的各个节点之间传输,应该遵循hadoop的序列化机制 就必须实现hadoop相应的序列化接口Writable 实现的方法包括:write(),readFiel ...
- js Promise fetch demo
<html> <head> <title></title> </head> <body> </body> <s ...
- 九度OJ1049题-去特定字符(和1111题特别像)
题目1049:字符串去特定字符 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:11329 解决:5169 题目描述: 输入字符串s和字符c,要求去掉s中所有的c字符,并输出结果. 输入: ...
- [LeetCode&Python] Problem 830. Positions of Large Groups
In a string S of lowercase letters, these letters form consecutive groups of the same character. For ...
- Unity 3D用简单的Cube、Sphere实现镜面/哈哈镜效果,只需十几秒哦!
Unity实现镜面和哈哈镜效果 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- 心分 ...
- Gym .102021 .German Collegiate Programming Contest (GCPC 18) (寒假gym自训第三场)
B .Battle Royale 题意:给你两个点A,B,以及一个圆S,保证两个点在圆外,且其连线与圆相交,求两点间最短距离. 思路:显然是要分别与圆相切,然后在圆弧想走,直到相交. 那么ans=与圆 ...