golang 工程组件:grpc

编程入门 行业动态 更新时间:2024-10-28 08:31:37

golang 工程<a href=https://www.elefans.com/category/jswz/34/1771375.html style=组件:grpc"/>

golang 工程组件:grpc

option自定义http规则和http body响应

简介

本篇接上文

golang 工程组件:grpc-gateway 环境安装+默认网关测试

默认网关配置终究是难用,本篇介绍一下proto里采用option自定义http规则以及让网关返回http响应而不是我们定义的grpc响应

option定义http规则和httpbody响应

引入库。可以直接拷贝grpc-gateway源码下google文件夹到项目下

import "google/api/annotations.proto";
import "google/api/httpbody.proto";
import "google/protobuf/empty.proto";

user.proto

syntax = "proto3";
package  echo;
option go_package = "echo/proto";import "google/api/annotations.proto";
import "google/api/httpbody.proto";
import "google/protobuf/empty.proto";message User{int64 id = 1;// 改成下划线形式string userName = 2[json_name="user_name"];int32 age = 3;string phone = 4;Addr addr = 5;
}message Addr {string province = 1;string city = 2;string county = 3;
}service Echo{rpc Get(User) returns (User) {//get请求option (google.api.http) = {get: "/echo/user/{id}"};}rpc AddOrUpdate(User) returns (User) {option (google.api.http) = {post: "/echo/user"// * 表示接受user所有字段body: "*"additional_bindings {put: "/echo/user"body: "*"}//patch 请求,只更新部分字段additional_bindings {patch: "/echo/user"body: "addr"}};}rpc Delete(User) returns (User) {option (google.api.http) = {delete: "/echo/user/{id}"};}// httpbody响应,前面是grpc定义的消息rpc List(google.protobuf.Empty) returns (stream google.api.HttpBody) {option (google.api.http) = {get: "/echo/user/list"};}
}

对应grpc实现

server.go

package serverimport ("context""echo/proto""fmt""github/golang/protobuf/jsonpb"_ "github/golang/protobuf/jsonpb""google.golang/genproto/googleapis/api/httpbody"_ "google.golang/genproto/googleapis/api/httpbody""google.golang/protobuf/types/known/emptypb"_ "google.golang/protobuf/types/known/emptypb"
)type echoServer struct {proto.UnimplementedEchoServer
}func NewServer() proto.EchoServer {return &echoServer{}
}
func (s *echoServer) Get(ctx context.Context, in *proto.User) (*proto.User, error) {fmt.Printf("%+v\n", in)return in, nil
}
func (s *echoServer) AddOrUpdate(ctx context.Context, in *proto.User) (*proto.User, error) {fmt.Printf("%+v\n", in)return in, nil
}
func (s *echoServer) Delete(ctx context.Context, in *proto.User) (*proto.User, error) {fmt.Printf("%+v\n", in)return in, nil
}func (s *echoServer) List(in *emptypb.Empty, stream proto.Echo_ListServer) error {userList := []*proto.User{{Id:       1,UserName: "test1",Addr: &proto.Addr{Province: "深圳1",},},{Id:       2,UserName: "test2",Addr: &proto.Addr{Province: "深圳2",},},{Id:       3,UserName: "test3",Addr: &proto.Addr{Province: "深圳3",},},}for _, u := range userList {//jsonpb库序列化返回的才是下划线形式。 json序列化不读tag里定义m := jsonpb.Marshaler{}data, _ := m.MarshalToString(u)msg := &httpbody.HttpBody{ContentType: "application/json",Data:        []byte(data),}stream.Send(msg)}return nil
}

启动后按对应路由访问即可。 网关和启动源码在上文里

更多推荐

golang 工程组件:grpc

本文发布于:2023-12-03 23:09:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1658757.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组件   工程   golang   grpc

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!