-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
49 lines (41 loc) · 890 Bytes
/
service.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
package grpc
import (
"net"
"runtime"
)
import (
"github.com/lethexixin/go-funcs/common/logger"
)
import (
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
)
const (
network = "tcp"
)
type Server struct {
Server *grpc.Server
}
func NewServer() *Server {
return &Server{
Server: grpc.NewServer(),
}
}
type ResisterCallBack func() (s *Server)
func (gs *Server) Service(name string, port string, registerFunc ResisterCallBack) {
runtime.GOMAXPROCS(runtime.NumCPU())
//起服务
lis, err := net.Listen(network, ":"+port)
if err != nil {
logger.Errorf("failed to listen %s, err:%s", name, err.Error())
return
}
s := registerFunc()
reflection.Register(s.Server)
err = s.Server.Serve(lis)
if err != nil {
logger.Errorf("failed to start grpc server:%s, err:%s", name, err.Error())
return
}
logger.Infof("grpc server:%s start in: %s", name, port)
}