func main() {
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
go func() {
lis, err := net.Listen("tcp", "localhost:8181")
if err != nil {
log.Fatalf("Failed start TCP Server on %s, %v", "localhost:8181")
}
grpcServer := grpc.NewServer()
protocol.RegisterPlatformServiceServer(grpcServer, platform.NewPlatformService())
log.Printf("Start RPC Server on %s", "localhost:8181")
if err := grpcServer.Serve(lis); err != nil {
log.Fatalf("Failed to start GRPC Server on %s : %v", "localhost:8181", err)
}
}()
go func() {
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
opts := []grpc.DialOption{
grpc.WithInsecure(),
grpc.WithWaitForHandshake(),
}
grpcMux := runtime.NewServeMux(runtime.WithMarshalerOption(runtime.MIMEWildcard, &runtime.JSONPb{OrigName: true, EmitDefaults: true}))
protocol.RegisterPlatformServiceHandlerFromEndpoint(ctx, grpcMux, "localhost:8182", opts)
cors := cors.AllowAll()
serverMux := http.NewServeMux()
serverMux.Handle("/", cors.Handler(grpcMux))
//http.ListenAndServe(":8182", grpcMux)
log.Printf("Start Http Server on %s", "localhost:8182")
log.Fatal(http.ListenAndServe("localhost:8182", serverMux))
}()
<-stop
log.Println("\nShutting down the server...")
log.Println("Server gracefully stopped")
}
这个地址注册错误导致了这个异常。