Java实现Thrift实例

Java实现Thrift实例

前提

需要先下载编译器http://archive.apache.org/dist/thrift/,分为Linux版和Windows,具体编译器的版本跟thriftlib包的版本是相同配套的。

选择使用的是thrift-0.17.0版本

JDK1.8

步骤说明

  1. 编写.thrift文件确认服务功能
  • 定义命名空间,即语言和生成的源码文件的位置
  • 定义服务,即源码文件类名
  • 定义方法
// 定义命名空间,即语言和生成的源码文件的位置
namespace java com.donny.thrift.service
// 定义服务,即源码文件类名
service  HelloService {
  // 定义一个无返回的方法
  void report(1:string data)
  // 一个有返回的方法
  string ping(1:string data)
}
  1. 从thrift文件构建源代码

    利用maven-thrift-plugin插件进行操作。POM文件的相关配置

    注意:在执行源码生成会清空目录下原来的所有文件和文件夹

                <plugin>
                    <groupId>org.apache.thrift.toolsgroupId>
                    <artifactId>maven-thrift-pluginartifactId>
                    <version>0.1.11version>
                    <configuration>
                        
                        <thriftExecutable>${basedir}/src/main/resources/thrift/thrift-${thrift.version}.exethriftExecutable>
                        
                        <thriftSourceRoot>${basedir}/src/main/resources/thriftthriftSourceRoot>
                        
                        <generator>javagenerator>
                        
                        
                    configuration>
                    <executions>
                        <execution>
                            <id>thrift-sourcesid>
                            <phase>generate-sourcesphase>
                            <goals>
                                <goal>compilegoal>
                            goals>
                        execution>
                        <execution>
                            <id>thrift-test-sourcesid>
                            <phase>generate-test-sourcesphase>
                            <goals>
                                <goal>testCompilegoal>
                            goals>
                        execution>
                    executions>
                plugin>
    

    mvn clean install 或者通过IDEA的图形界面上点击按钮。

    将生成的文件copy至项目工程源码文件夹中。

  2. 编写服务端

    编写服务实现类,实现具体的功能。

    public class HelloServiceImpl implements HelloService.Iface

    编写服务端的启动类

         try {
                System.out.println("服务端开启....");
                // 声明一个HelloService的处理器,绑定接口和实现类
                TProcessor tprocessor = new HelloService.Processor<HelloService.Iface>(new HelloServiceImpl());
                // 绑定服务端的端口
                TServerSocket serverTransport = new TServerSocket(50005);
                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();
            }
    
  3. 编写客户端

    测试服务端的功能

        public static void main(String[] args) {
            System.out.println("客户端启动....");
            try (TTransport transport = new TSocket("localhost", 50005)) {
                TProtocol protocol = new TBinaryProtocol(transport);
                HelloService.Client client = new HelloService.Client(protocol);
                // 打开传输
                transport.open();
                // 客户端调用
                String ping = client.ping("活着吗");
                if ("active".equals(ping)) {
                    System.out.println("服务端正常!");
                    client.report("我来了 我是消息");
                }
    
                System.out.println("结束");
            } catch (TException e) {
                e.printStackTrace();
            }
        }
    
  4. 先启动服务启动类,在执行Client的main。

    客户端打印

    客户端启动....
    服务端正常!
    结束
    

    服务端打印

    服务端开启....
    接收数据:我来了 我是消息
    

采用多模块方式开发的,完整的pom文件。

根模块pom.xml


<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>

    <groupId>com.donnygroupId>
    <artifactId>ThriftJavaartifactId>
    <packaging>pompackaging>
    <version>1.0-SNAPSHOTversion>
    <modules>
        <module>servermodule>
        <module>clientmodule>
    modules>

    <properties>
        <maven.compiler.source>8maven.compiler.source>
        <maven.compiler.target>8maven.compiler.target>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-assembly-pluginartifactId>
                <version>3.6.0version>
            plugin>
        plugins>
    build>
project>

服务端pom.xml


<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>ThriftJavaartifactId>
        <groupId>com.donnygroupId>
        <version>1.0-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <artifactId>serverartifactId>

    <properties>
        <maven.compiler.source>8maven.compiler.source>
        <maven.compiler.target>8maven.compiler.target>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <thrift.version>0.17.0thrift.version>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.thriftgroupId>
            <artifactId>libthriftartifactId>
            <version>${thrift.version}version>
        dependency>

        <dependency>
            <groupId>org.slf4jgroupId>
            <artifactId>slf4j-apiartifactId>
            <version>2.0.7version>
        dependency>

        <dependency>
            <groupId>org.slf4jgroupId>
            <artifactId>slf4j-nopartifactId>
            <version>2.0.7version>
        dependency>
    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.thrift.toolsgroupId>
                <artifactId>maven-thrift-pluginartifactId>
                <version>0.1.11version>
                <configuration>
                    
                    <thriftExecutable>${basedir}/src/main/resources/thrift/thrift-${thrift.version}.exe
                    thriftExecutable>
                    
                    <thriftSourceRoot>${basedir}/src/main/resources/thriftthriftSourceRoot>
                    
                    <generator>javagenerator>
                    
                    
                configuration>
                <executions>
                    <execution>
                        <id>thrift-sourcesid>
                        <phase>generate-sourcesphase>
                        <goals>
                            <goal>compilegoal>
                        goals>
                    execution>
                    <execution>
                        <id>thrift-test-sourcesid>
                        <phase>generate-test-sourcesphase>
                        <goals>
                            <goal>testCompilegoal>
                        goals>
                    execution>
                executions>
            plugin>
        plugins>
    build>
project>

客户端pom.xml


<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>ThriftJavaartifactId>
        <groupId>com.donnygroupId>
        <version>1.0-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <artifactId>clientartifactId>

    <properties>
        <maven.compiler.source>8maven.compiler.source>
        <maven.compiler.target>8maven.compiler.target>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    properties>

    <dependencies>
        <dependency>
            <groupId>com.donnygroupId>
            <artifactId>serverartifactId>
            <version>1.0-SNAPSHOTversion>
        dependency>
    dependencies>
project>

你可能感兴趣的:(JAVA,java,开发语言)