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

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

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

 
反对 0举报 0 评论 0
 

免责声明:本文仅代表作者个人观点,与乐学笔记(本网)无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
    本网站有部分内容均转载自其它媒体,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责,若因作品内容、知识产权、版权和其他问题,请及时提供相关证明等材料并与我们留言联系,本网站将在规定时间内给予删除等相关处理.

  • #新闻拍一拍# Oracle 调研如何避免让 Java 开发者投奔 Rust 和 Kotlin | Linux 中国
    #新闻拍一拍# Oracle 调研如何避免让 Java 开发
     导读:• 英特尔对迟迟不被 Linux 主线接受的 SGX Enclave 进行了第 38 次修订 • ARM 支持开源的 Panfrost Gallium3D 驱动本文字数:977,阅读时长大约:1分钟作者:硬核老王Oracle 调研如何避免让 Java 开发者投奔 Rust 和 KotlinOracle 委托分析公司 Omd
    03-08
  • Linux系统下Rust快速安装:国内镜像加速
    Linux系统下Rust快速安装:国内镜像加速
    官方网址和方法Install Rust - Rust Programming Language然而速度慢得让人难以置信。利用国内镜像进行windows的Linux子系统的Rust安装。rust 使用国内镜像,快速安装方法参考:RUST安装慢怎么办,使用镜像方式安装_网络_为中华之崛起而编程-CSDN博客我的操作
    03-08
  • Rust到底值不值得学--Rust对比、特色和理念
    前言其实我一直弄不明白一点,那就是计算机技术的发展,是让这个世界变得简单了,还是变得更复杂了。当然这只是一个玩笑,可别把这个问题当真。然而对于IT从业者来说,这可不是一个玩笑。几乎每一次的技术发展,都让这个生态变得更为复杂。“英年早秃”已经成
    03-08
  • 超33000行新代码,为Linux内核添加Rust支持的补丁已准备就绪
    超33000行新代码,为Linux内核添加Rust支持的补
    https://mp.weixin.qq.com/s/oKw9aBJSdmRoO6-rbLAkNw7 月 4 日,一套修订后的补丁被提交至 Linux 内核的邮件列表中,该补丁为在 Linux 内核中以 Rust 作为辅助编程语言提供了支持,借助 Rust 可以提高 Linux 内核和内存的安全。整套补丁包含 17 个子项,不光
    03-08
  • 【译】Rust 的 Result 类型入门
    【译】Rust 的 Result 类型入门
    A Primer on Rust’s Result Type 译文原文链接:https://medium.com/@JoeKreydt/a-primer-on-rusts-result-type-66363cf18e6a原文作者:Joe Kreydt译文出处:https://github.com/suhanyujie/article-transfer-rs译者:suhanyujietips:水平有限,翻译不当之
    03-08
  • Rust实战系列-基本语法
    Rust实战系列-基本语法
    主要介绍 Rust 的语法、基本类型和数据结构,通过实现一个简单版 grep 命令行工具,来理解 Rust 独有的特性。本文是《Rust in action》学习总结系列的第二部分,更多内容请看已发布文章:一、Rust实战系列-Rust介绍“主要介绍 Rust 的语法、基本类型和数据结
    03-08
  • 全栈程序员的新玩具Rust(三)板条箱
    上次用到了stdout,这次我们来写一个更复杂一点的游戏rust的标准库叫做std,默认就会引入。这次我们要用到一个随机数函数,而随机数比较尴尬的一点是这玩意不在标准库中,我们要额外依赖一个库。很多编程方案都有自己的模块化库系统,rust也不例外,不过rust
    02-10
  • 全栈程序员的新玩具Rust(六)第一个WASM程序
    全栈程序员的新玩具Rust(六)第一个WASM程序
    先上代码https://gitee.com/lightsever/rust_study/tree/master/wasm_hello01webassembly就不用再赘述了,耳朵里面快磨出茧子来了。rustwasm是火狐自家的玩具,让我们来继续做实验,让rust飞起来吧。环境安装安装好rust环境之后仍然需要 一个 wasm 工具包carg
    02-10
  • 【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 =
    02-09
  • unity rust_移植到Unity 5 –不为人知的Rust之旅
    unity rust_移植到Unity 5 –不为人知的Rust之
    unity rust Facepunch Studio的创始人Garry Newman和他的团队一直在将Rust移植到Unity 5。 这篇博客文章讲述了尚未结束的《Rust之旅》。 你敢和我们一起旅行吗? (Facepunch Studio’s founder Garry Newman and his team have been porting Rust over to Uni
    02-09
点击排行