当前位置: 首页 > 后端技术 > PHP

BringingRPC-UnaryandStreaminterceptor

时间:2023-03-29 18:20:06 PHP

鍘熸枃鍦板潃锛欱ringingRPC:UnaryandStreaminterceptor椤圭洰鍦板潃锛歨ttps://github.com/EDDYCJY/go...锛熸湰绔犲皢浠嬬粛鐨勬嫤鎴櫒锛坕nterceptor锛夊彲浠ュ府鍔╀綘鍦ㄥ悎閫傜殑鍦版柟瀹炵幇杩欎簺鍔熻兘鏂规硶锛氫竴鍏冩嫤鎴櫒锛坓rpc.UnaryInterceptor锛夋祦鏂规硶锛氭祦鎷︽埅鍣紙grpc.StreamInterceptor锛夌湅鐪媑rpc.UnaryInterceptorfuncUnaryInterceptor(iUnaryServerInterceptor)ServerOption{returnfunc(o*options){ifo.unaryInt!=nil{panic("Theunaryserverinterceptorwasalreadysetandmaynotbereset.")}o.unaryInt=i}}typeUnaryServerInterceptorfunc(ctxcontext.Context,reqinterface{},info*UnaryServerInfo,handlerUnaryHandler)(respinterface{},errerror)鏌ョ湅婧愮爜鍙互鐭ラ亾锛岃瀹屾垚涓€涓嫤鎴櫒锛岄渶瑕佸疄鐜癠naryServerInterceptor鏂规硶銆傚舰鍙傚涓嬶細ctxcontext.Context锛氳姹備笂涓嬫枃reqinterface{}锛歊PC鏂规硶璇锋眰鍙傛暟info*UnaryServerInfo锛歊PC鏂规硶鐨勬墍鏈変俊鎭痟andlerUnaryHandler锛歊PC鏂规硶鏈韩grpc.StreamInterceptorfuncStreamInterceptor(iStreamServerInterceptor)ServerOptiontypeStreamServerInterceptorfunc(srvinterface{},ssServerStream,info*StreamServerInfo,handlerStreamHandler)errorStreamServerInterceptor鍜孶naryServerInterceptor鐨勫舰鍙傛剰涔夌浉鍚岋紝濡備綍瀹炵幇澶氫釜鎷︽埅鍣ㄥ氨涓嶈禈杩颁簡銆傚彟澶栧彲浠ュ彂鐜癵RPC鏈韩鍙兘璁剧疆涓€涓嫤鎴櫒銆傛槸涓嶆槸鎵€鏈夌殑閫昏緫鍙兘鍐欏湪涓€璧凤紵杩欎釜浣犲ぇ鍙斁蹇冦€傝繖涓棶棰樺彲浠ヤ娇鐢ㄥ紑婧愰」鐩甮o-grpc-middleware鏉ヨВ鍐筹紝鏈珷涔熶細鐢ㄥ埌馃槃import"github.com/grpc-ecosystem/go-grpc-middleware"myServer:=grpc.NewServer(grpc锛?logging:RPC鏂规硶鐨勮緭鍏ヨ緭鍑哄弬鏁版棩蹇楄緭鍑簉ecover:RPC鏂规硶寮傚父淇濇姢鍜屾棩蹇楄緭鍑哄疄鐜癷nterceptorloggingfuncLoggingInterceptor(ctxcontext.Context,reqinterface{},info*grpc.UnaryServerInfo,handlergrpc.UnaryHandler)(interface{},error){log.Printf("gRPC鏂规硶:%s,%v",info.FullMethod,req)resp,err:=handler(ctx,req)log.Printf("gRPC鏂规硶:%s,%v",info.FullMethod,resp)returnresp,err}recoverfuncRecoveryInterceptor(ctxcontext.Context,reqinterface{},info*grpc.UnaryServerInfo,handlergrpc.UnaryHandler)(respinterface{},errerror){deferfunc(){ife:=recover();e!=nil{debug.PrintStack()err=status.Errorf(codes.Internal,"Panicerr:%v",e)}}()杩斿洖澶勭悊绋嬪簭(ctx,req)}Serverimport("context""crypto/tls""crypto/x509""errors"鈥渋o/ioutil鈥濃€渓og鈥濃€渘et鈥濃€渞untime/debug鈥濃€済oogle.golang.org/grpc鈥濃€済oogle.golang.org/grpc/credentials鈥濃€済oogle.golang.org/grpc/status鈥濃€済oogle.golang.org/grpc/codes""github.com/grpc-ecosystem/go-grpc-middleware"pb"github.com/EDDYCJY/go-grpc-example/proto")...funcmain(){c,err:=GetTLSCredentialsByCA()iferr!=nil{log.Fatalf("GetTLSCredentialsByCA閿欒:%v",閿欒)}opts:=[]grpc.ServerOption{grpc.Creds(c),grpc_middleware.WithUnaryServerChain(RecoveryInterceptor,LoggingInterceptor,),}server:=grpc.NewServer(opts...)pb.RegisterSearchServiceServer(server,&SearchService{})lis,err:=net.Listen("tcp",":"+PORT)濡傛灉閿欒!=nil{log.Fatalf("net.Listenerr:%v",err)}server.Serve(lis)}楠岃瘉璁板綍鍚姩simple_server/server.go锛屾墽琛宻imple_client/client.go鍙戣捣璇锋眰锛屽緱鍒扮粨鏋滐細$gorunserver.go2018/10/0213:46:35gRPC鏂规硶锛?proto.SearchService/Search锛岃姹傦細鈥済RPC鈥?018/10/0213:46:35gRPC鏂规硶锛?proto.SearchService/Search锛屽搷搴旓細鈥済RPC鏈嶅姟鍣ㄢ€濇仮澶嶅湪RPC鏂规硶涓汉涓洪€犳垚杩愯閿欒锛岀劧鍚庡弽澶嶅惎鍔╯erver/client.go锛屽緱鍒扮粨鏋滐細client$gorunclient.go2018/10/0213:19:03client.Searcherr:rpcerror:code=Internaldesc=Panicerr:鍒嗛厤缁檔ilmapexitstatus1server$gorunserver.gogoroutine23[running]:runtime/debug.Stack(0xc420223588,0x1033da9,0xc420001980)/usr/local/Cellar/go/1.10.1/libexec/src/runtime/debug/stack.go:24+0xa7runtime/debug.PrintStack()/usr/local/Cellar/go/1.10.1/libexec/src/runtime/debug/stack.go:16+0x22main.RecoveryInterceptor.func1(0xc420223a10)...妫€鏌ユ湇鍔℃槸鍚﹁繕鍦ㄨ繍琛岋紝鍙互鐭ラ亾wRecovery鏄惁鎴愬姛鐢熸晥灏忕粨閫氳繃鏈珷锛屼綘鍙互瀛︿範鍒版渶甯歌鐨勬嫤鎴櫒浣跨敤鏂规硶鎺ヤ笅鏉ュ叾浠栫殑鈥滄柊鈥濋渶姹傜被姣斿嵆鍙煒堝弬鑰冩湰绯诲垪绀轰緥浠g爜go-grpc-example绯诲垪鐩綍寮曞叆gRPC锛歡RPC鍙婄浉鍏充粙缁嶅紩鍏RPC锛歡RPCClientandServer寮曞叆gRPC锛歡RPCStreaming锛孋lientandServer寮曞叆gRPC锛歍LS璇佷功璁よ瘉寮曞叆gRPC锛氬熀浜嶤A鐨凾LS璇佷功璁よ瘉寮曞叆gRPC锛歎nary鍜孲tream鎷︽埅鍣ㄥ紩鍏RPC锛氳浣犵殑鏈嶅姟鍚屾椂鎻愪緵HTTP鎺ュ彛寮曞叆gRPC锛氬仛鑷畾涔夎璇丷PC鏂规硶寮曞叆gRPC锛歡RPC鎴鏃ユ湡

最新推荐
猜你喜欢