Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use GRPC in golang

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

How to use GRPC in golang? for this problem, this article introduces the corresponding analysis and answer in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.

Write proto intermediate files

Syntax= "proto3"; package services;message ProdRequest {int32 prod_id=1;} message ProdResponse {int32 prod_stock=1;} generate Prod.pb.go file

Go to the directory where the proto file is located

Protoc-go_out=../services Prod.protoproto file year add rpc interface definition syntax= "proto3"; package services;message ProdRequest {int32 prod_id=1;} message ProdResponse {int32 prod_stock=1;} service ProdService {rpc GetProdStock (ProdRequest) returns (ProdResponse);} generate rpc interface Prod.pb.go file protoc-go_out=plugins=grpc:../services Prod.protogrpc-gateway function

Provide http access to rpc services externally

Download dependent go get-u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway go get-u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger to modify proto definition syntax= "proto3"; package services;import "google/api/annotations.proto"; message ProdRequest {int32 prod_id=1;} message ProdResponse {int32 prod_stock=1 } service ProdService {rpc GetProdStock (ProdRequest) returns (ProdResponse) {option (google.api.http) = {get: "/ v1/prod/ {prod_id}"} }} protoc generates gateway class protoc-this is the answer to grpc-gateway_out=logtostderr=true:../services Prod.proto 's question on how to use GRPC in golang. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report