apache_dubbo/protocol/grpc/
grpc_protocol.rs

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
18use std::collections::HashMap;
19
20use super::grpc_exporter::GrpcExporter;
21use super::grpc_invoker::GrpcInvoker;
22use super::grpc_server::GrpcServer;
23use crate::common::url::Url;
24use crate::protocol::Protocol;
25
26pub struct GrpcProtocol {
27    server_map: HashMap<String, GrpcServer>,
28    export_map: HashMap<String, GrpcExporter<GrpcInvoker>>,
29}
30
31impl GrpcProtocol {
32    pub fn new() -> Self {
33        Self {
34            server_map: HashMap::new(),
35            export_map: HashMap::new(),
36        }
37    }
38}
39
40impl Default for GrpcProtocol {
41    fn default() -> Self {
42        Self::new()
43    }
44}
45
46#[async_trait::async_trait]
47impl Protocol for GrpcProtocol {
48    type Invoker = GrpcInvoker;
49
50    type Exporter = GrpcExporter<Self::Invoker>;
51
52    fn destroy(&self) {
53        todo!()
54    }
55
56    async fn refer(self, url: Url) -> Self::Invoker {
57        GrpcInvoker::new(url)
58    }
59
60    async fn export(self, url: Url) -> Self::Exporter {
61        let service_key = url.service_key.join(",");
62
63        let exporter: GrpcExporter<GrpcInvoker> =
64            GrpcExporter::new(service_key.clone(), GrpcInvoker::new(url.clone()));
65        let mut export = self.export_map;
66        export.insert(service_key.clone(), exporter.clone());
67
68        // 启动服务
69
70        let server = super::grpc_server::GrpcServer::new(service_key.clone());
71        let mut server_map = self.server_map;
72        server_map.insert(service_key.clone(), server.clone());
73        server.serve(url.clone()).await;
74        exporter
75    }
76}