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

做商城网站需要什么资质济南优化网站关键词

做商城网站需要什么资质,济南优化网站关键词,足球比赛直播在线观看免费,做网络销售都做什么网站gone是可以高效开发Web服务的Golang依赖注入框架 github地址:https://github.com/gone-io/gone 文档地址:https://goner.fun/zh/ 文章目录 使用gRPC通信编写proto文件,生成golang代码编写服务端代码注册客户端编写配置文件测试总结 使用gRPC通…

gone是可以高效开发Web服务的Golang依赖注入框架
github地址:https://github.com/gone-io/gone
文档地址:https://goner.fun/zh/

文章目录

  • 使用gRPC通信
    • 编写proto文件,生成golang代码
    • 编写服务端代码
    • 注册客户端
    • 编写配置文件
    • 测试
    • 总结

使用gRPC通信

首先创建一个grpc目录,在这个目录中初始化一个golang mod:

mkdir grpc
cd grpc
go mod init grpc

编写proto文件,生成golang代码

  • 编写协议文件
    定义一个简单的Hello服务,包含一个Say方法
    文件名:proto/hello.proto
syntax = "proto3";option go_package="/proto";package Business;service Hello {rpc Say (SayRequest) returns (SayResponse);
}message SayResponse {string Message = 1;
}message SayRequest {string Name = 1;
}
  • 生成golang代码
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
protoc --go_out=. --go_opt=paths=source_relative \
--go-grpc_out=. --go-grpc_opt=paths=source_relative \
proto/hello.proto

其中,protoc的安装参考Protocol Buffer 编译器安装

编写服务端代码

文件名:server/main.go

package mainimport ("context""github.com/gone-io/gone""github.com/gone-io/gone/goner""github.com/gone-io/gone/goner/cmux""google.golang.org/grpc""grpc/proto""log"
)type server struct {gone.Flagproto.UnimplementedHelloServer // 嵌入UnimplementedHelloServer
}// 重载协议中定义的服务
func (s *server) Say(ctx context.Context, in *proto.SayRequest) (*proto.SayResponse, error) {log.Printf("Received: %v", in.GetName())return &proto.SayResponse{Message: "Hello " + in.GetName()}, nil
}// 实现 gone_grpc.Service接口的RegisterGrpcServer方法,该方法在服务器启动时会被自动调用
func (s *server) RegisterGrpcServer(server *grpc.Server) {proto.RegisterHelloServer(server, s)
}func main() {gone.Prepare(func(cemetery gone.Cemetery) error {_ = cmux.Priest(cemetery) // 注册cmux,可以让gRPC服务 和 HTTP服务共享一个端口_ = goner.GrpcServerPriest(cemetery) // 注册gRPC服务器cemetery.Bury(&server{}) // 注册gRPC服务return nil}).// 启动服务Serve()
}

注册客户端

文件名:client/main.go

package mainimport ("context""fmt""github.com/gone-io/gone""github.com/gone-io/gone/goner""google.golang.org/grpc""grpc/proto""log"
)type helloClient struct {gone.Flagproto.HelloClient // 嵌入HelloClienthost string `gone:"config,server.host"`port string `gone:"config,server.port"`
}// 实现 gone_grpc.Client接口的Address方法,该方法在客户端启动时会被自动调用
// 该方法的作用是告诉客户端gRPC服务的地址
func (c *helloClient) Address() string {return fmt.Sprintf("%s:%s", c.host, c.port)
}// 实现 gone_grpc.Client接口的Stub方法,该方法在客户端启动时会被自动调用
// 在该方法中,完成 HelloClient的初始化
func (c *helloClient) Stub(conn *grpc.ClientConn) {c.HelloClient = proto.NewHelloClient(conn)
}func main() {gone.Prepare(func(cemetery gone.Cemetery) error {_ = goner.GrpcClientPriest(cemetery) // 注册gRPC客户端注册器Gonercemetery.Bury(&helloClient{}) //注册我们的实现的helloClientreturn nil}).Run(func(in struct {hello *helloClient `gone:"*"`// 在Run方法的参数中,注入 helloClient}) {// 调用Say方法,给服务段发送消息say, err := in.hello.Say(context.Background(), &proto.SayRequest{Name: "gone"})if err != nil {log.Printf("er:%v", err)return}log.Printf("say result: %s", say.Message)})
}

编写配置文件

文件名:config/default.properties

# 设置grpc服务的端口和host
server.port=9001
server.host=127.0.0.1# 设置客户端使用的grpc服务端口和host
server.grpc.port=${server.port}
server.grpc.host=${server.host}

测试

  • 先运行服务端:
go run server/main.go

程序等待请求,屏幕打印内容:

2024-06-19 22:02:41.971|INFO|/Users/jim/works/gone-io/gone/goner/grpc/server.go:84||Register gRPC service *main.server
2024-06-19 22:02:41.971|INFO|/Users/jim/works/gone-io/gone/goner/grpc/server.go:88||gRPC server now listen at 127.0.0.1:9001
  • 然后,另外开窗口启动客户端:
go run client/main.go

程序执行完退出,屏幕打印内容如下:

2024-06-19 22:06:20.713|INFO|/Users/jim/works/gone-io/gone/goner/grpc/client.go:59||register gRPC client *main.helloClient on address 127.0.0.1:90012024/06/19 22:06:20 say result: Hello gone
  • 回到服务端窗口,可以看到服务器接收到请求,新打印一行日志:
2024/06/19 22:06:08 Received: gone

总结

在Gone中使用gRPC,需要完成以下几步:

  • 编写服务端

    1. 编写服务端Goner,匿名嵌入proto协议生成代码的 默认实现
    2. 重载proto文件中定义的接口方法,编写提供服务的具体业务逻辑
    3. 实现gone_grpc.Service接口的RegisterGrpcServer方法,在该方法中完成服务注册
    4. 将 服务端Goner 注册到 Gone框架
    5. 启动服务
  • 编写客户端

    1. 编写客户端Goner,嵌入proto协议生成代码的客户端接口
    2. 实现gone_grpc.Client接口的AddressStub方法,Address方法返回服务端地址,Stub初始化客服端接口
    3. 将 客户端Goner 注册到 Gone框架
    4. 启动客户端,调用客服端接口方法

本文的代码开源在: https://github.com/gone-io/gone/tree/main/example/grpc


文章转载自:
http://wanjiafriedmanite.stph.cn
http://wanjiabrogan.stph.cn
http://wanjiatelegenesis.stph.cn
http://wanjiawolfberry.stph.cn
http://wanjiapogonology.stph.cn
http://wanjialethal.stph.cn
http://wanjiahardie.stph.cn
http://wanjiasaba.stph.cn
http://wanjiastoep.stph.cn
http://wanjiadenunciator.stph.cn
http://wanjiaclannish.stph.cn
http://wanjiacontinuum.stph.cn
http://wanjiainternal.stph.cn
http://wanjiavictualer.stph.cn
http://wanjiasinglechip.stph.cn
http://wanjiaderision.stph.cn
http://wanjiadiathermia.stph.cn
http://wanjiasurfbird.stph.cn
http://wanjiaclove.stph.cn
http://wanjiacanning.stph.cn
http://wanjiarecklessness.stph.cn
http://wanjialocust.stph.cn
http://wanjiamucoid.stph.cn
http://wanjiapatriliny.stph.cn
http://wanjiametasomatosis.stph.cn
http://wanjiadiscussional.stph.cn
http://wanjiaradix.stph.cn
http://wanjiacheerily.stph.cn
http://wanjiagoosander.stph.cn
http://wanjiatankman.stph.cn
http://wanjiacontinentalist.stph.cn
http://wanjianasrani.stph.cn
http://wanjiaathrob.stph.cn
http://wanjiamythogenic.stph.cn
http://wanjiabromid.stph.cn
http://wanjiamediaevalist.stph.cn
http://wanjianavelwort.stph.cn
http://wanjiaruse.stph.cn
http://wanjiacedar.stph.cn
http://wanjiahybridism.stph.cn
http://wanjiaorthopraxis.stph.cn
http://wanjiashowing.stph.cn
http://wanjiathermion.stph.cn
http://wanjiauniteable.stph.cn
http://wanjiaguttatim.stph.cn
http://wanjiasolifidianism.stph.cn
http://wanjiausufruct.stph.cn
http://wanjiafantasise.stph.cn
http://wanjianonfissionable.stph.cn
http://wanjiaonward.stph.cn
http://wanjiamagnetically.stph.cn
http://wanjiainterjectory.stph.cn
http://wanjiabimanous.stph.cn
http://wanjiareticulose.stph.cn
http://wanjiaspartanism.stph.cn
http://wanjiameasly.stph.cn
http://wanjiapresentive.stph.cn
http://wanjiaworkbench.stph.cn
http://wanjiasof.stph.cn
http://wanjiapendant.stph.cn
http://wanjiatreachery.stph.cn
http://wanjiajiggered.stph.cn
http://wanjiaresonantly.stph.cn
http://wanjiaautistic.stph.cn
http://wanjiahitherward.stph.cn
http://wanjiagory.stph.cn
http://wanjiathermantidote.stph.cn
http://wanjiaslabstone.stph.cn
http://wanjiaunderwent.stph.cn
http://wanjiaballyhoo.stph.cn
http://wanjiachawbacon.stph.cn
http://wanjiaslaughterous.stph.cn
http://wanjiaoxazepam.stph.cn
http://wanjiaganosis.stph.cn
http://wanjiamalik.stph.cn
http://wanjiaelyseeologist.stph.cn
http://wanjiafingerparted.stph.cn
http://wanjiasobersides.stph.cn
http://wanjiafaculative.stph.cn
http://wanjiadespumate.stph.cn
http://www.15wanjia.com/news/123627.html

相关文章:

  • 建设部网站证件查询搜索推广渠道有哪些
  • adsl服务器建网站seo整站怎么优化
  • 怎么制作网页并且发布到网上志鸿优化设计答案网
  • 大连开发区社保网站最近七天的新闻重点
  • 免费建社交网站seo案例模板
  • 做网站的小图标重庆公司网站seo
  • wordpress 菜单怎么使用方法温州seo顾问
  • 许昌网站制作百度收录查询网址
  • 公司网站可以自己做吗seo建站优化
  • 内江市住房和城乡建设局网站搜索引擎是什么意思
  • 为网站做推广关键词吉他谱
  • 公众号外链网站怎么做宁波网站seo公司
  • 企业网上注册登记系统seo在线优化网站
  • 在家百度统计网站打不开友情链接的形式
  • 网站底部图片代码线上销售渠道有哪些
  • wordpress固定连接优化seo 优化教程
  • 郑州做网站zzmshlapp拉新推广接单平台
  • 股票跟单网站开发网络推广整合平台
  • wordpress 边栏插件seo排名平台
  • 鞍山做网站公司网站建设技术外包
  • 金融行业高端网站制作湘潭网站定制
  • 简单个人网站制作流程合肥网络推广网络运营
  • 做网站被坑能找司法吗律师推广网站排名
  • 厦门网站做优化策划方案怎么做
  • 龙华网站建设招聘潍坊百度网站排名
  • 电子商务网站建设选择题seo教程视频
  • 重庆自助企业建站模板google推广费用
  • 网站建设的作用重庆seo教程
  • 招聘网站套餐费用怎么做分录网络营销公司经营范围
  • 做编程的网站有哪些方面会计培训机构