-
Notifications
You must be signed in to change notification settings - Fork 147
/
registry_test.go
129 lines (116 loc) · 2.91 KB
/
registry_test.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
117
118
119
120
121
122
123
124
125
126
127
128
129
package main
import (
"context"
"fmt"
"log"
"testing"
"time"
"github.com/go-kratos/examples/helloworld/helloworld"
pb "github.com/go-kratos/examples/helloworld/helloworld"
consulregistry "github.com/go-kratos/kratos/contrib/registry/consul/v2"
etcdregistry "github.com/go-kratos/kratos/contrib/registry/etcd/v2"
"github.com/go-kratos/kratos/v2"
"github.com/go-kratos/kratos/v2/registry"
"github.com/go-kratos/kratos/v2/transport/grpc"
"github.com/go-kratos/kratos/v2/transport/http"
consul "github.com/hashicorp/consul/api"
etcd "go.etcd.io/etcd/client/v3"
)
// server is used to implement helloworld.GreeterServer.
type server struct {
pb.UnimplementedGreeterServer
}
// SayHello implements helloworld.GreeterServer
func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
return &pb.HelloReply{Message: fmt.Sprintf("Welcome %+v!", in.Name)}, nil
}
func startServer(r registry.Registrar) (app *kratos.App, err error) {
httpSrv := http.NewServer()
grpcSrv := grpc.NewServer()
s := &server{}
pb.RegisterGreeterServer(grpcSrv, s)
pb.RegisterGreeterHTTPServer(httpSrv, s)
app = kratos.New(
kratos.Name("helloworld"),
kratos.Server(
httpSrv,
grpcSrv,
),
kratos.Registrar(r),
kratos.RegistrarTimeout(5*time.Second),
)
go func() {
err = app.Run()
}()
time.Sleep(time.Second)
return
}
func callGRPC(t *testing.T, r registry.Discovery) {
conn, err := grpc.DialInsecure(
context.Background(),
grpc.WithEndpoint("discovery:///helloworld"),
grpc.WithDiscovery(r),
)
if err != nil {
t.Fatal(err)
}
defer conn.Close()
client := helloworld.NewGreeterClient(conn)
reply, err := client.SayHello(context.Background(), &helloworld.HelloRequest{Name: "kratos"})
if err != nil {
t.Fatal(err)
}
t.Logf("[grpc] SayHello %+v\n", reply)
}
func callHTTP(t *testing.T, r registry.Discovery) {
conn, err := http.NewClient(
context.Background(),
http.WithEndpoint("discovery:///helloworld"),
http.WithDiscovery(r),
http.WithBlock(),
)
if err != nil {
t.Fatal(err)
}
defer conn.Close()
client := helloworld.NewGreeterHTTPClient(conn)
reply, err := client.SayHello(context.Background(), &helloworld.HelloRequest{Name: "kratos"})
if err != nil {
t.Fatal(err)
}
t.Logf("[http] SayHello %+v\n", reply)
}
func TestETCD(t *testing.T) {
client, err := etcd.New(etcd.Config{
Endpoints: []string{"127.0.0.1:2379"},
})
if err != nil {
t.Fatal(err)
}
r := etcdregistry.New(client)
srv, err := startServer(r)
if err != nil {
t.Fatal(err)
}
callHTTP(t, r)
callGRPC(t, r)
if srv.Stop() != nil {
t.Errorf("srv.Stop() got error: %v", err)
}
}
func TestConsul(t *testing.T) {
client, err := consul.NewClient(consul.DefaultConfig())
if err != nil {
log.Fatal(err)
}
r := consulregistry.New(client)
srv, err := startServer(r)
if err != nil {
t.Fatal(err)
}
callHTTP(t, r)
callGRPC(t, r)
if srv.Stop() != nil {
t.Errorf("srv.Stop() got error: %v", err)
}
}