加入收藏 | 设为首页 | 会员中心 | 我要投稿 威海站长网 (https://www.0631zz.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 服务器 > 搭建环境 > Unix > 正文

使用Unix套接字连接器通信

发布时间:2023-01-08 20:01:41 所属栏目:Unix 来源:互联网
导读: 使用Unix套接字连接器通信
介绍使用 Dubbo Rust Triple 协议使用 Unix 套接字连接器实现通信。
本文重点讲解 Dubbo Rust Triple 协议使用Unix 套接,请先查看 Quick Start 了解 Dubbo Rust

使用Unix套接字连接器通信

介绍使用 Dubbo Rust Triple 协议使用 Unix 套接字连接器实现通信。

本文重点讲解 Dubbo Rust Triple 协议使用Unix 套接,请先查看 Quick Start 了解 Dubbo Rust 基本使用,在此查看本文的完整示例。

1 Triple 使用 Unix 套接字连接器

TripleServer 调整如下。

// dubbo/src/protocol/triple/triple_server.rs
impl TripleServer {
    pub fn new(names: Vec<String>) -> TripleServer {
        Self {
            service_names: names,
            s: DubboServer::new(),
        }
    }

    pub async fn serve(mut self, url: String) {
        {
            let lock = super::TRIPLE_SERVICES.read().unwrap();
            for name in self.service_names.iter() {
                if lock.get(name).is_none() {
                    tracing::warn!("service ({}) not register", name);
                    continue;
                }
                let svc = lock.get(name).unwrap();

                self.s = self.s.add_service(name.clone(), svc.clone());
            }
        }

        let uri = match http::Uri::from_str(&url) {
            Ok(v) => v,
            Err(err) => {
                tracing::error!("http uri parse error: {}, url: {:?}", err, &url);
                return;
            }
        };

        let authority = match uri.authority() {
            Some(v) => v.to_owned(),
            None => {
                tracing::error!("http authority is none");
                return;
            }
        };

        self.s
            .with_listener("unix".to_string())
            .serve(
                authority
                    .to_string()
                    .to_socket_addrs()
                    .unwrap()
                    .next()
                    .unwrap(),
            )
            .await
            .unwrap();
    }
}

}

2 使用 client/connection 使用 Unix 套接字连接器编写逻辑2.1 编写 Client 端

// examples/echo/src/echo/client.rs
...
let mut cli = EchoClient::connect("unix://0.0.0.0:8888".to_string());
...

2.2 编写 connection

// dubbo/src/triple/client/connection.rs
impl Connection {
    pub fn new() -> Self {
        Connection {
            host: hyper::Uri::default(),
            connector: "unix".to_string(),
            builder: Builder::new(),
        }
    }

    pub fn with_connector<C>(mut self, connector: String) -> Self {
        self.connector = connector;
        self
    }

    pub fn with_host(mut self, uri: hyper::Uri) -> Self {
        self.host = uri;
        self
    }

    pub fn with_builder(mut self, builder: Builder) -> Self {
        self.builder = builder;
        self
    }
}

3 运行示例编译

执行cargo build来编译server和client。

运行server

执行cargo run --bin echo-server来运行server,如上文dubbo.yaml所配置,server会监听8888端口UNIX 通信工具,并以triple协议提供RPC服务:

$ cargo run --bin echo-server
2022-12-05T10:40:20.829798Z  INFO dubbo::framework: url: triple://0.0.0.0:8888//grpc.examples.echo.Echo
2022-12-05T10:40:20.829830Z DEBUG dubbo::framework: service name: grpc.examples.echo.Echo, service_config: ServiceConfig { version: "1.0.0", group: "test", name: "", protocol: "triple", registry: "zookeeper", serializer: "json", protocol_configs: {"triple": ProtocolConfig { ip: "0.0.0.0", port: "8888", name: "triple", params: {} }} }
2022-12-05T10:40:20.829917Z  INFO dubbo::framework: protocol: "", service url: Url { uri: "triple://0.0.0.0:8888//grpc.examples.echo.Echo", protocol: "triple", location: "0.0.0.0:8888", ip: "0.0.0.0", port: "8888", service_key: ["grpc.examples.echo.Echo"], params: {} }
2022-12-05T10:40:20.830028Z DEBUG dubbo::triple::transport::listener: get_listener: "unix"

运行client,可以看到 Unix 通信效果

执行cargo run --bin echo-client来运行client,调用triple://127.0.0.1:8888/org.apache.dubbo.sample.tri.Greeter下的各种方法:

$ cargo run --bin echo-client
# unary call
fake filter: Metadata { inner: {"path": "/grpc.examples.echo.Echo/UnaryEcho", "tri-unit-info": "dubbo-rust/0.1.0", "grpc-accept-encoding": "gzip", "authority": "0.0.0.0:8888", "te": "trailers", "method": "POST", "grpc-encoding": "gzip", "tri-request-time": "1670237034581", "tri-service-group": "cluster", "content-type": "application/grpc+json", "tri-service-version": "dubbo-rust/0.1.0", "scheme": "unix", "user-agent": "dubbo-rust/0.1.0"} }
Response: EchoResponse { message: "hello, dubbo-rust" }
client streaming, Response: EchoResponse { message: "hello client streaming" }
parts: Metadata { inner: {"date": "Mon, 05 Dec 2022 10:43:57 GMT", "content-type": "application/grpc"} }
reply: EchoResponse { message: "server reply: \"msg1 from client\"" }
reply: EchoResponse { message: "server reply: \"msg2 from client\"" }
reply: EchoResponse { message: "server reply: \"msg3 from client\"" }
trailer: Some(Metadata { inner: {"grpc-message": "poll trailer successfully.", "content-type": "application/grpc", "grpc-accept-encoding": "gzip,identity", "grpc-status": "0"} })
parts: Metadata { inner: {"content-type": "application/grpc", "date": "Mon, 05 Dec 2022 10:43:58 GMT"} }
reply: EchoResponse { message: "msg1 from server" }
reply: EchoResponse { message: "msg2 from server" }
reply: EchoResponse { message: "msg3 from server" }
trailer: Some(Metadata { inner: {"grpc-status": "0", "grpc-message": "poll trailer successfully.", "grpc-accept-encoding": "gzip,identity", "content-type": "application/grpc"} })

Feedback

Was this page helpful?

YesNo

Glad to hear it! Please tell us how we can improve.

Sorry to hear that. Please tell us how we can improve.

最后修改 December 9, 2022: Fix check (#1688) (5e21581)

(编辑:威海站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章