Back to Examples
- Binding patterns
- Typed binding pattern
- Wildcard binding pattern
- List binding patterns
- Rest binding pattern in list binding pattern
- Mapping binding pattern
- Rest binding pattern in mapping binding pattern
- Error binding pattern
- Rest binding pattern in error binding pattern
- Single use of typed binding patterns
- Single use of typed binding patterns with on fail clause
- Iterative use of typed binding patterns
- List binding pattern in match statement
- Mapping binding pattern in match statement
- Error binding pattern in match statement
- Query expressions
- Sort iterable objects
- Let clause
- Limit clause
- Join iterable objects
- Outer Join clause
- Query tables
- Create tables with a query
- Create maps with a query
- Create streams with a query
- On conflict clause
- Advanced conflict handling
- Iterate over XML with a query
- Nested query expressions
- Destructure records using a query
- Querying streams
- Aggregation
- JSON type
- Access JSON elements
- Access optional JSON elements
- Match statement with maps
- Convert from user-defined type to JSON
- Convert from table and XML to JSON
- Convert from JSON to user-defined type
- Cast JSON to user-defined type
- Resource method typing
- JSON numbers
- JSON to record
- JSON to record with projection
- JSONPath expressions
- Asynchronous function calls
- Named workers
- Sequence diagrams
- Wait for workers
- Strands
- Named worker return values
- Alternate wait
- Multiple wait
- Named workers and futures
- Inter-worker message passing
- Alternate receive
- Multiple receive
- Conditional send
- Inter-worker failure propagation
- Named worker with on fail clause
- Synchronize message passing
- Asynchronize message passing
- Flush
- Fork
gRPC client - Simple RPC
A grpc:Client
is created by providing the endpoint URL of a gRPC server. In the simple RPC scenario, once connected, the client sends a request message to the remote service and waits for the response message. Use this to send a single request message and get a single response message back.
Generate the service definition
- 1.Create a new Protocol Buffers definition file named
grpc_simple.proto
and add the service definition below.
syntax = "proto3";
import "google/protobuf/wrappers.proto";
service HelloWorld {
rpc hello (google.protobuf.StringValue) returns (google.protobuf.StringValue);
}
- 2.Run the command below from the Ballerina tools distribution for stub generation.
$ bal grpc --input grpc_simple.proto --output stubs
Once you run the command, the grpc_simple_pb.bal
file gets generated inside the stubs
directory.
Prerequisites
- • Run the gRPC service given in the gRPC service - Simple RPC example.
Implement and run the client
- 1.Create a Ballerina package (e.g.,
client
). Delete themain.bal
file created by default as it is not required for this example.
- 2.Copy the generated
grpc_simple_pb.bal
file from thestubs
directory to theclient
package.
- 3.Create a new
grpc_simple_client.bal
file inside theclient
package and add the client implementation below.
import ballerina/io;
public function main() returns error? {
// Creates a gRPC client to interact with the remote server.
HelloWorldClient ep = check new ("http://localhost:9090");
// Executes a simple remote call.
string result = check ep->hello("WSO2");
// Prints the received result.
io:println(result);
}
- 4.Run the client by executing the command below.
$ bal run clientHello WSO2
Related links
PreviousCheck deadline
NextServer-side streaming RPC