RPC API
What is RPC API
Layotto's RPC API is based on Mosn's grpc handler, which provides multi-protocol, modular, intelligent, and secure service calls.
The interface of the RPC API are consistent with Dapr, you could see its details in invoke.go.
Using Layotto RPC invocation, your application can reliably and securely communicate with other applications using the standard HTTP or X-Protocol protocols.
Layotto uses a sidecar architecture. To invoke an application using Layotto, you use the invoke API on any Layotto instance. The sidecar programming model encourages each applications to talk to its own instance of Layotto. The Layotto instances discover and communicate with one another.
When to use RPC API and what are the benefits?
In many environments with multiple services that need to communicate with each other, developers often ask themselves the following questions:
- How do I discover and invoke methods on different services?
- How do I call other services securely with encryption and apply access control on the methods?
- How do I handle retries and transient errors?
- How do I use tracing to see a call graph with metrics to diagnose issues in production?
Layotto addresses these challenges by providing a service invocation API that acts as a combination of a reverse proxy with built-in service discovery, while leveraging built-in distributed tracing, metrics, error handling, encryption and more.
How to use RPC API?
You can do RPC calls through grpc interface InvokeService. The API is defined in api.go.
The component needs to be configured before use. For detailed configuration instructions, see configuration reference, currently, Layotto uses MOSN layer 4 filter integrated with MOSN and runs on MOSN, so the configuration file used by Layotto is actually a MOSN configuration file. So you can also refer to the documentation of the MOSN configuration file.
Demo 1: Hello World -- Basic Golang HTTP Server
Quick start Document for this demo: Hello World
The echoserver publish a simple interface in port 8889, the config file example.json use mosn's routing capabilities to forward the request whose id field in the http header is equal to HelloService:1.0 to the local port 8889, and then the echoclient do a RPC calls though grpc interface InvokeService.
resp, err := cli.InvokeService(
ctx,
&runtimev1pb.InvokeServiceRequest{
Id: "HelloService:1.0",
Message: &runtimev1pb.CommonInvokeRequest{
Method: "/hello",
ContentType: "",
Data: &anypb.Any{Value: []byte(*data)}}},
)
Demo 2: Dubbo JSON RPC
Quick start Document for this demo: Dubbo JSON RPC Example
The server is dubbo-go-samples, the config file example.json use the callback function dubbo_json_rpc to generate a request header. And then,the client do a RPC calls though grpc interface InvokeService.
resp, err := cli.InvokeService(
ctx,
&runtimev1pb.InvokeServiceRequest{
Id: "com.ikurento.user.UserProvider",
Message: &runtimev1pb.CommonInvokeRequest{
Method: "GetUser",
ContentType: "",
Data: &anypb.Any{Value: []byte(*data)},
HttpExtension: &runtimev1pb.HTTPExtension{Verb: runtimev1pb.HTTPExtension_POST},
},
},
)
Implementation Principle
If you are interested in the implementation principle, or want to extend some functions, you can read RPC design document.