资讯专栏INFORMATION COLUMN

Thrift RPC使用入门

junfeng777 / 1226人阅读

摘要:使用入门目前项目使用来作为服务之间调用的底层框架,组内人员说可以尝试使用替换他,性能更出色,且使用更方便。所以找了一些资料,为做一个入门。客户端客户端引入接口,使用生成代理,来访问远程服务对象。

thrift使用入门

目前项目使用hessian来作为服务之间RPC调用的底层框架,组内人员说可以尝试使用thrift替换他,thrift性能更出色,且使用更方便。所以找了一些资料,为thrift做一个入门。

服务描述

thrift的服务描述使用IDL(服务描述语言),然后提供一个代码生成工具,将之转化成不同语言的源代码文件,服务端实现该接口,客户端通过该接口进行服务调用。

样例

接口描述语言
thrift自己定义了接口描述语言,如下,更多的参考 Thrift IDL,或者这篇文章

namespace java com.tongyin.thrift.demo
service HelloWorldServcie{
    string sayHello(1:string username)
}

使用官方提供的代码生成工具
首先官方去下载代码生成工具(提供了多个语言的版本),然后运行

thrift -r --gen java demo.thrift

下面是生成的java版本的java接口文件,没有贴完,给了个大概

package com.szp.mvp.thrift.demo1;

@SuppressWarnings({"cast", "rawtypes", "serial", "unchecked", "unused"})
@javax.annotation.Generated(value = "Autogenerated by Thrift Compiler (0.10.0)", date = "2017-09-26")
public class HelloWorldServcie {
  public interface Iface {
    public String sayHello(String username) throws org.apache.thrift.TException;
  }

服务端
服务器端要实现接口对应的方法

//HelloWorldImpl.java
public class HelloWorldImpl implements HelloWorldServcie.Iface {
    public String sayHello(String username)throws TException{
        System.out.println("server:" + username);
        return "Hi, "+ username;
    }
}

接口实现了,需要将该实现暴露出去,这里又涉及了很多Thrift的类,监听端口.这里需要研究下如何通过一个端口暴率多个服务类,不然对端口资源太浪费

    public class HelloServerDemo {
        public static final int SERVER_PORT=7911;
        public void startServer(){
            try{
                System.out.println("Thrift TThreadPoolServer start...");
                TProcessor tprocessor = new HelloWorldServcie.Processor(new HelloWorldImpl());
                TServerSocket serverTransport = new TServerSocket(SERVER_PORT);
                TThreadPoolServer.Args ttpsArgs = new TThreadPoolServer.Args(serverTransport);
                ttpsArgs.processor(tprocessor);
                ttpsArgs.protocolFactory(new TBinaryProtocol.Factory());
                TServer server = new TThreadPoolServer(ttpsArgs);
                server.serve();
            }catch (Exception e){
                System.out.println("Server start error!!!");
                e.printStackTrace();
            }
    
        }
        public static void main(String[] args){
            HelloServerDemo server = new HelloServerDemo();
            server.startServer();
        }
    }

客户端
客户端引入接口,使用thrift生成代理,来访问远程服务对象。

    public class HelloClientDemo {
        public static final String SERVER_IP="127.0.0.1";
        public static final int SERVER_PORT=7911;
        public static final int TIMEOUT=30000;
    
        public  void startClient(String userName){
            TTransport transport = null;
            try{
                transport = new TSocket(SERVER_IP,SERVER_PORT,TIMEOUT);
                TProtocol protocol = new TBinaryProtocol(transport);
                HelloWorldServcie.Client client = new HelloWorldServcie.Client(protocol);
                transport.open();;
                String result = client.sayHello(userName);
                System.out.println("Thrify client result = " + result);
            }catch(TTransportException e){
                e.printStackTrace();
            }catch(TException e1){
                e1.printStackTrace();
            }finally{
                if(null !=transport){
                    transport.close();
                }
            }
    
    
        }
        public static void main(String[] args){
            HelloClientDemo client = new HelloClientDemo();
            client.startClient("Linda");
        }
    }

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/67718.html

相关文章

  • 后端必备——数据通信知识(RPC、消息队列)一站式总结

    摘要:具体可以参考消息队列之具体可以参考实战之快速入门十分钟入门阿里中间件团队博客是一个分布式的可分区的可复制的基于发布订阅的消息系统主要用于大数据领域当然在分布式系统中也有应用。目前市面上流行的消息队列就是阿里借鉴的原理用开发而得。 我自己总结的Java学习的系统知识点以及面试问题,目前已经开源,会一直完善下去,欢迎建议和指导欢迎Star: https://github.com/Snail...

    Kahn 评论0 收藏0
  • Spring Boot 中使用 thrift 入门

    摘要:简介是什么是一个软件框架,用来进行可扩展且跨语言的服务的开发。的功能允许定义一个简单的定义文件中的数据类型和服务接口,以作为输入文件,编译器生成代码用来方便地生成客户端和服务器通信的无缝跨编程语言。 Thrift 简介 Thrift 是什么 Thrift是一个软件框架,用来进行可扩展且跨语言的服务的开发。它结合了功能强大的软件堆栈和代码生成引擎,以构建在 C++, Java, Go,P...

    cnio 评论0 收藏0
  • RPC框架实践之:Apache Thrift

    摘要:在文章微服务调用链追踪中心搭建一文中模拟出来的调用链就是一个远程调用的例子,只不过这篇文章里是通过这种同步调用方式,利用的是协议在应用层完成的,这种方法虽然奏效,但有时效率并不高。 showImg(https://segmentfault.com/img/remote/1460000014858219); 一、概述 RPC(Remote Procedure Call)即 远程过程调...

    Gilbertat 评论0 收藏0
  • RPC框架实践之:Apache Thrift

    摘要:在文章微服务调用链追踪中心搭建一文中模拟出来的调用链就是一个远程调用的例子,只不过这篇文章里是通过这种同步调用方式,利用的是协议在应用层完成的,这种方法虽然奏效,但有时效率并不高。 showImg(https://segmentfault.com/img/remote/1460000014858219); 一、概述 RPC(Remote Procedure Call)即 远程过程调...

    keithxiaoy 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<