java thrift开发姿势

1、编写idl文件

helloService.thrift

namespace java com.bjcaijing.thrift.service

service HelloService {
    string sayString(1:string param)
}


2、生成java文件

thrift-0.9.2.exe -gen java ..\src\main\resources\thrift\*

3、编写服务端

关键在这里Processor<HelloService.Iface> processor = new HelloService.Processor<HelloService.Iface>(new HelloServiceImpl());

package com.bjcaijing.thrift;

import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TBinaryProtocol.Factory;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TThreadPoolServer;
import org.apache.thrift.transport.TServerSocket;
import com.bjcaijing.thrift.service.HelloService;
import com.bjcaijing.thrift.service.HelloService.Processor;
import com.bjcaijing.thrift.service.impl.HelloServiceImpl;

public class Server {

	public static void main(String[] args) throws Exception {

		// 设置服务器端口
		TServerSocket serverTransport = new TServerSocket(9090);

		// 设置二进制协议工厂
		Factory protocolFactory = new TBinaryProtocol.Factory();

		// 处理器关联业务实现
		Processor<HelloService.Iface> processor = new HelloService.Processor<HelloService.Iface>(
				new HelloServiceImpl());

		// 1. 使用单线程标准阻塞I/O模型

		// TServer.Args simpleArgs = new TServer.Args(serverTransport);
		//
		// simpleArgs.processor(processor);
		//
		// simpleArgs.protocolFactory(protocolFactory);
		//
		// TServer server = new TSimpleServer(simpleArgs);

		// server.serve();

		// 2. 使用线程池服务模型

		TThreadPoolServer.Args poolArgs = new TThreadPoolServer.Args(serverTransport);
		poolArgs.processor(processor);
		poolArgs.protocolFactory(protocolFactory);
		TServer poolServer = new TThreadPoolServer(poolArgs);
		System.out.println("开启thrift服务器,监听端口:9090");
		poolServer.serve();

	}

}

4、编写客户端调用

关键在:System.out.println(client.sayString("Hello world"));

package com.bjcaijing.thrift;

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 com.bjcaijing.thrift.service.HelloService;

public class Client {

	public static void main(String[] args) throws Exception {
		// 设置调用的服务地址-端口
		TTransport transport = new TSocket("localhost", 9090);

		// 使用二进制协议
		TProtocol protocol = new TBinaryProtocol(transport);

		// 使用的接口
		HelloService.Client client = new HelloService.Client(protocol);

		// 打开socket
		transport.open();
		System.out.println(client.sayString("Hello world"));
		transport.close();
		
	}

}

package com.bjcaijing.thrift.service.impl;

import org.apache.thrift.TException;

public class HelloServiceImpl implements com.bjcaijing.thrift.service.HelloService.Iface {

	@Override
	public String sayString(String param) throws TException {
		return "thrift server say:" + param;
	}

}




5、结果展示



      


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java thrift开发通常需要以下步骤: 1. 安装thrift编译器:Thrift编译器可用于将thrift文件编译成Java源代码。 2. 编写thrift文件:thrift文件定义了数据类型和服务接口。可以使用thrift IDL语言编写thrift文件。 3. 编译thrift文件:使用thrift编译器将thrift文件编译成Java源代码。 4. 实现服务端:实现thrift生成的服务接口和处理程序。 5. 实现客户端:使用thrift生成的客户端代码调用服务。 下面是一个简单的Java thrift开发示例: 1. 编写thrift文件,定义一个服务接口和一个相关的数据类型: ``` namespace java com.example struct Person { 1: required string name 2: optional i32 age } service PersonService { void addPerson(1: Person person) Person getPerson(1: string name) } ``` 2. 使用thrift编译器将thrift文件编译成Java源代码: ``` thrift --gen java person.thrift ``` 3. 实现服务端: ``` public class PersonServiceImpl implements PersonService.Iface { @Override public void addPerson(Person person) throws TException { // 实现添加人员的逻辑 } @Override public Person getPerson(String name) throws TException { // 实现获取人员信息的逻辑 } } ``` 4. 实现客户端: ``` public class PersonServiceClient { public static void main(String[] args) throws TException { TTransport transport = new TSocket("localhost", 9090); transport.open(); TProtocol protocol = new TBinaryProtocol(transport); PersonService.Client client = new PersonService.Client(protocol); Person person = new Person(); person.setName("张三"); person.setAge(20); client.addPerson(person); Person result = client.getPerson("张三"); System.out.println(result.getName() + " " + result.getAge()); transport.close(); } } ``` 以上就是一个简单的Java thrift开发示例。当然,在实际开发中可能会涉及到更复杂的数据类型和服务接口,需要更多的代码和实现。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值