安裝consul依賴
go get -u github.com/zeromicro/zero-contrib/zrpc/registry/consul
rpc端
修改rpc配置
user/rpc/etc/user.yaml
Name: user.rpc
ListenOn: 0.0.0.0:9000
#Etcd:
# Hosts:
# - 127.0.0.1:2379
# Key: user.rpc
Consul:
Host: 127.0.0.1:8500 #consul地址
Key: user.rpc
Mysql:
DataSource: go_zero_shop:123456@tcp(127.0.0.1:3306)/go_zero_shop?charset=utf8mb4&parseTime=true&loc=Asia%2FShanghai
CacheRedis:
- Host: 127.0.0.1:6379
Type: node
Pass:
Salt: HWVOFkGgPTryzICwd7qnJaZR9KQ2i8xe
增加consul配置
user/rpc/internal/config/config.go
package config
import (
"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
}
修改 user.go,將 rpc注冊到consul
user/rpc/user.go
package main
import (
"flag"
"fmt"
"github.com/zeromicro/zero-contrib/zrpc/registry/consul"
"go-zero-shop/user/rpc/internal/config"
"go-zero-shop/user/rpc/internal/server"
"go-zero-shop/user/rpc/internal/svc"
"go-zero-shop/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)
s := zrpc.MustNewServer(c.RpcServerConf, func(grpcServer *grpc.Server) {
user.RegisterUserServer(grpcServer, server.NewUserServer(ctx))
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()
}
api端
修改api配置
user/api/etc/user-api.yaml
Name: User-api
Host: 0.0.0.0
Port: 8002
Mysql:
DataSource: go_zero_shop:123456@tcp(127.0.0.1:3306)/go_zero_shop?charset=utf8mb4&parseTime=true&loc=Asia%2FShanghai
CacheRedis:
Host:
- 127.0.0.1:6379
Type: node
Pass:
Auth:
AccessSecret: uOvKLmVfztaXGpNYd4Z0I1SiT7MweJhl
AccessExpire: 86400
UserRpc:
Target: consul://127.0.0.1:8500/user.rpc?wait=14s
Consul:
Host: 127.0.0.1:8500
Key: user.rpc
#UserRpc:
# Etcd:
# Hosts:
# - 127.0.0.1:2379
# Key: user.rpc
增加consul配置
user/api/internal/config/config.go
package config
import (
"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
}
修改 user.go
user/api/user.go
package main
import (
"flag"
"fmt"
"go-zero-shop/user/api/internal/config"
"go-zero-shop/user/api/internal/handler"
"go-zero-shop/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-api.yaml", "the config file")
func main() {
flag.Parse()
var c config.Config
conf.MustLoad(*configFile, &c)
server := rest.MustNewServer(c.RestConf)
defer server.Stop()
ctx := svc.NewServiceContext(c)
handler.RegisterHandlers(server, ctx)
fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
server.Start()
}