當前位置: 首頁>>代碼示例>>Golang>>正文


Golang thrift.NewTTransportFactory函數代碼示例

本文整理匯總了Golang中github.com/apache/thrift/lib/go/thrift.NewTTransportFactory函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewTTransportFactory函數的具體用法?Golang NewTTransportFactory怎麽用?Golang NewTTransportFactory使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了NewTTransportFactory函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: Test_Proto_Thrift

func Test_Proto_Thrift(t *testing.T) {
	t.Skip()
	server := &TServer{}
	err := server.Start(thrift.NewTTransportFactory())
	assert.NoError(t, err)
	defer server.Server.Stop()

	var transport thrift.TTransport
	transport, err = thrift.NewTSocket(fmt.Sprintf("127.0.0.1:%d", server.Port))
	assert.NoError(t, err)

	transportFactory := thrift.NewTTransportFactory()
	transport = transportFactory.GetTransport(transport)

	err = transport.Open()
	assert.NoError(t, err)
	defer transport.Close()

	protoFactory := thrift.NewTBinaryProtocolFactoryDefault()
	client := srv.NewTSrvClientFactory(transport, protoFactory)

	req := srv.TestRep{
		ID:   "test",
		Data: []byte("mama myla ramy"),
	}
	res, err := client.Test(&req)
	assert.NoError(t, err)
	assert.EqualValues(t, req, *res)
}
開發者ID:pombredanne,項目名稱:bar,代碼行數:29,代碼來源:t10_test.go

示例2: GetTOptions

// Get clone of default Thrift client options or just return first
// given. This logic is little funny. But very convenient for per-client
// options.
func GetTOptions(options ...*TOptions) (res *TOptions) {
	if len(options) > 0 {
		return options[0]
	}
	if defaultTOptions == nil {
		defaultTOptions = &TOptions{
			DiscoveryBase: ".thrift",
			DiscoveryEnv:  "",
			TransportFactory: thrift.NewTFramedTransportFactory(
				thrift.NewTTransportFactory()),
			DialTimeout:  time.Minute * 3,
			PoolCapacity: 32,
			PoolMax:      256,
			PoolTTL:      time.Hour,
			ProtoFactory: func() thrift.TProtocolFactory {
				return thrift.NewTBinaryProtocolFactoryDefault()
			},
			Ctx:        context.Background(),
			HubFactory: DefaultTTransportHub,
		}
	}
	clone := *defaultTOptions
	res = &clone
	return
}
開發者ID:godeep,項目名稱:goth,代碼行數:28,代碼來源:options.go

示例3: New

// New returns a stateful factory for Sum and Concat Endpoints
func New(protocol string, bufferSize int, framed bool, logger log.Logger) client {
	var protocolFactory thrift.TProtocolFactory
	switch protocol {
	case "compact":
		protocolFactory = thrift.NewTCompactProtocolFactory()
	case "simplejson":
		protocolFactory = thrift.NewTSimpleJSONProtocolFactory()
	case "json":
		protocolFactory = thrift.NewTJSONProtocolFactory()
	case "binary", "":
		protocolFactory = thrift.NewTBinaryProtocolFactoryDefault()
	default:
		panic("invalid protocol")
	}

	var transportFactory thrift.TTransportFactory
	if bufferSize > 0 {
		transportFactory = thrift.NewTBufferedTransportFactory(bufferSize)
	} else {
		transportFactory = thrift.NewTTransportFactory()
	}
	if framed {
		transportFactory = thrift.NewTFramedTransportFactory(transportFactory)
	}

	return client{transportFactory, protocolFactory, logger}
}
開發者ID:qband,項目名稱:down,代碼行數:28,代碼來源:client.go

示例4: NewThriftServer

// New thrift server with defaults and common
func NewThriftServer(processor thrift.TProcessor) (res *TServer) {
	res = &TServer{
		ProtoFactory:     thrift.NewTBinaryProtocolFactoryDefault(),
		TransportFactory: thrift.NewTTransportFactory(),
		Processor:        processor,
	}
	return
}
開發者ID:godeep,項目名稱:goth,代碼行數:9,代碼來源:thrift.go

示例5: GuessTransportFactory

func GuessTransportFactory(framed bool, buffer int) (res thrift.TTransportFactory) {
	if buffer != 0 {
		res = thrift.NewTBufferedTransportFactory(buffer)
	} else {
		res = thrift.NewTTransportFactory()
	}

	if framed {
		res = thrift.NewTFramedTransportFactory(res)
	}
	return
}
開發者ID:godeep,項目名稱:goth,代碼行數:12,代碼來源:guess.go

示例6: main

func main() {

	transportFactory := thrift.NewTFramedTransportFactory(thrift.NewTTransportFactory())
	protocolFactory := thrift.NewTBinaryProtocolFactoryDefault()

	transport, err := thrift.NewTSocket(net.JoinHostPort("218.244.131.175", "8900"))
	if err != nil {
		fmt.Fprintln(os.Stderr, "error resolving address:", err)
		os.Exit(1)
	}

	useTransport := transportFactory.GetTransport(transport)
	client := ui2bs.NewBSServiceClientFactory(useTransport, protocolFactory)
	if err := transport.Open(); err != nil {
		fmt.Fprintln(os.Stderr, "Error opening socket to 127.0.0.1:8084", " ", err)
		os.Exit(1)
	}
	defer transport.Close()

	bs_req := new(ui2bs.BSRequest)
	bs_req.Searchid = "123"
	bs_req.Media = new(ui2bs.Media)
	bs_req.Media.Appsid = "123"
	bs_req.Media.ChannelId = "123"
	bs_req.Adslot = new(ui2bs.AdSlot)
	bs_req.Device = new(ui2bs.Device)
	bs_req.Device.Os = ui2bs.OSType_ANDROID
	bs_resp := new(ui2bs.BSResponse)
	//	bs_resp.Ads = new([]ui2bs.Ad)
	bs_resp, err = client.Search(bs_req)
	if err != nil {
		fmt.Println("error occuse")
		fmt.Println(err)
	}
	//	for i:=0; i < 10; i++ {
	//		err := client.Search2()
	//		if err != nil {
	//			fmt.Println("serach fail")
	//			transport, err = thrift.NewTSocket(net.JoinHostPort("127.0.0.1", "8084"))
	//			transport.Open()
	//			useTransport = transportFactory.GetTransport(transport)
	//			client = bs.NewBSServiceClientFactory(useTransport, protocolFactory)
	//		}
	//		time.Sleep(1*time.Second)
	//	}
	//
	fmt.Println(bs_resp)
	fmt.Println("I am exit ")

}
開發者ID:ifzz,項目名稱:seller_adptor,代碼行數:50,代碼來源:client_stub.go

示例7: NewTRpcServer

func NewTRpcServer(listen string, handler thrift.TProcessor, prot thrift.TProtocolFactory) (*TRpcServer, error) {
	transport, err := thrift.NewTServerSocket(listen)

	if err != nil {
		return nil, err
	}
	server := thrift.NewTSimpleServer4(
		handler,
		transport,
		thrift.NewTFramedTransportFactory(thrift.NewTTransportFactory()),
		prot,
	)

	return &TRpcServer{server, transport}, nil
}
開發者ID:jcdavis,項目名稱:quiver,代碼行數:15,代碼來源:thrift-rpc.go

示例8: main

func main() {

	transportFactory := thrift.NewTFramedTransportFactory(thrift.NewTTransportFactory())
	protocolFactory := thrift.NewTBinaryProtocolFactoryDefault()

	serverTransport, err := thrift.NewTServerSocket(NetworkAddr)
	if err != nil {
		fmt.Println("new socket fail")
	}
	handler := &BsServiceImpl{}
	processor := bs.NewBSServiceProcessor(handler)

	server := thrift.NewTSimpleServer4(processor, serverTransport, transportFactory, protocolFactory)
	fmt.Println("server start")
	server.Serve()

}
開發者ID:ifzz,項目名稱:seller_adptor,代碼行數:17,代碼來源:bs_stub.go

示例9: newScribeServer

func newScribeServer(t *testing.T) *scribeServer {
	protocolFactory := thrift.NewTBinaryProtocolFactoryDefault()
	transportFactory := thrift.NewTFramedTransportFactory(thrift.NewTTransportFactory())

	var port int
	var transport *thrift.TServerSocket
	var err error
	for i := 0; i < 10; i++ {
		port = 10000 + rand.Intn(10000)
		transport, err = thrift.NewTServerSocket(fmt.Sprintf(":%d", port))
		if err != nil {
			t.Logf("port %d: %v", port, err)
			continue
		}
		break
	}
	if err != nil {
		t.Fatal(err)
	}

	handler := newScribeHandler(t)
	server := thrift.NewTSimpleServer4(
		scribe.NewScribeProcessor(handler),
		transport,
		transportFactory,
		protocolFactory,
	)

	go server.Serve()

	deadline := time.Now().Add(time.Second)
	for !canConnect(port) {
		if time.Now().After(deadline) {
			t.Fatal("server never started")
		}
		time.Sleep(time.Millisecond)
	}

	return &scribeServer{
		transport: transport,
		address:   fmt.Sprintf("127.0.0.1:%d", port),
		handler:   handler,
	}
}
開發者ID:jllopis,項目名稱:kit,代碼行數:44,代碼來源:collector_test.go

示例10: main

func main() {
	transportFactory := thrift.NewTFramedTransportFactory(thrift.NewTTransportFactory())
	protocolFactory := thrift.NewTBinaryProtocolFactoryDefault()

	transport, err := thrift.NewTSocket(net.JoinHostPort("127.0.0.1", "19090"))
	if err != nil {
		fmt.Fprintln(os.Stderr, "error resolving address:", err)
		os.Exit(1)
	}

	useTransport := transportFactory.GetTransport(transport)
	client := account.NewAccountClientFactory(useTransport, protocolFactory)
	if err := transport.Open(); err != nil {
		fmt.Fprintln(os.Stderr, "Error opening socket to 127.0.0.1:19090", "", err)
		os.Exit(1)
	}
	defer transport.Close()

	// 第一個請求,登錄賬號
	request := &account.Request{
		Name:     "super",
		Password: "123",
		Op:       account.Operation_LOGIN,
	}

	r, err := client.DoAction(request)
	fmt.Println(r)

	// 第二個請求,注冊賬號
	request.Op = account.Operation_REGISTER
	r, err = client.DoAction(request)
	fmt.Println(r)

	// 第三個請求,name為空的請求
	request.Name = ""
	r, err = client.DoAction(request)
	fmt.Println(r)

}
開發者ID:LC2010,項目名稱:thrift-step-by-step-go,代碼行數:39,代碼來源:account_client.go

示例11: main


//.........這裏部分代碼省略.........
	e = zipkin.AnnotateServer(zipkinSpanFunc, zipkinCollector)(e)

	// Mechanical stuff
	rand.Seed(time.Now().UnixNano())
	root := context.Background()
	errc := make(chan error)

	go func() {
		errc <- interrupt()
	}()

	// Transport: HTTP (debug/instrumentation)
	go func() {
		logger.Log("addr", *debugAddr, "transport", "debug")
		errc <- http.ListenAndServe(*debugAddr, nil)
	}()

	// Transport: HTTP (JSON)
	go func() {
		ctx, cancel := context.WithCancel(root)
		defer cancel()
		before := []httptransport.BeforeFunc{zipkin.ToContext(zipkinSpanFunc)}
		after := []httptransport.AfterFunc{}
		handler := makeHTTPBinding(ctx, e, before, after)
		logger.Log("addr", *httpAddr, "transport", "HTTP/JSON")
		errc <- http.ListenAndServe(*httpAddr, handler)
	}()

	// Transport: gRPC
	go func() {
		ln, err := net.Listen("tcp", *grpcAddr)
		if err != nil {
			errc <- err
			return
		}
		s := grpc.NewServer() // uses its own context?
		pb.RegisterAddServer(s, grpcBinding{e})
		logger.Log("addr", *grpcAddr, "transport", "gRPC")
		errc <- s.Serve(ln)
	}()

	// Transport: net/rpc
	go func() {
		ctx, cancel := context.WithCancel(root)
		defer cancel()
		s := rpc.NewServer()
		s.RegisterName("addsvc", NetrpcBinding{ctx, e})
		s.HandleHTTP(rpc.DefaultRPCPath, rpc.DefaultDebugPath)
		logger.Log("addr", *netrpcAddr, "transport", "net/rpc")
		errc <- http.ListenAndServe(*netrpcAddr, s)
	}()

	// Transport: Thrift
	go func() {
		ctx, cancel := context.WithCancel(root)
		defer cancel()

		var protocolFactory thrift.TProtocolFactory
		switch *thriftProtocol {
		case "binary":
			protocolFactory = thrift.NewTBinaryProtocolFactoryDefault()
		case "compact":
			protocolFactory = thrift.NewTCompactProtocolFactory()
		case "json":
			protocolFactory = thrift.NewTJSONProtocolFactory()
		case "simplejson":
			protocolFactory = thrift.NewTSimpleJSONProtocolFactory()
		default:
			errc <- fmt.Errorf("invalid Thrift protocol %q", *thriftProtocol)
			return
		}

		var transportFactory thrift.TTransportFactory
		if *thriftBufferSize > 0 {
			transportFactory = thrift.NewTBufferedTransportFactory(*thriftBufferSize)
		} else {
			transportFactory = thrift.NewTTransportFactory()
		}

		if *thriftFramed {
			transportFactory = thrift.NewTFramedTransportFactory(transportFactory)
		}

		transport, err := thrift.NewTServerSocket(*thriftAddr)
		if err != nil {
			errc <- err
			return
		}

		logger.Log("addr", *thriftAddr, "transport", "Thrift")
		errc <- thrift.NewTSimpleServer4(
			thriftadd.NewAddServiceProcessor(thriftBinding{ctx, e}),
			transport,
			transportFactory,
			protocolFactory,
		).Serve()
	}()

	logger.Log("fatal", <-errc)
}
開發者ID:EthanK28,項目名稱:kit,代碼行數:101,代碼來源:main.go

示例12: main


//.........這裏部分代碼省略.........
			mux             = http.NewServeMux()
			sum, concat     endpoint.Endpoint
		)

		sum = makeSumEndpoint(svc)
		sum = kitot.TraceServer(tracer, "sum")(sum)
		mux.Handle("/sum", httptransport.NewServer(
			root,
			sum,
			server.DecodeSumRequest,
			server.EncodeSumResponse,
			httptransport.ServerErrorLogger(transportLogger),
			httptransport.ServerBefore(kitot.FromHTTPRequest(tracer, "sum", tracingLogger)),
		))

		concat = makeConcatEndpoint(svc)
		concat = kitot.TraceServer(tracer, "concat")(concat)
		mux.Handle("/concat", httptransport.NewServer(
			root,
			concat,
			server.DecodeConcatRequest,
			server.EncodeConcatResponse,
			httptransport.ServerErrorLogger(transportLogger),
			httptransport.ServerBefore(kitot.FromHTTPRequest(tracer, "concat", tracingLogger)),
		))

		transportLogger.Log("addr", *httpAddr)
		errc <- http.ListenAndServe(*httpAddr, mux)
	}()

	// Transport: gRPC
	go func() {
		transportLogger := log.NewContext(logger).With("transport", "gRPC")
		tracingLogger := log.NewContext(transportLogger).With("component", "tracing")
		ln, err := net.Listen("tcp", *grpcAddr)
		if err != nil {
			errc <- err
			return
		}
		s := grpc.NewServer() // uses its own, internal context
		pb.RegisterAddServer(s, newGRPCBinding(root, tracer, svc, tracingLogger))
		transportLogger.Log("addr", *grpcAddr)
		errc <- s.Serve(ln)
	}()

	// Transport: net/rpc
	go func() {
		transportLogger := log.NewContext(logger).With("transport", "net/rpc")
		s := rpc.NewServer()
		if err := s.RegisterName("addsvc", netrpcBinding{svc}); err != nil {
			errc <- err
			return
		}
		s.HandleHTTP(rpc.DefaultRPCPath, rpc.DefaultDebugPath)
		transportLogger.Log("addr", *netrpcAddr)
		errc <- http.ListenAndServe(*netrpcAddr, s)
	}()

	// Transport: Thrift
	go func() {
		var protocolFactory thrift.TProtocolFactory
		switch *thriftProtocol {
		case "binary":
			protocolFactory = thrift.NewTBinaryProtocolFactoryDefault()
		case "compact":
			protocolFactory = thrift.NewTCompactProtocolFactory()
		case "json":
			protocolFactory = thrift.NewTJSONProtocolFactory()
		case "simplejson":
			protocolFactory = thrift.NewTSimpleJSONProtocolFactory()
		default:
			errc <- fmt.Errorf("invalid Thrift protocol %q", *thriftProtocol)
			return
		}
		var transportFactory thrift.TTransportFactory
		if *thriftBufferSize > 0 {
			transportFactory = thrift.NewTBufferedTransportFactory(*thriftBufferSize)
		} else {
			transportFactory = thrift.NewTTransportFactory()
		}
		if *thriftFramed {
			transportFactory = thrift.NewTFramedTransportFactory(transportFactory)
		}
		transport, err := thrift.NewTServerSocket(*thriftAddr)
		if err != nil {
			errc <- err
			return
		}
		transportLogger := log.NewContext(logger).With("transport", "thrift")
		transportLogger.Log("addr", *thriftAddr)
		errc <- thrift.NewTSimpleServer4(
			thriftadd.NewAddServiceProcessor(thriftBinding{svc}),
			transport,
			transportFactory,
			protocolFactory,
		).Serve()
	}()

	logger.Log("fatal", <-errc)
}
開發者ID:zyanho,項目名稱:kit,代碼行數:101,代碼來源:main.go

示例13: main


//.........這裏部分代碼省略.........
	// Mechanical domain.
	errc := make(chan error)
	ctx := context.Background()

	// Interrupt handler.
	go func() {
		c := make(chan os.Signal, 1)
		signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
		errc <- fmt.Errorf("%s", <-c)
	}()

	// Debug listener.
	go func() {
		logger := log.NewContext(logger).With("transport", "debug")

		m := http.NewServeMux()
		m.Handle("/debug/pprof/", http.HandlerFunc(pprof.Index))
		m.Handle("/debug/pprof/cmdline", http.HandlerFunc(pprof.Cmdline))
		m.Handle("/debug/pprof/profile", http.HandlerFunc(pprof.Profile))
		m.Handle("/debug/pprof/symbol", http.HandlerFunc(pprof.Symbol))
		m.Handle("/debug/pprof/trace", http.HandlerFunc(pprof.Trace))
		m.Handle("/metrics", stdprometheus.Handler())

		logger.Log("addr", *debugAddr)
		errc <- http.ListenAndServe(*debugAddr, m)
	}()

	// HTTP transport.
	go func() {
		logger := log.NewContext(logger).With("transport", "HTTP")
		h := addsvc.MakeHTTPHandler(ctx, endpoints, tracer, logger)
		logger.Log("addr", *httpAddr)
		errc <- http.ListenAndServe(*httpAddr, h)
	}()

	// gRPC transport.
	go func() {
		logger := log.NewContext(logger).With("transport", "gRPC")

		ln, err := net.Listen("tcp", *grpcAddr)
		if err != nil {
			errc <- err
			return
		}

		srv := addsvc.MakeGRPCServer(ctx, endpoints, tracer, logger)
		s := grpc.NewServer()
		pb.RegisterAddServer(s, srv)

		logger.Log("addr", *grpcAddr)
		errc <- s.Serve(ln)
	}()

	// Thrift transport.
	go func() {
		logger := log.NewContext(logger).With("transport", "Thrift")

		var protocolFactory thrift.TProtocolFactory
		switch *thriftProtocol {
		case "binary":
			protocolFactory = thrift.NewTBinaryProtocolFactoryDefault()
		case "compact":
			protocolFactory = thrift.NewTCompactProtocolFactory()
		case "json":
			protocolFactory = thrift.NewTJSONProtocolFactory()
		case "simplejson":
			protocolFactory = thrift.NewTSimpleJSONProtocolFactory()
		default:
			errc <- fmt.Errorf("invalid Thrift protocol %q", *thriftProtocol)
			return
		}

		var transportFactory thrift.TTransportFactory
		if *thriftBufferSize > 0 {
			transportFactory = thrift.NewTBufferedTransportFactory(*thriftBufferSize)
		} else {
			transportFactory = thrift.NewTTransportFactory()
		}
		if *thriftFramed {
			transportFactory = thrift.NewTFramedTransportFactory(transportFactory)
		}

		transport, err := thrift.NewTServerSocket(*thriftAddr)
		if err != nil {
			errc <- err
			return
		}

		logger.Log("addr", *thriftAddr)
		errc <- thrift.NewTSimpleServer4(
			thriftadd.NewAddServiceProcessor(addsvc.MakeThriftHandler(ctx, endpoints)),
			transport,
			transportFactory,
			protocolFactory,
		).Serve()
	}()

	// Run!
	logger.Log("exit", <-errc)
}
開發者ID:crezam,項目名稱:kit,代碼行數:101,代碼來源:main.go

示例14: main

func main() {
	var (
		transport        = flag.String("transport", "httpjson", "httpjson, grpc, netrpc, thrift")
		httpAddr         = flag.String("http.addr", "localhost:8001", "Address for HTTP (JSON) server")
		grpcAddr         = flag.String("grpc.addr", "localhost:8002", "Address for gRPC server")
		netrpcAddr       = flag.String("netrpc.addr", "localhost:8003", "Address for net/rpc server")
		thriftAddr       = flag.String("thrift.addr", "localhost:8004", "Address for Thrift server")
		thriftProtocol   = flag.String("thrift.protocol", "binary", "binary, compact, json, simplejson")
		thriftBufferSize = flag.Int("thrift.buffer.size", 0, "0 for unbuffered")
		thriftFramed     = flag.Bool("thrift.framed", false, "true to enable framing")
	)
	flag.Parse()
	if len(os.Args) < 4 {
		fmt.Fprintf(os.Stderr, "\n%s [flags] method arg1 arg2\n\n", filepath.Base(os.Args[0]))
		flag.Usage()
		os.Exit(1)
	}

	root := context.Background()
	method, s1, s2 := flag.Arg(0), flag.Arg(1), flag.Arg(2)

	var logger log.Logger
	logger = log.NewLogfmtLogger(os.Stdout)
	logger = log.NewContext(logger).With("caller", log.DefaultCaller)
	logger = log.NewContext(logger).With("transport", *transport)

	var svc server.AddService
	switch *transport {
	case "grpc":
		cc, err := grpc.Dial(*grpcAddr)
		if err != nil {
			_ = logger.Log("err", err)
			os.Exit(1)
		}
		defer cc.Close()
		svc = grpcclient.New(root, cc, logger)

	case "httpjson":
		rawurl := *httpAddr
		if !strings.HasPrefix("http", rawurl) {
			rawurl = "http://" + rawurl
		}
		baseurl, err := url.Parse(rawurl)
		if err != nil {
			_ = logger.Log("err", err)
			os.Exit(1)
		}
		svc = httpjsonclient.New(root, baseurl, logger, nil)

	case "netrpc":
		cli, err := rpc.DialHTTP("tcp", *netrpcAddr)
		if err != nil {
			_ = logger.Log("err", err)
			os.Exit(1)
		}
		defer cli.Close()
		svc = netrpcclient.New(cli, logger)

	case "thrift":
		var protocolFactory thrift.TProtocolFactory
		switch *thriftProtocol {
		case "compact":
			protocolFactory = thrift.NewTCompactProtocolFactory()
		case "simplejson":
			protocolFactory = thrift.NewTSimpleJSONProtocolFactory()
		case "json":
			protocolFactory = thrift.NewTJSONProtocolFactory()
		case "binary", "":
			protocolFactory = thrift.NewTBinaryProtocolFactoryDefault()
		default:
			_ = logger.Log("protocol", *thriftProtocol, "err", "invalid protocol")
			os.Exit(1)
		}
		var transportFactory thrift.TTransportFactory
		if *thriftBufferSize > 0 {
			transportFactory = thrift.NewTBufferedTransportFactory(*thriftBufferSize)
		} else {
			transportFactory = thrift.NewTTransportFactory()
		}
		if *thriftFramed {
			transportFactory = thrift.NewTFramedTransportFactory(transportFactory)
		}
		transportSocket, err := thrift.NewTSocket(*thriftAddr)
		if err != nil {
			_ = logger.Log("during", "thrift.NewTSocket", "err", err)
			os.Exit(1)
		}
		trans := transportFactory.GetTransport(transportSocket)
		defer trans.Close()
		if err := trans.Open(); err != nil {
			_ = logger.Log("during", "thrift transport.Open", "err", err)
			os.Exit(1)
		}
		cli := thriftadd.NewAddServiceClientFactory(trans, protocolFactory)
		svc = thriftclient.New(cli, logger)

	default:
		_ = logger.Log("err", "invalid transport")
		os.Exit(1)
	}
//.........這裏部分代碼省略.........
開發者ID:nimmen,項目名稱:kit,代碼行數:101,代碼來源:main.go

示例15: main

func main() {
	// Flag domain. Note that gRPC transitively registers flags via its import
	// of glog. So, we define a new flag set, to keep those domains distinct.
	fs := flag.NewFlagSet("", flag.ExitOnError)
	var (
		transport        = fs.String("transport", "grpc", "http, grpc, netrpc, thrift")
		httpAddr         = fs.String("http.addr", "localhost:8001", "HTTP (JSON) address")
		grpcAddr         = fs.String("grpc.addr", "localhost:8002", "gRPC address")
		netrpcAddr       = fs.String("netrpc.addr", "localhost:8003", "net/rpc address")
		thriftAddr       = fs.String("thrift.addr", "localhost:8004", "Thrift address")
		thriftProtocol   = fs.String("thrift.protocol", "binary", "binary, compact, json, simplejson")
		thriftBufferSize = fs.Int("thrift.buffer.size", 0, "0 for unbuffered")
		thriftFramed     = fs.Bool("thrift.framed", false, "true to enable framing")
		a                = fs.Int64("a", 1, "a value")
		b                = fs.Int64("b", 2, "b value")
	)
	flag.Usage = fs.Usage // only show our flags
	fs.Parse(os.Args[1:])
	log.SetFlags(0)
	log.SetOutput(os.Stdout)

	var e endpoint.Endpoint
	switch *transport {
	case "http":
		if !strings.HasPrefix(*httpAddr, "http") {
			*httpAddr = "http://" + *httpAddr
		}
		u, err := url.Parse(*httpAddr)
		if err != nil {
			log.Fatalf("url.Parse: %v", err)
		}
		if u.Path == "" {
			u.Path = "/add"
		}
		e = httpclient.NewClient("GET", u.String())

	case "grpc":
		cc, err := grpc.Dial(*grpcAddr)
		if err != nil {
			log.Fatalf("grpc.Dial: %v", err)
		}
		e = grpcclient.NewClient(cc)

	case "netrpc":
		client, err := rpc.DialHTTP("tcp", *netrpcAddr)
		if err != nil {
			log.Fatalf("rpc.DialHTTP: %v", err)
		}
		e = netrpcclient.NewClient(client)

	case "thrift":
		var protocolFactory thrift.TProtocolFactory
		switch *thriftProtocol {
		case "compact":
			protocolFactory = thrift.NewTCompactProtocolFactory()
		case "simplejson":
			protocolFactory = thrift.NewTSimpleJSONProtocolFactory()
		case "json":
			protocolFactory = thrift.NewTJSONProtocolFactory()
		case "binary", "":
			protocolFactory = thrift.NewTBinaryProtocolFactoryDefault()
		default:
			log.Fatalf("invalid protocol %q", *thriftProtocol)
		}

		var transportFactory thrift.TTransportFactory
		if *thriftBufferSize > 0 {
			transportFactory = thrift.NewTBufferedTransportFactory(*thriftBufferSize)
		} else {
			transportFactory = thrift.NewTTransportFactory()
		}

		if *thriftFramed {
			transportFactory = thrift.NewTFramedTransportFactory(transportFactory)
		}

		transportSocket, err := thrift.NewTSocket(*thriftAddr)
		if err != nil {
			log.Fatalf("thrift.NewTSocket: %v", err)
		}

		transport := transportFactory.GetTransport(transportSocket)
		defer transport.Close()
		if err := transport.Open(); err != nil {
			log.Fatalf("Thrift transport.Open: %v", err)
		}

		e = thriftclient.NewClient(thriftadd.NewAddServiceClientFactory(transport, protocolFactory))

	default:
		log.Fatalf("unsupported transport %q", *transport)
	}

	response, err := e(context.Background(), reqrep.AddRequest{A: *a, B: *b})
	if err != nil {
		log.Fatalf("when invoking request: %v", err)
	}
	addResponse, ok := response.(reqrep.AddResponse)
	if !ok {
		log.Fatalf("when type-asserting response: %v", endpoint.ErrBadCast)
//.........這裏部分代碼省略.........
開發者ID:EthanK28,項目名稱:kit,代碼行數:101,代碼來源:main.go


注:本文中的github.com/apache/thrift/lib/go/thrift.NewTTransportFactory函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。