分享好友 编程语言首页 频道列表

bloom-server 基于 rust 编写的 rest api cache 中间件

rust文章/教程  2023-03-08 13:250

bloom-server 基于 rust 编写的 rest api cache 中间件,他位于lb 与api worker 之间,使用redis 作为缓存内容存储,
我们需要做的就是配置proxy,同时他使用基于share 的概念,进行cache 的分布存储,包含了请求端口(proxy,访问数据)
以及cache 控制端口(api 方便cache 策略的控制)

测试环境使用openresty+ docker + docker-compose 运行

一张参考图

bloom-server 基于 rust 编写的 rest api cache 中间件

环境准备

  • 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

查看更多关于【rust文章/教程】的文章

展开全文
相关推荐
反对 0
举报 0
评论 0
图文资讯
热门推荐
优选好物
更多热点专题
更多推荐文章
Rust到底值不值得学--Rust对比、特色和理念
前言其实我一直弄不明白一点,那就是计算机技术的发展,是让这个世界变得简单了,还是变得更复杂了。当然这只是一个玩笑,可别把这个问题当真。然而对于IT从业者来说,这可不是一个玩笑。几乎每一次的技术发展,都让这个生态变得更为复杂。“英年早秃”已经成

0评论2023-03-08818

全栈程序员的新玩具Rust(三)板条箱
上次用到了stdout,这次我们来写一个更复杂一点的游戏rust的标准库叫做std,默认就会引入。这次我们要用到一个随机数函数,而随机数比较尴尬的一点是这玩意不在标准库中,我们要额外依赖一个库。很多编程方案都有自己的模块化库系统,rust也不例外,不过rust

0评论2023-02-10729

【Rust】标准库-Result rust数据库
环境Rust 1.56.1VSCode 1.61.2概念参考:https://doc.rust-lang.org/stable/rust-by-example/std/result.html示例main.rsmod checked {#[derive(Debug)]pub enum MathError {DivisionByZero,NonPositiveLogarithm,NegativeSquareRoot,}pub type MathResult =

0评论2023-02-09978

【Rust】标准库-引用 rust 数据库框架
环境Rust 1.56.1VSCode 1.61.2概念参考:https://doc.rust-lang.org/stable/rust-by-example/std/rc.html示例rust 使用 Rc 来实现引用计数。main.rsuse std::rc::Rc;fn main() {let rc_examples = "Rc examples".to_string();{println!("--- rc_a is created

0评论2023-02-09638

rust 打印当前时间
let now = time::now();let f_now = time::strftime("%Y-%m-%dT%H:%M:%S", now).unwrap();println!("now: {:?}", f_now);

0评论2023-02-09689

【Rust】线程 rust编程语言
环境Rust 1.56.1VSCode 1.61.2概念参考:https://doc.rust-lang.org/stable/rust-by-example/std_misc/threads.html示例main.rsuse std::thread;const N_THREADS: u32 = 10;fn main() {let mut children = vec![];for i in 0..N_THREADS {children.push(threa

0评论2023-02-09956

rust安装 rust安装在固态进不去游戏
http://blog.csdn.net/teamlet/article/details/50838996

0评论2023-02-09614

更多推荐