示例#1
0
文件: main.go 项目: xinlaini/golibs
func main() {
	flag.Parse()

	logger := xlog.NewConsoleLogger()

	ctrl, err := rpc.NewController(rpc.Config{
		Logger: logger,
	})

	helloClient, err := hello.NewHelloClient(ctrl, rpc.ClientOptions{
		ServiceName:  "Hello",
		ServiceAddr:  "localhost:9090",
		ConnPoolSize: 5,
		Retry:        rpc.DefaultDialRetry,
	})
	if err != nil {
		logger.Fatalf("Failed to create client: %s", err)
	}

	runSay(logger, helloClient, nil)
	runSay(logger, helloClient, &say.Request{})
	runSay(logger, helloClient, &say.Request{Hdr: &hello_proto.Header{}, Body: proto.String("say body")})

	runSing(logger, helloClient, nil)
	runSing(logger, helloClient, &sing.Request{})
	runSing(logger, helloClient, &sing.Request{Hdr: &hello_proto.Header{}, Body: proto.String("sing body")})
	helloClient.Close()
}
示例#2
0
文件: rcall.go 项目: xinlaini/gotools
func main() {
	flag.Parse()
	logger := xlog.NewPlainLogger()

	const usage = "usage: rcall service::method@addr [request_contents]"

	if len(flag.Args()) != 1 && len(flag.Args()) != 2 {
		logger.Fatal(usage)
	}

	svcAndAddr := strings.Split(flag.Arg(0), "@")
	if len(svcAndAddr) != 2 {
		logger.Fatal(usage)
	}
	svcAndMethod := strings.Split(svcAndAddr[0], "::")
	if len(svcAndMethod) != 2 {
		logger.Fatal(usage)
	}

	ctrl, err := rpc.NewController(rpc.Config{
		Logger: xlog.NewNilLogger(),
	})
	if err != nil {
		logger.Fatalf("Failed to create RPC controller: %s", err)
	}

	client, err := ctrl.NewClient(rpc.ClientOptions{
		ServiceName:  svcAndMethod[0],
		ServiceAddr:  svcAndAddr[1],
		ConnPoolSize: 1,
		Retry:        rpc.DefaultDialRetry,
	})
	if err != nil {
		logger.Fatalf("Failed to create RPC client: %s", err)
	}

	var requestPB *string
	if len(flag.Args()) == 2 {
		s := flag.Arg(1)
		requestPB = &s
	}
	parentCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()
	ctx := &rpc.ClientContext{Context: parentCtx}
	responsePB, err := client.CallWithTextPB(svcAndMethod[1], ctx, requestPB)
	if err != nil {
		logger.Errorf("Error: %s", err)
	}
	if ctx.Metadata != nil {
		logger.Infof("Response metadata:\n%s", proto.MarshalTextString(ctx.Metadata))
	}
	if responsePB == nil {
		logger.Info("Response: <nil>")
	} else {
		logger.Infof("Response:\n%s", *responsePB)
	}
}
示例#3
0
文件: main.go 项目: xinlaini/golibs
func main() {
	flag.Parse()

	logger := xlog.NewConsoleLogger()

	ctrl, err := rpc.NewController(rpc.Config{
		Logger: logger,
		Services: map[string]rpc.ServiceConfig{
			"Hello": {hello.HelloServiceType, &helloService{logger: logger}},
		},
	})
	if err != nil {
		logger.Fatalf("Failed to create controller: %s", err)
	}
	if err = ctrl.Serve(9090); err != nil {
		logger.Fatalf("Failed to start server: %s", err)
	}
}