Thrift入门

news/2024/6/17 4:28:52 标签: java, python

从官网介绍看应该是个RPC框架,通过thrift定义接口,根据thrift文件生成各种语言的代码,c++, python, java....这里工作主要用到java。从晚上抄了个乘法的例子

1. pom依赖

<dependency>
            <groupId>org.apache.thrift</groupId>
            <artifactId>libthrift</artifactId>
            <version>0.10.0</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.0.13</version>
        </dependency>

这里加了个logback日志,方便查看框架日志输出

2. 定义接口

multiple.thrift

namespace java tutorial
namespace py tutorial

/*
 C like comments are supported
*/
// This is also a valid comment

typedef i32 int // We can use typedef to get pretty names for the types we are using
service MultiplicationService
{
        int multiply(1:int n1, 2:int n2),
}

这里定义了一个乘法接口,接收两个整形参数,返回他们的乘积

3. 使用thrift编译器生成java代码

thrift --gen java multiple.thrift

4. 服务端代码

MultiplicationHandler.java
/**
 * Created by GuanXF on 2017/10/8.
 */
public class MultiplicationHandler implements MultiplicationService.Iface {
    public int multiply(int n1, int n2) throws TException {
        System.out.println("n1 = " + n1 + ", n2 = " + n2);
        return  n1 * n2;
    }
}
MultiplicationServer.java
/**
 * Created by GuanXF on 2017/10/8.
 */
public class MultiplicationServer {
    public static MultiplicationHandler handler;
    public static MultiplicationService.Processor processor;

    public static void main(String[] args) {
        handler = new MultiplicationHandler();
        processor = new MultiplicationService.Processor(handler);

        final Runnable simple = new Runnable() {
            public void run() {
                simple(processor);
            }
        };
        new Thread(simple).start();
    } //main

    public static void simple(MultiplicationService.Processor processor){
        try{
            TServerTransport serverTransport = new TServerSocket(9090);
            TServer server = new TSimpleServer(new TServer.Args(serverTransport).processor(processor));

            System.out.println("Starting the simple server...");
            server.serve();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

这里,服务端,应该是使用socket监听9090端口。

5.客户端代码

MultiplicationClient.java
/**
 * Created by GuanXF on 2017/10/8.
 */
public class MultiplicationClient {
    public static void main(String[] args) {
        try{
            TTransport transport;
            transport = new TSocket("localhost", 9090);
            transport.open();
            TProtocol protocol = new TBinaryProtocol(transport);
            MultiplicationService.Client client = new MultiplicationService.Client(protocol);
            perform(client);
            transport.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    } //main

    private static void perform(MultiplicationService.Client client) throws TException {
        int product = client.multiply(3, 5);
        System.out.println("3 * 5 = " + product);
    }
}

 

转载于:https://www.cnblogs.com/luckygxf/p/7636961.html


http://www.niftyadmin.cn/n/1379148.html

相关文章

把水洒到键盘上怎么办_今天才发现,手机键盘还隐藏5个实用功能,不会用实在太可惜了...

在日常生活中&#xff0c;大家对手机的依赖性越来越大了&#xff0c;经常用手机发短信交流。可是&#xff0c;在用手机打字的过程中&#xff0c;你知道手机键盘里还隐藏着其它功能吗&#xff1f;你真的会用手机键盘吗&#xff1f;你知道手机键盘里还隐藏着什么功能吗&#xff1…

线程交互:生产消费模型

这个例子利用线程的wait(),notify(),以及同步和锁来实现,主要为了加深方法和交互理解,简单介绍: 1.仓储初始100 2.随机生产或消费,大于90时不生产,小于20时不消费 3.无限运行 package timeInterval;public class InitNum {public static int num 100;public static int timeIn…

securecrt中使用上传下载sftp

securecrt中使用上传下载sftpSecureCRT这个工具自带了一个FTP&#xff0c;方便我们上传和下载&#xff0c;而且做的比较人性化&#xff0c;由于其基本命令和linux中基本命令大都相似&#xff0c;熟悉LINUX人能很容易上手。 当我们用SecureCRT连接上一台主机时&#xff0c;点击该…

时间复杂度详细分析

常用的时间复杂度所耗费的时间从小到大依次是&#xff1a; O(1) < O(logn) < O(n) < O(nlogn) < O(n2) < O(n3) < O(2n) < O(n!) < O(n^n) 可以利用高等数学中无穷小的比阶进行判断&#xff08;当n->∞时&#xff09; 转载于:https://www.cnblogs.c…

manjora上好玩的游戏_智能电视上的云游戏平台哪个好玩?聊聊云游戏怎么玩

“云游戏”一词火了&#xff0c;说得通俗一点&#xff0c;它指的是&#xff1a;利用云计算而发展出的一种在线游戏点播技术。玩家可以通过各种的设备连接到云服务器中&#xff0c;不用下载安装&#xff0c;点击即可进入游戏。云游戏的优势在于它摆脱了对硬件的依赖&#xff0c;…

俄罗斯方块和贪吃蛇游戏软件:C语言应用初始感受

C语言课程设以一节课&#xff0c;老师提供了一个C语言的飞俄罗斯方块让我们感受&#xff0c;我们所学的C语言课程&#xff0c;主要是各种语句的练习&#xff0c;这次是用我们所学过的知识来感受一个实际的系统。首先安装c-free&#xff0c;然后是将代码贴进去运行界面虽然有点简…

poj2065 SETI(飘忽的英文题面)

译文&#xff1a; 描述 多年来&#xff0c;为了解存在于遥远星系中的其他文明可能要告诉我们的信息&#xff0c;我们投入了大量的人力物力倾听来自太空的电磁无线电信号。其中一种令UTS的科学家们特别感兴趣的信号源便是星云Stupidicus。 最近&#xff0c;我们发现&#xff0…

redis的四大特性和原理

一、redis的过期A.应用场景cookie自动过期&#xff0c;限时优惠价格&#xff0c;限制每分钟的访问次数B.实现方式setex(String key, int seconds, String value)expire key time #秒pexpire key time #毫秒expireat key time #秒pexpireat key time #毫秒C.实现原理定时清理设置…