首页 > 其他分享 >hertz集成kitex筆記

hertz集成kitex筆記

时间:2023-01-14 18:00:21浏览次数:36  
标签:筆記 helloworld rpc api srv hertz go kitex

安裝代碼生成工具

go install github.com/cloudwego/hertz/cmd/hz@latest

生成代碼

新建項目
並新建三個文件夾web-srv, rpc-srv, idl
其中web-srv中是後臺服務, rpc-srv用於放基於kitex的rpc服務器,idl中的是proto文件,用於定義接口

定義idl文件用於生成代碼
helloworld.proto
這個文件用於定義rpc服務的接口

syntax = "proto3";

option go_package="helloworld";
package helloworld;

message StreamReq {
  string name = 1;
}

message StreamResp {
  string greet = 1;
}

service StreamGreeter {
  rpc greet(StreamReq) returns (StreamResp);
}

helloworld_api.thrift
這個文件用於定義後臺服務的接口

namespace go helloworld_api

struct HelloworldReq {
  1: string Name(api.query="name");
}

struct HelloworldResp {
  1: string Greet;
}

service HelloworldApi {
  HelloworldResp Greet(1: HelloworldReq req) (api.get="/hello");
}

然後進入web-srv文件夾生成所需代碼

cd web-srv
hz new -module=hz-kitex-helloworld-demo/web-srv -idl ../idl/helloworld_api.thrift
kitex -module="hz-kitex-helloworld-demo/web-srv" -I=../idl helloworld.proto
go mod tidy

如果修改了thrift文件,那麼可以使用如下命令更新生成的代碼

hz update -idl ../idl/helloworld_api.thrift

然後是生成rpc端的代碼

cd rpc-srv
kitex -module="hz-kitex-helloworld-demo/rpc-srv" -service=helloworld -I=../idl helloworld.proto
go mod tidy

生成出的代碼結構最終如下

├── idl
│   ├── helloworld_api.thrift
│   └── helloworld.proto
├── rpc-srv
│   ├── build.sh
│   ├── go.mod
│   ├── go.sum
│   ├── handler.go
│   ├── kitex_gen
│   │   └── helloworld
│   │   ├── helloworld.pb.fast.go
│   │   ├── helloworld.pb.go
│   │   └── streamgreeter
│   │   ├── client.go
│   │   ├── invoker.go
│   │   ├── server.go
│   │   └── streamgreeter.go
│   ├── kitex.yaml
│   ├── main.go
│   ├── output
│   │   ├── bin
│   │   │   └── helloworld
│   │   ├── bootstrap.sh
│   │   └── log
│   │   ├── app
│   │   └── rpc
│   └── script
│   └── bootstrap.sh
└── web-srv
├── biz
│   ├── handler
│   │   ├── helloworld_api
│   │   │   └── helloworld_api.go
│   │   └── ping.go
│   ├── model
│   │   └── helloworld_api
│   │   └── helloworld_api.go
│   └── router
│   ├── helloworld_api
│   │   ├── helloworld_api.go
│   │   └── middleware.go
│   └── register.go
├── go.mod
├── go.sum
├── kitex_gen
│   └── helloworld
│   ├── helloworld.pb.fast.go
│   ├── helloworld.pb.go
│   └── streamgreeter
│   ├── client.go
│   ├── invoker.go
│   ├── server.go
│   └── streamgreeter.go
├── main.go
├── router_gen.go
└── router.go

實現服務

實現rpc端接口

生成rpc服務端代碼之後需要實現其接口
修改rpc-srv/handler.go文件

package main

import (
	"context"
	"fmt"
	helloworld "hz-kitex-helloworld-demo/rpc-srv/kitex_gen/helloworld"
)

// StreamGreeterImpl implements the last service interface defined in the IDL.
type StreamGreeterImpl struct{}

// Greet implements the StreamGreeterImpl interface.
func (s *StreamGreeterImpl) Greet(ctx context.Context, req *helloworld.StreamReq) (resp *helloworld.StreamResp, err error) {
	resp = &helloworld.StreamResp{Greet: fmt.Sprintf("Hello, %s", req.GetName())}
	return
}

實現web端接口

修改web-srv/biz/handler目錄下的接口文件
例如這個項目中的web-srv/biz/handler/helloworld_api/helloworld_api.go

// Code generated by hertz generator.

package helloworld_api

import (
	"context"
	"fmt"
	"github.com/cloudwego/kitex/client"
	"hz-kitex-helloworld-demo/web-srv/kitex_gen/helloworld"
	"hz-kitex-helloworld-demo/web-srv/kitex_gen/helloworld/streamgreeter"

	"github.com/cloudwego/hertz/pkg/app"
	"github.com/cloudwego/hertz/pkg/protocol/consts"
	helloworld_api "hz-kitex-helloworld-demo/web-srv/biz/model/helloworld_api"
)

// Greet .
// @router /hello [GET]
func Greet(ctx context.Context, c *app.RequestContext) {
	var err error
	var req helloworld_api.HelloworldReq
	err = c.BindAndValidate(&req)
	if err != nil {
		c.String(consts.StatusBadRequest, err.Error())
		return
	}

	// rpc call begin
	cli, err := streamgreeter.NewClient("helloworld", client.WithHostPorts("0.0.0.0:8888"))
	if err != nil {
		panic(err)
	}
	reqRpc := &helloworld.StreamReq{Name: req.GetName()}
	respRpc, err := cli.Greet(ctx, reqRpc)
	if err != nil {
		panic(err)
	}
	// rpc call end

	resp := new(helloworld_api.HelloworldResp)
	resp.Greet = fmt.Sprintf("Hello, %s", respRpc.GetGreet())

	c.JSON(consts.StatusOK, resp)
}

然後修改web端服務的一些配置
修改web-srv/main.go
配置服務的port

// Code generated by hertz generator.

package main

import (
	"github.com/cloudwego/hertz/pkg/app/server"
)

func main() {
	h := server.Default(
		server.WithHostPorts("127.0.0.1:8080"),
	)

	register(h)
	h.Spin()
}

啓動服務

啓動rpc端

sh rpc-srv/build.sh
sh rpc-srv/output/bootstrap.sh

啓動web端

go run ./web-srv

最後訪問localhost:8080/hello?name=tom
可以看到的相應的json信息

{
    "Greet": "Hello, Hello, tom"
}

标签:筆記,helloworld,rpc,api,srv,hertz,go,kitex
From: https://www.cnblogs.com/poifa/p/17052290.html

相关文章

  • 简单易用的监控告警 | HertzBeat 在 Rainbond 上的使用分享
    在现有的监控告警体系中Prometheus+AlertManger+Grafana一直是主流,但对于中小团队或个人来说,这种体系显的较为复杂。而HertzBeat能让中小团队或个人很快速的搭建监......
  • 玩转 Go 生态|Hertz WebSocket 扩展简析
    WebSocket是一种可以在单个TCP连接上进行全双工通信,位于OSI模型的应用层。WebSocket使得客户端和服务器之间的数据交换变得更加简单,允许服务端主动向客户端推送数据......
  • kitex使用系列(一):kitex的安装与快速启动
    kitex介绍kitex是字节跳动内部使用的Golang微服务RPC框架。具有高性能、强可扩展的特点。kitex使用字节跳动自研的Netpoll网络库,比gonet的性能更高,这也是kitex高性能的......
  • 使用 Go HTTP 框架 Hertz 进行 JWT 认证
    前言上一篇文章简单介绍了一个高性能的GoHTTP框架——Hertz,本篇文章将围绕Hertz开源仓库的一个demo,讲述如何使用Hertz完成JWT的认证与授权流程。这里要说明的......
  • phoenix 筆記
    初始化sudoapt-getinstallinotify-toolsgitclonehttps://github.com/phoenixframework/phoenix--depth=1cdphoenix/installermixphx.newdev_app--dev#這行......
  • kitex报错解决
    1、linux系统安装kitex工具还是报错commandnotfound//找到etc/profile文件,输入如下(path是kitex的安装目录)exportPATH=$PATH:/root/goCode/bin//更新配置source/......
  • 【筆記】虛數類
    C++爲我們提供的虛數類不僅功能全面,性能上也吊打自己寫的虛數類。所以學會使用C++的虛數類還是較有必要的。數學函數:abs()arg()conj()polar()cos()cosh()exp()log......
  • 字节微服务HTTP框架Hertz使用与源码分析|拥抱开源
    一、前言Hertz[həːts]是一个Golang微服务HTTP框架,在设计之初参考了其他开源框架fasthttp、gin、echo的优势,并结合字节跳动内部的需求,使其具有高易用性、高性能......