zoukankan      html  css  js  c++  java
  • Thrift简单调用

    pom.xml

     1 <project xmlns="http://maven.apache.org/POM/4.0.0"
     2     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     3     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     4     <modelVersion>4.0.0</modelVersion>
     5     <groupId>io.guangsoft</groupId>
     6     <artifactId>thrift</artifactId>
     7     <version>0.1</version>
     8     <packaging>jar</packaging>
     9     <properties>
    10         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    11     </properties>
    12     <dependencies>
    13         <dependency>
    14             <groupId>org.apache.thrift</groupId>
    15             <artifactId>libthrift</artifactId>
    16             <version>0.10.0</version>
    17         </dependency>
    18         <dependency>
    19             <groupId>org.slf4j</groupId>
    20             <artifactId>slf4j-log4j12</artifactId>
    21             <version>1.7.5</version>
    22         </dependency>
    23     </dependencies>
    24     <build>
    25         <plugins>
    26             <plugin>
    27                 <groupId>org.apache.maven.plugins</groupId>
    28                 <artifactId>maven-compiler-plugin</artifactId>
    29                 <configuration>
    30                     <source>1.8</source>
    31                     <target>1.8</target>
    32                 </configuration>
    33             </plugin>
    34         </plugins>
    35     </build>
    36 </project>

    在官网下载thrift工具 http://thrift.apache.org/download 

    编写Thrift.thrift

    1 namespace java io.guangsoft.thrift
    2 service Thrift{
    3     string TransmitStr(1:string para)
    4 }

    执行命令 thrift -r -gen java Thrift.thrift

    ThriftImpl.java

     1 package io.guangsoft.thrift;
     2 
     3 import org.apache.thrift.TException;
     4 
     5 public class ThriftImpl implements Thrift.Iface{
     6 
     7     @Override
     8     public String TransmitStr(String para) throws TException {
     9         return "GuangSoft Thrift : " + para;
    10     }
    11 
    12 }

    ThriftServer.java

     1 package io.guangsoft.thrift.Server;
     2 
     3 import org.apache.thrift.TProcessor;
     4 import org.apache.thrift.protocol.TBinaryProtocol;
     5 import org.apache.thrift.server.TServer;
     6 import org.apache.thrift.server.TSimpleServer;
     7 import org.apache.thrift.transport.TServerSocket;
     8 import org.apache.thrift.transport.TTransportException;
     9 
    10 import io.guangsoft.thrift.Thrift;
    11 import io.guangsoft.thrift.ThriftImpl;
    12 
    13 public class ThriftServer {
    14     /**
    15      * 启动thrift服务器
    16      * 
    17      * @param args
    18      */
    19     public static void main(String[] args) {
    20         try {
    21             System.out.println("服务端开启....");
    22             TProcessor tprocessor = new Thrift.Processor<Thrift.Iface>(new ThriftImpl());
    23             // 简单的单线程服务模型
    24             TServerSocket serverTransport = new TServerSocket(9898);
    25             TServer.Args tArgs = new TServer.Args(serverTransport);
    26             tArgs.processor(tprocessor);
    27             tArgs.protocolFactory(new TBinaryProtocol.Factory());
    28             TServer server = new TSimpleServer(tArgs);
    29             server.serve();
    30         } catch (TTransportException e) {
    31             e.printStackTrace();
    32         }
    33     }
    34 }

    ThriftClient.java

     1 package io.guangsoft.thrift.client;
     2 
     3 import org.apache.thrift.TException;
     4 import org.apache.thrift.protocol.TBinaryProtocol;
     5 import org.apache.thrift.protocol.TProtocol;
     6 import org.apache.thrift.transport.TSocket;
     7 import org.apache.thrift.transport.TTransport;
     8 import org.apache.thrift.transport.TTransportException;
     9 
    10 import io.guangsoft.thrift.Thrift;
    11 
    12 public class ThriftClient {
    13     public static void main(String[] args) {
    14         System.out.println("客户端启动....");
    15         TTransport transport = null;
    16         try {
    17             transport = new TSocket("localhost", 9898, 30000);
    18             // 协议要和服务端一致
    19             TProtocol protocol = new TBinaryProtocol(transport);
    20             Thrift.Client client = new Thrift.Client(protocol);
    21             transport.open();
    22             String result = client.TransmitStr("你好呀,Thrift!");
    23             System.out.println(result);
    24         } catch (TTransportException e) {
    25             e.printStackTrace();
    26         } catch (TException e) {
    27             e.printStackTrace();
    28         } finally {
    29             if (null != transport) {
    30                 transport.close();
    31             }
    32         }
    33     }
    34 }
  • 相关阅读:
    ElasticSearch搜索引擎安装配置拼音插件pinyin
    ElasticSearch搜索引擎安装配置中文分词器IK插件
    Linux系统中ElasticSearch搜索引擎安装配置Head插件
    阿里云服务器Linux系统安装配置ElasticSearch搜索引擎
    盒子模型
    div与span的区别
    .与localhost与 .sqlexpress的区别
    C#-多态
    C#-里氏转换
    C#-继承
  • 原文地址:https://www.cnblogs.com/guanghe/p/9282238.html
Copyright © 2011-2022 走看看