message HelloRequest {
string greeting = 1;
}
message HelloResponse {
string reply = 1;
}
service HelloService {
rpc SayHello(HelloRequest) returns (HelloResponse);
}
message Demo {
string Value = 1;
}
service DemoService {
rpc Hello(Demo) returns (Demo);
rpc Download(Demo) returns (stream Demo);
rpc Upload(stream Demo) returns (Demo);
rpc BestOfBoth(stream Demo) returns (stream Demo);
}
func main() {
grpcAddr := "localhost:12345"
conn, err := grpc.Dial(grpcAddr)
if err != nil {
log.Fatalf("Dial(%s) = %v", grpcAddr, err)
}
client := NewDemoServiceClient(conn)
msg := &Demo{"hello world"}
reply, err := client.Hello(context.Background(), msg)
if err != nil {
log.Fatalf("UnaryCall(%#v) failed with %v", msg, err)
}
println("received message " + reply.Value)
}
what is context: blog.golang.org/context
type server struct{}
func (this *server) Hello(c context.Context, msg *Demo)
(*Demo, error) {
msg.Value = "Allo" + msg.Value
return msg, nil
}
func main() {
lis, err := net.Listen("tcp", "localhost:12345")
if err != nil {
log.Fatalf("Failed to listen: %v", err)
}
grpcServer := grpc.NewServer()
RegisterDemoServiceServer(grpcServer, &server{})
grpcServer.Serve(lis)
}
13025 ns/op
19130 ns/op
19885 ns/op
11563 ns/op
23612 ns/op
21062 ns/op
PRELIMINARY SPEED
12460 ns/op
15972 ns/op
20603 ns/op
11105 ns/op
12332 ns/op
13536 ns/op
PRELIMINARY SPEED
import "github.com/gengo/grpc-gateway/options/options.proto";
message Demo {
string value = 1;
}
service DemoService {
rpc Hello(Demo) returns (Demo) {
option (gengo.grpc.gateway.ApiMethodOptions.api_options) = {
path: "/v1/demo/hello"
method: "POST"
}
}
}
message Allo {
string Name = 1;
int64 Age = 2;
string Country = 3;
bool Spy = 4;
}
service OnionSeller {
rpc OnlyOnce(Allo)
returns (Allo);
}