-
Notifications
You must be signed in to change notification settings - Fork 27
/
init.go
111 lines (95 loc) · 3.84 KB
/
init.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
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package rpc
import (
"context"
"fmt"
"log"
"time"
"github.com/CocaineCong/tangseng/pkg/prometheus"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/resolver"
"github.com/CocaineCong/tangseng/config"
"github.com/CocaineCong/tangseng/consts"
"github.com/CocaineCong/tangseng/idl/pb/favorite"
"github.com/CocaineCong/tangseng/idl/pb/index_platform"
"github.com/CocaineCong/tangseng/idl/pb/search_engine"
"github.com/CocaineCong/tangseng/idl/pb/search_vector"
"github.com/CocaineCong/tangseng/idl/pb/user"
"github.com/CocaineCong/tangseng/pkg/discovery"
)
var (
Register *discovery.Resolver
ctx context.Context
CancelFunc context.CancelFunc
UserClient user.UserServiceClient
FavoriteClient favorite.FavoritesServiceClient
SearchEngineClient search_engine.SearchEngineServiceClient
IndexPlatformClient index_platform.IndexPlatformServiceClient
SearchVectorClient search_vector.SearchVectorServiceClient
)
// Init 初始化所有的rpc请求
func Init() {
Register = discovery.NewResolver([]string{config.Conf.Etcd.Address}, logrus.New())
resolver.Register(Register)
ctx, CancelFunc = context.WithTimeout(context.Background(), 3*time.Second)
defer Register.Close()
// initClient(config.Conf.Domain[consts.UserServiceName].Name, &UserClient)
// initClient(config.Conf.Domain[consts.FavoriteServiceName].Name, &FavoriteClient)
initClient(config.Conf.Domain[consts.SearchServiceName].Name, &SearchEngineClient)
initClient(config.Conf.Domain[consts.IndexPlatformName].Name, &IndexPlatformClient)
initClient(config.Conf.Domain[consts.SearchVectorName].Name, &SearchVectorClient)
}
// initClient 初始化所有的rpc客户端
func initClient(serviceName string, client interface{}) {
prometheus.EnableHandlingTimeHistogram()
conn, err := connectServer(serviceName)
if err != nil {
panic(err)
}
switch c := client.(type) {
case *user.UserServiceClient:
*c = user.NewUserServiceClient(conn)
case *favorite.FavoritesServiceClient:
*c = favorite.NewFavoritesServiceClient(conn)
case *search_engine.SearchEngineServiceClient:
*c = search_engine.NewSearchEngineServiceClient(conn)
case *index_platform.IndexPlatformServiceClient:
*c = index_platform.NewIndexPlatformServiceClient(conn)
case *search_vector.SearchVectorServiceClient:
*c = search_vector.NewSearchVectorServiceClient(conn)
default:
panic("unsupported worker type")
}
}
func connectServer(serviceName string) (conn *grpc.ClientConn, err error) {
opts := []grpc.DialOption{
grpc.WithTransportCredentials(insecure.NewCredentials()),
}
addr := fmt.Sprintf("%s:///%s", Register.Scheme(), serviceName)
// Load balance
if config.Conf.Services[serviceName].LoadBalance {
log.Printf("load balance enabled for %s\n", serviceName)
opts = append(opts, grpc.WithDefaultServiceConfig(fmt.Sprintf(`{"LoadBalancingPolicy": "%s"}`, "round_robin")))
}
conn, err = grpc.DialContext(ctx, addr, opts...)
err = errors.Wrapf(err, "failed to connect to gRPC service,address is %v", addr)
return
}