目录
在rpc服务中添加配置
导入包:
在rpc服务中添加配置:
引入 Consul config 配置项
user.yml 文件
修改 user.go,将 rpc注册到consul
rpc的发现
在api服务中添加配置:
修改api/etc/user.yam 文件
修改 user.yml
修改api/user.go
启动rpc服务和api服务
在rpc服务中添加配置
导入包:
go get -u github.com/zeromicro/zero-contrib/zrpc/registry/consul
在rpc服务中添加配置:
进入项目代码 /gopath/gonivinck/code/go-zero-mall/
修改 service/user/rpc/internal/configrpc/internal/config 文件
引入 Consul config 配置项
vim /usr/src/code/go-zero-mall/service/user/rpc/internal/config/config.go
package config
import (
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/core/stores/cache"
"github.com/zeromicro/go-zero/zrpc"
"github.com/zeromicro/zero-contrib/zrpc/registry/consul"
)
type Config struct {
zrpc.RpcServerConf
Mysql struct {
DataSource string
}
CacheRedis cache.CacheConf
Salt string
//引入consul配置
Consul consul.Conf
Log logx.LogConf
}
user.yml 文件
vim /usr/src/code/go-zero-mall/service/user/rpc/etc/user.yml
Name: user.rpc
ListenOn: 0.0.0.0:9000
#Etcd:
# Hosts:
# - etcd:2379
# Key: user.rpc
Consul:
Host: 192.168.20.51:8500 #consul地址
Key: user.rpc
Log:
level: error
Mysql:
DataSource: root:123456@tcp(mysql:3306)/mall?charset=utf8mb4&parseTime=true&loc=Asia%2FShanghai
CacheRedis:
- Host: redis:6379
Type: node
Pass:
Salt: HWVOFkGgPTryzICwd7qnJaZR9KQ2i8xe
修改 user.go,将 rpc注册到consul
vim /usr/src/code/go-zero-mall/service/user/rpc/user.go
package main
import (
"flag"
"fmt"
"github.com/zeromicro/zero-contrib/zrpc/registry/consul"
"mall/service/user/rpc/internal/config"
"mall/service/user/rpc/internal/server"
"mall/service/user/rpc/internal/svc"
"mall/service/user/rpc/types/user"
"github.com/zeromicro/go-zero/core/conf"
"github.com/zeromicro/go-zero/core/service"
"github.com/zeromicro/go-zero/zrpc"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
)
var configFile = flag.String("f", "etc/user.yaml", "the config file")
func main() {
flag.Parse()
var c config.Config
conf.MustLoad(*configFile, &c)
ctx := svc.NewServiceContext(c)
svr := server.NewUserServer(ctx)
s := zrpc.MustNewServer(c.RpcServerConf, func(grpcServer *grpc.Server) {
user.RegisterUserServer(grpcServer, svr)
if c.Mode == service.DevMode || c.Mode == service.TestMode {
reflection.Register(grpcServer)
}
})
//将 rpc注册到consul
_= consul.RegisterService(c.ListenOn,c.Consul)
defer s.Stop()
fmt.Printf("Starting rpc server at %s...\n", c.ListenOn)
s.Start()
}
rpc的发现
在api服务中添加配置:
修改 api/internal/config config.go 文件
vim /usr/src/code/go-zero-mall/service/user/api/internal/config/config.go
package config
import (
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/rest"
"github.com/zeromicro/go-zero/zrpc"
"github.com/zeromicro/zero-contrib/zrpc/registry/consul"
)
type Config struct {
rest.RestConf
Auth struct {
AccessSecret string
AccessExpire int64
}
UserRpc zrpc.RpcClientConf
Consul consul.Conf
Log logx.LogConf
}
修改api/etc/user.yam 文件
Name: User
Host: 0.0.0.0
Port: 8000
Mysql:
DataSource: root:123456@tcp(mysql:3306)/mall?charset=utf8mb4&parseTime=true&loc=Asia%2FShanghai
CacheRedis:
- Host: redis:6379
Pass:
Type: node
Auth:
AccessSecret: uOvKLmVfztaXGpNYd4Z0I1SiT7MweJhl
AccessExpire: 86400
Log:
Encoding: json
Level: error
UserRpc:
Target: consul://192.168.20.51:8500/user.rpc?wait=14s
Consul:
Host: 192.168.20.51:8500
Key: user.rpc
# Etcd:
# Hosts:
# - etcd:2379
# Key: user.rpc
修改 user.yml
添加一个UserRpc配置,去掉官方自带的Etcd配置
vim /usr/src/code/go-zero-mall/service/user/api/etc/user.yml
Name: User
Host: 0.0.0.0
Port: 8000
Mysql:
DataSource: root:123456@tcp(mysql:3306)/mall?charset=utf8mb4&parseTime=true&loc=Asia%2FShanghai
CacheRedis:
- Host: redis:6379
Pass:
Type: node
Auth:
AccessSecret: uOvKLmVfztaXGpNYd4Z0I1SiT7MweJhl
AccessExpire: 86400
Log:
Encoding: json
Level: error
UserRpc:
Target: consul://192.168.20.51:8500/user.rpc?wait=14s
Consul:
Host: 192.168.20.51:8500
Key: user.rpc
# Etcd:
# Hosts:
# - etcd:2379
# Key: user.rpc
修改api/user.go
vim /usr/src/code/go-zero-mall/service/user/api/user.go
package main
import (
"flag"
"fmt"
"mall/service/user/api/internal/config"
"mall/service/user/api/internal/handler"
"mall/service/user/api/internal/svc"
"github.com/zeromicro/go-zero/core/conf"
"github.com/zeromicro/go-zero/rest"
_ "github.com/zeromicro/zero-contrib/zrpc/registry/consul"
)
var configFile = flag.String("f", "etc/user.yaml", "the config file")
func main() {
flag.Parse()
var c config.Config
conf.MustLoad(*configFile, &c)
ctx := svc.NewServiceContext(c)
server := rest.MustNewServer(c.RestConf)
defer server.Stop()
handler.RegisterHandlers(server, ctx)
fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
server.Start()
}
启动rpc服务和api服务
可以看到rpc服务已经被注册和发现了
root@7ce3fbb23e76:/usr/src/code/go-zero-mall/service/user/rpc# pwd
/usr/src/code/go-zero-mall/service/user/rpc
root@7ce3fbb23e76:/usr/src/code/go-zero-mall/service/user/rpc#
root@7ce3fbb23e76:/usr/src/code/go-zero-mall/service/user/rpc# go run user.go -f etc/user.yaml
Starting rpc server at 0.0.0.0:9000...
{"@timestamp":"2022-10-21T01:56:11.694+08:00","caller":"stat/usage.go:61","content":"CPU: 175m, MEMORY: Alloc=2.2Mi, TotalAlloc=7.9Mi, Sys=14.8Mi, NumGC=3","level":"stat"}
{"@timestamp":"2022-10-21T01:56:11.699+08:00","caller":"load/sheddingstat.go:61","content":"(rpc) shedding_stat [1m], cpu: 175, total: 0, pass: 0, drop: 0","level":"stat"}
{"@timestamp":"2022-10-21T01:57:11.699+08:00","caller":"load/sheddingstat.go:61","content":"(rpc) shedding_stat [1m], cpu: 0, total: 0, pass: 0, drop: 0","level":"stat"}
{"@timestamp":"2022-10-21T01:57:11.699+08:00","caller":"stat/usage.go:61","content":"CPU: 1m, MEMORY: Alloc=3.4Mi, TotalAlloc=11.0Mi, Sys=14.8Mi, NumGC=4","level":"stat"}
root@7ce3fbb23e76:/usr/src/code/go-zero-mall/service/user/api# pwd
/usr/src/code/go-zero-mall/service/user/api
root@7ce3fbb23e76:/usr/src/code/go-zero-mall/service/user/api# go run user.go -f etc/user.yaml
{"@timestamp":"2022-10-21T02:05:44.290+08:00","caller":"consul@v0.0.0-20220909060747-217517c21c16/resolver.go:76","content":"[Consul resolver] 1 endpoints fetched in(+wait) 1.557115ms for target={service='user.rpc' healthy='false' tag=''}","level":"info"}
Starting server at 0.0.0.0:8000...