Closed tangyoha closed 3 years ago
Question I try to build go-base server,but it can not connect. go-base server code
package main import ( "context" "flag" "fmt" "log" "net" "google.golang.org/grpc" "google.golang.org/grpc/credentials" "google.golang.org/grpc/examples/data" pb "google.golang.org/grpc/examples/features/proto/simplechat" ) var port = flag.Int("port", 65002, "the port to serve on") type simplechatServer struct { pb.UnimplementedSimpleChatServer } func (s *simplechatServer) UsersOnline(*pb.None, pb.SimpleChat_UsersOnlineServer) error { fmt.Println("has request!") return nil } func (s *simplechatServer) MessageList(*pb.None, pb.SimpleChat_MessageListServer) error { fmt.Println("has request!") return nil } func (s *simplechatServer) SendMessage(context.Context, *pb.ChatMessage) (*pb.None, error) { fmt.Println("has request!") return nil,nil } func (s *simplechatServer) Login(context.Context, *pb.User) (*pb.None, error) { fmt.Println("has request!") return &pb.None{},nil } func main() { flag.Parse() lis, err := net.Listen("tcp", fmt.Sprintf(":%d", *port)) if err != nil { log.Fatalf("failed to listen: %v", err) } fmt.Println("server listen " + fmt.Sprintf(":%d", *port)) // Create tls based credential. creds, err := credentials.NewServerTLSFromFile(data.Path("root-ca.pem"), data.Path("root-key.pem")) if err != nil { log.Fatalf("failed to create credentials: %v", err) } s := grpc.NewServer(grpc.Creds(creds)) // Register EchoServer on the server. pb.RegisterSimpleChatServer(s, &simplechatServer{}) if err := s.Serve(lis); err != nil { log.Fatalf("failed to serve: %v", err) } }
but go-base client can work
package main import ( "context" "flag" "fmt" "log" "time" "crypto/tls" "crypto/x509" "io/ioutil" "google.golang.org/grpc" "google.golang.org/grpc/credentials" "google.golang.org/grpc/examples/data" ecpb "google.golang.org/grpc/examples/features/proto/echo" pb "google.golang.org/grpc/examples/features/proto/simplechat" ) var addr = flag.String("addr", "localhost:65002", "the address to connect to") func callUnaryEcho(client ecpb.EchoClient, message string) { ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() resp, err := client.UnaryEcho(ctx, &ecpb.EchoRequest{Message: message}) if err != nil { log.Fatalf("client.UnaryEcho(_) = _, %v: ", err) } fmt.Println("UnaryEcho: ", resp.Message) } func callLogin(client pb.SimpleChatClient , message string) { ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() resp, err := client.Login(ctx,&pb.User{Name:"123",Password:"123"}) if err != nil { log.Fatalf("callLogin %v: ", err) } fmt.Println("callLogin: ",resp) } func main() { flag.Parse() cert, err := tls.LoadX509KeyPair(data.Path("client-cert.pem"), data.Path("client-key.pem")) if err != nil { log.Fatalf("keypair: %s", err) } cp := x509.NewCertPool() rootFileContents, err := ioutil.ReadFile(data.Path("root-ca.pem")) if err != nil { log.Fatalf("rootFile (%s) read failed: %v",data.Path("root-ca.pem")) return } if ok := cp.AppendCertsFromPEM(rootFileContents); !ok { log.Fatalf("addcert: %s", err) } creds := credentials.NewTLS(&tls.Config{ Certificates: []tls.Certificate{cert}, RootCAs: cp, InsecureSkipVerify: true, }) conn, err := grpc.Dial(*addr, grpc.WithTransportCredentials(creds)) if err != nil { log.Fatalf("did not connect: %v", err) } defer conn.Close() fmt.Println("conn: ") rgc := pb.NewSimpleChatClient(conn) callLogin(rgc,"helloWorld") }
Additional context
Question I try to build go-base server,but it can not connect. go-base server code
but go-base client can work
Additional context