码迷,mamicode.com
首页 > 其他好文 > 详细

Thrift 入门之helloWorld

时间:2018-03-24 13:31:28      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:ret   trace   names   XML   技术分享   location   实现   prot   自动加载   

不多说,先看项目结构

技术分享图片

首先先编写一个hello.thrift的文件

hello.thrift

namespace java sawshaw

service HelloService {
     string hello(1:string method, 2:string param)
}

 注意了,这个namespace是 thrif 根目录下tutorial目录的gen-java目录下的,如果没有这个目录,先cmd到tutorial目录,执行thrift -r --gen java tutorial.thrif。就会看到一个gen-java目录了,而这个sawshaw是我自定义的目录,把这个hello.thrift文件放到和tutorial目录同级,cmd到该目录后执行thrift -r --gen hello.thrift

可以看到生成了一个HelloService的java类

技术分享图片

把这个类复制到项目下面改下包名就可以了

再写个实现类HelloImpl对客户端的请求作响应

HelloImpl

package com.sawshaw.thrift;

import org.apache.thrift.TException;

import com.sawshaw.thrift.HelloService.Iface;

public class HelloImpl implements Iface{

	public String hello(String method, String param) throws TException {
		return "method:"+method+",param:"+param;
	}

}

编写服务端代码以启动server

HelloServer

package com.sawshaw.thrift;

import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TThreadPoolServer;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TServerTransport;
import org.apache.thrift.transport.TTransportException;

import com.sawshaw.thrift.HelloService.Iface;

public class HelloServer {
	public static void main(String[] args) throws TTransportException {
        HelloService.Iface impl=new HelloImpl();
        HelloService.Processor<Iface> processor =   
        		new HelloService.Processor<Iface>(impl);
        TServerTransport serverTransport = new TServerSocket(8080);  
        TThreadPoolServer.Args tArgs = new TThreadPoolServer.Args(serverTransport);  
        System.out.println("server has start...");
        tArgs.processor(processor);  
        tArgs.protocolFactory(new TBinaryProtocol.Factory());  
        TServer server = new TThreadPoolServer(tArgs);  
        server.serve();  
	}
}  

编写客户端HelloClient调用服务端代码

HelloClient

package com.sawshaw.thrift;

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 HelloClient {
	public static void main(String[] args) {
		TTransport transport;
		transport = new TSocket("localhost", 8080);
        try {
			transport.open();
		} catch (TTransportException e1) {
			e1.printStackTrace();
		}
        TProtocol protocol = new TBinaryProtocol(transport);
        HelloService.Iface client = new HelloService.Client(protocol);
        // 调用服务的 hello 方法
        String resp = null;
		try {
			resp = client.hello("method","param");
		} catch (TException e) {
			e.printStackTrace();
		}
        System.out.println("resp:"+resp);
        transport.close();
	}

}

 当然pom.xml要引用thrift的包,会自动加载thrift的依赖包

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.sawshaw</groupId>
  <artifactId>thrift</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>thrift</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.11.0</version>
</dependency>
  </dependencies>
</project> 

服务端启动后,客户端运行结果:

resp:method:method,param:param  

 

Thrift 入门之helloWorld

标签:ret   trace   names   XML   技术分享   location   实现   prot   自动加载   

原文地址:https://www.cnblogs.com/JAYIT/p/8638403.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!