kratos discovery
常见的微服务注册中心
- consul
- etcd
- zookeeper
- Eureka
- kratos(discovery)
discovery
安装
源码安装
- github
- 下载生成可执行文件
1
2
3
git clone https://github.com/bilibili/discovery.git
cd discovery/cmd/discovery
go build
- 启动
./discovery -conf discovery-example.toml -alsologtostderr
生成 docker images
docker build -t cc360428/discovery:latest .
docker 安装
docker run -itd --name disvoery -p 7171:7171 lichaocheng/discovery
注册
使用 kraots 之前
set DISCOVERY_NODES=127.0.0.1:7171
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package main
import (
"context"
"flag"
"github.com/go-kratos/kratos/pkg/conf/env"
"github.com/go-kratos/kratos/pkg/conf/paladin"
"github.com/go-kratos/kratos/pkg/naming"
"github.com/go-kratos/kratos/pkg/naming/discovery"
"github.com/go-kratos/kratos/pkg/net/rpc/warden/resolver"
"im_square/internal/dao"
"im_square/internal/di"
"os"
"os/signal"
"syscall"
"time"
"github.com/go-kratos/kratos/pkg/log"
)
func main() {
flag.Parse()
log.Init(&log.Config{Stdout: true}) // debug flag: log.dir={path}
defer log.Close()
log.Info("im_square start")
_ = paladin.Init()
resolver.Register(discovery.Builder())
_, closeFunc, err := di.InitApp()
if err != nil {
panic(err)
}
//consul 注册发现
var RegCancel context.CancelFunc
if _gRPCAddr != "" {
hn, _ := os.Hostname()
dis := discovery.New(nil)
ins := &naming.Instance{
Zone: env.Zone,
Env: env.DeployEnv,
AppID: env.AppID,
Hostname: hn,
Addrs: []string{
"grpc://" + _gRPCAddr,
},
}
if cancel, err := dis.Register(context.Background(), ins); err != nil {
panic(err)
} else {
RegCancel = cancel
}
}
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT)
for {
s := <-c
log.Info("get a signal %s", s.String())
switch s {
case syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT:
closeFunc()
RegCancel()
log.Info("main exit")
time.Sleep(time.Second)
return
case syscall.SIGHUP:
default:
return
}
}
}
var _gRPCAddr string
func init() {
addFlag(flag.CommandLine)
dao.SynchronizationRedisAll()
}
func addFlag(fs *flag.FlagSet) {
v := os.Getenv("DISCOVERY_GRPC_ADDR")
fs.StringVar(&_gRPCAddr, "grpc.addr", v, "app listen grpc port, default: nil")
}
发现
发现方式两种:1、直接连接输入 服务端 IP 地址;2、通过 discovery 注册中心(或其他的注册中心)
- 获取要发现的服务的 proto 文件生成对应的 pd 文件
- 设置 discovery 节点地址
- client 代码
- 通过 discovery 注册中心连接
1
2
3
4
5
6
7
8
9
10
11
12
13
14
const AppID = "square_recom_ai_v1"
// new grpc client
func NewAiRecomClient(cfg *warden.ClientConfig, opts ...grpc.DialOption) (SquareRecomAiClient, error) {
client := warden.NewClient(cfg, opts...)
cc, err := client.Dial(context.Background(), fmt.Sprintf("discovery://default/%s", AppID))
if err != nil {
log.Error("square_recom_ai_error", err.Error())
return nil, err
//return nil, nil
}
log.Info("square_recom_ai like ok")
return NewSquareRecomAiClient(cc), nil
}
- 已知服务 IP 地址
1
2
3
4
5
6
7
8
9
10
11
12
13
// NewImSquareClient new grpc client
func NewAiRecomClient(cfg *warden.ClientConfig, opts ...grpc.DialOption) (SquareRecomAiClient, error) {
client := warden.NewClient(cfg, opts...)
cc, err := client.Dial(context.Background(), fmt.Sprintf("direct://default/127.0.0.1:9003"))
if err != nil {
log.Error("square_recom_ai_error", err.Error())
return nil, err
//return nil, nil
}
log.Info("square_recom_ai like ok")
return NewSquareRecomAiClient(cc), nil
}
This post is licensed under
CC BY 4.0
by the author.