{"id":19347299,"url":"https://github.com/yjham2002/thrift_tutorial","last_synced_at":"2025-11-14T20:04:57.466Z","repository":{"id":115194552,"uuid":"78510382","full_name":"yjham2002/Thrift_Tutorial","owner":"yjham2002","description":":blue_book: A project for practicing thrift RPC communication with PHP and Java","archived":false,"fork":false,"pushed_at":"2017-01-31T05:54:29.000Z","size":9514,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-06T14:35:38.199Z","etag":null,"topics":["idl","java","java-bean","mybatis","thrift"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yjham2002.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-01-10T07:50:27.000Z","updated_at":"2017-01-20T05:13:30.000Z","dependencies_parsed_at":"2023-09-20T19:15:10.549Z","dependency_job_id":null,"html_url":"https://github.com/yjham2002/Thrift_Tutorial","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yjham2002%2FThrift_Tutorial","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yjham2002%2FThrift_Tutorial/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yjham2002%2FThrift_Tutorial/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yjham2002%2FThrift_Tutorial/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yjham2002","download_url":"https://codeload.github.com/yjham2002/Thrift_Tutorial/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240457940,"owners_count":19804489,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["idl","java","java-bean","mybatis","thrift"],"created_at":"2024-11-10T04:15:31.062Z","updated_at":"2025-11-14T20:04:57.352Z","avatar_url":"https://github.com/yjham2002.png","language":"Java","readme":"# Thrift based server with JAVA and PHP (Thrift 기반 서버 및 PHP 페이지)\n\n### 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\n\n- 본 문서는 파일 업로드를 지원하는 간단한 게시판을 구현하는 것을 기반으로 Thrift 기반 서버 구축에 대한 내용을 다룹니다.\n\n### *Integral Part* of this project is [on Github](https://github.com/yjham2002/Thrift_tutorial_PHP)\n\n- 본 프로젝트의 뷰로서 작용하는 필수불가결한 요소는 위 주소를 통해 참조하실 수 있습니다.\n\n##### This Project runs with (본 프로젝트의 구성요소)\n\n- Thrift as a multiplexer (RPC를 위한 플랫폼 다중화 모듈로서)\n- C styled IDL as a base declaration (자료형 및 메소드 선언을 위한 C 기반 모듈로서)\n- mybatis as a modeller (DB 모델링 모듈로서)\n- JAVA as a controller (Java 기반 컨트롤러로서)\n- Java bean for encapsulations (Bean 자료형 기반 캡슐화 기능으로서)\n- PHP as views (PHP 기반의 뷰로서)\n\n#### Defining thrift.idl file\n\n- Define the encapsulating structs, methods and exception.\n\n- Warning : The order of Bean or Structs is very important since the idl compiling mechanism is forward declarations.\n\n```c\nnamespace java thrift.gen.javacode // Namespace for java and this namespace will be the package\nnamespace php ThriftService // Namespace for PHP and this namespace will be the directory of PHP\n\ntypedef i16 short // Type redefinition for convenience\ntypedef i32 int // Type redefinition for convenience\ntypedef i64 long // Type redefinition for convenience\ntypedef string String // Type redefinition for convenience\ntypedef binary ThriftByteBuffer // Type redefinition for convenience\ntypedef bool boolean // Type redefinition for convenience\n\nexception ThriftServiceException { 1 : int ecode, 2 : String emsg } // Exception definition\n\nstruct ThriftUserBean{\n\t1. int id\n\t2: String userId\n\t3: String userPw\n\t4: String NAME\n\t5: String DATE\n} \n\nservice ThriftService { // Handler Method Interfaces for General Service\n\tString duplicateUserId(1:String id) throws (1:ThriftServiceException se);\n\tlist\u003cThriftFileBean\u003e imgFileUpload(1:list\u003cThriftFileBean\u003e fileBeans) throws (1:ThriftServiceException se); \n\tThriftUserBean getUserInfo(1:String id) throws (1:ThriftServiceException se);\n\tlist\u003cThriftFileBean\u003e fileUpload(1:list\u003cThriftFileBean\u003e fileBeans) throws (1:ThriftServiceException se); \n\tThriftUserBean getTest(1:int userNumber) throws (1:ThriftServiceException se);\n}\n\nservice ThriftAdminService { // Handler Method Interfaces for Admin Service\n\tlist\u003cThriftFileBean\u003e imgFileUpload(1:list\u003cThriftFileBean\u003e fileBeans) throws (1:ThriftServiceException se)  ; \n\tlist\u003cThriftFileBean\u003e fileUpload(1:list\u003cThriftFileBean\u003e fileBeans) throws (1:ThriftServiceException se)  ; \n\tThriftUserBean getTest(1:int userNumber) throws (1:ThriftServiceException se)  ; \n} \n```\n#### Compiling with Thrift.exe\n\n- Run gen.bat to generate interfaces and required files\n\n    * ThriftService.java and ThriftAdminService.java will be generated.\n    * The files that automatically generated are not necessary to check out.\n    * In directory named server, there are 2 handlers and it is for processing client's requests and providing responses.\n\n- Example code snippet in ThriftServiceHandler.java\n```java\n\t@Override\n\tpublic ThriftUserResult getUserInfo(String id) throws ThriftServiceException, TException{\n\t\t\n\t\tThriftUserResult result = null;\n\t\ttry{\n\t\t\tresult = mp.map(serviceEngine.getUserSvc().getUserInfo(id), ThriftUserResult.class);\n\t\t}\n\t\tcatch (ServiceException e){\n\t\t\tthrow new ThriftServiceException(e.getEcode(), e.getEmsg());\n\t\t}\n\t\treturn result;\n\t}\n```\n- 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.\n\n#### Implementing Service\n\n- There is a directory named svc in core then it is necessary that Implementation needs to be located in here.\n\n- 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.\n\n- Example code snippet in SvcUser.java\n```java\n@ThriftMethod\n\tpublic UserResult getUserInfo(String id) throws ServiceException{\n\t\t\n\t\tfinal String ERR_MSG = \"User doesn't exist\";\n\t\tString TAG = \"getUserInfo : \";\n\t\tlogger.info(TAG + id);\n\t\t\n\t\tSqlSession session = null;\n\t\tUserResult userInfo = null;\n\n\t\ttry{\n\t\t\tsession = this.getSession();\n\t\t\tUserMapper mapper = session.getMapper(UserMapper.class);\n\t\t\tuserInfo = mapper.getUserInfo(id);\n\t\t\tif(userInfo == null){\n\t\t\t\tthrow new ServiceException(-1, ERR_MSG);\n\t\t\t}\n\t\t}\n\t\tcatch (ServiceException e){\n\t\t\tthrow new ServiceException(-1, ERR_MSG);\n\t\t}\n\t\tfinally{\n\t\t\tcloseSession(session);\n\t\t}\n\t\treturn userInfo;\n\t}\n```\n\n#### Defining Databases query and its Mapper with mybatis\n\n- With mybatis, there is no need to implement in java\n\n\t* Declare required method in interfaces ~Mapper.java.\n\n\t* *Implement* detail query in ~Mapper.xml files with [mybatis syntax](http://www.mybatis.org/mybatis-3/ko/sqlmap-xml.html).\n\n- Example queries\n```xml\n\u003cmapper namespace=\"core.logic.mybatis.mapper.UserMapper\"\u003e\n    \n    \u003cselect id=\"getUserInfo\" parameterType=\"String\" resultType=\"UserResult\"\u003e\n        SELECT \n        \tid\n        \t, userId\n        \t, userPw\n        \t, name\n        \t, date\n        FROM\n        \ttbl_richware_user\n        WHERE\n        \tuserId = #{id}\n    \u003c/select\u003e\n```\n\t* the account information or DB connection settings would be asserted in config.xml.\n\n\t* *IMPORTANT* : User-defined Data type or bean must be aliased in config.xml.\n\n- Example code for showing Aliases and db settings\n```xml\n\u003c?xml version=\"1.0\" encoding=\"UTF-8\" ?\u003e\n\u003c!DOCTYPE configuration\n  PUBLIC \"-//mybatis.org//DTD Config 3.0//EN\"\n  \"http://mybatis.org/dtd/mybatis-3-config.dtd\"\u003e\n  \n\u003cconfiguration\u003e\n    \n    \u003csettings\u003e  \n        \u003csetting name=\"cacheEnabled\"              value=\"true\"/\u003e  \n        \u003csetting name=\"lazyLoadingEnabled\"        value=\"false\"/\u003e  \n        \u003csetting name=\"multipleResultSetsEnabled\" value=\"true\"/\u003e  \n        \u003csetting name=\"useColumnLabel\"            value=\"true\"/\u003e  \n        \u003csetting name=\"useGeneratedKeys\"          value=\"false\"/\u003e  \n        \u003csetting name=\"defaultExecutorType\"       value=\"SIMPLE\"/\u003e  \n    \u003c/settings\u003e    \n\t\n\t\u003ctypeAliases\u003e\n\t\t\u003ctypeAlias alias=\"UserBean\" type=\"core.logic.bean.persistence.UserBean\"/\u003e\n\t\t\u003ctypeAlias alias=\"UserResult\" type=\"core.logic.bean.result.UserResult\"/\u003e\n\t\t\u003ctypeAlias alias=\"BoardBean\" type=\"core.logic.bean.persistence.BoardBean\"/\u003e\n\t\t\u003ctypeAlias alias=\"ListBean\" type=\"core.logic.bean.persistence.ListBean\"/\u003e\n\t\u003c/typeAliases\u003e\n\t    \n\t\u003cenvironments default=\"development\"\u003e\n\t\t\u003cenvironment id=\"development\"\u003e\n\t\t\t\u003ctransactionManager type=\"JDBC\"/\u003e\n\t\t\t\u003cdataSource type=\"POOLED\"\u003e\n\t\t\t    \n\t\t\t\t\u003cproperty name=\"driver\" value=\"net.sf.log4jdbc.DriverSpy\"/\u003e  \n\t\t\t    \u003cproperty name=\"url\" value=\"jdbc:log4jdbc:mysql://182.161.118.74:3306/test?useUnicode=yes\u0026amp;characterEncoding=UTF-8\"/\u003e\n\t\t\t    \u003cproperty name=\"username\" value=\"test\"/\u003e  \n\t\t\t    \u003cproperty name=\"password\" value=\"$#@!test\"/\u003e\n\t\t\t     \n\t\t\t\u003c/dataSource\u003e\n\t\t\u003c/environment\u003e\n\t\u003c/environments\u003e\n\t \n\t\u003cmappers\u003e\n\t\t\u003cmapper resource=\"core/logic/mybatis/query/UserMapper.xml\"/\u003e\n\t\u003c/mappers\u003e\n\t\n\u003c/configuration\u003e\n```\n\n#### Getting started for thrift Compiling\n\n- Generating thrift.gen file is integral part for running.\n\n- In your thrift directory, run the command as follow.\n\n```sh\nC:\\thrift -out C:\\Users\\a\\workspace\\thrift_share\\src --gen java:beans C:\\Users\\a\\workspace\\thrift\\src\\tool\\thrift\\idl\\thrift.idl\nC:\\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\n```\n\n#### Going on to view part\n\n- Click the [Link](https://github.com/yjham2002/Thrift_tutorial_PHP) to go next\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyjham2002%2Fthrift_tutorial","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyjham2002%2Fthrift_tutorial","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyjham2002%2Fthrift_tutorial/lists"}