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
