一, golang
1, protocal buffer installation
https://github.com/google/protobuf/releases download the installation package
I see here is protoc.exe after decompression windows
Finally, you can set the environment variable
2, the installation golang protobuf
go get -u github.com/golang/protobuf/proto // golang protobuf 库 go get -u github.com/golang/protobuf/protoc-gen-go //protoc --go_out 工具
3, installation gRPC-go
go get google.golang.org/grpc
Can not download, then go https://github.com/grpc/grpc-go download
Well start writing code, we will use the Internet to test helloworld
4, project structure
golang for the server, other languages are customer service side.
helloworld.proto
syntax = "proto3"; package helloworld; // The greeting service definition. service Greeter { // Sends a greeting rpc SayHello (HelloRequest) returns (HelloReply) {} } // The request message containing the user's name. message HelloRequest { string name = 1; } // The response message containing the greetings message HelloReply { string message = 1; }
Running the compiler command
protoc --go_out=plugins=grpc:. helloworld.proto
It will generate a file helloworld.pb.go
Server
package main import ( "context" "google.golang.org/grpc" pb "grpc-test/server/proto/helloworld" "log" "net" ) const ( PORT = ":50001" ) type server struct {} func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) { log.Println("request: ", in.Name) return &pb.HelloReply{Message: "Hello " + in.Name}, nil } func main(){ lis, err := net.Listen("tcp", PORT) if err != nil { log.Fatalf("failed to listen: %v", err) } s := grpc.NewServer() pb.RegisterGreeterServer(s, &server{}) log.Println ( "rpc service has been opened") s.Serve (LIS) }
Kefuduan
package main import ( "context" "google.golang.org/grpc" pb "grpc-test/client/proto/helloworld" "log" "os" ) const ( address = "localhost:50001" ) func main() { conn, err := grpc.Dial(address, grpc.WithInsecure()) if err != nil { log.Fatalf("did not connect: %v", err) } defer conn.Close() c := pb.NewGreeterClient(conn) name := "lin" if len(os.Args) > 1 { name = os.Args[1] } r, err := c.SayHello(context.Background(), &pb.HelloRequest{Name: name}) if err != nil { log.Fatalf("could not greet: %v", err) } log.Println(r.Message) }
operation result
Two, nodejs
1, with the installed directly npm
npm install grpc-tools --save-dev npm install google-protobuf --save npm install grpc --save
In ./node_modules/grpc-tools/bin, you will find two files protoc.exe and grpc_node_plugin.exe
2, run the compiler command
./node_modules/grpc-tools/bin/protoc --js_out=import_style=commonjs,binary:./ --plugin=protoc-gen-grpc=./node_modules/grpc-tools/bin/grpc_node_plugin.exe --grpc_out=. helloworld.proto
It generates helloworld_grpc_pb.js helloworld_pb.js two js files
3、client.js
var grpc = require('grpc'); var messages = require('./proto/helloworld/helloworld_pb.js'); var services = require('./proto/helloworld/helloworld_grpc_pb.js') var request = new messages.HelloRequest(); request.setName('world'); var client = new services.GreeterClient( 'localhost:50001', grpc.credentials.createInsecure() ); client.sayHello(request, function(err,data){ if(err){ console.error(err); } console.log(data); })
4, operating results
Three, C # core
1, to install the necessary packages
Install-Package Grpc Install-Package Google.Protobuf Install-Package Grpc.Tools
2, run the compiler command
protoc.exe -I helloworld --csharp_out helloworld helloworld/helloworld.proto --grpc_out helloworld --plugin=protoc-gen-grpc=grpc_csharp_plugin.exe
Two files are generated Helloworld.cs HelloworldGrpc.cs
3, test code
using System; using Grpc.Core; using Helloworld; namespace grpc { class Program { static void Main(string[] args) { Channel channel = new Channel("127.0.0.1:50001", ChannelCredentials.Insecure); var client = new Greeter.GreeterClient(channel); var reply = client.SayHello(new HelloRequest{ Name = "lin" }); Console.WriteLine("来自" + reply.Message); channel.ShutdownAsync().Wait(); Console.WriteLine("任意键退出..."); Console.ReadKey(); } } }
operation result
Well tested on here, I'm here because the server is temporarily used in these languages. Other language all of you to the test.
Code Address: https://github.com/fanxiaoping/grpc-test