资讯专栏INFORMATION COLUMN

Thrift-简单实用

codergarden / 3027人阅读

摘要:安装作用跨语言调用,打破不同语言之间的隔阂。跨项目调用,微服务的么么哒。示例前提版本的版本版本版本说明该示例包含三种语言。

简介

</>复制代码

  1. The Apache Thrift software framework, for scalable cross-language services development, combines a software stack with a code generation engine to build services that work efficiently and seamlessly between C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, JavaScript, Node.js, Smalltalk, OCaml and Delphi and other languages.

Apache Thrift是一个软件框架,用来进行可扩展跨语言的服务开发,结合了软件堆栈和代码生成引擎,用来构建C++,Java,Python...等语言,使之它们之间无缝结合、高效服务。

安装

</>复制代码

  1. brew install thrift
作用

</>复制代码

  1. 跨语言调用,打破不同语言之间的隔阂。
    跨项目调用,微服务的么么哒。
示例 前提

thrift版本:

Go的版本、Php版本、Python版本:

说明

该示例包含python,php,go三种语言。(java暂无)

新建HelloThrift.thrift

进入目录

</>复制代码

  1. cd /Users/birjemin/Developer/Php/study-php

编写HelloThrift.thrift

</>复制代码

  1. vim HelloThrift.thrift

内容如下:

</>复制代码

  1. namespace php HelloThrift {
  2. string SayHello(1:string username)
  3. }
Php测试

加载thrift-php库

</>复制代码

  1. composer require apache/thrift

生成php版本的thrift相关文件

</>复制代码

  1. cd /Users/birjemin/Developer/Php/study-php
  2. thrift -r --gen php:server HelloThrift.thrift

这时目录中生成了一个叫做gen-php的目录。

建立Server.php文件

</>复制代码

  1. registerDefinition("HelloThrift",$GEN_DIR);
  2. $loader->register();
  3. class HelloHandler implements HelloThriftHelloServiceIf
  4. {
  5. public function SayHello($username)
  6. {
  7. return "Php-Server: " . $username;
  8. }
  9. }
  10. $handler = new HelloHandler();
  11. $processor = new HelloThriftHelloServiceProcessor($handler);
  12. $transport = new TBufferedTransport(new TPhpStream(TPhpStream::MODE_R | TPhpStream::MODE_W));
  13. $protocol = new TBinaryProtocol($transport,true,true);
  14. $transport->open();
  15. $processor->process($protocol,$protocol);
  16. $transport->close();

建立Client.php文件

</>复制代码

  1. registerDefinition("HelloThrift", $GEN_DIR);
  2. $loader->register();
  3. if (array_search("--http",$argv)) {
  4. $socket = new THttpClient("local.study-php.com", 80,"/Server.php");
  5. } else {
  6. $host = explode(":", $argv[1]);
  7. $socket = new TSocket($host[0], $host[1]);
  8. }
  9. $transport = new TBufferedTransport($socket,1024,1024);
  10. $protocol = new TBinaryProtocol($transport);
  11. $client = new HelloThriftHelloServiceClient($protocol);
  12. $transport->open();
  13. echo $client->SayHello("Php-Client");
  14. $transport->close();

测试

</>复制代码

  1. php Client.php --http

Python测试

加载thrift-python3模块(只测试python3,python2就不测试了)

</>复制代码

  1. pip3 install thrift

生成python3版本的thrift相关文件

</>复制代码

  1. thrift -r --gen py HelloThrift.thrift

这时目录中生成了一个叫做gen-py的目录。

建立Server.py文件

</>复制代码

  1. #!/usr/bin/python3
  2. # -*- coding: UTF-8 -*-
  3. import sys
  4. sys.path.append("./gen-py")
  5. from HelloThrift import HelloService
  6. from HelloThrift.ttypes import *
  7. from thrift.transport import TSocket
  8. from thrift.transport import TTransport
  9. from thrift.protocol import TBinaryProtocol
  10. from thrift.server import TServer
  11. class HelloWorldHandler:
  12. def __init__(self):
  13. self.log = {}
  14. def SayHello(self, user_name = ""):
  15. return "Python-Server: " + user_name
  16. handler = HelloWorldHandler()
  17. processor = HelloService.Processor(handler)
  18. transport = TSocket.TServerSocket("localhost", 9091)
  19. tfactory = TTransport.TBufferedTransportFactory()
  20. pfactory = TBinaryProtocol.TBinaryProtocolFactory()
  21. server = TServer.TSimpleServer(processor, transport, tfactory, pfactory)
  22. server.serve()

建立Client.py文件

</>复制代码

  1. #!/usr/bin/python3
  2. # -*- coding: UTF-8 -*-
  3. import sys
  4. sys.path.append("./gen-py")
  5. from HelloThrift import HelloService
  6. from HelloThrift.ttypes import *
  7. from HelloThrift.constants import *
  8. from thrift.transport import TSocket
  9. from thrift.transport import TTransport
  10. from thrift.protocol import TBinaryProtocol
  11. host = sys.argv[1].split(":")
  12. transport = TSocket.TSocket(host[0], host[1])
  13. transport = TTransport.TBufferedTransport(transport)
  14. protocol = TBinaryProtocol.TBinaryProtocol(transport)
  15. client = HelloService.Client(protocol)
  16. transport.open()
  17. msg = client.SayHello("Python-Client")
  18. print(msg)
  19. transport.close()

测试

开一个新窗口,运行下面命令:

</>复制代码

  1. python3 Server.php

Go测试

加载go的thrift模块

</>复制代码

  1. go get git.apache.org/thrift.git/lib/go/thrift

生成go版本的thrift相关文件

</>复制代码

  1. thrift -r --gen go HelloThrift.thrift

生成Server.go文件

</>复制代码

  1. package main
  2. import (
  3. "./gen-go/hellothrift"
  4. "git.apache.org/thrift.git/lib/go/thrift"
  5. "context"
  6. )
  7. const (
  8. NET_WORK_ADDR = "localhost:9092"
  9. )
  10. type HelloServiceTmpl struct {
  11. }
  12. func (this *HelloServiceTmpl) SayHello(ctx context.Context, str string) (s string, err error) {
  13. return "Go-Server:" + str, nil
  14. }
  15. func main() {
  16. transportFactory := thrift.NewTTransportFactory()
  17. protocolFactory := thrift.NewTBinaryProtocolFactoryDefault()
  18. transportFactory = thrift.NewTBufferedTransportFactory(8192)
  19. transport, _ := thrift.NewTServerSocket(NET_WORK_ADDR)
  20. handler := &HelloServiceTmpl{}
  21. processor := hellothrift.NewHelloServiceProcessor(handler)
  22. server := thrift.NewTSimpleServer4(processor, transport, transportFactory, protocolFactory)
  23. server.Serve()
  24. }

生成Client.go文件

</>复制代码

  1. package main
  2. import (
  3. "./gen-go/hellothrift"
  4. "git.apache.org/thrift.git/lib/go/thrift"
  5. "fmt"
  6. "context"
  7. "os"
  8. )
  9. func main() {
  10. var transport thrift.TTransport
  11. args := os.Args
  12. protocolFactory := thrift.NewTBinaryProtocolFactoryDefault()
  13. transport, _ = thrift.NewTSocket(args[1])
  14. transportFactory := thrift.NewTBufferedTransportFactory(8192)
  15. transport, _ = transportFactory.GetTransport(transport)
  16. //defer transport.Close()
  17. transport.Open()
  18. client := hellothrift.NewHelloServiceClientFactory(transport, protocolFactory)
  19. str, _ := client.SayHello(context.Background(), "Go-Client")
  20. fmt.Println(str)
  21. }

测试

开一个新窗口,运行下面命令:

</>复制代码

  1. go run Server.go

综合测试

开启两个窗口,保证server开启

</>复制代码

  1. go run Server.go # localhost:9092
  2. python3 Server.py # localhost:9091

php调用go-server、py-server

</>复制代码

  1. php Client.php localhost:9091
  2. php Client.php localhost:9092

python3调用go-server、py-server

</>复制代码

  1. python3 Client.py localhost:9091
  2. python3 Client.py localhost:9092

go调用go-server、py-server

</>复制代码

  1. go run Client.go localhost:9091
  2. go run Client.go localhost:9092
备注

没有测试php的socket,go、python3的http,可以花时间做一下

代码纯属组装,没有深刻了解其中原理,后期打算写一篇理论篇和封装类库篇

参考

https://studygolang.com/articles/1120

https://www.cnblogs.com/qufo/p/5607653.html

https://www.cnblogs.com/lovemdx/archive/2012/11/22/2782180.html

https://github.com/yuxel/thrift-examples

http://thrift.apache.org/

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/28276.html

相关文章

  • Spring Boot 中使用 thrift 入门

    摘要:简介是什么是一个软件框架,用来进行可扩展且跨语言的服务的开发。的功能允许定义一个简单的定义文件中的数据类型和服务接口,以作为输入文件,编译器生成代码用来方便地生成客户端和服务器通信的无缝跨编程语言。 Thrift 简介 Thrift 是什么 Thrift是一个软件框架,用来进行可扩展且跨语言的服务的开发。它结合了功能强大的软件堆栈和代码生成引擎,以构建在 C++, Java, Go,P...

    cnio 评论0 收藏0
  • thrift调用流程分析

    摘要:由于工作需要使用,并且需要根据需求修改源码,因此必须熟悉执行的流程。目前支持的模型包括使用阻塞的单线程服务器,主要用于调试。 由于工作需要使用thrift,并且需要根据需求修改thrift源码,因此必须熟悉thrift执行的流程。以下是根据thrift源码阅读而得出流程分析。 thrift协议栈概述 thrift是一个rpc框架,开发者可以通过thrift自带的接口定义语言(IDL)来...

    nidaye 评论0 收藏0
  • Thrift 简易入门与实战

    摘要:简介是一个软件框架用来进行可扩展且跨语言的服务的开发它结合了功能强大的软件堆栈和代码生成引擎以构建在这些编程语言间无缝结合的高效的服务官网地址安装的安装比较简单在下可以直接使用快速安装或可以通过官网下载这里就不再多说了当下载安装完毕后我们就 简介 thrift是一个软件框架, 用来进行可扩展且跨语言的服务的开发. 它结合了功能强大的软件堆栈和代码生成引擎, 以构建在 C++, Java...

    iliyaku 评论0 收藏0
  • Thrift

    摘要:远程调用服务调用远端的服务的就像直接在本地调用本质上来说是一种服务的是一种服务但它只限于与语言之间的调用提供了跨语言的服务调用服务的组成部件服务本质上是一种架构服务所以在编写一个组件时需要编写端端还要编写传输的协议框架主要是实现这三种部件的 thrift rpc(远程调用)服务:调用远端的服务的就像直接在本地调用.本质上来说是一种c/s服务. Java的RMI是一种rpc服务,但它只限...

    JerryC 评论0 收藏0

发表评论

0条评论

codergarden

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<