Thrift Demo

环境

ubuntu 12.0

thrift-0.8.0

1. 编写Hello.thrift文件


#!/usr/bin/thrift --gen java

namespace java com.cuijh.hello

    service Hello{  
      

        i32 helloInt(1:i32 para) 
       // string helloString(1:string para)  

       // bool helloBoolean(1:bool para)  
       // void helloVoid()  
       // string helloNull()  
}


2. 在Hello.thrift目录下执行命令:自动生成java代码


thrift -gen java Hell.thrift

执行后会在当前目录下生成一个目录gen-java,里面有Hello.java 


3.创建maven 工程


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.0</modelVersion>

  <groupId>com.cuijh.hello</groupId>
  <artifactId>hello</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>hello</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
		<groupId>org.apache.thrift</groupId>
		<artifactId>libthrift</artifactId>
		<version>0.8.0</version>
	</dependency>
	<dependency>
		<groupId>org.slf4j</groupId>
		<artifactId>slf4j-log4j12</artifactId>
		<version>1.5.8</version>
	</dependency>
  </dependencies>
</project>

4.编写接口:

接口实现Thrift定义文件中的服务。


package com.cuijh.hello;

import org.apache.thrift.TException;

public class HelloServiceImpl implements Hello.Iface{  
   
    public int helloInt(int para) throws TException{  
    	public boolean helloBoolean(boolean para) throws TException{  
            return para;  
        }  
        public int helloInt(int para) throws TException{  
            try {  
                Thread.sleep(20000);  
            } catch (InterruptedException e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
            }  
            return para;  
        }  
          
        public String helloNull() throws TException{  
            return null;  
        }  
          
        public String helloString(String para) throws TException{  
            return para;  
        }  
          
        public void helloVoid() throws TException{  
            System.out.println("Hello World!");  
        }  

}  

5.编写服务器端


package com.cuijh.hello;

import org.apache.thrift.TProcessor;  
import org.apache.thrift.protocol.TBinaryProtocol;  
import org.apache.thrift.protocol.TBinaryProtocol.Factory;  
import org.apache.thrift.server.TServer;  
import org.apache.thrift.server.TSimpleServer;  
import org.apache.thrift.server.TThreadPoolServer;  
import org.apache.thrift.server.TThreadPoolServer.Args;  
import org.apache.thrift.transport.TServerSocket;  
import org.apache.thrift.transport.TServerTransport;  
import org.apache.thrift.transport.TTransportException;

public class HelloServiceServer {  
    /** 
     * 启动thrift服务器 
     * @param args 
     */  
    public static void main(String[] args) {          
        try{  
        //设置服务器端口为7911  
        TServerSocket serverTransport = new TServerSocket(7911);  
        //设置协议工厂为TBinaryProtocol.Factory  
        Factory proFactory = new TBinaryProtocol.Factory();  
        //关联处理器与Hello服务的实现  
        TProcessor processor = new Hello.Processor<Hello.Iface>(new HelloServiceImpl());  
        TServer.Args tArgs = new TServer.Args(serverTransport);  
        tArgs.processor(processor);  
        tArgs.protocolFactory(proFactory);  
        //使用TSimpleServer  
        TServer server = new TSimpleServer(tArgs);  
        System.out.println("Start server on port 7911....");  
        server.serve();  
        }catch(TTransportException e){  
            e.printStackTrace();  
        }         
        /* 
        try{ 
            //设置服务器端口为7911 
            TServerSocket serverTransport = new TServerSocket(7911); 
            //设置协议工厂为TBinaryProtocol.Factory 
            Factory proFactory = new TBinaryProtocol.Factory(); 
            //关联处理器与Hello服务的实现 
            TProcessor processor = new Hello.Processor<Hello.Iface>(new HelloServiceImpl()); 
            Args tArgs = new Args(serverTransport); 
            tArgs.processor(processor); 
            tArgs.protocolFactory(proFactory); 
            TServer server = new TThreadPoolServer(tArgs); 
            System.out.println("Start server on port 7911...."); 
            server.serve(); 
            TServerTransport s = new TServerSocket(11);          
            }catch(TTransportException e){ 
                e.printStackTrace(); 
            } 
            */  
    }  
  
}  

6. 编写客户端


package com.cuijh.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;  
  
public class HelloServiceClient {  
    /** 
     * 调用Hello服务 
     * @param args 
     */  
    public static void main(String[] args) {                  
        try {  
            //设置调用的服务器为本地,端口为7911  
            TTransport transport = new TSocket("localhost", 7911);  
            transport.open();  
            //设置传输协议为TBinaryProtocol  
            TProtocol protocol = new TBinaryProtocol(transport);  
            Hello.Client client = new Hello.Client(protocol);  
            client.helloInt(32);  
            transport.close();  
              
        } catch (TTransportException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        } catch (TException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }  
    }  
  
}  

7、运行

先运行服务器端,再运行客户端。

得到:

  1. Start server on port 7911....

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小狼躲藏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值