syntax = "proto3";
package hello;
service HelloService {
rpc SayHello (HelloRequest) returns (HelloReply);
}
message HelloRequest {
string name = 1;
}
message HelloReply {
string message = 1;
}
. run.sh compile_protos
. run.sh go_server
func (server *grpc_server) SayHello(
ctx context.Context, args *pb.HelloRequest) (*pb.HelloReply, error) {
return &pb.HelloReply{
Message: "hello " + args.Name + "!"}, nil
}
server := buildServer()
listenServer(server, "50051")
. run.sh go_client
_, client := buildClient("localhost:50051")
response, _ := client.SayHello(
context.Background(), &pb.HelloRequest{Name: "gbaptista"})
response.Message // 'hello gbaptista!'
. run.sh ruby_client
client = Hello::Client.new(
'localhost:50051',
:this_channel_is_insecure
)
response = client.say_hello(
Hello::HelloRequest.new(name: 'gbaptista')
)
response.message # => 'hello gbaptista!'
docker-compose up -d
cd src/web_js_client/
npm install
npm start
> http://localhost:8080
grpc_client = new HelloServiceClient(
'http://localhost:50052'
);
const request = new HelloRequest();
request.setName('gbaptista');
grpc_client.sayHello(request, {}, (error, response) => {
if(error) {
console.error(error);
} else {
console.error(response); // 'hello gbaptista!'
}
});