Skip to content

๐Ÿ“˜ A project for practicing thrift RPC communication with PHP and Java

Notifications You must be signed in to change notification settings

yjham2002/Thrift_Tutorial

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

19 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Thrift based server with JAVA and PHP (Thrift ๊ธฐ๋ฐ˜ ์„œ๋ฒ„ ๋ฐ PHP ํŽ˜์ด์ง€)

This document is about the order of processes on constructing Apache thrift based server with making simple website with a board that allows files to be uploaded

  • ๋ณธ ๋ฌธ์„œ๋Š” ํŒŒ์ผ ์—…๋กœ๋“œ๋ฅผ ์ง€์›ํ•˜๋Š” ๊ฐ„๋‹จํ•œ ๊ฒŒ์‹œํŒ์„ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ์„ ๊ธฐ๋ฐ˜์œผ๋กœ Thrift ๊ธฐ๋ฐ˜ ์„œ๋ฒ„ ๊ตฌ์ถ•์— ๋Œ€ํ•œ ๋‚ด์šฉ์„ ๋‹ค๋ฃน๋‹ˆ๋‹ค.

Integral Part of this project is on Github

  • ๋ณธ ํ”„๋กœ์ ํŠธ์˜ ๋ทฐ๋กœ์„œ ์ž‘์šฉํ•˜๋Š” ํ•„์ˆ˜๋ถˆ๊ฐ€๊ฒฐํ•œ ์š”์†Œ๋Š” ์œ„ ์ฃผ์†Œ๋ฅผ ํ†ตํ•ด ์ฐธ์กฐํ•˜์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
This Project runs with (๋ณธ ํ”„๋กœ์ ํŠธ์˜ ๊ตฌ์„ฑ์š”์†Œ)
  • Thrift as a multiplexer (RPC๋ฅผ ์œ„ํ•œ ํ”Œ๋žซํผ ๋‹ค์ค‘ํ™” ๋ชจ๋“ˆ๋กœ์„œ)
  • C styled IDL as a base declaration (์ž๋ฃŒํ˜• ๋ฐ ๋ฉ”์†Œ๋“œ ์„ ์–ธ์„ ์œ„ํ•œ C ๊ธฐ๋ฐ˜ ๋ชจ๋“ˆ๋กœ์„œ)
  • mybatis as a modeller (DB ๋ชจ๋ธ๋ง ๋ชจ๋“ˆ๋กœ์„œ)
  • JAVA as a controller (Java ๊ธฐ๋ฐ˜ ์ปจํŠธ๋กค๋Ÿฌ๋กœ์„œ)
  • Java bean for encapsulations (Bean ์ž๋ฃŒํ˜• ๊ธฐ๋ฐ˜ ์บก์Šํ™” ๊ธฐ๋Šฅ์œผ๋กœ์„œ)
  • PHP as views (PHP ๊ธฐ๋ฐ˜์˜ ๋ทฐ๋กœ์„œ)

Defining thrift.idl file

  • Define the encapsulating structs, methods and exception.

  • Warning : The order of Bean or Structs is very important since the idl compiling mechanism is forward declarations.

namespace java thrift.gen.javacode // Namespace for java and this namespace will be the package
namespace php ThriftService // Namespace for PHP and this namespace will be the directory of PHP

typedef i16 short // Type redefinition for convenience
typedef i32 int // Type redefinition for convenience
typedef i64 long // Type redefinition for convenience
typedef string String // Type redefinition for convenience
typedef binary ThriftByteBuffer // Type redefinition for convenience
typedef bool boolean // Type redefinition for convenience

exception ThriftServiceException { 1 : int ecode, 2 : String emsg } // Exception definition

struct ThriftUserBean{
	1. int id
	2: String userId
	3: String userPw
	4: String NAME
	5: String DATE
} 

service ThriftService { // Handler Method Interfaces for General Service
	String duplicateUserId(1:String id) throws (1:ThriftServiceException se);
	list<ThriftFileBean> imgFileUpload(1:list<ThriftFileBean> fileBeans) throws (1:ThriftServiceException se); 
	ThriftUserBean getUserInfo(1:String id) throws (1:ThriftServiceException se);
	list<ThriftFileBean> fileUpload(1:list<ThriftFileBean> fileBeans) throws (1:ThriftServiceException se); 
	ThriftUserBean getTest(1:int userNumber) throws (1:ThriftServiceException se);
}

service ThriftAdminService { // Handler Method Interfaces for Admin Service
	list<ThriftFileBean> imgFileUpload(1:list<ThriftFileBean> fileBeans) throws (1:ThriftServiceException se)  ; 
	list<ThriftFileBean> fileUpload(1:list<ThriftFileBean> fileBeans) throws (1:ThriftServiceException se)  ; 
	ThriftUserBean getTest(1:int userNumber) throws (1:ThriftServiceException se)  ; 
} 

Compiling with Thrift.exe

  • Run gen.bat to generate interfaces and required files

    • ThriftService.java and ThriftAdminService.java will be generated.
    • The files that automatically generated are not necessary to check out.
    • In directory named server, there are 2 handlers and it is for processing client's requests and providing responses.
  • Example code snippet in ThriftServiceHandler.java

	@Override
	public ThriftUserResult getUserInfo(String id) throws ThriftServiceException, TException{
		
		ThriftUserResult result = null;
		try{
			result = mp.map(serviceEngine.getUserSvc().getUserInfo(id), ThriftUserResult.class);
		}
		catch (ServiceException e){
			throw new ThriftServiceException(e.getEcode(), e.getEmsg());
		}
		return result;
	}
  • As the code shows, Handlers provide just the data as somewhat routes like controller in Spring Frameworks. But the data type as well as ThriftUserResult must be declared as a Java bean.

Implementing Service

  • There is a directory named svc in core then it is necessary that Implementation needs to be located in here.

  • The Implementing code in svc file must be named exactly the same with method name in Handler and thrift.idl file. In addition, the detail business logics also need to be implemented in here.

  • Example code snippet in SvcUser.java

@ThriftMethod
	public UserResult getUserInfo(String id) throws ServiceException{
		
		final String ERR_MSG = "User doesn't exist";
		String TAG = "getUserInfo : ";
		logger.info(TAG + id);
		
		SqlSession session = null;
		UserResult userInfo = null;

		try{
			session = this.getSession();
			UserMapper mapper = session.getMapper(UserMapper.class);
			userInfo = mapper.getUserInfo(id);
			if(userInfo == null){
				throw new ServiceException(-1, ERR_MSG);
			}
		}
		catch (ServiceException e){
			throw new ServiceException(-1, ERR_MSG);
		}
		finally{
			closeSession(session);
		}
		return userInfo;
	}

Defining Databases query and its Mapper with mybatis

  • With mybatis, there is no need to implement in java

    • Declare required method in interfaces ~Mapper.java.

    • Implement detail query in ~Mapper.xml files with mybatis syntax.

  • Example queries

<mapper namespace="core.logic.mybatis.mapper.UserMapper">
    
    <select id="getUserInfo" parameterType="String" resultType="UserResult">
        SELECT 
        	id
        	, userId
        	, userPw
        	, name
        	, date
        FROM
        	tbl_richware_user
        WHERE
        	userId = #{id}
    </select>
* the account information or DB connection settings would be asserted in config.xml.

* *IMPORTANT* : User-defined Data type or bean must be aliased in config.xml.
  • Example code for showing Aliases and db settings
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-config.dtd">
  
<configuration>
    
    <settings>  
        <setting name="cacheEnabled"              value="true"/>  
        <setting name="lazyLoadingEnabled"        value="false"/>  
        <setting name="multipleResultSetsEnabled" value="true"/>  
        <setting name="useColumnLabel"            value="true"/>  
        <setting name="useGeneratedKeys"          value="false"/>  
        <setting name="defaultExecutorType"       value="SIMPLE"/>  
    </settings>    
	
	<typeAliases>
		<typeAlias alias="UserBean" type="core.logic.bean.persistence.UserBean"/>
		<typeAlias alias="UserResult" type="core.logic.bean.result.UserResult"/>
		<typeAlias alias="BoardBean" type="core.logic.bean.persistence.BoardBean"/>
		<typeAlias alias="ListBean" type="core.logic.bean.persistence.ListBean"/>
	</typeAliases>
	    
	<environments default="development">
		<environment id="development">
			<transactionManager type="JDBC"/>
			<dataSource type="POOLED">
			    
				<property name="driver" value="net.sf.log4jdbc.DriverSpy"/>  
			    <property name="url" value="jdbc:log4jdbc:mysql://182.161.118.74:3306/test?useUnicode=yes&amp;characterEncoding=UTF-8"/>
			    <property name="username" value="test"/>  
			    <property name="password" value="$#@!test"/>
			     
			</dataSource>
		</environment>
	</environments>
	 
	<mappers>
		<mapper resource="core/logic/mybatis/query/UserMapper.xml"/>
	</mappers>
	
</configuration>

Getting started for thrift Compiling

  • Generating thrift.gen file is integral part for running.

  • In your thrift directory, run the command as follow.

C:\thrift -out C:\Users\a\workspace\thrift_share\src --gen java:beans C:\Users\a\workspace\thrift\src\tool\thrift\idl\thrift.idl
C:\thrift -out C:\Users\a\workspace\thrift_share\src\thrift\gen\php  --gen php:oop C:\Users\a\workspace\thrift\src\tool\thrift\idl\thrift.idl

Going on to view part

  • Click the Link to go next

About

๐Ÿ“˜ A project for practicing thrift RPC communication with PHP and Java

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published