JAVA intercupt_thrift-Java 示例

本文详细介绍如何使用Homebrew安装Thrift并验证版本,通过Maven构建项目,并利用Thrift插件生成Java代码。此外,还提供了服务接口定义和服务实现的具体步骤,包括启动服务端和客户端的示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

安装thrift

mac

brew install thrift

安装完成检查

thrift --version

新建maven项目

pom.xml

org.apache.thrift

libthrift

0.11.0

org.apache.thrift

thrift-maven-plugin

0.10.0

/usr/local/bin/thrift

src/main/resources

thrift-sources

generate-sources

compile

定义服务

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

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个类

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;

}

}

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(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();

}

}

}

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用法差不多

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值