当前位置:网站首页>Microservice practice based on rustlang

Microservice practice based on rustlang

2022-06-13 03:15:00 flyweight_ visitor

be based on Rustlang Microservice practice of

The goal is

adopt 1 A simple scenario , Validation based on Rust The feasibility and challenges of building micro services .

framework

The code structure

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

Structure name example
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

 Insert picture description here

Practice practice

Brief description of development tool chain

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

Project creation

Create project

 by Rust Development and setup 1 A separate workspace , Name it rustlangws. Under the workspace are different microservices , Microservices are isolated by directories . Within each microservice , The different components are 1 Independent Rust project .
  1. Create a workspace
cd ~
mkdir -p Projects/rustlangws/
  1. Create an empty Github The project is the root project of the microservice project
https://github.com/AllenShi/rustlang-hacking-microservice-1
  1. Clone The root project of the microservice project
cd Projects/rustlangws/
git clone [email protected]:AllenShi/rustlang-hacking-microservice-1.git
  1. Create isolated directories for specific microservices in the root project
cd rustlang-hacking-microservice-1/
mkdir -p hacking_news
mkdir -p hacking_trading

stay hacking_news and hacking_trading Under the directory of 1 individual Cargo.toml, Describe the microservice components contained in the workspace of each specific microservice .

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. Create the required for the microservice Rust project ( Service components )
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

Project development

  1. open Visual Studio Code, Make sure Rust extension Is already installed . If not installed , Can pass View -> Command Palette … -> Extensions: Install Extensions …, And then the search Rust, Choose to install .

  2. stay VSC install Debugger Tools CodeLLDB(Native debugger based on LLDB.)
     Insert picture description here

  3. install Rust Test Lens
     Insert picture description here

The project build

  1. add to Rust Workspace to VSC The workspace . This can be done by File -> Add Folder to Workspace …
  • Add the root directory of the project to VSC The workspace rustlang-hacking-microservice-1
  • Add the of each microservice item independently rust Workspace to VSC The workspace : rustlang-hacking-microservice-1/hacking_news, rustlang-hacking-microservice-1/hacking_trading
     Insert picture description here
  1. ( Optional ) stay VSC Add... For each microservice item config

Click on the left side of the Run, Select the target micro service in the list that appears , increase config, Generate or update launch.json

 Insert picture description here

{
    // 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. Build project or debug run
cargo build

 Insert picture description here

  1. Check the built binaries
ls -alh target/debug/
total 55584
drwxr-xr-x   18 sjl  staff   576B 11 30 22:37 .
[email protected]   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

Project test

  1. Run test cases under the project
cd rustlangws/rustlang-hacking-microservice-1/hacking_news
cargo test

Project deployment

  1. Container-based deployment
  • establish Dockerfile
  • Create build scripts
cd rustlangws/rustlang-hacking-microservice-1/hacking_news
mkdir -p build
cross-compilation.sh
docker-build.sh
start-docker.sh
  1. be based on K8S Deployment of
  • Create deployed YAML spec
  • Create deployed scripts
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

Problem solving (Troubleshooting)

  1. Cargo build Command line execution stops , Show "Blocking waiting for file lock on the registry index"

Solution :

  1. Check if there are multiple processes compiling the same project at the same time . such as VSC Perform compilation in the background , The command line is also compiling . here , close VSC
    2) Clear file lock
    rm -rf ~/.cargo/registry/index/*
    rm -rf ~/.cargo/.package-cache
  1. Can't find crate postgres, Show “can’t find crate”

Solution

  1. stay crates.io Site search postgres, The display version is 0.18.1
  2. stay Cargo.toml Of dependencies The version displayed inside is lower than this version number , Update version number , Recompile it

Project source code

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

summary

Reference resources

  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
原网站

版权声明
本文为[flyweight_ visitor]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202280532045796.html