Skip to content

Instantly share code, notes, and snippets.

@tmarcus87
Created October 2, 2018 04:49
Show Gist options
  • Save tmarcus87/8efa8ea7705864a4e99700cf59de926f to your computer and use it in GitHub Desktop.
Save tmarcus87/8efa8ea7705864a4e99700cf59de926f to your computer and use it in GitHub Desktop.
package main
import (
"context"
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"time"
pb "google.golang.org/grpc/examples/helloworld/helloworld"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)
func main() {
var opts []grpc.DialOption
peerCert, err :=
tls.LoadX509KeyPair(
"./certificate/client/out/service1.crt",
"./certificate/client/out/service1.pkcs8.pem")
if err != nil {
panic(err)
}
caCert, err := ioutil.ReadFile("./certificate/ca/ca.crt")
if err != nil {
panic(err)
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(caCert)
creds := credentials.NewTLS(&tls.Config{
Certificates: []tls.Certificate{peerCert},
RootCAs: caCertPool,
})
opts = append(opts, grpc.WithTransportCredentials(creds))
conn, err := grpc.Dial("dev-grpc-server.example.com:443", opts...)
if err != nil {
panic(err)
}
defer conn.Close()
client := pb.NewGreeterClient(conn)
for i := 0; i < 60; i++ {
ctx, _ := context.WithTimeout(context.Background(), time.Second)
reply, err := client.SayHello(ctx, &echo.HelloRequest{Name: "Hello" + time.Now().String()})
if err != nil {
fmt.Println(err)
} else {
fmt.Println(reply.Reply)
}
time.Sleep(time.Second)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment