Java中使用Thrift

文章详细介绍了如何在Java中使用Thrift建立服务端和客户端的通信,包括定义Thrift接口,生成Java代码,实现服务端接口并启动服务器,以及在客户端直接调用接口进行通信。此外,还提到了使用JDK动态代理的方式创建Thrift客户端,使得业务代码可以更简洁地调用接口。
摘要由CSDN通过智能技术生成

Thrift建立(server端)

定义一个thrift接口:

service ThriftService {
    void myFunc();
}

通过 thrift compiler 生成 java 代码

thrift --gen java ThriftService.thrift

生成了Thrift的接口类:

public class ThriftService {
	// 同步和异步调用接口
    public interface Iface {...}
    public interface AsyncIface {...}
    
    // 接口的实现类(同步和异步)
    public static class Client {...}
    public static class AsyncClient {...}   
}

实现server端:

public class Server {
    public static class ThriftServiceImpl implements ThriftService.Iface{
 
        @Override
        public String myFunc(String name) throws TException {
            return "My Thrift Function";
        }
    }
 
    public static void main(String[] args) throws TTransportException {
        // 获取实现
        ThriftServiceImpl impl= new ThriftServiceImpl();
        // 接口与实现类的绑定关系在这里完成
        HelloWorldService.Processor<ThriftServiceImpl> processor = new ThriftService.Processor<ThriftServiceImpl>(impl);
        // 构建服务器
        TServerTransport serverTransport = new TServerSocket(9090);
        TServer server = new TSimpleServer(new Args(serverTransport).processor(processor));
        server.serve();
    }
}

Thrift调用(client端)

client端可以直接调用接口,二不用关心具体的实现类。

1 直接建立thrift通信

public class Client {
    public static void main(String[] args) throws TException {
        TTransport transport = new TSocket("localhost", 9090);
        transport.open();
        TProtocol protocol = new TBinaryProtocol(transport);
        ThriftService.Client client = new ThriftService.Client(protocol);
		client.myFunc();
        transport.close();
    }
}

或者:

	TFastFramedTransport transport = new TFastFramedTransport(new TSocket(host, port));
	TBinaryProtocol protocol = new TBinaryProtocol(transport);
	SimpleSerice.Client client = new SimpleService.Client(protocol);
	try {
	    transport.open();
	    client.myFunc();
	} finally {
	    transport.close();
	}

通信层协议,使用TTransport接口来描述,Thrift 提供了几种 transport 类供使用:

  • TSocket:BIO
  • TFramedTransport:NIO
  • TMemoryTransport:内存I/O

需要注意的是,transport 客户端要和服务端对应,比如如果要对 NIO 模型的服务端进行调研,transport 就必须使用 TFramedTransport或TFastFramedTransport。

传输层协议,引用 transport 并提供 thrift 的序列化协议,Thrift 也提供了几种常用的传输协议:

  • TBinaryProtocol:二进制协议
  • TCompactProtocol:压缩格式
  • TJSONProtocol:JSON 协议

2 使用JDK动态代理

首先创建代理类,这里只需要引用 thrift 接口中的 Service 类,并通过引用发现内部 Client 类:

public class ThriftClientProxy implements InvocationHandler {

    private Class clazz;
    private Class clientClazz;
    private String host;
    private int port;

    public Object newInstance(Class clazz, String host, int port) {
        this.clazz = clazz;
        this.host = host;
        this.port = port;
        this.clientClazz = Class.forName(classs.getName() + "$Client");

        return Proxy.newProxyInstance(
            getClass().getClassLoader(),
            clientClazz.getInterfaces(),
            this
        );
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        TFastFramedTransport transport = new TFastFramedTransport(new TSocket(host, port));
        TBinaryProtocol protocol = new TBinaryProtocol(transport);
        Class[] classes = {TProtocol.class};
        try {
            transport.open();
            return method.invoke(clientClazz.getConstructor(classes).newInstance(protocol), args);
        }
        finally {
            transport.close();
        }
    }
}

之后将相应的 Iface 注册到 IOC 容器中即可。

@Configuration
public class ThriftClientConfiguration {

    @Value("${simple.service.host}")
    private String host;

    @Value("${simple.service.port}")
    private int port;

    @Bean
    public SimpleService.Iface thriftClient() {
        return (SimpleService.Iface) new ThriftClientProxy().newInstance(SimpleService.class, host, port);
    }
}

业务代码中只需要注入 SimpleService.Iface,并直接调用接口方法,而完全不需要关注内部逻辑实现。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值