rpc配置控制着一个rpc服务的各种功能,包含但不限于监听地址,etcd配置,超时,熔断配置等,下面我们以一个常见的rpc服务配置来进行说明。

配置说明

  1. Config struct {
  2. zrpc.RpcServerConf
  3. CacheRedis cache.CacheConf // redis缓存配置,详情见api配置说明,这里不赘述
  4. Mysql struct { // mysql数据库访问配置,详情见api配置说明,这里不赘述
  5. DataSource string
  6. }
  7. }

zrpc.RpcServerConf

  1. RpcServerConf struct {
  2. service.ServiceConf // 服务配置,详情见api配置说明,这里不赘述
  3. ListenOn string // rpc监听地址和端口,如:127.0.0.1:8888
  4. Etcd discov.EtcdConf `json:",optional"` // etcd相关配置
  5. Auth bool `json:",optional"` // 是否开启Auth,如果是则Redis为必填
  6. Redis redis.RedisKeyConf `json:",optional"` // Auth验证
  7. StrictControl bool `json:",optional"` // 是否Strict模式,如果是则遇到错误是Auth失败,否则可以认为成功
  8. // pending forever is not allowed
  9. // never set it to 0, if zero, the underlying will set to 2s automatically
  10. Timeout int64 `json:",default=2000"` // 超时控制,单位:毫秒
  11. CpuThreshold int64 `json:",default=900,range=[0:1000]"` cpu降载阈值,默认900,可允许设置范围01000
  12. }

discov.EtcdConf

  1. type EtcdConf struct {
  2. Hosts []string // etcd host数组
  3. Key string // rpc注册key
  4. }

redis.RedisKeyConf

  1. RedisConf struct {
  2. Host string // redis 主机
  3. Type string `json:",default=node,options=node|cluster"` // redis类型
  4. Pass string `json:",optional"` // redis密码
  5. }
  6. RedisKeyConf struct {
  7. RedisConf
  8. Key string `json:",optional"` // 验证key
  9. }

Rpc示例

配置信息

D:\Projects\Github\NoobWu\go-zero-demo\book\service\user\cmd\rpc\etc\user.yaml

  1. Name: user.rpc
  2. #rpc监听地址和端口,如:127.0.0.1:8888
  3. ListenOn: 127.0.0.1:8080
  4. #etcd相关配置
  5. Etcd:
  6. #etcd host数组
  7. Hosts:
  8. - 127.0.0.1:2379
  9. # rpc注册key
  10. Key: user.rpc
  11. #mysql链接地址,满足 $user:$password@tcp($ip:$port)/$db?$queries 格式即可
  12. Mysql:
  13. DataSource: root:123456@tcp(127.0.0.1:3306)/gozero?charset=utf8mb4&parseTime=true&loc=Asia%2FShanghai
  14. #缓存相关设置
  15. CacheRedis:
  16. - Host: 127.0.0.1:6379
  17. Pass:
  18. Type: node

D:\Projects\Github\NoobWu\go-zero-demo\book\service\user\cmd\rpc\user.go

  1. // Code generated by goctl. DO NOT EDIT!
  2. // Source: user.proto
  3. package main
  4. import (
  5. "flag"
  6. "fmt"
  7. "go-zero-demo/book/service/user/cmd/rpc/internal/config"
  8. "go-zero-demo/book/service/user/cmd/rpc/internal/server"
  9. "go-zero-demo/book/service/user/cmd/rpc/internal/svc"
  10. "go-zero-demo/book/service/user/cmd/rpc/user"
  11. "github.com/tal-tech/go-zero/core/conf"
  12. "github.com/tal-tech/go-zero/zrpc"
  13. "google.golang.org/grpc"
  14. )
  15. var configFile = flag.String("f", "etc/user.yaml", "the config file")
  16. func main() {
  17. flag.Parse()
  18. var c config.Config
  19. conf.MustLoad(*configFile, &c) //读取配置文件信息
  20. ctx := svc.NewServiceContext(c)
  21. srv := server.NewUserServer(ctx)
  22. s := zrpc.MustNewServer(c.RpcServerConf, func(grpcServer *grpc.Server) {
  23. user.RegisterUserServer(grpcServer, srv)
  24. })
  25. defer s.Stop()
  26. fmt.Printf("Starting rpc server at %s...\n", c.ListenOn)
  27. s.Start()
  28. }

启动Rpc服务

  1. PS D:\Projects\Github\NoobWu\go-zero-demo\book\service\user\cmd\rpc> go run .\user.go -f .\etc\user.yaml

image.png
image.png

项目源代码

https://github.com/noobwu/go-zero-demo/tree/master/book/service/user/cmd/rpc

  1. PS D:\Projects\Github\NoobWu\go-zero-demo\book\service\user\cmd\rpc> tree /f
  2. .
  3. user.go
  4. user.proto
  5. ├─etc
  6. user.yaml
  7. ├─internal
  8. ├─config
  9. config.go
  10. ├─logic
  11. getuserlogic.go
  12. ├─server
  13. userserver.go
  14. └─svc
  15. servicecontext.go
  16. ├─user
  17. user.pb.go
  18. └─userclient
  19. user.go

原文链接

https://go-zero.dev/cn/rpc-config.html