Skip to content

Commit 1f5c94e

Browse files
committed
feat:完善配置中心客户端逻辑
1 parent 7f325c5 commit 1f5c94e

File tree

10 files changed

+368
-67
lines changed

10 files changed

+368
-67
lines changed

examples/config.rs

Lines changed: 185 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,193 @@
1313
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
1414
// specific language governing permissions and limitations under the License.
1515

16-
use polaris_rust::core::model::error::PolarisError;
16+
use std::{collections::HashMap, sync::Arc, time::Duration};
17+
18+
use polaris_rust::{
19+
config::{
20+
api::{new_config_file_api_by_context, ConfigFileAPI},
21+
req::{
22+
CreateConfigFileRequest, PublishConfigFileRequest, UpdateConfigFileRequest,
23+
UpsertAndPublishConfigFileRequest, WatchConfigFileRequest,
24+
},
25+
},
26+
core::{
27+
context::SDKContext,
28+
model::{
29+
config::{ConfigFile, ConfigFileRelease},
30+
error::PolarisError,
31+
},
32+
},
33+
};
34+
use tracing::level_filters::LevelFilter;
1735

1836
#[tokio::main]
1937
async fn main() -> Result<(), PolarisError> {
38+
tracing_subscriber::fmt()
39+
// all spans/events with a level higher than TRACE (e.g, info, warn, etc.)
40+
// will be written to stdout.
41+
.with_thread_names(true)
42+
.with_file(true)
43+
.with_level(true)
44+
.with_line_number(true)
45+
.with_thread_ids(true)
46+
.with_max_level(LevelFilter::INFO)
47+
// sets this to be the default, global collector for this application.
48+
.init();
49+
50+
let start_time = std::time::Instant::now();
51+
52+
let sdk_context_ret = SDKContext::default();
53+
if sdk_context_ret.is_err() {
54+
tracing::error!(
55+
"create sdk context fail: {}",
56+
sdk_context_ret.err().unwrap()
57+
);
58+
return Err(PolarisError::new(
59+
polaris_rust::core::model::error::ErrorCode::UnknownServerError,
60+
"".to_string(),
61+
));
62+
}
63+
let arc_ctx = Arc::new(sdk_context_ret.unwrap());
64+
65+
let config_file_api_ret = new_config_file_api_by_context(arc_ctx.clone());
66+
if config_file_api_ret.is_err() {
67+
tracing::error!(
68+
"create config_file api fail: {}",
69+
config_file_api_ret.err().unwrap()
70+
);
71+
return Err(PolarisError::new(
72+
polaris_rust::core::model::error::ErrorCode::UnknownServerError,
73+
"".to_string(),
74+
));
75+
}
76+
77+
tracing::info!(
78+
"create config_file api client cost: {:?}",
79+
start_time.elapsed()
80+
);
81+
82+
let config_file_api = config_file_api_ret.unwrap();
83+
84+
let mut labels = HashMap::<String, String>::new();
85+
86+
labels.insert("rust".to_string(), "rust".to_string());
87+
88+
// 创建文件
89+
let ret = config_file_api
90+
.create_config_file(CreateConfigFileRequest {
91+
flow_id: uuid::Uuid::new_v4().to_string(),
92+
timeout: Duration::from_secs(1),
93+
file: ConfigFile {
94+
namespace: "rust".to_string(),
95+
group: "rust".to_string(),
96+
name: "rust.toml".to_string(),
97+
content: "test".to_string(),
98+
labels: labels.clone(),
99+
..Default::default()
100+
},
101+
})
102+
.await;
103+
104+
if ret.is_err() {
105+
tracing::error!("create config_file fail: {}", ret.err().unwrap());
106+
return Err(PolarisError::new(
107+
polaris_rust::core::model::error::ErrorCode::UnknownServerError,
108+
"".to_string(),
109+
));
110+
}
111+
112+
// 更新文件
113+
let ret = config_file_api
114+
.update_config_file(UpdateConfigFileRequest {
115+
flow_id: uuid::Uuid::new_v4().to_string(),
116+
timeout: Duration::from_secs(1),
117+
file: ConfigFile {
118+
namespace: "rust".to_string(),
119+
group: "rust".to_string(),
120+
name: "rust.toml".to_string(),
121+
content: "test".to_string(),
122+
labels: labels.clone(),
123+
..Default::default()
124+
},
125+
})
126+
.await;
127+
128+
if ret.is_err() {
129+
tracing::error!("update config_file fail: {}", ret.err().unwrap());
130+
return Err(PolarisError::new(
131+
polaris_rust::core::model::error::ErrorCode::UnknownServerError,
132+
"".to_string(),
133+
));
134+
}
135+
136+
// 发布文件
137+
let ret = config_file_api
138+
.publish_config_file(PublishConfigFileRequest {
139+
flow_id: uuid::Uuid::new_v4().to_string(),
140+
timeout: Duration::from_secs(1),
141+
config_file: ConfigFileRelease {
142+
namespace: "rust".to_string(),
143+
group: "rust".to_string(),
144+
file_name: "rust.toml".to_string(),
145+
release_name: "rust".to_string(),
146+
md5: "".to_string(),
147+
},
148+
})
149+
.await;
150+
151+
if ret.is_err() {
152+
tracing::error!("publish config_file fail: {}", ret.err().unwrap());
153+
return Err(PolarisError::new(
154+
polaris_rust::core::model::error::ErrorCode::UnknownServerError,
155+
"".to_string(),
156+
));
157+
}
158+
159+
// 文件变更订阅
160+
let _ = config_file_api
161+
.watch_config_file(WatchConfigFileRequest {
162+
namespace: "rust".to_string(),
163+
group: "rust".to_string(),
164+
file: "rust.toml".to_string(),
165+
call_back: Arc::new(|event| {
166+
tracing::info!("event: {:?}", event);
167+
}),
168+
})
169+
.await;
170+
171+
// 变更 10 次配置文件并发布
172+
for i in 0..10 {
173+
let ret = config_file_api
174+
.upsert_publish_config_file(UpsertAndPublishConfigFileRequest {
175+
flow_id: uuid::Uuid::new_v4().to_string(),
176+
timeout: Duration::from_secs(1),
177+
release_name: format!("rust-{}", i),
178+
md5: "".to_string(),
179+
config_file: ConfigFile {
180+
namespace: "rust".to_string(),
181+
group: "rust".to_string(),
182+
name: "rust.toml".to_string(),
183+
content: format!("test-{}", i),
184+
labels: labels.clone(),
185+
..Default::default()
186+
},
187+
})
188+
.await;
189+
190+
if ret.is_err() {
191+
tracing::error!(
192+
"upsert and publish config_file fail: {}",
193+
ret.err().unwrap()
194+
);
195+
return Err(PolarisError::new(
196+
polaris_rust::core::model::error::ErrorCode::UnknownServerError,
197+
"".to_string(),
198+
));
199+
}
200+
201+
std::thread::sleep(Duration::from_secs(10));
202+
}
203+
20204
Ok(())
21205
}

examples/discover.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,10 @@ async fn main() -> Result<(), PolarisError> {
8080
let provider = provider_ret.unwrap();
8181
let consumer = consumer_ret.unwrap();
8282

83-
tracing::info!("create provider cost: {:?}", start_time.elapsed());
83+
tracing::info!(
84+
"create discovery api client cost: {:?}",
85+
start_time.elapsed()
86+
);
8487
let metadata = HashMap::new();
8588

8689
let req = InstanceRegisterRequest {

src/config/api.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ use crate::{
2424
};
2525

2626
use super::req::{
27-
CreateConfigFileRequest, DeleteConfigFileRequest, GetConfigFileRequest, GetConfigGroupRequest,
28-
PublishConfigFileRequest, UpdateConfigFileRequest, WatchConfigFileRequest,
27+
CreateConfigFileRequest, GetConfigFileRequest, GetConfigGroupRequest, PublishConfigFileRequest,
28+
UpdateConfigFileRequest, UpsertAndPublishConfigFileRequest, WatchConfigFileRequest,
2929
WatchConfigFileResponse, WatchConfigGroupRequest, WatchConfigGroupResponse,
3030
};
3131

@@ -66,6 +66,11 @@ where
6666
req: PublishConfigFileRequest,
6767
) -> Result<bool, PolarisError>;
6868

69+
async fn upsert_publish_config_file(
70+
&self,
71+
req: UpsertAndPublishConfigFileRequest,
72+
) -> Result<bool, PolarisError>;
73+
6974
async fn watch_config_file(
7075
&self,
7176
req: WatchConfigFileRequest,

src/config/default.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ use super::{
3737
api::{ConfigFileAPI, ConfigGroupAPI},
3838
req::{
3939
self, CreateConfigFileRequest, GetConfigFileRequest, GetConfigGroupRequest,
40-
PublishConfigFileRequest, UpdateConfigFileRequest, WatchConfigFileRequest,
41-
WatchConfigFileResponse, WatchConfigGroupRequest, WatchConfigGroupResponse,
40+
PublishConfigFileRequest, UpdateConfigFileRequest, UpsertAndPublishConfigFileRequest,
41+
WatchConfigFileRequest, WatchConfigFileResponse, WatchConfigGroupRequest,
42+
WatchConfigGroupResponse,
4243
},
4344
};
4445

@@ -132,6 +133,16 @@ impl ConfigFileAPI for DefaultConfigFileAPI {
132133
self.context.get_engine().publish_config_file(req).await
133134
}
134135

136+
async fn upsert_publish_config_file(
137+
&self,
138+
req: UpsertAndPublishConfigFileRequest,
139+
) -> Result<bool, PolarisError> {
140+
self.context
141+
.get_engine()
142+
.upsert_publish_config_file(req)
143+
.await
144+
}
145+
135146
async fn watch_config_file(
136147
&self,
137148
req: WatchConfigFileRequest,

src/config/req.rs

Lines changed: 19 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,11 @@
1313
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
1414
// specific language governing permissions and limitations under the License.
1515

16-
use std::{collections::HashMap, sync::Arc, time::Duration};
16+
use std::{sync::Arc, time::Duration};
1717

18-
use crate::core::model::{
19-
config::{ConfigFile, ConfigFileChangeEvent, ConfigFileRequest, ConfigReleaseRequest},
20-
naming::ServiceInstancesChangeEvent,
21-
pb::lib::ConfigFileRelease,
18+
use crate::core::model::config::{
19+
ConfigFile, ConfigFileChangeEvent, ConfigFileRelease, ConfigFileRequest, ConfigPublishRequest,
20+
ConfigReleaseRequest,
2221
};
2322

2423
#[derive(Clone, Debug)]
@@ -70,71 +69,45 @@ impl UpdateConfigFileRequest {
7069
}
7170

7271
#[derive(Clone, Debug)]
73-
pub struct DeleteConfigFileRequest {
72+
pub struct PublishConfigFileRequest {
7473
pub flow_id: String,
75-
pub namespace: String,
76-
pub group: String,
77-
pub file: String,
7874
pub timeout: Duration,
75+
pub config_file: ConfigFileRelease,
7976
}
8077

81-
impl DeleteConfigFileRequest {
82-
pub fn to_config_request(&self) -> ConfigFileRequest {
78+
impl PublishConfigFileRequest {
79+
pub fn to_config_request(&self) -> ConfigReleaseRequest {
8380
let mut flow_id = self.flow_id.clone();
8481
if flow_id.is_empty() {
8582
flow_id = uuid::Uuid::new_v4().to_string();
8683
}
87-
let mut file = ConfigFile::default();
88-
file.namespace = self.namespace.clone();
89-
file.group = self.group.clone();
90-
file.name = self.file.clone();
91-
ConfigFileRequest {
84+
ConfigReleaseRequest {
9285
flow_id: flow_id,
93-
config_file: file,
86+
config_file: self.config_file.clone(),
9487
}
9588
}
9689
}
9790

9891
#[derive(Clone, Debug)]
99-
pub struct PublishConfigFileRequest {
92+
pub struct UpsertAndPublishConfigFileRequest {
10093
pub flow_id: String,
101-
pub namespace: String,
102-
pub group: String,
103-
pub file: String,
94+
pub timeout: Duration,
10495
pub release_name: String,
10596
pub md5: String,
106-
pub timeout: Duration,
97+
pub config_file: ConfigFile,
10798
}
10899

109-
impl PublishConfigFileRequest {
110-
pub fn to_config_request(&self) -> ConfigReleaseRequest {
100+
impl UpsertAndPublishConfigFileRequest {
101+
pub fn to_config_request(&self) -> ConfigPublishRequest {
111102
let mut flow_id = self.flow_id.clone();
112103
if flow_id.is_empty() {
113104
flow_id = uuid::Uuid::new_v4().to_string();
114105
}
115-
ConfigReleaseRequest {
106+
ConfigPublishRequest {
116107
flow_id: flow_id,
117-
config_file: ConfigFileRelease {
118-
id: None,
119-
name: Some(self.release_name.clone()),
120-
namespace: Some(self.namespace.clone()),
121-
group: Some(self.group.clone()),
122-
file_name: Some(self.file.clone()),
123-
content: None,
124-
comment: None,
125-
md5: Some(self.md5.clone()),
126-
version: None,
127-
create_time: None,
128-
create_by: None,
129-
modify_time: None,
130-
modify_by: None,
131-
tags: vec![],
132-
active: None,
133-
format: None,
134-
release_description: None,
135-
release_type: None,
136-
beta_labels: vec![],
137-
},
108+
md5: self.md5.clone(),
109+
release_name: self.release_name.clone(),
110+
config_file: self.config_file.clone(),
138111
}
139112
}
140113
}

src/core/engine.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use tokio::sync::RwLock;
2121

2222
use crate::config::req::{
2323
CreateConfigFileRequest, GetConfigFileRequest, PublishConfigFileRequest,
24-
UpdateConfigFileRequest,
24+
UpdateConfigFileRequest, UpsertAndPublishConfigFileRequest,
2525
};
2626
use crate::core::config::config::Configuration;
2727
use crate::core::model::cache::{EventType, ResourceEventKey};
@@ -339,6 +339,22 @@ impl Engine {
339339
};
340340
}
341341

342+
/// upsert_publish_config_file 更新或发布配置文件
343+
pub async fn upsert_publish_config_file(
344+
&self,
345+
req: UpsertAndPublishConfigFileRequest,
346+
) -> Result<bool, PolarisError> {
347+
let config_file = req.to_config_request();
348+
349+
let connector = self.server_connector.clone();
350+
let rsp = connector.upsert_publish_config_file(config_file).await;
351+
352+
return match rsp {
353+
Ok(ret_rsp) => Ok(ret_rsp),
354+
Err(err) => Err(err),
355+
};
356+
}
357+
342358
pub async fn lookup_loadbalancer(&self, name: &str) -> Option<Arc<Box<dyn LoadBalancer>>> {
343359
let lb = self.load_balancer.read().await;
344360
lb.get(name).map(|lb| lb.clone())

0 commit comments

Comments
 (0)