`
halloffame
  • 浏览: 55136 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Apache Thrift 初学小讲(六)【spring】

阅读更多

这节把spring整合进Thrift,这次用maven来构建工程,maven的pom.xml加上依赖:

<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-context</artifactId>
	<version>4.3.7.RELEASE</version>
</dependency>

<dependency>
	<groupId>org.apache.thrift</groupId>
	<artifactId>libthrift</artifactId>
	<version>0.9.3</version>
</dependency>

 

1 客户端:我们可以把transport,protocol以及根据接口定义文件生成的ThriftTest.Client的实例化放到spring容器中,由spring容器维护它们的依赖关系,这样客户端的调用代码会简洁很多,并且ThriftTest.Client的transport和protocol将是可配置的。

 

spring-config-client.xml: 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context  
        http://www.springframework.org/schema/context/spring-context.xsd">
	
    <!-- <context:component-scan base-package="thrift.test"/> -->
    
    <bean id="tSocket" class="org.apache.thrift.transport.TSocket" scope="prototype">
	    <constructor-arg name="host" value="localhost"/>
	    <constructor-arg name="port" value="9090"/>
	    <property name="timeout" value="1000"/>
	</bean>
	
	<bean id="tFastFramedTransport" class="org.apache.thrift.transport.TFastFramedTransport" init-method="open" destroy-method="close" scope="prototype">
	    <constructor-arg name="underlying" ref="tSocket"/>
	</bean>
	
	<bean id="tCompactProtocol" class="org.apache.thrift.protocol.TCompactProtocol" scope="prototype">
	    <constructor-arg name="transport" ref="tFastFramedTransport"/>
	</bean>
	
	<bean id="testClient" class="thrift.test.ThriftTest.Client" scope="prototype">
	    <constructor-arg name="prot" ref="tCompactProtocol"/>
	</bean>

</beans>

 

TestClient.java:

package com.halloffame.thriftspring;

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import thrift.test.ThriftTest;
import thrift.test.User;

public class TestClient {
	public static final AbstractApplicationContext context = new ClassPathXmlApplicationContext("spring-config-client.xml");
	
    public static void main(String [] args) throws Exception {
	    ThriftTest.Client testClient = (ThriftTest.Client)context.getBean("testClient");
	    
        //getUser就是ThriftTest.thrift所定义的接口
        User user = testClient.getUser(2); 
        System.out.println("名字:"+ user.getName());
        
        //context.registerShutdownHook();
        //context.close();
        testClient.getInputProtocol().getTransport().close();
    }
}

 

2 服务端:本来也想把那些transport,protocol之类的放到spring当中,但是发现一些成员变量是没有set方法的,无法依赖注入。不过服务端最重要的还是业务逻辑的实现类TestHandler,把这个放到spring容器里。服务端采用注解配置类SpringConfigServer。

 

SpringConfigServer.java:

package com.halloffame.thriftspring;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration //表明这是注解配置类,相当于spring-config-client.xml
@ComponentScan //相当于xml配置文件里的标签<context:component-scan .../>
public class SpringConfigServer {

}

 

TestHandler.java:

package com.halloffame.thriftspring;

import org.apache.thrift.TException;
import org.springframework.stereotype.Component;

import thrift.test.ThriftTest;
import thrift.test.User;

/**
 * 具体的业务逻辑类
 * 实现ThriftTest.thrift里的getUser接口
 */
@Component //由spring容器实例化管理等
public class TestHandler implements ThriftTest.Iface {

	@Override
	public User getUser(int id) throws TException {
		System.out.println("id==>" + id); 
		if (id == 2 ) {
			User user = new User();
			user.setId(2);
			user.setName("另外一个烟火");
			return user;
		}
		return null;
	}

}

 

TestServer.java:

package com.halloffame.thriftspring;

import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.protocol.TProtocolFactory;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TThreadedSelectorServer;
import org.apache.thrift.transport.TFastFramedTransport;
import org.apache.thrift.transport.TNonblockingServerSocket;
import org.apache.thrift.transport.TTransportFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import thrift.test.ThriftTest;

public class TestServer {
	public static final ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfigServer.class);
	
	public static void main(String [] args) throws Exception {
		//从spring容器中取得业务处理bean TestHandler
		ThriftTest.Iface iface = context.getBean(TestHandler.class);
		 
        //ThriftTest.Processor是生成的服务端代码
        ThriftTest.Processor testProcessor = new ThriftTest.Processor(iface);	
        //指定的通信协议
        TProtocolFactory tProtocolFactory = new TCompactProtocol.Factory(); 
        //指定的通信方式
        TTransportFactory tTransportFactory = new TFastFramedTransport.Factory(); 

        int port = 9090;
    	TNonblockingServerSocket tNonblockingServerSocket =
    			new TNonblockingServerSocket(new TNonblockingServerSocket.NonblockingAbstractServerSocketArgs().port(port));
    	TThreadedSelectorServer.Args tThreadedSelectorServerArgs
          	= new TThreadedSelectorServer.Args(tNonblockingServerSocket);
    	tThreadedSelectorServerArgs.processor(testProcessor);
    	tThreadedSelectorServerArgs.protocolFactory(tProtocolFactory);
    	tThreadedSelectorServerArgs.transportFactory(tTransportFactory);
    	//指定的服务器模式
    	TServer serverEngine = new TThreadedSelectorServer(tThreadedSelectorServerArgs);
        System.out.println("Starting the server on port " + port + "...");
        serverEngine.serve();
	}
}

 

3 工程结构图:


 

依次运行TestServer,TestClient,结果如预期。

 

附件是整个eclipse的maven工程。

  • 大小: 58.3 KB
1
1
分享到:
评论

相关推荐

    Apache Thrift 初学小讲(五)【代理】

    在"Apache Thrift 初学小讲(五)【代理】"这篇博文中,我们将探讨Thrift如何实现代理服务,这在分布式系统中非常关键,因为代理可以提供负载均衡、安全控制、监控等功能。 1. **接口定义语言(IDL)**:Thrift ...

    Apache Thrift 初学小讲(七)【负载均衡】

    在本篇初学小讲中,我们将重点关注Thrift在负载均衡方面的应用,这对于构建大型分布式系统至关重要。Thrift通过提供一种高效的数据序列化机制以及RPC(远程过程调用)接口,使得不同编程语言之间可以轻松地进行通信...

    Apache Thrift 初学小讲(三)【http】

    在“Apache Thrift 初学小讲(三)【http】”中,我们将深入探讨Thrift如何与HTTP协议相结合,实现基于HTTP的服务通信。 首先,Thrift 提供了一个名为 `ThriftServlet` 的组件,它是将Thrift服务与Java Servlet容器...

    the programmer's guide to apache thrift

    Apache Thrift is an open source cross language serialization and RPC framework. With support for over 15 programming languages, Apache Thrift can play an important role in a range of distributed ...

    Apache Thrift 初学小讲(八)【zookeeper实现服务注册与发现】

    Apache Thrift 是一个开源的软件框架,用于构建可伸缩的、跨语言的服务。它结合了接口定义语言(IDL)和库,允许开发者定义数据结构和服务接口,然后自动生成多种编程语言的代码,使得不同语言之间可以高效地进行...

    Learning.Apache.Thrift.178588274

    Make applications cross-communicate using Apache Thrift! About This Book Leverage Apache Thrift to enable applications written in different programming languages (Java, C++, Python, PHP, Ruby, and so...

    用C#和C++写的Apache Thrift的小范例

    本例改编自Apache Thrift教程: http://mikecvet.wordpress.com/2010/05/13/apache-thrift-tutorial-the-sequel/ http://chanian.com/2010/05/13/thrift-tutorial-a-php-client/ 原教程使用的是c++ server和...

    Apache Thrift 使用说明

    详细介绍了Apache Thrift在Ubuntu以及Windows下基于C++和Java语言的安装和运行。附有小例子,亲自测试通过。所述方法网上应该有教程,但大多零散不统一或者不完整,因此本人整理了一份,特来分享。

    Learning Apache Thrift

    Apache Thrift 是一个开源的软件框架,用于构建可伸缩的、跨语言的服务。它结合了编译器、协议和库,使得开发人员能够轻松地定义数据类型和服务接口,然后在多种编程语言之间进行高效通信。这个框架最初由Facebook...

    Thrift-server与spring集成

    当我们将Thrift与Spring集成时,我们可以利用Spring的强大功能来管理和协调Thrift服务,从而构建出高效、灵活的分布式系统。 集成Thrift和Spring的主要目的是为了利用Spring的依赖注入(DI)和面向切面编程(AOP)...

    ThriftBook:《 Apache Thrift程序员指南》中示例的源代码

    Apache Thrift程序员指南以下示例中的源代码:Apache Thrift程序员指南 本书分为三个部分:第一部分-Apache Thrift概述对Apache Thrift及其体系结构的高级介绍。 这部分的例子非常有趣。 本部分还介绍了基本的Apache...

    spring-cloud-starter-thrift:spring-cloud-starter-thrift提供SpringCloud对可伸缩的跨语言服务调用框架Apache Thrift的封装和集成

    spring-cloud-starter-thrift简介spring-cloud-starter-thrift提供Spring Cloud对可伸缩的跨语言服务调用框架Apache Thrift的封装和集成。spring-cloud-starter-thrift包括客户端spring-cloud-starter-thrift-client...

    Learning Apache Thrift.pdf

    基于RPC技术的java开发,可用于分布式系统开发,高性能高可用。

    Apache Thrift——可伸缩的跨语言服务开发框架

    Apache Thrift——可伸缩的跨语言服务开发框架 Apache Thrift 是 Facebook 实现的一种高效的、支持多种编程语言的远程服务调用框架。它采用接口描述语言定义并创建服务,支持可扩展的跨语言服务开发,所包含的代码...

    Apache Thrift Java实战源码,包含了客户端和服务端源码

    Apache Thrift 是一款开源的软件框架,主要用于构建可扩展且跨语言的服务。它结合了编译器和库,能够高效地构建分布式系统。在Java领域,Thrift 提供了一种简洁的方式来实现远程过程调用(RPC)服务。在这个实战源码...

    spring与thrift集成

    将 Spring 与 Thrift 集成,可以利用 Spring 的强大功能来管理和调度 Thrift 服务,同时借助 Thrift 实现高效的数据传输和跨语言服务调用。 集成 Spring 和 Thrift 主要涉及以下几个步骤: 1. **创建 Thrift IDL ...

    fbthrift:Facebook的Apache Thrift分支,包括一个新的C ++服务器

    FacebookThrift最初是紧跟Apache Thrift发行的,但现在朝着新的方向发展。 特别是,编译器是从头开始重写的,新的实现具有完全异步的Thrift服务器。 在了解有关这些改进的。 您还可以在原始的Facebook Code 了解...

    Apache Thrift - 可伸缩的跨语言服务开发框架(代码已修正)

    Apache Thrift 是一个开源的跨语言服务开发框架,它由Facebook于2007年创建,现由Apache软件基金会维护。Thrift的主要目标是解决在分布式系统中不同编程语言之间高效、安全的数据通信问题。通过Thrift,开发者可以...

    《springcloud&学习资料》--Apache Thrift 在 SpringCloud 中的使用.zip

    个人花大量时间整理出的实战资料,内容丰富,文档也很详细。无论做毕业设计还是用于学习技能,或工作中当做参考资料,都能发挥重要作用 亲们下载我任何一个付费资源后,即可私信联系我免费下载其他相关资源哦~ ...

Global site tag (gtag.js) - Google Analytics