基于Rustlang的微服务实践

基于Rustlang的微服务实践

目标

通过1个简单的场景,验证基于Rust去构建微服务的可行性和挑战。

架构

代码结构

{Workspaces}/{top level workspace}/{micro-service workspace}/{micro-service component package}

结构名称实例
Workspacesrustlangws
Top level workspacerustlang-hacking-microservice-1
micro-service workspacehacking_news
micro-service component packagehacking_news_app
micro-service component packagehacking_news_domain
micro-service component packagehacking_news_infra
micro-service component packagehacking_news_migrations
micro-service workspacehacking_trading
micro-service component packagehacking_trading_app
micro-service component packagehacking_trading_domain
micro-service component packagehacking_trading_infra
micro-service component packagehacking_trading_migrations

在这里插入图片描述

实践演练

开发工具链简述

  1. Rustup and Cargo
  2. Visual studio code
  3. Github

项目创建

创建项目
为Rust开发设置1个独立的工作空间,命名为rustlangws。在工作空间之下是不同的微服务,微服务之间通过目录隔离。在每个微服务内部,不同的组件是1个独立的Rust项目。
  1. 创建工作空间
cd ~
mkdir -p Projects/rustlangws/
  1. 创建一个空的Github项目做为微服务项目的根项目
https://github.com/AllenShi/rustlang-hacking-microservice-1
  1. Clone微服务项目的根项目
cd Projects/rustlangws/
git clone git@github.com:AllenShi/rustlang-hacking-microservice-1.git
  1. 在根项目里面为具体的微服务创建隔离的目录
cd rustlang-hacking-microservice-1/
mkdir -p hacking_news
mkdir -p hacking_trading

在hacking_news和hacking_trading的目录下各自创建1个Cargo.toml,描述每个具体微服务的工作区里面包含的微服务组件。

hacking_news/Cargo.toml

[workspace]
members = [
    "hacking_news_app",
    "hacking-news_domain",
    "hacking_news_infra",
    "hacking_news_migrations"
]

hacking_trading/Cargo.toml

[workspace]
members = [
    "hacking_trading_app",
    "hacking_trading_domain",
    "hacking_trading_infra",
    "hacking_trading_migrations"
]
  1. 为微服务创建所需的个Rust项目(服务组件)
cd hacking_news
cargo new --bin hacking_news_app
cargo new --lib hacking_news_domain
cargo new --lib hacking_news_infra
cargo new --lib hacking_news_migrations
cd hacking_trading
cargo new --bin hacking_trading_app
cargo new --lib hacking_trading_domain
cargo new --lib hacking_trading_infra
cargo new --lib hacking_trading_migrations

项目开发

  1. 打开Visual Studio Code,确保Rust extension已经安装。如果没有安装,可以通过View -> Command Palette … -> Extensions: Install Extensions …,然后搜索Rust,选择安装。

  2. 在VSC安装Debugger工具CodeLLDB(Native debugger based on LLDB.)
    在这里插入图片描述

  3. 安装Rust Test Lens
    在这里插入图片描述

项目构建

  1. 添加Rust工作区到VSC的工作区。这可以通过 File -> Add Folder to Workspace …
  • 添加项目的根目录到VSC的工作区rustlang-hacking-microservice-1
  • 独立添加每个微服务项目的rust工作区到VSC的工作区: rustlang-hacking-microservice-1/hacking_news, rustlang-hacking-microservice-1/hacking_trading
    在这里插入图片描述
  1. (可选)在VSC为每个微服务项目添加config

点击左侧Run,在出现的列表里面选择目标微服务,增加config,生成或者更新launch.json

在这里插入图片描述

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "lldb",
            "request": "launch",
            "name": "Debug unit tests in library 'rust_playground'",
            "cargo": {
                "args": [
                    "test",
                    "--no-run",
                    "--lib",
                    "--package=rust_playground"
                ],
                "filter": {
                    "name": "rust_playground",
                    "kind": "lib"
                }
            },
            "args": [],
            "cwd": "${workspaceFolder}"
        },
        {
            "type": "lldb",
            "request": "launch",
            "name": "Debug executable 'rust_playground'",
            "cargo": {
                "args": [
                    "build",
                    "--bin=rust_playground",
                    "--package=rust_playground"
                ],
                "filter": {
                    "name": "rust_playground",
                    "kind": "bin"
                }
            },
            "args": [],
            "cwd": "${workspaceFolder}"
        }
    ]
}
  1. 构建项目或调试运行
cargo build

在这里插入图片描述

  1. 检查构建的二进制文件
ls -alh target/debug/
total 55584
drwxr-xr-x   18 sjl  staff   576B 11 30 22:37 .
drwxr-xr-x@   5 sjl  staff   160B 11 30 22:24 ..
-rw-r--r--    1 sjl  staff     0B 11 30 22:24 .cargo-lock
drwxr-xr-x  249 sjl  staff   7.8K 11 30 22:33 .fingerprint
drwxr-xr-x   60 sjl  staff   1.9K 11 30 22:33 build
drwxr-xr-x  569 sjl  staff    18K 11 30 22:37 deps
drwxr-xr-x    2 sjl  staff    64B 11 30 22:24 examples
-rwxr-xr-x    2 sjl  staff    21M 11 30 22:37 hacking_news_app
-rw-r--r--    1 sjl  staff   800B 11 30 22:37 hacking_news_app.d
lrwxr-xr-x    1 sjl  staff    26B 11 30 22:37 hacking_news_app.dSYM -> deps/hacking_news_app.dSYM
-rwxr-xr-x    2 sjl  staff   6.2M 11 30 22:35 hacking_news_migrations
-rw-r--r--    1 sjl  staff   348B 11 30 22:37 hacking_news_migrations.d
lrwxr-xr-x    1 sjl  staff    33B 11 30 22:35 hacking_news_migrations.dSYM -> deps/hacking_news_migrations.dSYM
drwxr-xr-x    6 sjl  staff   192B 11 30 22:35 incremental
-rw-r--r--    1 sjl  staff   225B 11 30 22:37 libhacking_news_domain.d
-rw-r--r--    2 sjl  staff   109K 11 30 22:35 libhacking_news_domain.rlib
-rw-r--r--    1 sjl  staff   330B 11 30 22:37 libhacking_news_infra.d
-rw-r--r--    2 sjl  staff   262K 11 30 22:35 libhacking_news_infra.rlib

项目测试

  1. 在项目下运行测试用例
cd rustlangws/rustlang-hacking-microservice-1/hacking_news
cargo test

项目部署

  1. 基于容器的部署
  • 创建Dockerfile
  • 创建构建脚本
cd rustlangws/rustlang-hacking-microservice-1/hacking_news
mkdir -p build
cross-compilation.sh
docker-build.sh
start-docker.sh
  1. 基于K8S的部署
  • 创建部署的YAML spec
  • 创建部署的脚本
cd rustlangws/rustlang-hacking-microservice-1/hacking_news
k8s-deployment.sh
mkdir -p build/spec
hacking_news_app_v1_deployment.yaml
hacking_news_app_v1_svc.yaml

问题解决(Troubleshooting)

  1. Cargo build命令行执行时停住,显示"Blocking waiting for file lock on the registry index"

解决方案:

  1. 检查是否有多个进程在同时编译同一个项目。比如VSC在后台执行编译,同时命令行也在执行编译。此时,关闭VSC
    2)清除文件锁
    rm -rf ~/.cargo/registry/index/*
    rm -rf ~/.cargo/.package-cache
  1. 找不到crate postgres,显示“can’t find crate”

解决方案

  1. 在crates.io站点搜索postgres,显示版本为0.18.1
  2. 在Cargo.toml的dependencies里面显示的版本低于此版本号,更新版本号,重新编译即可

项目源代码

https://github.com/AllenShi/rustlang-hacking-microservice-1

总结

参考

  1. Writing a Microservice in Rust
  2. Building a Microservice with Rust
  3. Crates IO
  4. Rust playground
  5. rust-musl-builder: Docker container for easily building static Rust binaries
  6. Visual Studio Code Debugging
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值