grpcClient
x/grpcClient
Component: v0.25.0+ gRPC client. Allows dynamic invocation of gRPC services.
The gRPC server must register the gRPC reflection service. This allows clients to query the services and methods supported by the server at runtime.
# Configuration
This component allows reusing shared connection clients through the server
field. Refer to Component Connection Reuse.
Field | Type | Description | Default |
---|---|---|---|
server | string | gRPC service address, format: hostname:port | None |
service | string | gRPC service name, can use Component Configuration Variables | None |
method | string | gRPC method name, can use Component Configuration Variables | None |
request | string | Request parameters, if empty, use the current message payload. Use JSON encoded, structure must match the requirements of service/method | None |
headers | map | Request header, can using Component Configuration Variables. |
# Relation Type
- Success: Execution successful, send the message to the
Success
chain - Failure: Execution failed, send the message to the
Failure
chain
# Execution Result
The query result is assigned to the message payload and passed to the next node.
# Example
- Define a gRPC service as follows:
syntax = "proto3";
package helloworld;
option go_package = ".;helloworld";
service Greeter {
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
message HelloRequest {
string name = 1;
}
message HelloReply {
string message = 1;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
- Generate protobuf code
protoc --go_out=. --go-grpc_out=. helloworld.proto
1
- Run the server
package main
import (
"context"
pb "github.com/rulego/rulego-components/external/grpc/testdata/helloworld"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
"log"
"net"
)
// server is the server implementation of the Greeter service
type server struct {
pb.UnimplementedGreeterServer
}
// SayHello implements the SayHello method of the Greeter service
func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
log.Printf("Received: %v", in.GetName())
return &pb.HelloReply{Message: "Hello " + in.GetName()}, nil
}
func main() {
listen, err := net.Listen("tcp", ":50051")
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
s := grpc.NewServer()
pb.RegisterGreeterServer(s, &server{})
// Register gRPC reflection service
reflection.Register(s)
// Start the server
if err := s.Serve(listen); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
- Call the server through the grpcClient component
{
"id": "s1",
"type": "x/grpcClient",
"name": "call grpc service",
"configuration": {
"server": "127.0.0.1:50051",
"service": "helloworld.Greeter",
"method": "SayHello",
"request": "{\"name\": \"lulu\"}"
}
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
Edit this page on GitHub (opens new window)
Last Updated: 2024/10/29, 13:25:01