pb2tarsgo/

directory
v1.0.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 16, 2022 License: BSD-3-Clause

README

Usage

  • Install protoc
git clone https://github.com/protocolbuffers/protobuf
cd protobuf
git checkout v3.6.1.3
./autogen.sh
./configure
make -j8
make install
  • Add tarsrpc plugin for protoc-gen-go
go get github.com/golang/protobuf/{proto,protoc-gen-go}
go get github.com/jslyzt/tarsgo/tars

cd $GOPATH/src/github.com/golang/protobuf/protoc-gen-go &&  cp -r $GOPATH/src/github.com/jslyzt/tarsgo/tars/tools/pb2tarsgo/protoc-gen-go/{link_tarsrpc.go, tarsrpc} .
go install
export PATH=$PATH:$GOPATH/bin

example

  • proto file
syntax = "proto3";
package helloworld;

// The greeting service definition.
service Greeter {
  // Sends a greeting
  rpc SayHello (HelloRequest) returns (HelloReply) {}
}

// The request message containing the user's name.
message HelloRequest {
  string name = 1;
}

// The response message containing the greetings
message HelloReply {
  string message = 1;
}

  • generate the code
protoc --go_out=plugins=tarsrpc:. helloworld.proto
  • server
package main

import (
    "github.com/jslyzt/tarsgo/tars"
    "helloworld" 
)

type GreeterImp  struct {
}

func (imp *GreeterImp) SayHello(input helloworld.HelloRequest)(output helloworld.HelloReply, err error) {
    output.Message = "hello" +  input.GetName() 
    return output, nil 
}

func main() { //Init servant

    imp := new(GreeterImp)                                    //New Imp
    app := new(helloworld.Greeter)                            //New init the A JCE
    cfg := tars.GetServerConfig()                              //Get Config File Object
    app.AddServant(imp, cfg.App+"."+cfg.Server+".GreeterTestObj") //Register Servant
    tars.Run()
}
	

  • client
package main

import (
    "fmt"
    "github.com/jslyzt/tarsgo/tars"
    "helloworld"
)

func main() {
    comm := tars.NewCommunicator()
    obj := fmt.Sprintf("StressTest.HelloPbServer.GreeterTestObj@tcp -h 127.0.0.1  -p 10014  -t 60000")
    app := new(helloworld.Greeter)
    comm.StringToProxy(obj, app)
    input := helloworld.HelloRequest{Name: "sandyskies"}
    output, err := app.SayHello(input)
    if err != nil {
        fmt.Println("err: ", err)
    }   
    fmt.Println("result is:", output.Message)
}

Directories

Path Synopsis
tarsrpc
Package tarsrpc outputs tars service descriptions in Go code.
Package tarsrpc outputs tars service descriptions in Go code.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL