-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.go
73 lines (57 loc) · 1.83 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
package main
import (
"context"
"log"
"net"
"time"
"google.golang.org/grpc"
"google.golang.org/grpc/health"
healthpb "google.golang.org/grpc/health/grpc_health_v1"
"google.golang.org/grpc/reflection"
userpb "github.com/MarioCarrion/grpc-microservice-example/gen/go/user/v1"
wearablepb "github.com/MarioCarrion/grpc-microservice-example/gen/go/wearable/v1"
)
type userService struct {
userpb.UnimplementedUserServiceServer
}
func (u *userService) GetUser(ctx context.Context, req *userpb.GetUserRequest) (*userpb.GetUserResponse, error) {
time.Sleep(3 * time.Second)
return &userpb.GetUserResponse{
User: &userpb.User{
Uuid: req.Uuid,
FullName: "Mario",
},
}, nil
}
func main() {
lis, err := net.Listen("tcp", "localhost:9879")
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
grpcServer := grpc.NewServer(
grpc.UnaryInterceptor(UnaryServerInterceptor),
grpc.StreamInterceptor(StreamServerInterceptor),
)
userServer := &userService{}
wearableServer := &wearableService{}
healthServer := health.NewServer()
go func() {
for {
status := healthpb.HealthCheckResponse_SERVING
// Check if user Service is valid
if time.Now().Second()%2 == 0 {
status = healthpb.HealthCheckResponse_NOT_SERVING
}
healthServer.SetServingStatus(userpb.UserService_ServiceDesc.ServiceName, status)
healthServer.SetServingStatus("", status)
time.Sleep(1 * time.Second)
}
}()
healthServer.SetServingStatus("", healthpb.HealthCheckResponse_SERVING)
healthServer.SetServingStatus(userpb.UserService_ServiceDesc.ServiceName, healthpb.HealthCheckResponse_SERVING)
userpb.RegisterUserServiceServer(grpcServer, userServer)
healthpb.RegisterHealthServer(grpcServer, healthServer)
wearablepb.RegisterWearableServiceServer(grpcServer, wearableServer)
reflection.Register(grpcServer)
grpcServer.Serve(lis)
}