You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
kratos/examples/metadata/client/main.go

60 lines
1.3 KiB

package main
import (
"context"
"log"
"github.com/go-kratos/kratos/examples/helloworld/helloworld"
"github.com/go-kratos/kratos/v2/metadata"
mmd "github.com/go-kratos/kratos/v2/middleware/metadata"
"github.com/go-kratos/kratos/v2/transport/grpc"
"github.com/go-kratos/kratos/v2/transport/http"
)
func main() {
callHTTP()
callGRPC()
}
func callHTTP() {
conn, err := http.NewClient(
context.Background(),
http.WithMiddleware(
mmd.Client(),
),
http.WithEndpoint("127.0.0.1:8000"),
)
if err != nil {
panic(err)
}
client := helloworld.NewGreeterHTTPClient(conn)
ctx := context.Background()
ctx = metadata.AppendToClientContext(ctx, "x-md-global-extra", "2233")
reply, err := client.SayHello(ctx, &helloworld.HelloRequest{Name: "kratos"})
if err != nil {
log.Fatal(err)
}
log.Printf("[http] SayHello %s\n", reply.Message)
}
func callGRPC() {
conn, err := grpc.DialInsecure(
context.Background(),
grpc.WithEndpoint("127.0.0.1:9000"),
grpc.WithMiddleware(
mmd.Client(),
),
)
if err != nil {
log.Fatal(err)
}
client := helloworld.NewGreeterClient(conn)
ctx := context.Background()
ctx = metadata.AppendToClientContext(ctx, "x-md-global-extra", "2233")
reply, err := client.SayHello(ctx, &helloworld.HelloRequest{Name: "kratos"})
if err != nil {
log.Fatal(err)
}
log.Printf("[grpc] SayHello %+v\n", reply)
}