Skip to content

Commit 1d78481

Browse files
committed
Rft(dubbo): impl memory_protocol, update registry config
1 parent 94c51a1 commit 1d78481

File tree

7 files changed

+100
-5
lines changed

7 files changed

+100
-5
lines changed

config/src/config.rs

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ pub struct RootConfig {
3737
pub name: String,
3838
pub service: HashMap<String, ServiceConfig>,
3939
pub protocols: HashMap<String, ProtocolConfig>,
40+
pub registries: HashMap<String, String>,
4041

4142
#[serde(skip_serializing, skip_deserializing)]
4243
pub data: HashMap<String, String>,
@@ -63,6 +64,7 @@ impl RootConfig {
6364
name: "dubbo".to_string(),
6465
service: HashMap::new(),
6566
protocols: HashMap::new(),
67+
registries: HashMap::new(),
6668
data: HashMap::new(),
6769
}
6870
}

dubbo/src/common/consts.rs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
pub const REGISTRY_PROTOCOL: &str = "registry";

dubbo/src/common/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@
1515
* limitations under the License.
1616
*/
1717

18+
pub mod consts;
1819
pub mod url;

dubbo/src/framework.rs

+8
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ use dubbo_config::{get_global_config, RootConfig};
3232
#[derive(Default)]
3333
pub struct Dubbo {
3434
protocols: HashMap<String, Vec<Url>>,
35+
registries: HashMap<String, Url>,
3536
config: Option<RootConfig>,
3637
}
3738

@@ -40,6 +41,7 @@ impl Dubbo {
4041
tracing_subscriber::fmt::init();
4142
Self {
4243
protocols: HashMap::new(),
44+
registries: HashMap::new(),
4345
config: None,
4446
}
4547
}
@@ -56,6 +58,12 @@ impl Dubbo {
5658

5759
let conf = self.config.as_ref().unwrap();
5860
tracing::debug!("global conf: {:?}", conf);
61+
62+
for (name, url) in conf.registries.iter() {
63+
self.registries
64+
.insert(name.to_string(), Url::from_url(url).unwrap());
65+
}
66+
5967
for (_, c) in conf.service.iter() {
6068
let u = if c.protocol_configs.is_empty() {
6169
let protocol = match conf.protocols.get(&c.protocol) {

dubbo/src/protocol/mod.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,16 @@ pub trait Invoker<ReqBody> {
5151
fn call(&mut self, req: ReqBody) -> Self::Future;
5252
}
5353

54-
pub type BoxExporter = Box<dyn Exporter>;
54+
pub type BoxExporter = Box<dyn Exporter + Send + Sync>;
55+
pub type BoxInvoker = Box<
56+
dyn Invoker<
57+
http::Request<hyper::Body>,
58+
Response = http::Response<crate::BoxBody>,
59+
Error = crate::Error,
60+
Future = crate::BoxFuture<http::Response<crate::BoxBody>, crate::Error>,
61+
> + Send
62+
+ Sync,
63+
>;
5564

5665
pub struct WrapperInvoker<T>(T);
5766

dubbo/src/registry/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,5 @@ pub struct ServiceEvent {
4242
service: Url,
4343
}
4444

45-
pub type BoxRegistry = Box<dyn Registry<NotifyListener = memory_registry::MemoryNotifyListener>>;
45+
pub type BoxRegistry =
46+
Box<dyn Registry<NotifyListener = memory_registry::MemoryNotifyListener> + Send + Sync>;

dubbo/src/registry/protocol.rs

+59-3
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,75 @@
1616
*/
1717

1818
use std::collections::HashMap;
19+
use std::sync::Arc;
1920

21+
use super::memory_registry::MemoryRegistry;
2022
use super::BoxRegistry;
23+
use crate::codegen::TripleInvoker;
24+
use crate::common::consts;
2125
use crate::common::url::Url;
26+
use crate::protocol::triple::triple_exporter::TripleExporter;
27+
use crate::protocol::BoxExporter;
28+
use crate::protocol::BoxInvoker;
29+
use crate::protocol::Protocol;
2230

31+
#[derive(Clone, Default)]
2332
pub struct RegistryProtocol {
24-
registries: HashMap<String, BoxRegistry>,
33+
// registerAddr: Registry
34+
registries: Arc<HashMap<String, BoxRegistry>>,
35+
// providerUrl: Exporter
36+
exporters: Arc<HashMap<String, BoxExporter>>,
2537
}
2638

2739
impl RegistryProtocol {
40+
pub fn new() -> Self {
41+
RegistryProtocol {
42+
registries: Arc::new(HashMap::new()),
43+
exporters: Arc::new(HashMap::new()),
44+
}
45+
}
46+
47+
pub fn get_registry(&self, url: Url) -> BoxRegistry {
48+
// self.registries.clone().insert(url.location.clone(), Box::new(MemoryRegistry::default()));
49+
50+
// *(self.registries.get(&url.location).unwrap())
51+
Box::new(MemoryRegistry::default())
52+
}
53+
}
54+
55+
#[async_trait::async_trait]
56+
impl Protocol for RegistryProtocol {
57+
type Invoker = BoxInvoker;
58+
2859
fn destroy(&self) {
2960
todo!()
3061
}
3162

32-
async fn export(self, url: Url) {}
33-
async fn refer(self, url: Url) {}
63+
async fn export(self, url: Url) -> BoxExporter {
64+
// getProviderUrl
65+
// getRegisterUrl
66+
// init Exporter based on provider_url
67+
// server registry based on register_url
68+
// start server health check
69+
Box::new(TripleExporter::new())
70+
}
71+
async fn refer(self, url: Url) -> Self::Invoker {
72+
// getRegisterUrl
73+
// get Registry from registry_url
74+
// init directory based on registry_url and Registry
75+
// init Cluster based on Directory generates Invoker
76+
Box::new(TripleInvoker::new(url))
77+
}
78+
}
79+
80+
fn get_registry_url(mut url: Url) -> Url {
81+
if url.protocol == consts::REGISTRY_PROTOCOL {
82+
url.protocol = url.get_param("registry".to_string()).unwrap();
83+
}
84+
85+
url
86+
}
87+
88+
fn get_provider_url(url: Url) -> Url {
89+
url
3490
}

0 commit comments

Comments
 (0)