资讯专栏INFORMATION COLUMN

Spring Boot 中使用 thrift 入门

cnio / 3322人阅读

摘要:简介是什么是一个软件框架,用来进行可扩展且跨语言的服务的开发。的功能允许定义一个简单的定义文件中的数据类型和服务接口,以作为输入文件,编译器生成代码用来方便地生成客户端和服务器通信的无缝跨编程语言。

Thrift 简介 Thrift 是什么

Thrift是一个软件框架,用来进行可扩展且跨语言的服务的开发。它结合了功能强大的软件堆栈和代码生成引擎,以构建在 C++, Java, Go,Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, JavaScript, Node.js, Smalltalk, and OCaml 这些编程语言间无缝结合的、高效的服务。

Thrift 的功能

Thrift允许定义一个简单的定义文件中的数据类型和服务接口,以作为输入文件,编译器生成代码用来方便地生成RPC客户端和服务器通信的无缝跨编程语言。

环境安装

基于MAC环境,打开终端,执行如下命令:

brew install thrift

查看是否安装成功:

thrift -version
使用示例 Maven 依赖

在服务端和客户端的 pom.xml 中添加 Thrift 依赖(版本和安装时保持一致):


  org.apache.thrift
  libthrift
  0.11.0
编写服务端 Thrift 文件

hello.thrift

namespace java com.hans.thriftserver
service Hello{
    string helloString(1:string param)
}
使用 Thrift 工具生成 java 代码
thrift -r -gen java hello.thrift

修改 namespace java com.hans.thriftclient 后再次执行则生成客户端 java 代码

将代码放到对应目录

即 com.hans.thriftserver 子目录 thrift 下

编写服务实现类
package com.hans.thriftserver.thrift.impl;

import com.hans.thriftserver.thrift.Hello;
import org.apache.thrift.TException;

public class HelloServiceImpl implements Hello.Iface {

    @Override
    public String helloString(String param) throws TException {
        return "hello: " + param;
    }
}
编写服务端
package com.hans.thriftserver;

import com.hans.thriftserver.thrift.Hello;
import com.hans.thriftserver.thrift.impl.HelloServiceImpl;
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;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @author huangxun
 */
@SpringBootApplication
public class ThriftServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ThriftServerApplication.class, args);

        try {
            System.out.println("服务端开启....");
            TProcessor tprocessor = new Hello.Processor(new HelloServiceImpl());
            // 简单的单线程服务模型
            TServerSocket serverTransport = new TServerSocket(9898);
            TServer.Args tArgs = new TServer.Args(serverTransport);
            tArgs.processor(tprocessor);
            tArgs.protocolFactory(new TBinaryProtocol.Factory());
            TServer server = new TSimpleServer(tArgs);
            server.serve();
        } catch (TTransportException e) {
            e.printStackTrace();
        }
    }
}
编写客户端
package com.hans.thriftclient;

import com.hans.thriftclient.thrift.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;
import org.apache.thrift.transport.TTransportException;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @author huangxun
 */
@SpringBootApplication
public class ThriftClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(ThriftClientApplication.class, args);

        System.out.println("客户端启动....");
        TTransport transport = null;
        try {
            transport = new TSocket("localhost", 9898, 30000);
            // 协议要和服务端一致
            TProtocol protocol = new TBinaryProtocol(transport);
            Hello.Client client = new Hello.Client(protocol);
            transport.open();
            String result = client.helloString("hans");
            System.out.println(result);
        } catch (TTransportException e) {
            e.printStackTrace();
        } catch (TException e) {
            e.printStackTrace();
        } finally {
            if (null != transport) {
                transport.close();
            }
        }
    }
}
启动服务端

后台打印输出:

服务端开启....
执行客户端

后台打印输出:

客户端启动....
hello: hans
Github 完整代码

https://github.com/hxun123/sp...

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

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

相关文章

  • Spring Boot应用监控实战

    摘要:概述之前讲过容器的可视化监控,即监控容器的运行情况,包括使用率内存占用网络状况以及磁盘空间等等一系列信息。实战一下中添加依赖启动应用程序之后,只要在浏览器中输入端点信息就能获得应用的一些状态信息。 showImg(https://segmentfault.com/img/remote/1460000014684947); 概述 之前讲过Docker容器的可视化监控,即监控容器的运行情...

    mtunique 评论0 收藏0
  • SpringBoot应用Docker化

    摘要:微服务的基本思想在于考虑围绕着业务领域组件来创建应用,这些应用可独立地进行开发管理和加速。在分散的组件中使用微服务云架构和平台,使部署管理和服务功能交付变得更加简单。 showImg(https://segmentfault.com/img/remote/1460000014332184); 概述 当下web服务端开发中最火的名词中绝对有微服务的一席之地,其也成为当下互联网后端服务架...

    U2FsdGVkX1x 评论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
  • Spring Boot Admin 2.0开箱体验

    摘要:概述在我之前的应用监控实战一文中,讲述了如何利用版本来可视化地监控应用。接下来我们就来创建一个待监控的示例。 showImg(https://segmentfault.com/img/remote/1460000015671446); 概述 在我之前的 《Spring Boot应用监控实战》 一文中,讲述了如何利用 Spring Boot Admin 1.5.X 版本来可视化地监控 ...

    CastlePeaK 评论0 收藏0

发表评论

0条评论

cnio

|高级讲师

TA的文章

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