This repository has been archived by the owner on Aug 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
main.go
116 lines (97 loc) · 2.58 KB
/
main.go
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package main
import (
"flag"
"fmt"
"net"
"github.com/cloudwego/kitex/pkg/klog"
"github.com/cloudwego/kitex/pkg/limit"
"github.com/cloudwego/kitex/pkg/rpcinfo"
"github.com/cloudwego/kitex/server"
"github.com/elastic/go-elasticsearch"
kitexlogrus "github.com/kitex-contrib/obs-opentelemetry/logging/logrus"
etcd "github.com/kitex-contrib/registry-etcd"
trace "github.com/kitex-contrib/tracer-opentracing"
"github.com/ozline/tiktok/cmd/chat/dal"
"github.com/ozline/tiktok/config"
chat "github.com/ozline/tiktok/kitex_gen/chat/messageservice"
"github.com/ozline/tiktok/pkg/constants"
"github.com/ozline/tiktok/pkg/eslogrus"
"github.com/ozline/tiktok/pkg/tracer"
"github.com/ozline/tiktok/pkg/utils"
"github.com/sirupsen/logrus"
)
var (
path *string
listenAddr string // listen port
EsClient *elasticsearch.Client
)
func Init() {
// config init
path = flag.String("config", "./config", "config path")
flag.Parse()
config.Init(*path, constants.ChatServiceName)
dal.Init()
tracer.InitJaeger(constants.ChatServiceName)
EsInit()
klog.SetLevel(klog.LevelDebug)
klog.SetLogger(kitexlogrus.NewLogger(kitexlogrus.WithHook(EsHookLog())))
}
func EsHookLog() *eslogrus.ElasticHook {
hook, err := eslogrus.NewElasticHook(EsClient, config.Elasticsearch.Host, logrus.DebugLevel, constants.ChatServiceName)
if err != nil {
panic(err)
}
return hook
}
// InitEs 初始化es
func EsInit() {
esConn := fmt.Sprintf("http://%s", config.Elasticsearch.Addr)
cfg := elasticsearch.Config{
Addresses: []string{esConn},
}
client, err := elasticsearch.NewClient(cfg)
if err != nil {
panic(err)
}
EsClient = client
}
func main() {
Init() // 做一些中间件的初始化
r, err := etcd.NewEtcdRegistry([]string{config.Etcd.Addr})
if err != nil {
panic(err)
}
// get available port from config set
for index, addr := range config.Service.AddrList {
if ok := utils.AddrCheck(addr); ok {
listenAddr = addr
break
}
if index == len(config.Service.AddrList)-1 {
klog.Fatal("not available port from config")
}
}
addr, err := net.ResolveTCPAddr("tcp", listenAddr)
if err != nil {
panic(err)
}
// ...
svr := chat.NewServer(
new(MessageServiceImpl),
server.WithServerBasicInfo(&rpcinfo.EndpointBasicInfo{
ServiceName: constants.ChatServiceName,
}),
server.WithMuxTransport(),
server.WithServiceAddr(addr),
server.WithRegistry(r),
server.WithSuite(trace.NewDefaultServerSuite()),
server.WithLimit(&limit.Option{
MaxConnections: constants.MaxConnections,
MaxQPS: constants.MaxQPS,
}),
)
// ...
if err = svr.Run(); err != nil {
panic(err)
}
}