Skip to content

Commit 253c2f1

Browse files
daedalus2022qunwei
and
qunwei
authored
通过target_os使用Unix套接字连接器通信 (apache#102)
* 使用Unix套接字连接器通信demo * 通过target_os使用Unix套接字连接器通信 Co-authored-by: qunwei <[email protected]>
1 parent d960abc commit 253c2f1

File tree

6 files changed

+18
-7
lines changed

6 files changed

+18
-7
lines changed

dubbo/src/triple/client/builder.rs

+7
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ impl ClientBuilder {
6060
}
6161
}
6262

63+
pub fn with_connector(self, connector: &'static str) -> Self {
64+
Self {
65+
connector: connector,
66+
..self
67+
}
68+
}
69+
6370
pub fn connect(self) -> ClientBoxService {
6471
let builder = ServiceBuilder::new();
6572
let timeout = self.timeout.unwrap_or(5);

dubbo/src/triple/transport/connection.rs

+1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ where
8787
let uri = self.host.clone();
8888
let fut = async move {
8989
let mut con = connector.call(uri).await.unwrap();
90+
9091
con.call(req)
9192
.await
9293
.map_err(|err| err.into())

dubbo/src/triple/transport/connector/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717

1818
pub mod http_connector;
19-
#[cfg(target_os = "unix")]
19+
#[cfg(any(target_os = "macos", target_os = "unix"))]
2020
pub mod unix_connector;
2121

2222
use hyper::Uri;
@@ -79,7 +79,7 @@ pub fn get_connector(connector: &'static str) -> BoxCloneService<Uri, BoxIO, cra
7979
let c = http_connector::HttpConnector::new();
8080
BoxCloneService::new(Connector::new(c))
8181
}
82-
#[cfg(target_os = "unix")]
82+
#[cfg(any(target_os = "macos", target_os = "unix"))]
8383
"unix" => {
8484
let c = unix_connector::UnixConnector::new();
8585
BoxCloneService::new(Connector::new(c))

dubbo/src/triple/transport/listener/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717

1818
pub mod tcp_listener;
19-
#[cfg(target_os = "unix")]
19+
#[cfg(any(target_os = "macos", target_os = "unix"))]
2020
pub mod unix_listener;
2121

2222
use std::net::SocketAddr;
@@ -64,7 +64,7 @@ impl<T: Listener> Listener for WrappedListener<T> {
6464
pub async fn get_listener(name: String, addr: SocketAddr) -> Result<BoxListener, crate::Error> {
6565
match name.as_str() {
6666
"tcp" => Ok(TcpListener::bind(addr).await?.boxed()),
67-
#[cfg(target_os = "unix")]
67+
#[cfg(any(target_os = "macos", target_os = "unix"))]
6868
"unix" => Ok(unix_listener::UnixListener::bind(addr).await?.boxed()),
6969
_ => {
7070
tracing::warn!("no support listener: {:?}", name);

examples/echo/src/echo/client.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ impl Filter for FakeFilter {
3131

3232
#[tokio::main]
3333
async fn main() {
34-
let mut cli = EchoClient::connect("http://127.0.0.1:8888".to_string());
34+
let builder = ClientBuilder::new()
35+
.with_connector("unix")
36+
.with_host("unix://127.0.0.1:8888");
37+
let mut cli = EchoClient::build(builder);
3538
// let mut unary_cli = cli.clone().with_filter(FakeFilter {});
3639
// let mut cli = EchoClient::build(ClientBuilder::from_static("http://127.0.0.1:8888"));
3740
let resp = cli

examples/echo/src/echo/server.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ async fn main() {
8080

8181
// 3. 通过serverbuilder来初始化Server
8282
let builder = ServerBuilder::new()
83-
.with_listener("tcp".to_string())
83+
.with_listener("unix".to_string())
8484
.with_service_names(vec!["grpc.examples.echo.Echo".to_string()])
85-
.with_addr("0.0.0.0:8888");
85+
.with_addr("127.0.0.1:8888");
8686
builder.build().serve().await.unwrap();
8787
}
8888

0 commit comments

Comments
 (0)