安装Spin
现在你已经熟悉了Spin和微服务,是时候将Spin二进制文件安装到系统中了。二进制文件允许你设置一个项目,并托管一个本地服务器,并在所有主要的操作系统上运行,包括Windows, Linux和MacOS。你还可以选择从源代码构建二进制文件,或者使用cargo将其安装到系统中。git clone https://github.com/fermyon/spin构建:
cd spin && make build验证构建的二进制文件:
./target/release/spin --help使用Cargo安装
git clone https://github.com/fermyon/spin -b v0.9.0进入spin目录:
cd spin为Rust安装wasm32-wasi target:
rustup target add wasm32-wasi编译并安装spin到你的系统:
cargo install --locked --path .验证安装是否成功:
spin --help设置项目文件夹
spin templates list如果系统中没有安装模板,执行如下命令:
spin templates install --git https://github.com/fermyon/spin --update上面的命令安装Spin GitHub repo中的所有可用模板。模板安装完成后,你可以重新检查列表,查看已安装的模板。
$ spin new Pick a template to start your application with: > http-c (HTTP request handler using C and the Zig toolchain) http-empty (HTTP application with no components) http-go (HTTP request handler using (Tiny)Go) http-grain (HTTP request handler using Grain) http-php (HTTP request handler using PHP) http-rust (HTTP request handler using Rust) http-swift (HTTP request handler using SwiftWasm) http-zig (HTTP request handler using Zig) redirect (Redirects a HTTP route) redis-go (Redis message handler using (Tiny)Go) redis-rust (Redis message handler using Rust) static-fileserver (Serves static files from an asset directory)
该命令提示你输入要使用的模板、项目名称、项目描述、HTTP路径。对于模板,选择http-rust。对于项目名称,写任何你想要的名称。对于描述、HTTP路径,可以按Enter来使用它们的默认值。
rustup target add wasm32-wasi # Install the WebAssembly target spin build # Build the project命令完成后,生成的WebAssembly组件位于target/wasm32-wasi/release文件夹中。spin允许在你的系统上托管WebAssembly组件,使用以下命令:
spin up它在http://localhost:3000/中托管微服务应用程序。一旦项目开始运行,你可以用curl命令测试它:
$ curl -i localhost:3000 HTTP/1.1 200 OK foo: bar content-length: 14 Hello, Fermyon在下面的部分中,我们将介绍Spin的关键组件spin.toml和项目中的lib.rs文件。
spin_version = "1" authors = ["Username <youremail@example.com>"] description = "" name = "project-name" trigger = { type = "http", base = "/" } version = "0.1.0" [[component]] id = "project-name" source = "target/wasm32-wasi/release/spin_test.wasm" allowed_http_hosts = [] [component.trigger] route = "/..." [component.build] command = "cargo build --target wasm32-wasi --release"第5行的trigger变量,用于配置微服务的性质。这个微服务为外部应用程序提供了一个HTTP接口,以便与微服务进行交互。
use anyhow::Result; use spin_sdk::{ http::{Request, Response}, http_component, }; /// A simple Spin HTTP component. #[http_component] fn handle_spin_test(req: Request) -> Result<Response> { println!("{:?}", req.headers()); Ok(http::Response::builder() .status(200) .header("foo", "bar") .body(Some("Hello, Fermyon".into()))?) }#[http_component]宏表示handle_spin_test函数是一个HTTP组件。每当向微服务发送HTTP请求时,Spin都会运行handle_spin_test函数,微服务在请求结束时返回handle_spin_test的结果。
use anyhow::Result; use spin_sdk::{ http::{Request, Response}, http_component, }; #[http_component] fn cat_facts(_req: Request) -> Result<Response> { // 堆代码 duidaima.com // fetch fact from the API let mut res = spin_sdk::http::send( http::Request::builder() .method("GET") .uri("https://catfact.ninja/fact") .body(None)?, )?; // Add "Server" key into the header res.headers_mut() .insert(http::header::SERVER, "spin/0.1.0".try_into()?); // Send response to the client Ok(res) }spin库提供了发送HTTP请求的方法。在这个微服务中,我们使用该方法从“https://catfact.ninja/fact”的API中获取数据。该方法生成一个响应,cat_facts函数可以将其作为微服务的响应对象返回。
allowed_http_hosts = ["catfact.ninja"]如果你不这样做,当你运行项目时,你会得到一个HttpError:: destinationnotalallowed错误。
{"fact":"Cats have 32 muscles that control the outer ear (humans have only 6). A cat can independently rotate its ears 180 degrees.","length":122}