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