You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by ya...@apache.org on 2024/07/02 03:00:53 UTC
(dubbo-rust) branch main updated: Add Loadbalance trait (#199)
This is an automated email from the ASF dual-hosted git repository.
yangyang pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/dubbo-rust.git
The following commit(s) were added to refs/heads/main by this push:
new 0e2050a Add Loadbalance trait (#199)
0e2050a is described below
commit 0e2050ad242f2b8346ca78556f557947c24db507
Author: Yang Yang <96...@qq.com>
AuthorDate: Tue Jul 2 11:00:49 2024 +0800
Add Loadbalance trait (#199)
* refact(ld): add LoadBalancer trait, impl random ld
* refact(loadbalance): update ld api design
* style: cargo fmt
* example(greet): update client
* chore: add tag field
* style: cargo fmt
* refact(dubbo): use target_family
* style: cargo check
---
application.yaml | 1 +
dubbo/src/config/config.rs | 3 +-
dubbo/src/config/service.rs | 4 +
dubbo/src/extension/invoker_extension.rs | 44 ++++---
dubbo/src/loadbalancer/mod.rs | 104 ++++++++++++++---
.../{config/service.rs => loadbalancer/random.rs} | 40 +++----
dubbo/src/triple/transport/connector/mod.rs | 4 +-
dubbo/src/triple/transport/listener/mod.rs | 4 +-
examples/echo/src/generated/grpc.examples.echo.rs | 127 ++++++---------------
examples/greeter/application.yaml | 1 +
examples/greeter/src/greeter/client.rs | 18 ++-
11 files changed, 195 insertions(+), 155 deletions(-)
diff --git a/application.yaml b/application.yaml
index bec29a6..3096bcf 100644
--- a/application.yaml
+++ b/application.yaml
@@ -13,6 +13,7 @@ dubbo:
provider:
services:
GreeterProvider:
+ tag: red
version: 1.0.0
group: test
protocol: triple
diff --git a/dubbo/src/config/config.rs b/dubbo/src/config/config.rs
index 3c60683..dd19610 100644
--- a/dubbo/src/config/config.rs
+++ b/dubbo/src/config/config.rs
@@ -119,7 +119,8 @@ impl RootConfig {
.group("test".to_string())
.version("1.0.0".to_string())
.interface("helloworld.Greeter".to_string())
- .protocol("triple".to_string()),
+ .protocol("triple".to_string())
+ .tag("read".to_string()),
);
self.protocols.insert(
"triple".to_string(),
diff --git a/dubbo/src/config/service.rs b/dubbo/src/config/service.rs
index 8a1f191..e883444 100644
--- a/dubbo/src/config/service.rs
+++ b/dubbo/src/config/service.rs
@@ -23,6 +23,7 @@ pub struct ServiceConfig {
pub group: String,
pub protocol: String,
pub interface: String,
+ pub tag: String,
}
impl ServiceConfig {
@@ -41,4 +42,7 @@ impl ServiceConfig {
pub fn protocol(self, protocol: String) -> Self {
Self { protocol, ..self }
}
+ pub fn tag(self, tag: String) -> Self {
+ Self { tag, ..self }
+ }
}
diff --git a/dubbo/src/extension/invoker_extension.rs b/dubbo/src/extension/invoker_extension.rs
index 8d59e5b..3373bd2 100644
--- a/dubbo/src/extension/invoker_extension.rs
+++ b/dubbo/src/extension/invoker_extension.rs
@@ -40,30 +40,32 @@ pub trait Invoker {
async fn url(&self) -> Result<Url, StdError>;
}
-pub enum CallType {
- Unary,
- ClientStream,
- ServerStream,
- BiStream,
-}
+// pub enum CallType {
+// Unary,
+// ClientStream,
+// ServerStream,
+// BiStream,
+// }
pub struct GrpcInvocation {
- service_name: String,
- method_name: String,
- arguments: Vec<Argument>,
- attachments: HashMap<String, String>,
- call_type: CallType,
+ // service_name: String,
+ // method_name: String,
+ // arguments: Vec<Argument>,
+ // attachments: HashMap<String, String>,
+ // call_type: CallType,
}
-pub struct Argument {
- name: String,
- value: Box<dyn Stream<Item = Box<dyn Serializable + Send + 'static>> + Send + 'static>,
-}
+// pub struct Argument {
+// name: String,
+// value: Box<dyn Stream<Item = Box<dyn Serializable + Send + 'static>> + Send + 'static>,
+// }
+#[allow(dead_code)]
pub trait Serializable {
fn serialize(&self, serialization_type: String) -> Result<Bytes, StdError>;
}
+#[allow(dead_code)]
pub trait Deserializable {
fn deserialize(&self, bytes: Bytes, deserialization_type: String) -> Result<Self, StdError>
where
@@ -138,8 +140,8 @@ pub mod proxy {
}
}
- impl From<Box<dyn Invoker + Send + 'static>> for InvokerProxy {
- fn from(invoker: Box<dyn Invoker + Send + 'static>) -> Self {
+ impl From<Box<dyn Invoker + Send + Sync + 'static>> for InvokerProxy {
+ fn from(invoker: Box<dyn Invoker + Send + Sync + 'static>) -> Self {
let (tx, mut rx) = tokio::sync::mpsc::channel(64);
tokio::spawn(async move {
while let Some(opt) = rx.recv().await {
@@ -220,7 +222,11 @@ impl InvokerExtensionLoader {
type InvokerExtensionConstructor = fn(
Url,
) -> Pin<
- Box<dyn Future<Output = Result<Box<dyn Invoker + Send + 'static>, StdError>> + Send + 'static>,
+ Box<
+ dyn Future<Output = Result<Box<dyn Invoker + Send + Sync + 'static>, StdError>>
+ + Send
+ + 'static,
+ >,
>;
pub(crate) struct InvokerExtensionFactory {
constructor: InvokerExtensionConstructor,
@@ -275,7 +281,7 @@ where
impl<T> ExtensionMetaInfo for InvokerExtension<T>
where
T: Invoker + Send + 'static,
- T: Extension<Target = Box<dyn Invoker + Send + 'static>>,
+ T: Extension<Target = Box<dyn Invoker + Send + Sync + 'static>>,
{
fn name() -> String {
T::name()
diff --git a/dubbo/src/loadbalancer/mod.rs b/dubbo/src/loadbalancer/mod.rs
index 6f4ec89..d49ec5d 100644
--- a/dubbo/src/loadbalancer/mod.rs
+++ b/dubbo/src/loadbalancer/mod.rs
@@ -14,26 +14,33 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-use crate::StdError;
+
+pub mod random;
+
use futures_core::future::BoxFuture;
+use std::error::Error;
+use tokio::time::Duration;
use tower::{discover::ServiceList, ServiceExt};
use tower_service::Service;
+use tracing::debug;
use crate::{
codegen::RpcInvocation,
+ invocation::Metadata,
invoker::{clone_body::CloneBody, clone_invoker::CloneInvoker},
+ loadbalancer::random::RandomLoadBalancer,
param::Param,
+ protocol::triple::triple_invoker::TripleInvoker,
svc::NewService,
+ StdError,
};
-use crate::protocol::triple::triple_invoker::TripleInvoker;
-
pub struct NewLoadBalancer<N> {
inner: N,
}
#[derive(Clone)]
-pub struct LoadBalancer<S> {
+pub struct LoadBalancerSvc<S> {
inner: S, // Routes service
}
@@ -53,17 +60,17 @@ where
// NewRoutes
N: NewService<T>,
{
- type Service = LoadBalancer<N::Service>;
+ type Service = LoadBalancerSvc<N::Service>;
fn new_service(&self, target: T) -> Self::Service {
// Routes service
let svc = self.inner.new_service(target);
- LoadBalancer { inner: svc }
+ LoadBalancerSvc { inner: svc }
}
}
-impl<N> Service<http::Request<CloneBody>> for LoadBalancer<N>
+impl<N> Service<http::Request<CloneBody>> for LoadBalancerSvc<N>
where
// Routes service
N: Service<(), Response = Vec<CloneInvoker<TripleInvoker>>> + Clone,
@@ -94,18 +101,87 @@ where
Ok(routes) => routes,
};
- let service_list: Vec<_> = routes
- .into_iter()
- .map(|invoker| tower::load::Constant::new(invoker, 1))
- .collect();
+ // let service_list: Vec<_> = routes
+ // .into_iter()
+ // // .map(|invoker| tower::load::Constant::new(invoker, 1))
+ // .collect();
- let service_list = ServiceList::new(service_list);
+ // let rdm = RandomLoadBalancer::default();
+ let metadata = Metadata::from_headers(req.headers().clone());
+ // let invks = rdm.select_invokers(service_list, metadata);
+ // invks.oneshot(req).await
+ // let service_list = ServiceList::new(service_list);
- let p2c = tower::balance::p2c::Balance::new(service_list);
+ // let p2c = tower::balance::p2c::Balance::new(service_list);
+ // let p: Box<dyn LoadBalancer<Invoker = BoxService<http::Request<CloneBody>, http::Response<UnsyncBoxBody<bytes::Bytes, status::Status>>, Box<dyn std::error::Error + std::marker::Send + std::marker::Sync>>> + std::marker::Send + std::marker::Sync> = get_loadbalancer("p2c").into();
+ let p = get_loadbalancer("p2c");
+ // let ivk = p.select_invokers(invokers, metadata);
+ let ivk = p.select_invokers(routes, metadata);
- p2c.oneshot(req).await
+ ivk.oneshot(req).await
};
Box::pin(fut)
}
}
+
+type DubboBoxService = tower::util::BoxService<
+ http::Request<CloneBody>,
+ http::Response<crate::BoxBody>,
+ Box<dyn Error + Send + Sync>,
+>;
+
+pub trait LoadBalancer {
+ type Invoker;
+
+ fn select_invokers(
+ &self,
+ invokers: Vec<CloneInvoker<TripleInvoker>>,
+ metadata: Metadata,
+ ) -> Self::Invoker;
+}
+
+fn get_loadbalancer(
+ loadbalancer: &str,
+) -> Box<dyn LoadBalancer<Invoker = DubboBoxService> + Send + Sync + 'static> {
+ match loadbalancer {
+ "random" => {
+ println!("random!");
+ Box::new(RandomLoadBalancer::default())
+ }
+ "p2c" => Box::new(P2cBalancer::default()),
+ _ => Box::new(P2cBalancer::default()),
+ }
+}
+const DEFAULT_RTT: Duration = Duration::from_millis(30);
+#[derive(Debug, Default)]
+pub struct P2cBalancer {}
+
+impl LoadBalancer for P2cBalancer {
+ type Invoker = DubboBoxService;
+
+ fn select_invokers(
+ &self,
+ invokers: Vec<CloneInvoker<TripleInvoker>>,
+ _metadata: Metadata,
+ ) -> Self::Invoker {
+ debug!("p2c load balancer");
+ let service_list: Vec<_> = invokers
+ .into_iter()
+ .map(|invoker| tower::load::Constant::new(invoker, 1))
+ .collect();
+
+ let decay = Duration::from_secs(10);
+ let service_list = ServiceList::new(service_list);
+ let s = tower::load::PeakEwmaDiscover::new(
+ service_list,
+ DEFAULT_RTT,
+ decay,
+ tower::load::CompleteOnResponse::default(),
+ );
+
+ let p = tower::balance::p2c::Balance::new(s);
+ let svc = DubboBoxService::new(p);
+ svc
+ }
+}
diff --git a/dubbo/src/config/service.rs b/dubbo/src/loadbalancer/random.rs
similarity index 55%
copy from dubbo/src/config/service.rs
copy to dubbo/src/loadbalancer/random.rs
index 8a1f191..7739cba 100644
--- a/dubbo/src/config/service.rs
+++ b/dubbo/src/loadbalancer/random.rs
@@ -15,30 +15,28 @@
* limitations under the License.
*/
-use serde::{Deserialize, Serialize};
+use rand::prelude::SliceRandom;
+use tracing::debug;
-#[derive(Debug, Default, Serialize, Deserialize, Clone)]
-pub struct ServiceConfig {
- pub version: String,
- pub group: String,
- pub protocol: String,
- pub interface: String,
-}
+use super::{DubboBoxService, LoadBalancer};
+use crate::{
+ invocation::Metadata, loadbalancer::CloneInvoker,
+ protocol::triple::triple_invoker::TripleInvoker,
+};
-impl ServiceConfig {
- pub fn interface(self, interface: String) -> Self {
- Self { interface, ..self }
- }
+#[derive(Clone, Default)]
+pub struct RandomLoadBalancer {}
- pub fn version(self, version: String) -> Self {
- Self { version, ..self }
- }
-
- pub fn group(self, group: String) -> Self {
- Self { group, ..self }
- }
+impl LoadBalancer for RandomLoadBalancer {
+ type Invoker = DubboBoxService;
- pub fn protocol(self, protocol: String) -> Self {
- Self { protocol, ..self }
+ fn select_invokers(
+ &self,
+ invokers: Vec<CloneInvoker<TripleInvoker>>,
+ metadata: Metadata,
+ ) -> Self::Invoker {
+ debug!("random loadbalance {:?}", metadata);
+ let ivk = invokers.choose(&mut rand::thread_rng()).unwrap().clone();
+ DubboBoxService::new(ivk)
}
}
diff --git a/dubbo/src/triple/transport/connector/mod.rs b/dubbo/src/triple/transport/connector/mod.rs
index 690b781..66f95e8 100644
--- a/dubbo/src/triple/transport/connector/mod.rs
+++ b/dubbo/src/triple/transport/connector/mod.rs
@@ -17,7 +17,7 @@
pub mod http_connector;
pub mod https_connector;
-#[cfg(any(target_os = "macos", target_os = "unix"))]
+#[cfg(any(target_os = "macos", target_family = "unix"))]
pub mod unix_connector;
use hyper::Uri;
@@ -84,7 +84,7 @@ pub fn get_connector(connector: &str) -> BoxCloneService<Uri, BoxIO, crate::Erro
let c = https_connector::HttpsConnector::new();
BoxCloneService::new(Connector::new(c))
}
- #[cfg(any(target_os = "macos", target_os = "unix"))]
+ #[cfg(any(target_os = "macos", target_family = "unix"))]
"unix" => {
let c = unix_connector::UnixConnector::new();
BoxCloneService::new(Connector::new(c))
diff --git a/dubbo/src/triple/transport/listener/mod.rs b/dubbo/src/triple/transport/listener/mod.rs
index 0be7415..4f01ef0 100644
--- a/dubbo/src/triple/transport/listener/mod.rs
+++ b/dubbo/src/triple/transport/listener/mod.rs
@@ -16,7 +16,7 @@
*/
pub mod tcp_listener;
-#[cfg(any(target_os = "macos", target_os = "unix"))]
+#[cfg(any(target_os = "macos", target_family = "unix"))]
pub mod unix_listener;
use std::net::SocketAddr;
@@ -65,7 +65,7 @@ impl<T: Listener> Listener for WrappedListener<T> {
pub async fn get_listener(name: String, addr: SocketAddr) -> Result<BoxListener, crate::Error> {
match name.as_str() {
"tcp" => Ok(TcpListener::bind(addr).await?.boxed()),
- #[cfg(any(target_os = "macos", target_os = "unix"))]
+ #[cfg(any(target_os = "macos", target_family = "unix"))]
"unix" => Ok(unix_listener::UnixListener::bind(addr).await?.boxed()),
_ => {
warn!("no support listener: {:?}", name);
diff --git a/examples/echo/src/generated/grpc.examples.echo.rs b/examples/echo/src/generated/grpc.examples.echo.rs
index ee8cc1e..fc48dc5 100644
--- a/examples/echo/src/generated/grpc.examples.echo.rs
+++ b/examples/echo/src/generated/grpc.examples.echo.rs
@@ -43,9 +43,7 @@ pub mod echo_client {
let invocation = RpcInvocation::default()
.with_service_unique_name(String::from("grpc.examples.echo.Echo"))
.with_method_name(String::from("UnaryEcho"));
- let path = http::uri::PathAndQuery::from_static(
- "/grpc.examples.echo.Echo/UnaryEcho",
- );
+ let path = http::uri::PathAndQuery::from_static("/grpc.examples.echo.Echo/UnaryEcho");
self.inner.unary(request, path, invocation).await
}
/// ServerStreamingEcho is server side streaming.
@@ -102,9 +100,7 @@ pub mod echo_server {
request: Request<super::EchoRequest>,
) -> Result<Response<super::EchoResponse>, dubbo::status::Status>;
///Server streaming response type for the ServerStreamingEcho method.
- type ServerStreamingEchoStream: futures_util::Stream<
- Item = Result<super::EchoResponse, dubbo::status::Status>,
- >
+ type ServerStreamingEchoStream: futures_util::Stream<Item = Result<super::EchoResponse, dubbo::status::Status>>
+ Send
+ 'static;
/// ServerStreamingEcho is server side streaming.
@@ -118,19 +114,14 @@ pub mod echo_server {
request: Request<Decoding<super::EchoRequest>>,
) -> Result<Response<super::EchoResponse>, dubbo::status::Status>;
///Server streaming response type for the BidirectionalStreamingEcho method.
- type BidirectionalStreamingEchoStream: futures_util::Stream<
- Item = Result<super::EchoResponse, dubbo::status::Status>,
- >
+ type BidirectionalStreamingEchoStream: futures_util::Stream<Item = Result<super::EchoResponse, dubbo::status::Status>>
+ Send
+ 'static;
/// BidirectionalStreamingEcho is bidi streaming.
async fn bidirectional_streaming_echo(
&self,
request: Request<Decoding<super::EchoRequest>>,
- ) -> Result<
- Response<Self::BidirectionalStreamingEchoStream>,
- dubbo::status::Status,
- >;
+ ) -> Result<Response<Self::BidirectionalStreamingEchoStream>, dubbo::status::Status>;
}
/// Echo is the echo service.
#[derive(Debug)]
@@ -160,10 +151,7 @@ pub mod echo_server {
type Response = http::Response<BoxBody>;
type Error = std::convert::Infallible;
type Future = BoxFuture<Self::Response, Self::Error>;
- fn poll_ready(
- &mut self,
- _cx: &mut Context<'_>,
- ) -> Poll<Result<(), Self::Error>> {
+ fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
fn call(&mut self, req: http::Request<B>) -> Self::Future {
@@ -176,24 +164,16 @@ pub mod echo_server {
}
impl<T: Echo> UnarySvc<super::EchoRequest> for UnaryEchoServer<T> {
type Response = super::EchoResponse;
- type Future = BoxFuture<
- Response<Self::Response>,
- dubbo::status::Status,
- >;
- fn call(
- &mut self,
- request: Request<super::EchoRequest>,
- ) -> Self::Future {
+ type Future = BoxFuture<Response<Self::Response>, dubbo::status::Status>;
+ fn call(&mut self, request: Request<super::EchoRequest>) -> Self::Future {
let inner = self.inner.0.clone();
let fut = async move { inner.unary_echo(request).await };
Box::pin(fut)
}
}
let fut = async move {
- let mut server = TripleServer::<
- super::EchoRequest,
- super::EchoResponse,
- >::new();
+ let mut server =
+ TripleServer::<super::EchoRequest, super::EchoResponse>::new();
let res = server.unary(UnaryEchoServer { inner }, req).await;
Ok(res)
};
@@ -204,30 +184,20 @@ pub mod echo_server {
struct ServerStreamingEchoServer<T: Echo> {
inner: _Inner<T>,
}
- impl<T: Echo> ServerStreamingSvc<super::EchoRequest>
- for ServerStreamingEchoServer<T> {
+ impl<T: Echo> ServerStreamingSvc<super::EchoRequest> for ServerStreamingEchoServer<T> {
type Response = super::EchoResponse;
type ResponseStream = T::ServerStreamingEchoStream;
- type Future = BoxFuture<
- Response<Self::ResponseStream>,
- dubbo::status::Status,
- >;
- fn call(
- &mut self,
- request: Request<super::EchoRequest>,
- ) -> Self::Future {
+ type Future =
+ BoxFuture<Response<Self::ResponseStream>, dubbo::status::Status>;
+ fn call(&mut self, request: Request<super::EchoRequest>) -> Self::Future {
let inner = self.inner.0.clone();
- let fut = async move {
- inner.server_streaming_echo(request).await
- };
+ let fut = async move { inner.server_streaming_echo(request).await };
Box::pin(fut)
}
}
let fut = async move {
- let mut server = TripleServer::<
- super::EchoRequest,
- super::EchoResponse,
- >::new();
+ let mut server =
+ TripleServer::<super::EchoRequest, super::EchoResponse>::new();
let res = server
.server_streaming(ServerStreamingEchoServer { inner }, req)
.await;
@@ -240,29 +210,21 @@ pub mod echo_server {
struct ClientStreamingEchoServer<T: Echo> {
inner: _Inner<T>,
}
- impl<T: Echo> ClientStreamingSvc<super::EchoRequest>
- for ClientStreamingEchoServer<T> {
+ impl<T: Echo> ClientStreamingSvc<super::EchoRequest> for ClientStreamingEchoServer<T> {
type Response = super::EchoResponse;
- type Future = BoxFuture<
- Response<Self::Response>,
- dubbo::status::Status,
- >;
+ type Future = BoxFuture<Response<Self::Response>, dubbo::status::Status>;
fn call(
&mut self,
request: Request<Decoding<super::EchoRequest>>,
) -> Self::Future {
let inner = self.inner.0.clone();
- let fut = async move {
- inner.client_streaming_echo(request).await
- };
+ let fut = async move { inner.client_streaming_echo(request).await };
Box::pin(fut)
}
}
let fut = async move {
- let mut server = TripleServer::<
- super::EchoRequest,
- super::EchoResponse,
- >::new();
+ let mut server =
+ TripleServer::<super::EchoRequest, super::EchoResponse>::new();
let res = server
.client_streaming(ClientStreamingEchoServer { inner }, req)
.await;
@@ -275,54 +237,39 @@ pub mod echo_server {
struct BidirectionalStreamingEchoServer<T: Echo> {
inner: _Inner<T>,
}
- impl<T: Echo> StreamingSvc<super::EchoRequest>
- for BidirectionalStreamingEchoServer<T> {
+ impl<T: Echo> StreamingSvc<super::EchoRequest> for BidirectionalStreamingEchoServer<T> {
type Response = super::EchoResponse;
type ResponseStream = T::BidirectionalStreamingEchoStream;
- type Future = BoxFuture<
- Response<Self::ResponseStream>,
- dubbo::status::Status,
- >;
+ type Future =
+ BoxFuture<Response<Self::ResponseStream>, dubbo::status::Status>;
fn call(
&mut self,
request: Request<Decoding<super::EchoRequest>>,
) -> Self::Future {
let inner = self.inner.0.clone();
- let fut = async move {
- inner.bidirectional_streaming_echo(request).await
- };
+ let fut =
+ async move { inner.bidirectional_streaming_echo(request).await };
Box::pin(fut)
}
}
let fut = async move {
- let mut server = TripleServer::<
- super::EchoRequest,
- super::EchoResponse,
- >::new();
+ let mut server =
+ TripleServer::<super::EchoRequest, super::EchoResponse>::new();
let res = server
- .bidi_streaming(
- BidirectionalStreamingEchoServer {
- inner,
- },
- req,
- )
+ .bidi_streaming(BidirectionalStreamingEchoServer { inner }, req)
.await;
Ok(res)
};
Box::pin(fut)
}
- _ => {
- Box::pin(async move {
- Ok(
- http::Response::builder()
- .status(200)
- .header("grpc-status", "12")
- .header("content-type", "application/grpc")
- .body(empty_body())
- .unwrap(),
- )
- })
- }
+ _ => Box::pin(async move {
+ Ok(http::Response::builder()
+ .status(200)
+ .header("grpc-status", "12")
+ .header("content-type", "application/grpc")
+ .body(empty_body())
+ .unwrap())
+ }),
}
}
}
diff --git a/examples/greeter/application.yaml b/examples/greeter/application.yaml
index 6683c6d..9a4b47f 100644
--- a/examples/greeter/application.yaml
+++ b/examples/greeter/application.yaml
@@ -15,5 +15,6 @@ dubbo:
GreeterProvider:
version: 1.0.0
group: test
+ tag: red
protocol: triple
interface: org.apache.dubbo.sample.tri.Greeter
\ No newline at end of file
diff --git a/examples/greeter/src/greeter/client.rs b/examples/greeter/src/greeter/client.rs
index f6fea89..2493fbc 100644
--- a/examples/greeter/src/greeter/client.rs
+++ b/examples/greeter/src/greeter/client.rs
@@ -38,13 +38,17 @@ async fn main() {
let builder = ClientBuilder::new().with_registry("nacos://127.0.0.1:8848".parse().unwrap());
let mut cli = GreeterClient::new(builder);
+ let mut mtdata = Metadata::default();
+ mtdata = mtdata.insert("static_tag".to_string(), "red".to_string());
+ let req = Request::from_parts(
+ mtdata.clone(),
+ GreeterRequest {
+ name: "message from client".to_string(),
+ },
+ );
println!("# unary call");
- let resp = cli
- .greet(Request::new(GreeterRequest {
- name: "message from client".to_string(),
- }))
- .await;
+ let resp = cli.greet(req).await;
let resp = match resp {
Ok(resp) => resp,
Err(err) => return println!("response error: {:?}", err),
@@ -64,7 +68,9 @@ async fn main() {
name: "msg3 from client streaming".to_string(),
},
];
- let req = futures_util::stream::iter(data);
+ let mut mtdata = Metadata::default();
+ mtdata = mtdata.insert("client_streaming".to_string(), "true".to_string());
+ let req = Request::from_parts(mtdata, futures_util::stream::iter(data));
let resp = cli.greet_client_stream(req).await;
let client_streaming_resp = match resp {
Ok(resp) => resp,