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.
64 lines
1.4 KiB
64 lines
1.4 KiB
3 years ago
|
package main
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"crypto/tls"
|
||
|
"crypto/x509"
|
||
|
"io/ioutil"
|
||
|
"log"
|
||
|
|
||
|
pb "github.com/go-kratos/kratos/examples/helloworld/helloworld"
|
||
|
"github.com/go-kratos/kratos/v2/transport/grpc"
|
||
|
"github.com/go-kratos/kratos/v2/transport/http"
|
||
|
)
|
||
|
|
||
|
func main() {
|
||
|
b, err := ioutil.ReadFile("../cert/server.crt")
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
cp := x509.NewCertPool()
|
||
|
if !cp.AppendCertsFromPEM(b) {
|
||
|
panic(err)
|
||
|
}
|
||
|
tlsConf := &tls.Config{ServerName: "www.kratos.com", RootCAs: cp}
|
||
|
callHTTP(tlsConf)
|
||
|
callGRPC(tlsConf)
|
||
|
}
|
||
|
|
||
|
func callHTTP(tlsConf *tls.Config) {
|
||
|
conn, err := http.NewClient(
|
||
|
context.Background(),
|
||
|
http.WithEndpoint("https://127.0.0.1:8000"),
|
||
|
http.WithTLSConfig(tlsConf),
|
||
|
)
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
defer conn.Close()
|
||
|
client := pb.NewGreeterHTTPClient(conn)
|
||
|
reply, err := client.SayHello(context.Background(), &pb.HelloRequest{Name: "kratos"})
|
||
|
if err != nil {
|
||
|
log.Fatal(err)
|
||
|
}
|
||
|
log.Printf("[http] SayHello %s\n", reply.Message)
|
||
|
}
|
||
|
|
||
|
func callGRPC(tlsConf *tls.Config) {
|
||
|
conn, err := grpc.Dial(
|
||
|
context.Background(),
|
||
|
grpc.WithEndpoint("127.0.0.1:9000"),
|
||
|
grpc.WithTLSConfig(tlsConf),
|
||
|
)
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
defer conn.Close()
|
||
|
client := pb.NewGreeterClient(conn)
|
||
|
reply, err := client.SayHello(context.Background(), &pb.HelloRequest{Name: "kratos"})
|
||
|
if err != nil {
|
||
|
log.Fatal(err)
|
||
|
}
|
||
|
log.Printf("[grpc] SayHello %+v\n", reply)
|
||
|
}
|