Skip to content

Commit ac0a18c

Browse files
committed
Update examples
1 parent d8488bb commit ac0a18c

5 files changed

Lines changed: 39 additions & 44 deletions

File tree

conreg-client/Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,8 @@ path = "examples/feign_basic.rs"
4242

4343
[[example]]
4444
name = "client_register_with_yaml"
45-
path = "examples/client_register_with_yaml.rs"
45+
path = "examples/client_register_with_yaml.rs"
46+
47+
[[example]]
48+
name = "test_server"
49+
path = "examples/test_server.rs"

conreg-client/examples/bootstrap.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
conreg:
22
# 服务ID
3-
service-id: test
3+
service-id: test-server
44
# 客户端配置
55
client:
66
# 监听地址
77
address: 127.0.0.1
88
# 端口
9-
port: 8000
9+
port: 8080
1010
# 配置中心
1111
config:
1212
# 配置中心地址

conreg-client/examples/client_register.rs

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,10 @@ use conreg_client::conf::{
22
ClientConfigBuilder, ConRegConfigBuilder, ConfigConfigBuilder, DiscoveryConfigBuilder,
33
};
44
use conreg_client::{AppConfig, init_with};
5-
use rocket::data::{ByteUnit, Limits};
6-
use rocket::{Config, get};
7-
use std::net::IpAddr;
85

96
/// 将一个HTTP服务注册到 `Conreg Server`。
107
///
11-
/// 这里使用 `Rocket` 框架作为示例,你可以使用任何你喜欢的框架,例如 `Actix` 等。
8+
/// 这里使用 `Rocket` 框架作为示例,你可以使用任何你喜欢的框架,例如 `Actix`, `Axum` 等。
129
///
1310
/// # Run example
1411
/// ```
@@ -67,19 +64,6 @@ async fn main() {
6764
}
6865
});
6966

70-
// 启动 Rocket 服务器
71-
rocket::build()
72-
.configure(Config {
73-
port: 8080,
74-
..Config::debug_default()
75-
})
76-
.mount("/", rocket::routes![test])
77-
.launch()
78-
.await
79-
.unwrap();
67+
h.await.unwrap();
8068
}
8169

82-
#[get("/hello")]
83-
fn test() -> &'static str {
84-
"world"
85-
}
Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11
use conreg_client::AppConfig;
2-
use conreg_client::conf::{
3-
ClientConfigBuilder, ConRegConfigBuilder, ConfigConfigBuilder, DiscoveryConfigBuilder,
4-
};
5-
use rocket::data::{ByteUnit, Limits};
6-
use rocket::{Config, get};
7-
use std::net::IpAddr;
82

93
/// 通过yaml配置文件注册客户端。
104
///
11-
/// 这里使用 `Rocket` 框架作为示例,你可以使用任何你喜欢的框架,例如 `Actix` 等。
5+
/// 这里使用 `Rocket` 框架作为示例,你可以使用任何你喜欢的框架,例如 `Actix`, `Axum` 等。
126
///
137
/// # Run example
148
/// ```
@@ -20,7 +14,7 @@ async fn main() {
2014
conreg_client::init_from_file("./conreg-client/examples/bootstrap.yaml").await;
2115

2216
// 从配置中心获取配置
23-
tokio::spawn(async move {
17+
let h = tokio::spawn(async move {
2418
loop {
2519
// 获取配置
2620
let name = AppConfig::get::<String>("name");
@@ -31,19 +25,5 @@ async fn main() {
3125
}
3226
});
3327

34-
// 启动 Rocket 服务器
35-
rocket::build()
36-
.configure(Config {
37-
port: 8080,
38-
..Config::debug_default()
39-
})
40-
.mount("/", rocket::routes![test])
41-
.launch()
42-
.await
43-
.unwrap();
44-
}
45-
46-
#[get("/hello")]
47-
fn test() -> &'static str {
48-
"world"
28+
h.await.unwrap();
4929
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use rocket::{Config, get};
2+
use conreg_client::{init_from_file};
3+
4+
/// # Run example
5+
/// ```
6+
/// cargo run --example test_server -F tracing -- --nocapture
7+
/// ```
8+
#[tokio::main]
9+
async fn main() {
10+
init_from_file("./conreg-client/examples/bootstrap.yaml").await;
11+
12+
rocket::build()
13+
.configure(Config {
14+
port: 8080,
15+
cli_colors: false,
16+
..Config::debug_default()
17+
})
18+
.mount("/", rocket::routes![test])
19+
.launch()
20+
.await
21+
.unwrap();
22+
}
23+
24+
#[get("/hello")]
25+
fn test() -> &'static str {
26+
"world"
27+
}

0 commit comments

Comments
 (0)