thrift

环境准备:

1.下载window版本的thrift编译器

2.下载idea的thirft插件

3.设置thrift编译工具为:步骤1下载的编译器

4.编写thrift文件

namespace java org.chenhao.test.thrift
    service  Hello{
        void helloVoid()
    }

5.编译改文件,生成Hello.java

6.编写HelloServiceImpl,实现了Iface接口

package org.chenhao.test.thrift;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HelloServiceImpl implements Hello.Iface {
    private static final Logger logger= LoggerFactory.getLogger(HelloServiceImpl.class);
    @Override
    public void helloVoid(){
        logger.info("hello world");
    }
}

7.编写HelloServer.java

package org.chenhao.test.thrift;

import org.apache.thrift.TProcessor;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TThreadPoolServer;
import org.apache.thrift.transport.TServerSocket;

public class HelloServer {
    public static void main(String[] args) {
        try {
            TServerSocket tansport = new TServerSocket(7091);
            TProcessor processor = new Hello.Processor(new HelloServiceImpl());
            TBinaryProtocol.Factory factory = new TBinaryProtocol.Factory();
            TServer server = new TThreadPoolServer(new TThreadPoolServer.Args(tansport).processor(processor).protocolFactory(factory));
            server.serve();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

8.编写HelloClient.java

package org.chenhao.test.thrift;

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 HelloClient {
    public static void main(String[] args) {
        try {
            TTransport transport = new TSocket("localhost", 7091);
            transport.open();
            TProtocol protocol = new TBinaryProtocol(transport);
            Hello.Client client = new Hello.Client(protocol);
            client.helloVoid();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

9.运行HelloServer.java

10.运行HelloClient.java,可以观察到命令行打印如下信息:

 

posted @ 2016-11-07 17:56  swaggyC  阅读(250)  评论(0编辑  收藏  举报