Skip to content

Commit

Permalink
add nacos 2.x grpc server
Browse files Browse the repository at this point in the history
  • Loading branch information
heqingpan committed Oct 29, 2022
1 parent b08d70d commit 032d138
Show file tree
Hide file tree
Showing 8 changed files with 801 additions and 4 deletions.
273 changes: 270 additions & 3 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@ actix-rt = "2"
env_logger = "0.7"
rust-crypto="0.2"
tokio = {version="1", features=["full"]}
tokio-stream = "0.1"
chrono = {version = "0.4", features =["serde"] }
flate2 = "1.0"
log="0.4"
tonic = "0.4"
prost="0.7"

# db
rusqlite={version="0.25",features = ["bundled"]}
Expand Down
61 changes: 61 additions & 0 deletions proto/nacos_grpc_service.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@

/*
* Copyright 1999-2020 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

syntax = "proto3";



option java_multiple_files = true;
option java_package = "com.alibaba.nacos.api.grpc.auto";

//package _;

message Any {
string type_url = 1;
bytes value = 2;
}

message Metadata {
string type = 3;
string clientIp = 8;
map<string, string> headers = 7;
}


message Payload {
Metadata metadata = 2;
Any body = 3;
}

service RequestStream {
// build a streamRequest
rpc requestStream (Payload) returns (stream Payload) {
}
}

service Request {
// Sends a commonRequest
rpc request (Payload) returns (Payload) {
}
}

service BiRequestStream {
// Sends a commonRequest
rpc requestBiStream (stream Payload) returns (stream Payload) {
}
}

18 changes: 17 additions & 1 deletion src/bin/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@

use actix_web::{App, web::Data};
use actix::Actor;
use nacos_rust::naming::core::NamingActor;
use nacos_rust::grpc::nacos_proto::bi_request_stream_server::BiRequestStreamServer;
use nacos_rust::grpc::nacos_proto::request_server::RequestServer;
use nacos_rust::grpc::server::BiRequestStreamServerImpl;
use nacos_rust::{naming::core::NamingActor, grpc::server::RequestServerImpl};
use nacos_rust::config::config::ConfigActor;
use tonic::transport::Server;
use std::error::Error;

use nacos_rust::web_config::app_config;
Expand All @@ -16,6 +20,18 @@ async fn main() -> Result<(), Box<dyn Error>> {
env_logger::init();
let config_addr = ConfigActor::new().start();
let naming_addr = NamingActor::new_and_create(5000);

tokio::spawn(async move {
let addr = "0.0.0.0:9848".parse().unwrap();
let request_server = RequestServerImpl::default();
let bi_request_stream_server = BiRequestStreamServerImpl::default();
Server::builder()
.add_service(RequestServer::new(request_server))
.add_service(BiRequestStreamServer::new(bi_request_stream_server))
.serve(addr)
.await.unwrap();
});

HttpServer::new(move || {
let config_addr = config_addr.clone();
let naming_addr = naming_addr.clone();
Expand Down
28 changes: 28 additions & 0 deletions src/grpc/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
pub mod nacos_proto;
pub mod server;

pub fn build_payload(val:&str) -> nacos_proto::Payload {
let body = nacos_proto::Any {
type_url:"".into(),
value:val.as_bytes().to_vec(),
};
nacos_proto::Payload{
body:Some(body),
metadata:Default::default(),
}
}

pub fn get_payload_string(value:&nacos_proto::Payload) -> String {
let mut str = String::default();
if let Some(meta) = &value.metadata {
str.push_str(&format!("type:{},\n\t",meta.r#type));
str.push_str(&format!("client_ip:{},\n\t",meta.client_ip));
str.push_str(&format!("header:{:?},\n\t",meta.headers));
}
if let Some(body) = &value.body {
let new_value = body.clone();
let value_str = String::from_utf8(new_value.value).unwrap();
str.push_str(&format!("body:{}",value_str));
}
str
}
Loading

0 comments on commit 032d138

Please sign in to comment.