zoukankan      html  css  js  c++  java
  • thrift入门

    一:thrift 框架引入

    上一篇文章我们了解了基于java的rpc实现方式。非常的核心,但是也非常的简单。代码不够通用,不支持异步等详细的功能。如果需要的话,我们得手动写代码去做封装和功能的完善。我们常说不要重复造轮子,有没有现成的rpc框架能让我直接用呢?肯定有,而且不止一种。比如 Dubbo,gRPC,Thrift等。

    二:thrift的使用

    (1)下载 thrift

    去官网(http://thrift.apache.org/download)下载 thrift-x.x.x.exe 文件

    (2)配置环境变量(为了在任意路径都能使用thrift命令)

    在path中加一下thrift-x.x.x.exe文件的路径。比如我把这个文件放到了 C:softdevelopSoft hrift-0.12.0 目录下

    (3)编写Hello.thrift文件,声明rpc方法和类名等信息

    namespace java com.cs.thrift
    
    service Hello{
    
    	string helloString(1:string para)
    	i32 helloInt(1:string para)
    	bool helloBoolean(1:bool para)
    	void helloVoid()
    	string helloNull()
    
    }
    

    (4)通过thrift生成对应的java类文件

    (5)新建一个maven项目,把生成的类copy到myeclipse中(拷贝gen-java目录下的所有文件即可)

    (6)编写服务的实现类

    新建 HelloServiceImpl,实现 Hello.Iface。并实现里面的方法。

    public class HelloServiceImpl implements Hello.Iface{
    
        @Override
        public String helloString(String para) throws TException {
            // TODO Auto-generated method stub
            return "这是我的一个thrift hello java——————" + para;
        }
    
        @Override
        public int helloInt(String para) throws TException {
            // TODO Auto-generated method stub
            return 0;
        }
    
        @Override
        public boolean helloBoolean(boolean para) throws TException {
            // TODO Auto-generated method stub
            return false;
        }
    
        @Override
        public void helloVoid() throws TException {
            // TODO Auto-generated method stub
            
        }
    
        @Override
        public String helloNull() throws TException {
            // TODO Auto-generated method stub
            return null;
        }
    
    }

    (7)编写server

    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.TTransportException;
    
    /**
     * 编写 Thrift服务端,并启动
     * @author cuishuai01
     */
    public class HelloServiceServer {
    
        public static void main(String[] args) throws TTransportException {
            
            //设置服务监听端口为 8888
            TServerSocket tServerSocket = new TServerSocket(8888);
            //创建自己具体的processor 并 关联Hello服务的实现
            TProcessor tProcessor = new Hello.Processor<Hello.Iface>(new HelloServiceImpl());
            //关联processor
            TServer.Args tArgs = new TServer.Args(tServerSocket);
            tArgs.processor(tProcessor);
            
            //设置协议工厂为 TBinaryProtocol.Factory
            tArgs.protocolFactory(new TBinaryProtocol.Factory());
            
            TServer tServer = new TSimpleServer(tArgs);
            
         System.out.println("下面开始监听客户端发来的服务请求!"); tServer.serve(); } }

    (8)编写client

    /**
     * 编写客户端,并启动
     * @author cuishuai01
     */
    public class HelloClient {
    
        public static void main(String[] args) throws Exception {
            //设置服务端的信息
            TTransport tTransport = new TSocket("127.0.0.1", 8888, 3000);
            
            //协议要和服务端一致
            TProtocol tProtocol = new TBinaryProtocol(tTransport);
    //        TProtocol tProtocol = new TCompactProtocol(tTransport);
    //        TProtocol tProtocol = new TJSONProtocol(tTransport);
            Hello.Client client = new Hello.Client(tProtocol);
            
            tTransport.open();//开启socket
            
            String result = client.helloString("thrift java client");//客户端调用服务端方法
            System.out.println(result);//打印服务端方法的返回结果
            
        }
        
    }

    (9)测试

    启动 HelloServiceServer, 再执行 HelloClient 来调用服务端的方法,效果如下。

  • 相关阅读:
    dependencies 和 devDependencies 区别
    safari下地址跳转bug
    forEach,map和filter的区别
    设置video标签的默认样式
    还不懂移动端适配快看这篇文章
    使用微信帐号登录网站的实现
    基于阿里云市场API服务实现增值税发票识别【OCR】
    词云wordcloud的实现
    数据统计的简单实现思路
    京东联盟API简易PHP接口类
  • 原文地址:https://www.cnblogs.com/CUI-S/p/11625910.html
Copyright © 2011-2022 走看看