Giter VIP home page Giter VIP logo

gothrift's Introduction

gothrift

Golang thrift Server and Client

一个简单的thrift服务与客户端封装

传输层使用:BufferedTransport 二进制传输

协议层使用:BinaryProtocol 二进制协议

处理器使用:MultiplexedProcessor 多服务处理器

示例

examples

创建测试目录

mkdir thrift-go-server

mkdir thrift-go-client

生成thrift文件

Test1.thrift

namespace go test1

/**
* 测试
**/
service Test1 {
    string hello1();
    string hello2();
}

Test2.thrift

namespace go test2

/**
* 测试
**/
service Test2 {
    string hello1();
    string hello2();
}
  • 使用命令生成

thrift-0.14.2.exe --gen go Test1.thrift

thrift-0.14.2.exe --gen go Test2.thrift

  • 把文件夹复制到项目

cp test1 thrift-go-server/

cp test2 thrift-go-server/

cp test1 thrift-go-client/

cp test2 thrift-go-client/

  • 删除里面的remote示例代码 test1-remote test2-remote.go

服务端代码

go mod init thriftgos

go get github.com/wenjy/gothrift

main.go

package main

import (
	"context"
	"fmt"
	"thriftgos/test1"
	"thriftgos/test2"

	"github.com/wenjy/gothrift/server"
)

func main() {
	addr := "localhost:9090"
	handler1 := newTest1Handler()
	handler2 := newTest2Handler()

	tServer := server.NewTServer(addr)
	tServer.RegisterProcessor("test1", test1.NewTest1Processor(handler1))
	tServer.RegisterProcessor("test2", test2.NewTest2Processor(handler2))
	err := tServer.Serve()
	if err != nil {
		fmt.Println(err)
	}
}

type test1Handler struct {
}

func newTest1Handler() *test1Handler {
	return &test1Handler{}
}

func (h *test1Handler) Hello1(ctx context.Context) (r string, err error) {
	return "test1->hello1", nil
}

func (h *test1Handler) Hello2(ctx context.Context) (r string, err error) {
	return "test1->hello2", nil
}

type test2Handler struct {
}

func newTest2Handler() *test2Handler {
	return &test2Handler{}
}

func (h *test2Handler) Hello1(ctx context.Context) (r string, err error) {
	return "test2->hello1", nil
}

func (h *test2Handler) Hello2(ctx context.Context) (r string, err error) {
	return "test2->hello2", nil
}

客户端代码

go mod init thriftgoc

go get github.com/wenjy/gothrift

main.go

package main

import (
	"context"
	"fmt"
	"thriftgoc/test1"
	"thriftgoc/test2"
	"time"

	"github.com/wenjy/gothrift/client"
)

func main() {
	addr := "localhost:9090"

	mc := client.NewMClient(addr, time.Second, time.Second)

	c1, err := mc.Client("test1")
	if err != nil {
		fmt.Println(err)
		return
	}
	defer c1.Trans.Close()
	client1 := test1.NewTest1Client(c1.Client)
	fmt.Println(client1.Hello1(context.Background()))
	fmt.Println(client1.Hello2(context.Background()))

	c2, err := mc.Client("test2")
	if err != nil {
		fmt.Println(err)
		return
	}
	defer c2.Trans.Close()
	client2 := test2.NewTest2Client(c2.Client)
	fmt.Println(client2.Hello1(context.Background()))
	fmt.Println(client2.Hello2(context.Background()))
}

构建&运行

服务端:

go build

./thriftgos

客户端:

go build

./thriftgoc

输出:

test1->hello1 <nil>
test1->hello2 <nil>
test2->hello1 <nil>
test2->hello2 <nil>

gothrift's People

Contributors

wenjy avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.