windows基于Thrift的php客户端访问java的服务端
安装下载thrift
下载地址如下:
http://thrift.apache.org/download
一个压缩包,一个.exe都要下载下来。
其中:
php运行需要的是压缩包中的 thrift-0.9.3\lib\php\lib\Thrift
.thrift文件生成接口文件需要的是.exe文件
Thrift文件
编写Thrift文件 demoHello.thrift
可以参考
http://thrift.apache.org/docs/types
http://sowow.sinaapp.com/archives/257
namespace java com.thrift.test
service HelloWorldService {
string sayHello(1:string username)
}
F:\thrift\thrift-0.9.3.exe -r -gen java C:\Users\guangliang\Desktop\demoHello.thrift
F:\thrift\thrift-0.9.3.exe -r -gen php C:\Users\guangliang\Desktop\demoHello.thrift
生成两个文件夹
java服务端
创建一个java工程,需要导入两个jar包
libthrift-0.9.3.jar
slf4j-api-1.7.10.jar
Thrift的jar包可以从下面地址找到
http://repo1.maven.org/maven2/org/apache/thrift/libthrift/0.9.3/
进入gen-java,将里面的文件夹复制到工程里面
创建该接口的实现类 HelloWorldImpl.java
package com.thrift.test;
import org.apache.thrift.TException;
/*
* thrift生成接口的实现类
*/
public class HelloWorldImpl implements HelloWorldService.Iface{
@Override
public String sayHello(String username) throws TException {
return "Hi,"+username+" welcome";
}
}
创建java服务端 HelloServiceDemo.java
package com.thrift.test;
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;
/**
* 服务端server
* @author guangliang
*
*/
public class HelloServiceDemo {
public static final int SERVER_PORT = 8090;
public void startServer() {
try {
System.out.println("HelloWorld TSimpleServer start ...");
TProcessor tprocessor = new HelloWorldService.Processor<HelloWorldService.Iface>(new HelloWorldImpl());
//简单的单线程服务模型,一般用于测试
TServerSocket serverTransport = new TServerSocket(SERVER_PORT);
TServer.Args tArgs = new TServer.Args(serverTransport);
tArgs.processor(tprocessor);
tArgs.protocolFactory(new TBinaryProtocol.Factory());
TServer server = new TSimpleServer(tArgs);
server.serve();
}catch(Exception e) {
System.out.println("Server start error!!");
e.printStackTrace();
}
}
public static void main(String[] args) {
HelloServiceDemo helloServiceDemo = new HelloServiceDemo();
helloServiceDemo.startServer();
}
}
java客户端
创建java服务端 HelloClientDemo.java
package com.thrift.test;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
public class HelloClientDemo {
public static final String SERVER_IP="localhost";
public static final int SERVER_PORT=8090;
public static final int TIMEOUT=30000;
public void startClient(String username) {
TTransport transport = null;
try {
transport = new TSocket(SERVER_IP,SERVER_PORT,TIMEOUT);
//协议要和服务端一致
TProtocol protocol = new TBinaryProtocol(transport);
HelloWorldService.Client client = new HelloWorldService.Client(protocol);
transport.open();
String result = client.sayHello(username);
System.out.println("Thrift client result="+result);
}catch(Exception e) {
e.printStackTrace();
}finally {
if(transport!=null) {
transport.close();
}
}
}
public static void main(String[] args) {
HelloClientDemo helloClientDemo = new HelloClientDemo();
helloClientDemo.startClient("qingjian");
}
}
php客户端
假设 C:\Users\guangliang\Desktop\ThriftPhpDemo 为php的工作目录
将gen-php复制进去
将thrift-0.9.3\lib\php\lib\Thrift 复制进去
创建文件 PhpClient.php
代码如下:
<?php
$dir=dirname(__FILE__); #C:\Users\guangliang\Desktop\ThriftPhpDemo
$GEN_DIR = $dir.'\gen-php';
require_once $dir.'\\Thrift\\ClassLoader\\ThriftClassLoader.php';
require_once $dir.'\\Thrift\\Transport\\TTransport.php';
require_once $dir.'\\Thrift\\Transport\\TSocket.php';
require_once $dir.'\\Thrift\\Transport\\TBufferedTransport.php';
require_once $dir.'\\Thrift\\Protocol\\TProtocol.php';
require_once $dir.'\\Thrift\\Protocol\\TBinaryProtocol.php';
require_once $GEN_DIR.'\\HelloWorldService.php';
require_once $dir.'\\Thrift\\Type\\TType.php';
require_once $dir.'\\Thrift\\Type\\TMessageType.php';
require_once $dir.'\\Thrift\\Transport\\TBufferedTransport.php';
require_once $dir.'\\Thrift\\Factory\\TStringFuncFactory.php';
require_once $dir.'\\Thrift\\Exception\\TException.php';
require_once $dir.'\\Thrift\\StringFunc\\TStringFunc.php';
require_once $dir.'\\Thrift\\StringFunc\\Core.php';
error_reporting(E_ALL);
use Thrift\ClassLoader\ThriftClassLoader;
$loader = new ThriftClassLoader();
$loader->registerNamespace('Thrift', $dir.'Thrift');
$loader->registerDefinition('HelloWorldService', $GEN_DIR);
$loader->register();
$loader = new ThriftClassLoader();
use Thrift\Transport\TSocket;
use Thrift\Transport\THttpClient;
use Thrift\Transport\TBufferedTransport;
use Thrift\Exception\TException;
use Thrift\Protocol\TBinaryProtocol;
try{
$socket = new TSocket('localhost',8090);
$ransport = new TBufferedTransport($socket, 1024, 1024);
$protocol = new TBinaryProtocol($ransport);
$client = new HelloWorldServiceClient($protocol);
$ransport->open();
$result=$client->sayHello("qingjian");
echo $result;
}catch(Exception $e) {
print "Exception: ".$e->getMessage();
}
?>
测试
启动java服务器:
右键 HelloServiceDemo -> run as ->java applicaiton
运行php客户端
代码文件
全部的文件,java工程文件,jar包,php都打进去了。
可以直接运行
下载地址:http://download.csdn.net/detail/silentxyz/9246977