zoukankan      html  css  js  c++  java
  • 创建Thrift Server和Thrift Client

    1、创建Server

    package cn.horace.thrift.server;
    
    import cn.horace.thrift.idl.IUserService;
    import cn.horace.thrift.rpc.IUserServiceImpl;
    import org.apache.thrift.protocol.TBinaryProtocol;
    import org.apache.thrift.protocol.TProtocolFactory;
    import org.apache.thrift.server.TServer;
    import org.apache.thrift.server.TThreadPoolServer;
    import org.apache.thrift.transport.*;
    
    import java.io.IOException;
    
    /**
     * Created by Horace on 15-4-10下午9:00.
     */
    public class SimpleServer
    {
        public static void main(String[] args) throws TTransportException, IOException
        {
            // 创建处理器
            IUserService.Processor<IUserServiceImpl> processor = new IUserService.Processor<IUserServiceImpl>(new IUserServiceImpl());
    
            // 创建传输对象
            TServerTransport serverTransport = new TServerSocket(9090);
    
            // 创建传输工厂,非阻塞
            TTransportFactory transportFactory = new TFramedTransport.Factory();
    
            // 创建协议工厂
            TProtocolFactory protocolFactory = new TBinaryProtocol.Factory();
    
            // 设置服务器参数
            TThreadPoolServer.Args serverArgs = new TThreadPoolServer.Args(serverTransport);
    
            // 设置处理器
            serverArgs.processor(processor);
    
            // 设置使用的协议
            serverArgs.protocolFactory(protocolFactory);
    
            // 设置使用的传输对象
            serverArgs.transportFactory(transportFactory);
    
            // 创建服务器
            TServer server = new TThreadPoolServer(serverArgs);
    
            System.out.println("Starting the simple server...");
            server.serve();
        }
    }

    2、创建客户端

    package cn.horace.thrift.client;
    
    import cn.horace.thrift.idl.IUserService;
    import org.apache.thrift.TException;
    import org.apache.thrift.protocol.TBinaryProtocol;
    import org.apache.thrift.protocol.TProtocol;
    import org.apache.thrift.transport.TFramedTransport;
    import org.apache.thrift.transport.TSocket;
    import org.apache.thrift.transport.TTransport;
    
    /**
     * Created by Horace on 15-4-10下午9:00.
     */
    public class SimpleClient
    {
        public static void main(String[] args) throws TException, InterruptedException
        {
            // 创建传输对象
            TSocket baseTransport = new TSocket("127.0.0.1", 9090);
    
            // 创建传输对象,非阻塞式
            TTransport transport = new TFramedTransport.Factory().getTransport(baseTransport);
    
            // 打开连接通道
            transport.open();
    
            // 创建协议对象
            TProtocol protocol = new TBinaryProtocol(transport);
    
            // 创建客户端对象
            IUserService.Client client = new IUserService.Client(protocol);
    
            client.findAll();
    
            transport.close();
    
            System.out.println("findAll ...");
        }
    }
  • 相关阅读:
    [置顶] flex4事件监听与自定义事件分发(三)
    不要通过终止进程的方式清理内存
    两个关于XML解析报错问题小记
    HDU H204 阿牛的EOF牛肉串
    <Win32_8>由浅入深——滚动条
    设计模式19---设计模式之状态模式(State)(行为型)
    NetworkX学习笔记-5-NetworkX中怎样对多个网络赋属性,并根据属性排序
    OpenRisc-41-or1200的cache模块分析
    Hibernate 入门的第一个程序
    NDK GDB 中打印vector , vector<vector <> >
  • 原文地址:https://www.cnblogs.com/horace/p/4422489.html
Copyright © 2011-2022 走看看