GRPChttp请求遇到的异常(all SubConns are in TransientFailure, latest connection error: connection closed)

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")
}

 

这个地址注册错误导致了这个异常。

 

  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值