GRPC as a classical RPC protocol, although slightly heavy, but is worth learning
Here’s a quick guide to getting started with the GRPC framework
The full case code has been uploaded to github: github.com/neatlife/my…
Install the command line tool
PHP requires this extra protoc, grpc_php_plugin tool to generate the protobuf file into PHP classes
Go requires the installation of the protoc-gen-Go tool to generate class definitions in go from the protobuf interface definition file
There is no need to manually install additional tools for Java. Maven is available, again demonstrating the superiority of Java (
protoc
Github.com/protocolbuf…
Compile grpc_php_plugin
Build the reference
git clone https://github.com/grpc/grpc.git
cd grpc
brew install autoconf automake libtool shtool
LIBTOOL=glibtool LIBTOOLIZE=glibtoolize make -j8
Copy the code
Grpc_php_plugin can be compiled by bins of GRpc_php_plugin in the PATH under directory bins/opt/
Install the GRPC extension for PHP
Extended address: pecl.php.net/package/gRP…
A compiler protoc – gen – go
Project address: github.com/golang/prot…
git clone https://github.com/golang/protobuf.git
cd protobuf/protoc-gen-go
go build
Copy the code
The operation effect is as follows
Case design
Go: provides GRPC server, provides a sayHello interface, the argument is a string representing the name, the return value is “Hello” splicing the name Java and PHP as the client to call the GO client
GRPC rely on
go
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
Copy the code
java
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-protobuf</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-stub</artifactId>
<version>1.0.0</version>
</dependency>
Copy the code
php
"grpc/grpc": "^ v1.9." "."google/protobuf": "^ v3.5"
Copy the code
The core code
Proto file
syntax = "proto3";
option go_package = "pbf";
package helloworld;
// The greeting service definition.
service Hello {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloResponse) {}
}
// The request message containing the user's name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings
message HelloResponse {
string message = 1;
}
Copy the code
The Protobuf file defines two data structures, HelloRequest and HelloResponse, and a SayHello interface
Use the following command to compile the proto file into the corresponding PHP and GO data structures
#! /bin/bashfor file in ./protos/*.proto; Do echo "generate the corresponding PHP class file: $file" protoc -I ./protos --php_out=./php/pbf --grpc_out=./php/pbf --plugin=protoc-gen-grpc=$(which grpc_php_plugin) $file done protoc -I ./protos --go_out=plugins=grpc:./go/pbf ./protos/*.protoCopy the code
Go server core code
// helloServer implements helloworld.HelloServer
type helloServer struct{}
// SayHello implements helloworld.HelloServer
func (s *helloServer) SayHello(ctx context.Context, in *pbf.HelloRequest) (*pbf.HelloResponse, error) {
return &pbf.HelloResponse{Message: "Hello " + in.Name}, nil
}
func main(a) {
s := grpc.NewServer()
pbf.RegisterHelloServer(s, &helloServer{})
reflection.Register(s)
s.Serve(lis)
}
Copy the code
func (s *helloServer) SayHello(ctx context.Context, in *pbf.HelloRequest) (*pbf.HelloResponse, Error is the implementation of the SayHello interface
PHP client core code
$name = 'Ming';
$client = new Helloworld\HelloClient('localhost:8488'['credentials' => Grpc\ChannelCredentials::createInsecure(),
]);
$request = new Helloworld\HelloRequest();
$request->setName($name);
list($response, $status) = $client->SayHello($request)->wait();
echo 'Server return status code:' . $status->code . PHP_EOL;
echo "Server replies:" . $response->getMessage() . PHP_EOL;
Copy the code
Code 0 indicates normal
Java client core code
private final HelloGrpc.HelloBlockingStub blockingStub;
public HelloClient(String host, int port) {
// Initialize the connection
channel = ManagedChannelBuilder.forAddress(host, port)
.usePlaintext(true)
.build();
// Initialize the remote service Stub
blockingStub = HelloGrpc.newBlockingStub(channel);
}
public String sayHello(String name) {
// Construct the service call parameter object
Helloworld.HelloRequest request = Helloworld.HelloRequest.newBuilder().setName(name).build();
// Invoke the remote service method
Helloworld.HelloResponse response = blockingStub.sayHello(request);
/ / the return value
return response.getMessage();
}
Copy the code
SayHello () is a GRPC call to go’s sayHello method.
See the effect
A couple of points to note
The go, Java, and PHP compilations are already written into makefiles and can be compiled with one click using make
Refer to the link
- Github.com/golang/prot…
- Blog.csdn.net/strongyoung…