Thrift(四)——几种服务端类型

本文深入探讨Thrift的四种服务端类型:TSimpleServer的阻塞式单线程模式,TNonblockingServer的非阻塞模式,TThreadPoolServer的线程池模式,以及THsHaServer的半同步半异步模式。通过HelloServer和HelloClient类展示了每种模式的应用。
摘要由CSDN通过智能技术生成

TSimpleServer:简单的阻塞式单线程模式

HelloTSimpleServer类

package com.simplemode;

import com.service.HelloServiceImpl;
import com.xxx.Hello;
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;
import org.apache.thrift.transport.TServerTransport;
import org.apache.thrift.transport.TTransportException;

/**
 * @Auther: liuzhenbing
 * @Date: 2018/7/31 15:09
 * @Description:
 */
public class HelloTSimpleServer {
   
    public static final int SERVER_PORT = 8080;

    /**
     * 单线程阻塞式IO
     * @throws TTransportException
     */
    public void startServer() throws TTransportException {
        TProcessor processor = new Hello.Processor<HelloServiceImpl>(new HelloServiceImpl());
        TServerTransport serverTransport = new TServerSocket(SERVER_PORT);
        TServer.Args args = new TServer.Args(serverTransport);
        args.processor(processor);
        args.protocolFactory(new TBinaryProtocol.Factory());
        TServer server = new TSimpleServer(args);
        System.out.println("server starting.....");
        server.serve();
    }

    public static void main(String[] args) throws TTransportException {
        HelloTSimpleServer server = new HelloTSimpleServer();
        server.startServer();
    }
}

HelloClient类

package com.simplemode;

import com.xxx.Hello;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;

/**
 * @Auther: liuzhenbing
 * @Date: 2018/7/31 15:14
 * @Description:
 */
public class HelloClient {
   
    public static final String SERVER_IP = "localhost";
    public static final int SERVER_PORT = 8080;
    public static final int TIMEOUT = 3000;

    public static void main(Stri
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Vi_NSN

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值