🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
> ### 第二例 gRPC使用例子 * 第二例代码 : 链接:[https://pan.baidu.com/s/1VhfvHols\_lXlaf-gIwEcRA](https://pan.baidu.com/s/1VhfvHols_lXlaf-gIwEcRA)密码:iq75 * 其它gRPC例子 :[https://gitee.com/winnie\_gss/microService](https://gitee.com/winnie_gss/microService) * 如果是手动下载golang/protobuf, 需要编译后将protoc-gen-go.exe文件放在bin目录下 ~~~ go get github.com/golang/protobuf/proto go get github.com/golang/protobuf/protoc-gen-go go get github.com/grpc/grpc-go 修改文件名: google.golang.org/grpc go get github.com/golang/net 修改文件名: golang.org/x/net go get github.com/golang/text 修改文件名: golang.org/x/text go get github.com/google/go-genproto 修改文件名:google.golang.org/genproto ~~~ * 编译.proto文件 (protoc生成Go文件: protoc --go\_out=plugins=grpc:. ./article.proto) ~~~ syntax = "proto3"; //留言板 package article; option java_package = "source-open.com.messagebox.article"; service Article { //TODO : 留言板列表页 rpc ArticleList (ArticleListRequest) returns (ArticleListResponse) { } //TODO : 添加留言 //TODO : 获取一条留言 //TODO : 修改一条留言 //TODO : 观看一条留言 } enum RPC_RESULT { RESULT_FAIL = 0; //失败 RESULT_SUCCESS = 1; //成功 } enum ARTICLE_SORT { ORDER_BY_TIME = 0; //最新时间 ORDER_BY_CLICK = 1; //点击次数 } message ArticleStruct { uint32 id = 1; //文章id string title = 2; //标题 bytes content = 3; //内容 uint32 viewnum = 4; //点击次数 uint32 time = 5; //发布时间 } message ArticleListRequest { ARTICLE_SORT sort = 1; //排序方式 } message ArticleListResponse { RPC_RESULT code = 1; //调用结果 string msg = 2; //错误消息 repeated ArticleStruct list = 3; //列表数据 } ~~~ * 客户端代码 ~~~ package main import ( "context" "fmt" _ "github.com/go-sql-driver/mysql" "github.com/jmoiron/sqlx" "google.golang.org/grpc" "google.golang.org/grpc/reflection" "log" pb "messagebox/article/article" "net" "runtime" ) type server struct { } type ArticleData struct { Id uint32 `db:"id"` Title string `db:"title"` Content []byte `db:"content"` Viewnum uint32 `db:"viewnum"` Time uint32 `db:"time"` } func (s *server) ArticleList(ctx context.Context, request *pb.ArticleListRequest) (response *pb.ArticleListResponse, err error) { orderby := "time" if request.Sort == pb.ARTICLE_SORT_ORDER_BY_TIME { orderby = "time" } if request.Sort == pb.ARTICLE_SORT_ORDER_BY_CLICK { orderby = "viewnum" } host := "localhost" dbname := "messagebox" username := "root" password := "123456" db, err := sqlx.Open("mysql", username+":"+password+"@tcp("+host+")/"+dbname+"?charset=utf8") SimplePanic(err) defer db.Close() //定义一个结构重新赋值 var list1 []*ArticleData var list2 []*pb.ArticleStruct sql := fmt.Sprintf("select id, title, content, viewnum, time from msg_article order by %s desc limit 10", orderby) err = db.Select(&list1, sql) SimplePanic(err) for _, v := range list1 { var list *pb.ArticleStruct = &pb.ArticleStruct{ Id: v.Id, Title: v.Title, Content: v.Content, Viewnum: v.Viewnum, Time: v.Time, } list2 = append(list2, list) } return &pb.ArticleListResponse{Code: pb.RPC_RESULT_RESULT_SUCCESS, Msg: "", List: list2}, nil } func main() { //绑定和监听一个端口 listen, err := net.Listen("tcp", "127.0.0.1:9005") if err != nil { fmt.Println(err) } //创建 gRPC 服务器的一个实例(初始化配置) s := grpc.NewServer() //绑定一个实现该gRPC的服务 pb.RegisterArticleServer(s, &server{}) //在gRPC上注册服务 reflection.Register(s) //启动服务(阻塞等待客户端连接) if err := s.Serve(listen); err != nil { log.Fatalf("failed to serve: %v", err) } } func SimplePanic(err error) { if err != nil { _, file, line, _ := runtime.Caller(1) fmt.Println(file, line, err) runtime.Goexit() } } ~~~ * 服务端代码 ~~~ package main import ( "fmt" "golang.org/x/net/context" //"context"标准包 "google.golang.org/grpc" pb "messagebox/article/article" "time" ) func main() { conn, err := grpc.Dial("127.0.0.1:9005", grpc.WithInsecure()) if err != nil { fmt.Println(err) } defer conn.Close() client := pb.NewArticleClient(conn) ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) defer cancel() response, err := client.ArticleList(ctx, &pb.ArticleListRequest{Sort: pb.ARTICLE_SORT_ORDER_BY_TIME}) if err != nil { fmt.Println(err) } fmt.Println(response) } ~~~