Commit 6fcc015a authored by AlexStocks's avatar AlexStocks

Add: protobuf protocol file

parent 57fda234
package main
import (
"errors"
)
import (
log "github.com/AlexStocks/log4go"
)
type TestABC struct {
A, B, C string
}
type TestRpc struct {
i int
}
func (r *TestRpc) Service() string {
return "TestRpc"
}
func (r *TestRpc) Version() string {
return "v1.0"
}
func (r *TestRpc) Test(arg TestABC, res *string) error {
log.Debug("arg:%+v", arg)
*res = "this is a test"
return nil
}
func (r *TestRpc) Add(n int, res *int) error {
r.i += n
*res = r.i + 100
return nil
}
func (r *TestRpc) Err(n int, res *int) error {
return errors.New("this is a error test")
}
...@@ -19,6 +19,7 @@ import ( ...@@ -19,6 +19,7 @@ import (
) )
import ( import (
"github.com/AlexStocks/getty-examples/rpc/proto"
"github.com/AlexStocks/getty/rpc" "github.com/AlexStocks/getty/rpc"
"github.com/AlexStocks/goext/log" "github.com/AlexStocks/goext/log"
"github.com/AlexStocks/goext/net" "github.com/AlexStocks/goext/net"
...@@ -108,72 +109,70 @@ func initSignal() { ...@@ -108,72 +109,70 @@ func initSignal() {
} }
func testJSON() { func testJSON() {
var ( testReq := rpc_examples.TestReq{"aaa", "bbb", "ccc"}
err error testRsp := rpc_examples.TestRsp{}
testResult string err := client.Call(rpc.CodecJson, "127.0.0.1:20000", "TestService", "Test", &testReq, &testRsp)
)
param := TestABC{"aaa", "bbb", "ccc"}
err = client.Call(rpc.CodecJson, "127.0.0.1:20000", "TestRpc", "Test", param, &testResult)
if err != nil { if err != nil {
log.Error("client.Call(TestRpc::Test) = error:%s", jerrors.ErrorStack(err)) log.Error("client.Call(TestService::Test) = error:%s", jerrors.ErrorStack(err))
return return
} }
log.Info("TestRpc::Test(param:%#v) = res:%s", param, testResult) gxlog.CError("TestService::Test(param:%#v) = res:%s", testReq, testRsp)
return
var addResult int var addResult int
err = client.Call(rpc.CodecJson, "127.0.0.1:20000", "TestRpc", "Add", 1, &addResult) err = client.Call(rpc.CodecJson, "127.0.0.1:10000", "TestService", "Add", 1, &addResult)
if err != nil { if err != nil {
log.Error("client.Call(TestRpc::Add) = error:%s", jerrors.ErrorStack(err)) log.Error("client.Call(TestService::Add) = error:%s", jerrors.ErrorStack(err))
return return
} }
log.Info("TestRpc::Add(1) = res:%d", addResult) log.Info("TestService::Add(1) = res:%d", addResult)
var errResult int var errResult int
err = client.Call(rpc.CodecJson, "127.0.0.1:20000", "TestRpc", "Err", 2, &errResult) err = client.Call(rpc.CodecJson, "127.0.0.1:10000", "TestService", "Err", 2, &errResult)
if err != nil { if err != nil {
// error test case, this invocation should step into this branch. // error test case, this invocation should step into this branch.
log.Error("client.Call(TestRpc::Err) = error:%s", jerrors.ErrorStack(err)) log.Error("client.Call(TestService::Err) = error:%s", jerrors.ErrorStack(err))
return return
} }
log.Info("TestRpc::Err(2) = res:%s", errResult) log.Info("TestService::Err(2) = res:%s", errResult)
} }
func testProtobuf() { func testProtobuf() {
var ( testReq := rpc_examples.TestReq{"aaa", "bbb", "ccc"}
err error testRsp := rpc_examples.TestRsp{}
testResult string err := client.Call(rpc.CodecProtobuf, "127.0.0.1:20000", "TestService", "Test", &testReq, &testRsp)
)
param := TestABC{"aaa", "bbb", "ccc"}
err = client.Call(rpc.CodecProtobuf, "127.0.0.1:20000", "TestRpc", "Test", param, &testResult)
if err != nil { if err != nil {
log.Error("client.Call(TestRpc::Test) = error:%s", jerrors.ErrorStack(err)) log.Error("client.Call(TestService::Test) = error:%s", jerrors.ErrorStack(err))
return return
} }
log.Info("TestRpc::Test(param:%#v) = res:%s", param, testResult) gxlog.CError("TestService::Test(param:%#v) = res:%s", testReq, testRsp)
var addResult int
err = client.Call(rpc.CodecProtobuf, "127.0.0.1:20000", "TestRpc", "Add", 1, &addResult)
if err != nil {
log.Error("client.Call(TestRpc::Add) = error:%s", jerrors.ErrorStack(err))
return return
}
log.Info("TestRpc::Add(1) = res:%d", addResult) // var addResult int
// err = client.Call(rpc.CodecProtobuf, "127.0.0.1:20000", "TestService", "Add", 1, &addResult)
// if err != nil {
// log.Error("client.Call(TestService::Add) = error:%s", jerrors.ErrorStack(err))
// return
// }
// log.Info("TestService::Add(1) = res:%d", addResult)
var errResult int var errResult int
err = client.Call(rpc.CodecProtobuf, "127.0.0.1:20000", "TestRpc", "Err", 2, &errResult) err = client.Call(rpc.CodecProtobuf, "127.0.0.1:20000", "TestService", "Err", 2, &errResult)
if err != nil { if err != nil {
// error test case, this invocation should step into this branch. // error test case, this invocation should step into this branch.
log.Error("client.Call(TestRpc::Err) = error:%s", jerrors.ErrorStack(err)) log.Error("client.Call(TestService::Err) = error:%s", jerrors.ErrorStack(err))
return return
} }
log.Info("TestRpc::Err(2) = res:%s", errResult) log.Info("TestService::Err(2) = res:%s", errResult)
} }
func test() { func test() {
gxlog.CInfo("start to run json rpc example:\n") gxlog.CInfo("\nstart to run json rpc example:")
testJSON() testJSON()
time.Sleep(2e9) time.Sleep(2e9)
gxlog.CInfo("start to run protobuf rpc example:\n") gxlog.CInfo("\nstart to run protobuf rpc example:")
testJSON() testProtobuf()
} }
...@@ -37,5 +37,5 @@ FailFastTimeout = "3s" ...@@ -37,5 +37,5 @@ FailFastTimeout = "3s"
TcpReadTimeout = "1s" TcpReadTimeout = "1s"
TcpWriteTimeout = "5s" TcpWriteTimeout = "5s"
WaitTimeout = "1s" WaitTimeout = "1s"
MaxMsgLen = 128 MaxMsgLen = 1024
SessionName = "getty-rpc-client" SessionName = "getty-rpc-client"
...@@ -37,5 +37,5 @@ FailFastTimeout = "3s" ...@@ -37,5 +37,5 @@ FailFastTimeout = "3s"
TcpReadTimeout = "1s" TcpReadTimeout = "1s"
TcpWriteTimeout = "5s" TcpWriteTimeout = "5s"
WaitTimeout = "1s" WaitTimeout = "1s"
MaxMsgLen = 128 MaxMsgLen = 1024
SessionName = "getty-rpc-client" SessionName = "getty-rpc-client"
...@@ -37,5 +37,5 @@ FailFastTimeout = "3s" ...@@ -37,5 +37,5 @@ FailFastTimeout = "3s"
TcpReadTimeout = "1s" TcpReadTimeout = "1s"
TcpWriteTimeout = "5s" TcpWriteTimeout = "5s"
WaitTimeout = "1s" WaitTimeout = "1s"
MaxMsgLen = 128 MaxMsgLen = 1024
SessionName = "getty-rpc-client" SessionName = "getty-rpc-client"
#!/usr/bin/env bash
# ******************************************************
# DESC :
# AUTHOR : Alex Stocks
# VERSION : 1.0
# LICENCE : Apache License 2.0
# EMAIL : alexstocks@foxmail.com
# MOD : 2017-09-04 22:53
# FILE : pb.sh
# ******************************************************
# descriptor.proto
gopath=~/test/golang/lib/src/github.com/gogo/protobuf/protobuf
# If you are using any gogo.proto extensions you will need to specify the
# proto_path to include the descriptor.proto and gogo.proto.
# gogo.proto is located in github.com/gogo/protobuf/gogoproto
gogopath=~/test/golang/lib/src/
# protoc -I=$gopath:$gogopath:/Users/alex/test/golang/lib/src/github.com/AlexStocks/goext/database/redis/:./ --gogoslick_out=Mredis_meta.proto="github.com/AlexStocks/goext/database/redis":../app/ response.proto
# protoc -I=$gopath:$gogopath:./ --gogoslick_out=Mrole.proto="github.com/AlexStocks/goext/database/registry":./src/ service.proto
protoc -I=$gopath:$gogopath:./ --gogoslick_out=./ service.proto
This diff is collapsed.
syntax = "proto2";
package rpc_examples;
import "github.com/gogo/protobuf/gogoproto/gogo.proto";
option (gogoproto.gostring_all) = true;
option (gogoproto.equal_all) = true;
option (gogoproto.verbose_equal_all) = true;
// option (gogoproto.goproto_stringer_all) = false;
// option (gogoproto.stringer_all) = true;
// option (gogoproto.populate_all) = true;
// option (gogoproto.testgen_all) = true;
// option (gogoproto.benchgen_all) = true;
option (gogoproto.marshaler_all) = true;
option (gogoproto.sizer_all) = true;
option (gogoproto.unmarshaler_all) = true;
option (gogoproto.goproto_getters_all) = false;
option (gogoproto.goproto_enum_prefix_all) = false;
//////////////////////////////////////////
// Request Header
//////////////////////////////////////////
message TestReq {
optional string A = 1 [(gogoproto.nullable) = false];
optional string B = 2 [(gogoproto.nullable) = false];
optional string C = 3 [(gogoproto.nullable) = false];
}
message TestRsp {
optional string A = 1 [(gogoproto.nullable) = false];
}
package rpc_examples
import (
"errors"
)
type TestService struct {
i int
}
func (r *TestService) Service() string {
return "TestService"
}
func (r *TestService) Version() string {
return "v1.0"
}
func (r *TestService) Test(arg TestReq, rsp *TestRsp) error {
rsp.A = arg.A + ", " + arg.B + ", " + arg.C
return nil
}
func (r *TestService) Add(n int, res *int) error {
r.i += n
*res = r.i + 100
return nil
}
func (r *TestService) Err(n int, res *int) error {
return errors.New("this is a error test")
}
package main
import (
"errors"
)
import (
log "github.com/AlexStocks/log4go"
)
type TestABC struct {
A, B, C string
}
type TestRpc struct {
i int
}
func (r *TestRpc) Service() string {
return "TestRpc"
}
func (r *TestRpc) Version() string {
return "v1.0"
}
func (r *TestRpc) Test(arg TestABC, res *string) error {
log.Debug("arg:%+v", arg)
*res = "this is a test"
return nil
}
func (r *TestRpc) Add(n int, res *int) error {
r.i += n
*res = r.i + 100
return nil
}
func (r *TestRpc) Err(n int, res *int) error {
return errors.New("this is a error test")
}
...@@ -19,6 +19,7 @@ import ( ...@@ -19,6 +19,7 @@ import (
) )
import ( import (
"github.com/AlexStocks/getty-examples/rpc/proto"
"github.com/AlexStocks/getty/rpc" "github.com/AlexStocks/getty/rpc"
"github.com/AlexStocks/goext/log" "github.com/AlexStocks/goext/log"
"github.com/AlexStocks/goext/net" "github.com/AlexStocks/goext/net"
...@@ -68,7 +69,7 @@ func initServer() { ...@@ -68,7 +69,7 @@ func initServer() {
panic(jerrors.ErrorStack(err)) panic(jerrors.ErrorStack(err))
return return
} }
err = server.Register(new(TestRpc)) err = server.Register(&rpc_examples.TestService{})
if err != nil { if err != nil {
panic(jerrors.ErrorStack(err)) panic(jerrors.ErrorStack(err))
return return
......
...@@ -29,5 +29,5 @@ FailFastTimeout = "3s" ...@@ -29,5 +29,5 @@ FailFastTimeout = "3s"
TcpReadTimeout = "1s" TcpReadTimeout = "1s"
TcpWriteTimeout = "5s" TcpWriteTimeout = "5s"
WaitTimeout = "1s" WaitTimeout = "1s"
MaxMsgLen = 128 MaxMsgLen = 1024
SessionName = "getty-rpc-server" SessionName = "getty-rpc-server"
...@@ -29,5 +29,5 @@ FailFastTimeout = "3s" ...@@ -29,5 +29,5 @@ FailFastTimeout = "3s"
TcpReadTimeout = "1s" TcpReadTimeout = "1s"
TcpWriteTimeout = "5s" TcpWriteTimeout = "5s"
WaitTimeout = "1s" WaitTimeout = "1s"
MaxMsgLen = 128 MaxMsgLen = 1024
SessionName = "getty-rpc-server" SessionName = "getty-rpc-server"
...@@ -29,5 +29,5 @@ FailFastTimeout = "3s" ...@@ -29,5 +29,5 @@ FailFastTimeout = "3s"
TcpReadTimeout = "1s" TcpReadTimeout = "1s"
TcpWriteTimeout = "5s" TcpWriteTimeout = "5s"
WaitTimeout = "1s" WaitTimeout = "1s"
MaxMsgLen = 128 MaxMsgLen = 1024
SessionName = "getty-rpc-server" SessionName = "getty-rpc-server"
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment