Skip to content

Commit

Permalink
Change: all async API; If you need sync, maybe `futures::executor::bl…
Browse files Browse the repository at this point in the history
…ock_on(future_fn)` (#230)

* Change: all async API; If you need sync, maybe `futures::executor::block_on(future_fn)`

* test: fix async api in test
  • Loading branch information
CherishCai authored May 19, 2024
1 parent 09c5341 commit 9213793
Show file tree
Hide file tree
Showing 11 changed files with 296 additions and 616 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# 变更日志 | Change log

### 0.4.0

- 破坏性变更: 使 NamingService 和 ConfigService impl Send + Sync
- 破坏性变更: 默认 async,去掉 sync api,需要的话建议 `futures::executor::block_on(future_fn)`

---

- Change: make NamingService and ConfigService Send + Sync
- Change: all async API; If you need sync, maybe `futures::executor::block_on(future_fn)`

### 0.3.6

- 文档: 补充说明 `NamingService``ConfigService` 需要全局的生命周期
Expand Down
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ config = []
naming = []
tls = ["reqwest/default-tls"]
auth-by-http = ["reqwest"]
async = []

[dependencies]
nacos-macro = { version = "0.1.0", path = "nacos-macro" }
Expand Down
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ Add the dependency in `Cargo.toml`:

```toml
[dependencies]
# If you need async API, which can be enabled via `features = ["async"]`
nacos-sdk = { version = "0.3", features = ["default"] }
# If you need sync API, maybe `futures::executor::block_on(future_fn)`
nacos-sdk = { version = "0.4", features = ["default"] }
```

### Usage of Config
Expand All @@ -32,7 +32,7 @@ nacos-sdk = { version = "0.3", features = ["default"] }
// 因为它内部会初始化与服务端的长链接,后续的数据交互及变更订阅,都是实时地通过长链接告知客户端的。
let config_service = ConfigServiceBuilder::new(
ClientProps::new()
.server_addr("0.0.0.0:8848")
.server_addr("127.0.0.1:8848")
// Attention! "public" is "", it is recommended to customize the namespace with clear meaning.
.namespace("")
.app_name("simple_app"),
Expand All @@ -43,7 +43,7 @@ nacos-sdk = { version = "0.3", features = ["default"] }
.build()?;

// example get a config
let config_resp = config_service.get_config("todo-data-id".to_string(), "todo-group".to_string());
let config_resp = config_service.get_config("todo-data-id".to_string(), "todo-group".to_string()).await;
match config_resp {
Ok(config_resp) => tracing::info!("get the config {}", config_resp),
Err(err) => tracing::error!("get the config {:?}", err),
Expand All @@ -62,7 +62,7 @@ nacos-sdk = { version = "0.3", features = ["default"] }
"todo-data-id".to_string(),
"todo-group".to_string(),
Arc::new(ExampleConfigChangeListener {}),
);
).await;
match _listen {
Ok(_) => tracing::info!("listening the config success"),
Err(err) => tracing::error!("listen config error {:?}", err),
Expand All @@ -75,7 +75,7 @@ nacos-sdk = { version = "0.3", features = ["default"] }
// 因为它内部会初始化与服务端的长链接,后续的数据交互及变更订阅,都是实时地通过长链接告知客户端的。
let naming_service = NamingServiceBuilder::new(
ClientProps::new()
.server_addr("0.0.0.0:8848")
.server_addr("127.0.0.1:8848")
// Attention! "public" is "", it is recommended to customize the namespace with clear meaning.
.namespace("")
.app_name("simple_app"),
Expand All @@ -100,7 +100,7 @@ nacos-sdk = { version = "0.3", features = ["default"] }
Some(constants::DEFAULT_GROUP.to_string()),
Vec::default(),
subscriber,
);
).await;

// example naming register instances
let service_instance1 = ServiceInstance {
Expand All @@ -112,7 +112,7 @@ nacos-sdk = { version = "0.3", features = ["default"] }
"test-service".to_string(),
Some(constants::DEFAULT_GROUP.to_string()),
vec![service_instance1],
);
).await;
```

### Props
Expand Down
60 changes: 34 additions & 26 deletions examples/simple_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ use nacos_sdk::api::naming::{
};
use nacos_sdk::api::props::ClientProps;

const NACOS_ADDRESS: &str = "127.0.0.1:8848";

/// enable https auth run with command:
/// cargo run --example simple_app --features default,tls
#[tokio::main]
Expand All @@ -23,7 +21,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
.init();

let client_props = ClientProps::new()
.server_addr(NACOS_ADDRESS)
.server_addr(constants::DEFAULT_SERVER_ADDR)
// .remote_grpc_port(9838)
// Attention! "public" is "", it is recommended to customize the namespace with clear meaning.
.namespace("")
Expand All @@ -38,17 +36,21 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let config_service = ConfigServiceBuilder::new(client_props.clone())
.enable_auth_plugin_http() // TODO You can choose not to enable auth
.build()?;
let config_resp = config_service.get_config("todo-data-id".to_string(), "LOVE".to_string());
let config_resp = config_service
.get_config("todo-data-id".to_string(), "LOVE".to_string())
.await;
match config_resp {
Ok(config_resp) => tracing::info!("get the config {}", config_resp),
Err(err) => tracing::error!("get the config {:?}", err),
}

let _listen = config_service.add_listener(
"todo-data-id".to_string(),
"LOVE".to_string(),
std::sync::Arc::new(SimpleConfigChangeListener {}),
);
let _listen = config_service
.add_listener(
"todo-data-id".to_string(),
"LOVE".to_string(),
std::sync::Arc::new(SimpleConfigChangeListener {}),
)
.await;
match _listen {
Ok(_) => tracing::info!("listening the config success"),
Err(err) => tracing::error!("listen config error {:?}", err),
Expand All @@ -62,31 +64,37 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
.build()?;

let listener = std::sync::Arc::new(SimpleInstanceChangeListener);
let _subscribe_ret = naming_service.subscribe(
"test-service".to_string(),
Some(constants::DEFAULT_GROUP.to_string()),
Vec::default(),
listener,
);
let _subscribe_ret = naming_service
.subscribe(
"test-service".to_string(),
Some(constants::DEFAULT_GROUP.to_string()),
Vec::default(),
listener,
)
.await;

let service_instance1 = ServiceInstance {
ip: "127.0.0.1".to_string(),
port: 9090,
..Default::default()
};
let _register_instance_ret = naming_service.batch_register_instance(
"test-service".to_string(),
Some(constants::DEFAULT_GROUP.to_string()),
vec![service_instance1],
);
let _register_instance_ret = naming_service
.batch_register_instance(
"test-service".to_string(),
Some(constants::DEFAULT_GROUP.to_string()),
vec![service_instance1],
)
.await;
tokio::time::sleep(tokio::time::Duration::from_millis(666)).await;

let instances_ret = naming_service.get_all_instances(
"test-service".to_string(),
Some(constants::DEFAULT_GROUP.to_string()),
Vec::default(),
false,
);
let instances_ret = naming_service
.get_all_instances(
"test-service".to_string(),
Some(constants::DEFAULT_GROUP.to_string()),
Vec::default(),
false,
)
.await;
match instances_ret {
Ok(instances) => tracing::info!("get_all_instances {:?}", instances),
Err(err) => tracing::error!("naming get_all_instances error {:?}", err),
Expand Down
Loading

0 comments on commit 9213793

Please sign in to comment.