Skip to content

Commit ee0ca33

Browse files
committed
refactor(config): add provider config
1 parent 30b9c1a commit ee0ca33

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

config/src/config.rs

+20
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use lazy_static::lazy_static;
2121
use serde::{Deserialize, Serialize};
2222

2323
use super::protocol::ProtocolConfig;
24+
use super::provider::ProviderConfig;
2425
use super::service::ServiceConfig;
2526

2627
pub const DUBBO_CONFIG_PATH: &str = "./dubbo.yaml";
@@ -35,10 +36,14 @@ lazy_static! {
3536
#[derive(Debug, Default, Serialize, Deserialize, Clone)]
3637
pub struct RootConfig {
3738
pub name: String,
39+
40+
#[serde(skip_serializing, skip_deserializing)]
3841
pub service: HashMap<String, ServiceConfig>,
3942
pub protocols: HashMap<String, ProtocolConfig>,
4043
pub registries: HashMap<String, String>,
4144

45+
pub provider: ProviderConfig,
46+
4247
#[serde(skip_serializing, skip_deserializing)]
4348
pub data: HashMap<String, String>,
4449
}
@@ -65,6 +70,7 @@ impl RootConfig {
6570
service: HashMap::new(),
6671
protocols: HashMap::new(),
6772
registries: HashMap::new(),
73+
provider: ProviderConfig::new(),
6874
data: HashMap::new(),
6975
}
7076
}
@@ -96,6 +102,10 @@ impl RootConfig {
96102
}
97103

98104
pub fn test_config(&mut self) {
105+
let mut provider = ProviderConfig::new();
106+
provider.protocol_ids = vec!["triple".to_string()];
107+
provider.registry_ids = vec![];
108+
99109
let service_config = ServiceConfig::default()
100110
.group("test".to_string())
101111
.serializer("json".to_string())
@@ -127,6 +137,10 @@ impl RootConfig {
127137
.ip("0.0.0.0".to_string())
128138
.port("8889".to_string()),
129139
);
140+
141+
provider.services = self.service.clone();
142+
self.provider = provider.clone();
143+
println!("provider config: {:?}", provider);
130144
// 通过环境变量读取某个文件。加在到内存中
131145
self.data.insert(
132146
"dubbo.provider.url".to_string(),
@@ -172,6 +186,12 @@ pub trait Config {
172186
mod tests {
173187
use super::*;
174188

189+
#[test]
190+
fn test_config() {
191+
let mut r = RootConfig::new();
192+
r.test_config();
193+
}
194+
175195
#[test]
176196
fn test_load() {
177197
// case 1: read config yaml from default path

config/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
pub mod config;
1919
pub mod protocol;
20+
pub mod provider;
2021
pub mod service;
2122

2223
pub use config::*;

config/src/provider.rs

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
use std::collections::HashMap;
19+
20+
use serde::{Deserialize, Serialize};
21+
22+
use super::service::ServiceConfig;
23+
24+
#[derive(Debug, Default, Serialize, Deserialize, Clone)]
25+
pub struct ProviderConfig {
26+
pub registry_ids: Vec<String>,
27+
28+
pub protocol_ids: Vec<String>,
29+
30+
pub services: HashMap<String, ServiceConfig>,
31+
}
32+
33+
impl ProviderConfig {
34+
pub fn new() -> Self {
35+
ProviderConfig {
36+
registry_ids: vec![],
37+
protocol_ids: vec![],
38+
services: HashMap::new(),
39+
}
40+
}
41+
42+
pub fn with_registry_ids(mut self, registry_ids: Vec<String>) -> Self {
43+
self.registry_ids = registry_ids;
44+
self
45+
}
46+
47+
pub fn with_protocol_ids(mut self, protocol_ids: Vec<String>) -> Self {
48+
self.protocol_ids = protocol_ids;
49+
self
50+
}
51+
52+
pub fn with_services(mut self, services: HashMap<String, ServiceConfig>) -> Self {
53+
self.services = services;
54+
self
55+
}
56+
}

0 commit comments

Comments
 (0)