前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Facebook远程调用框架thrift入门示例

Facebook远程调用框架thrift入门示例

作者头像
acupt
发布2019-08-26 10:41:01
9860
发布2019-08-26 10:41:01
举报
文章被收录于专栏:一杯82年的JAVA一杯82年的JAVA

和谷歌的gRPC类似,Facebook的thrift也是个优秀的远程调用框架,来入个门。

安装thrift

mac

brew install thrift

安装完成检查

thrift --version

新建maven项目

pom.xml

代码语言:javascript
复制
<dependencies>
        <dependency>
            <groupId>org.apache.thrift</groupId>
            <artifactId>libthrift</artifactId>
            <version>0.11.0</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.thrift</groupId>
                <artifactId>thrift-maven-plugin</artifactId>
                <version>0.10.0</version>
                <configuration>
                    <thriftExecutable>/usr/local/bin/thrift</thriftExecutable> <!--thrift安装路径-->
                    <thriftSourceRoot>src/main/resources</thriftSourceRoot> <!--thrift配置文件路径-->
                </configuration>
                <executions>
                    <execution>
                        <id>thrift-sources</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

定义服务

新建文件 src/main/resources/service.thrift

代码语言:javascript
复制
namespace java com.acupt.thritf.service
service HelloService{
    string hello(1:string name)
}

构建

使用maven插件根据.proto文件生成Java代码,插件已在pom.xml中配置,只需执行命令:

mvn install

构建完成后可以在target中找到生成的Java代码,用这些代码可以实现thrift远程调用。

target/generated-sources/thrift/com/acupt/thritf/service/HelloService.java

如果在项目中无法直接引用上面的类,IDEA右键thrift文件夹 -> Mark Directory as -> Generated Sources Root

现在就可以在项目中引用了

代码

3个类

代码语言:javascript
复制
package com.acupt.thrift;

import com.acupt.thritf.service.HelloService;
import org.apache.thrift.TException;

/**
 * 服务实现类
 */
public class HelloServiceImpl implements HelloService.Iface {

    @Override
    public String hello(String name) throws TException {
        return "hello," + name;
    }
}
代码语言:javascript
复制
package com.acupt.thrift;

import com.acupt.thritf.service.HelloService;
import org.apache.thrift.TProcessor;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TSimpleServer;
import org.apache.thrift.transport.TServerSocket;

/**
 * 服务提供方
 */
public class MyServer {
    public static void main(String args[]) {
        try {
            TProcessor tprocessor = new HelloService.Processor<HelloService.Iface>(new HelloServiceImpl());
            TServerSocket serverTransport = new TServerSocket(50005);
            TServer.Args tArgs = new TServer.Args(serverTransport);
            tArgs.processor(tprocessor);
            tArgs.protocolFactory(new TBinaryProtocol.Factory());
            TServer server = new TSimpleServer(tArgs);
            System.out.println("server starting");
            //定时关闭
            new Thread(() -> {
                try {
                    System.out.println("server wait stop");
                    Thread.sleep(30000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("server stopping");
                server.stop();
                System.out.println("server stop");
            }).start();
            server.serve();//会阻塞
            System.out.println("server finish");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
代码语言:javascript
复制
package com.acupt.thrift;

import com.acupt.thritf.service.HelloService;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;

/**
 * 服务调用方
 */
public class MyClient {

    public static void main(String[] args) {
        TTransport transport = null;
        try {
            transport = new TSocket("localhost", 50005);
            TProtocol protocol = new TBinaryProtocol(transport);
            HelloService.Client client = new HelloService.Client(protocol);
            transport.open();
            String result = client.hello("tom");
            System.out.println(result);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != transport) {
                transport.close();
            }
        }
    }

}

先启动MyServer,成功启动后再启动MyClient。

和grpc用法差不多,gRPC-Java 示例

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-07-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 一杯82年的JAVA 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 安装thrift
  • 新建maven项目
  • 定义服务
  • 构建
  • 代码
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档