|
|
|
@ -10,6 +10,7 @@ import ( |
|
|
|
|
"github.com/go-kratos/kratos/v2/transport/grpc" |
|
|
|
|
"github.com/go-kratos/kratos/v2/transport/http" |
|
|
|
|
clientv3 "go.etcd.io/etcd/client/v3" |
|
|
|
|
srcgrpc "google.golang.org/grpc" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
func main() { |
|
|
|
@ -20,15 +21,8 @@ func main() { |
|
|
|
|
panic(err) |
|
|
|
|
} |
|
|
|
|
r := registry.New(cli) |
|
|
|
|
for { |
|
|
|
|
callHTTP(r) |
|
|
|
|
callGRPC(r) |
|
|
|
|
time.Sleep(time.Second) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func callGRPC(r *registry.Registry) { |
|
|
|
|
conn, err := grpc.DialInsecure( |
|
|
|
|
connGrpc, err := grpc.DialInsecure( |
|
|
|
|
context.Background(), |
|
|
|
|
grpc.WithEndpoint("discovery:///helloworld"), |
|
|
|
|
grpc.WithDiscovery(r), |
|
|
|
@ -36,17 +30,9 @@ func callGRPC(r *registry.Registry) { |
|
|
|
|
if err != nil { |
|
|
|
|
log.Fatal(err) |
|
|
|
|
} |
|
|
|
|
defer conn.Close() |
|
|
|
|
client := helloworld.NewGreeterClient(conn) |
|
|
|
|
reply, err := client.SayHello(context.Background(), &helloworld.HelloRequest{Name: "kratos"}) |
|
|
|
|
if err != nil { |
|
|
|
|
log.Fatal(err) |
|
|
|
|
} |
|
|
|
|
log.Printf("[grpc] SayHello %+v\n", reply) |
|
|
|
|
} |
|
|
|
|
defer connGrpc.Close() |
|
|
|
|
|
|
|
|
|
func callHTTP(r *registry.Registry) { |
|
|
|
|
conn, err := http.NewClient( |
|
|
|
|
connHttp, err := http.NewClient( |
|
|
|
|
context.Background(), |
|
|
|
|
http.WithEndpoint("discovery:///helloworld"), |
|
|
|
|
http.WithDiscovery(r), |
|
|
|
@ -55,7 +41,25 @@ func callHTTP(r *registry.Registry) { |
|
|
|
|
if err != nil { |
|
|
|
|
log.Fatal(err) |
|
|
|
|
} |
|
|
|
|
defer conn.Close() |
|
|
|
|
defer connHttp.Close() |
|
|
|
|
|
|
|
|
|
for { |
|
|
|
|
callHTTP(r, connHttp) |
|
|
|
|
callGRPC(r, connGrpc) |
|
|
|
|
time.Sleep(time.Second) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func callGRPC(r *registry.Registry, conn *srcgrpc.ClientConn) { |
|
|
|
|
client := helloworld.NewGreeterClient(conn) |
|
|
|
|
reply, err := client.SayHello(context.Background(), &helloworld.HelloRequest{Name: "kratos"}) |
|
|
|
|
if err != nil { |
|
|
|
|
log.Fatal(err) |
|
|
|
|
} |
|
|
|
|
log.Printf("[grpc] SayHello %+v\n", reply) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func callHTTP(r *registry.Registry,conn *http.Client) { |
|
|
|
|
client := helloworld.NewGreeterHTTPClient(conn) |
|
|
|
|
reply, err := client.SayHello(context.Background(), &helloworld.HelloRequest{Name: "kratos"}) |
|
|
|
|
if err != nil { |
|
|
|
|