github项目地址
1. 前置工作
-
安装 protobuf -
安装PHP相关扩展(grpc 、protobuf ) -
生成grpc_php_plugin
$ git clone -b RELEASE_TAG_HERE https://github.com/grpc/grpc
$ cd grpc
$ git submodule update --init
$ mkdir -p cmake/build
$ cd cmake/build
$ cmake ../..
$ make protoc grpc_php_plugin
$ mv ./grpc_php_plugin /opt/homebrew/bin/
-
生成相应语言的文件命令 GO protoc -I. --go_out=plugins=grpc:./pb pb/user.proto
PHP cd php-server
protoc --proto_path=../pb --php_out=. --grpc_out=. --plugin=protoc-gen-grpc=/opt/homebrew/bin/grpc_php_plugin ../pb/user.proto
2. 关键代码片段
go server
var (
port = flag.Int("port", 50051, "The server port")
)
type UserService struct {
}
func (us *UserService) UserIndex(ctx context.Context, in *user.UserIndexRequest) (*user.UserIndexResponse, error) {
log.Printf("receive user index request: page %d page_size %d", in.Page, in.PageSize)
return &user.UserIndexResponse{
Err: 0,
Msg: "success",
Data: []*user.UserEntity{
{Name: "u1", Age: 11},
{Name: "u2", Age: 12},
},
}, nil
}
func main() {
lis, err := net.Listen("tcp", fmt.Sprintf("localhost:%d", *port))
if err != nil {
log.Fatalf("failed listen: %v", err)
}
grpcServer := grpc.NewServer()
user.RegisterUserServer(grpcServer, &UserService{})
reflection.Register(grpcServer)
if err := grpcServer.Serve(lis); err != nil {
log.Printf("failed to server: %v", err)
}
}
go client
var (
addr = flag.String("addr", "localhost:50051", "the address to connect to")
)
func main() {
conn, err := grpc.Dial(*addr, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
log.Fatalf("did not connect: %v", err)
}
defer conn.Close()
userClient := user.NewUserClient(conn)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
defer cancel()
userIndexReponse, err := userClient.UserIndex(ctx, &user.UserIndexRequest{Page: 1, PageSize: 12})
if err != nil {
log.Printf("user index could not greet: %v", err)
}
if 0 == userIndexReponse.Err {
log.Printf("user index success: %s", userIndexReponse.Msg)
userEntityList := userIndexReponse.Data
for _, row := range userEntityList {
fmt.Println(row.Name, row.Age)
}
} else {
log.Printf("user index error: %d", userIndexReponse.Err)
}
}
php client
require dirname(__FILE__).'/vendor/autoload.php';
$hostname = !empty($argv[2]) ? $argv[2] : 'localhost:50051';
$client = new \User\UserClient($hostname, [
'credentials' => Grpc\ChannelCredentials::createInsecure(),
]);
$indexRequest = new \User\UserIndexRequest();
$indexRequest->setPage(1);
$indexRequest->setPageSize(12);
list($response, $status) = $client->UserIndex($indexRequest)->wait();
if ($status->code !== Grpc\STATUS_OK) {
echo "ERROR: " . $status->code . ", " . $status->details . PHP_EOL;
}
foreach ($response->getData() as $key=>$value) {
echo "name =>{$value->getName()} --- age =>{$value->getAge()} " . PHP_EOL;
}
echo "index {$response->getMsg()}" . PHP_EOL;
3. 启动GRPC服务
? grpc-demo go run server.go
2022/01/24 09:55:49 receive user index request: page 1 page_size 12
2022/01/24 09:55:49 receive user view request: uid 1
2022/01/24 09:55:49 receive user post request: name 12312 password 324523 age 12
4. 启动GRPC客户端
GO GRPC客户端
? grpc-demo go run client/client.go
2022/01/24 09:56:46 user index success: success
u1 11
u2 12
2022/01/24 09:56:46 user view success: success
james 28
2022/01/24 09:56:46 user post success: success
2022/01/24 09:56:46 user delete success: success
PHP GRPC客户端
? php-server php client.php
success
success
success
|