当前位置: 首页 > news >正文

潍城区住房和城乡建设局网站购物网站排名

潍城区住房和城乡建设局网站,购物网站排名,济南12345官网,电子商务网站建设概括grpc-gateway grpc-gateway 顾名思义是专门是grpc的网关。也是一个protobuf的编译器#xff0c;是一个proto的插件。 grpc-gateway就是将http请求处理后转发到对应grpc服务上。很多浏览器#xff0c;或者客户端开箱不支持grpc#xff0c;只支持传统的restful API。 grpc网关…grpc-gateway grpc-gateway 顾名思义是专门是grpc的网关。也是一个protobuf的编译器是一个proto的插件。 grpc-gateway就是将http请求处理后转发到对应grpc服务上。很多浏览器或者客户端开箱不支持grpc只支持传统的restful API。 grpc网关而且也支持负载兼容不同版本。 官方文档 grpc-gateway 源码 架构 大致流程如下 写好服务的proto文件。代理grpc 根据proto文件生成反向代理服务代码 根据proto文件生成grpc服务存根 启动反向代理和grpc 客户端使用http json访问 或别的restful api形式 环境安装 protobuf protobuf链接 下载对应环境的porotbuf。解压后bin路径配置环境变量 插件安装 博主 go 用的 1.19 windows预先安装好protobuf go install github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc- gatewayv2.12.0 go install google.golang.org/protobuf/cmd/protoc-gen-gov1.28 go install google.golang.org/grpc/cmd/protoc-gen-go-grpcv1.2可以把对应GO_PATH bin下插件的二进制文件拷到对应go安装的bin目录下 grpc默认网关测试 proto文件 echo.proto syntax proto3; package echo; option go_package echo/proto;message User{int64 id 1;string name 2;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) {}rpc AddOrUpdate(User) returns (User) {}rpc Delete(User) returns (User) {} }生成grpc stub # 生成message protoc --proto_pathproto --go_outproto --go_optpathssource_relative proto/echo.proto # 生成grpc service protoc --proto_pathproto --go-grpc_outproto --go-grpc_optpathssource_relative proto/echo.proto生成默认网关 # 生成gateway protoc --proto_pathproto --grpc-gateway_outproto --grpc-gateway_opt logtostderrtrue --grpc-gateway_opt pathssource_relative --grpc-gateway_opt generate_unbound_methodstrue proto/echo.protogrpc服务器代码 server.go package serverimport (contextecho/protofmt )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 }gateway代码 这里直接用官网http代理的代码。需要修改端口和引用自己的grpc服务和网关package gateway.go package gatewayimport (contextflagnet/httpgithub.com/grpc-ecosystem/grpc-gateway/v2/runtimegoogle.golang.org/grpcgoogle.golang.org/grpc/credentials/insecure_ google.golang.org/grpc/grpcloggw echo/proto // Update )var (// command-line options:// gRPC server endpointgrpcServerEndpoint flag.String(grpc-server-endpoint, localhost:50051, gRPC server endpoint) )func Run() error {ctx : context.Background()ctx, cancel : context.WithCancel(ctx)defer cancel()// Register gRPC server endpoint// Note: Make sure the gRPC server is running properly and accessiblemux : runtime.NewServeMux()opts : []grpc.DialOption{grpc.WithTransportCredentials(insecure.NewCredentials())}// 注册对应grpc服务端点handlererr : gw.RegisterEchoHandlerFromEndpoint(ctx, mux, *grpcServerEndpoint, opts)if err ! nil {return err}// Start HTTP server (and proxy calls to gRPC server endpoint)return http.ListenAndServe(:8081, mux) } 测试 main.go package mainimport (contextecho/echo_server/gatewayecho/echo_server/serverecho/protofmtgoogle.golang.org/grpclognetosos/signaltime )func main() {// 先启动grpc servicego func() {if err : run(); err ! nil {log.Fatal(err)}}()time.Sleep(time.Second * 2)//后启动gatewaygo func() {if err : gateway.Run(); err ! nil {log.Fatal(err)}}()ctx, stop : signal.NotifyContext(context.Background(), os.Interrupt, os.Kill)defer stop()-ctx.Done() }func run() error {lis, err : net.Listen(tcp, :50051)if err ! nil {log.Fatal(err)}s : grpc.NewServer()userServiceServer : server.NewServer()proto.RegisterEchoServer(s, userServiceServer)fmt.Println(listening )return s.Serve(lis) }默认路由 路由为proto文件中{包名}.{服务名}/{方法}。 gateway对外默认是post方法 PS D:\GIT\gorun\grpc-gateway-practice\echo Invoke-RestMethod -Uri http://10.5.81.57:8081/echo.Echo/Get -Method Postid : 0 name : age : 0 phone : addr : 用postman更方便些 总结 grpc-gateway 只是提供一个反向代理可以通过配置进行grpc版本兼容。 grpc-gateway对外提供restful API风格的http接口更好兼容各种客户端接入无需grpc客户端
http://www.dnsts.com.cn/news/2359.html

相关文章:

  • 重庆承越网站建设公司最稳定的灰色词排名
  • 免费获取源码的网站网络整合营销公司
  • 建立电商网站秦皇岛网站seo
  • 网站是用织梦系统做的首页打开超慢进一步优化营商环境
  • 建设网站怎么赚钱十大室内设计网站
  • 万博法务网站2345网址导航
  • 南京做网站的公司排名aso优化注意什么
  • 代办公司注册流程及材料石家庄seo推广公司
  • 2017政府网站建设标准最新新闻事件今天
  • wordpress 视差滚动整站优化的公司
  • 企业是如何做电子商务网站最好的网站优化公司
  • 东莞品牌网站建设天天seo百度点击器
  • 做视频素材网站突发大事震惊全国
  • 国家住房和城乡建设部官方网站百度广告联盟一个月能赚多少
  • 做网站与不做网站的区别seo精华网站
  • 清河哪里做网站百度推广没有效果怎么办
  • 求职简历在哪个网站做今日军事新闻最新消息新闻
  • 南京网站建设关键词热度分析工具
  • ecshop商城模板广州seo网站推广公司
  • 做暧暖爱视频1000部在线网站2023年百度小说风云榜
  • 网站推广咋做的精准大数据获客系统
  • 试玩app推广网站建设网站维护一般都是维护什么
  • 自己怎么拍做美食视频网站如何自己弄个免费网站
  • wordpress推广网站插件网络产品运营与推广
  • 低价建设网站安卓优化大师app
  • h5网站动画怎么做的兰州网站seo
  • 宁波网站设计企业微信做单30元一单
  • 宝塔怎么做两个网站360摄像头海澳門地区限制解除
  • 源代码如何做网站搜索关键词优化服务
  • 没有网站服务器空间如何用ftp营销模式方案