项目作者: TarsCloud

项目描述 :
Java语言框架rpc源代码实现
高级语言: Java
项目地址: git://github.com/TarsCloud/TarsJava.git
创建时间: 2018-09-10T08:50:36Z
项目社区:https://github.com/TarsCloud/TarsJava

开源协议:

下载


Tars Java - An RPC library and framework

Latest release

This project is the source code of the Tars RPC framework Java language.










Homepage: tarscloud.org
中文版: 点我查看中文版

Environmental dependence

  • JDK1.8 or above
  • Maven 3.5 or above

#

TarsFramework deployment by Docker

This guide uses Docker to complete the deployment of Tars.[Macos、 Linux]

2. Start TarsFramework in Docker

  1. docker pull tarscloud/framework:latest
  2. docker pull tarscloud/tars-node:latest
  3. docker pull mysql:5.6
  4. docker network create -d bridge --subnet=172.25.0.0/16 --gateway=172.25.0.1 tars
  5. docker run -d \
  6. --net=tars \
  7. -e MYSQL_ROOT_PASSWORD="root@appinside" \
  8. --ip="172.25.0.2" \
  9. --name=tars-mysql \
  10. mysql:5.6
  11. sleep 30s
  12. docker run -d \
  13. --net=tars \
  14. -e MYSQL_HOST=172.25.0.2 \
  15. -e MYSQL_ROOT_PASSWORD='root@appinside' \
  16. -eREBUILD=false -eSLAVE=false \
  17. -e INET=eth0 \
  18. --ip="172.25.0.4" \
  19. -p 3000-3001:3000-3001 \
  20. tarscloud/framework
  21. sleep 60s
  22. docker run -d --net=tars --ip="172.25.0.3" -eWEB_HOST=http://172.25.0.4:3000 tarscloud/tars-node

Note: - P 18600-18700:18600-18700 parameter opens 18600-18700 port for application. You can add more ports if necessary

Quick Start To TarsServer

This guide gives you a quick introduction to Tars in Java through simple server

Project structure

  1. ├── pom.xml
  2. └── src
  3. └── main
  4. ├── java
  5. └── tars
  6. └── testapp
  7. ├── HelloServant.java
  8. ├── QuickStartApplication.java
  9. └── impl
  10. └── HelloServantImpl.java
  11. └── resources
  12. └── hello.tars

Dependency configuration

The following configuration needs to be added in pom.xml:

Spring boot and Tars framework dependency

  1. <properties>
  2. <spring-boot.version>2.0.3.RELEASE</spring-boot.version>
  3. </properties>
  4. <dependencyManagement>
  5. <dependencies>
  6. <dependency>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-dependencies</artifactId>
  9. <version>${spring-boot.version}</version>
  10. <type>pom</type>
  11. <scope>import</scope>
  12. </dependency>
  13. </dependencies>
  14. </dependencyManagement>
  15. <dependencies>
  16. <dependency>
  17. <groupId>com.tencent.tars</groupId>
  18. <artifactId>tars-spring-boot-starter</artifactId>
  19. <version>1.7.4</version>
  20. </dependency>
  21. </dependencies>

Plugin dependency

  1. <!--tars2java plugin-->
  2. <plugin>
  3. <groupId>com.tencent.tars</groupId>
  4. <artifactId>tars-maven-plugin</artifactId>
  5. <version>1.7.0</version>
  6. <configuration>
  7. <tars2JavaConfig>
  8. <!-- tars file location -->
  9. <tarsFiles>
  10. <tarsFile>${basedir}/src/main/resources/hello.tars</tarsFile>
  11. </tarsFiles>
  12. <!-- Source file encoding -->
  13. <tarsFileCharset>UTF-8</tarsFileCharset>
  14. <!-- Generate server code -->
  15. <servant>true</servant>
  16. <!-- Generated source code encoding -->
  17. <charset>UTF-8</charset>
  18. <!-- Generated source code directory -->
  19. <srcPath>${basedir}/src/main/java</srcPath>
  20. <!-- Generated source code package prefix -->
  21. <packagePrefixName>com.qq.tars.quickstart.server.</packagePrefixName>
  22. </tars2JavaConfig>
  23. </configuration>
  24. </plugin>
  25. <!--package plugin-->
  26. <plugin>
  27. <groupId>org.apache.maven.plugins</groupId>
  28. <artifactId>maven-jar-plugin</artifactId>
  29. <version>2.6</version>
  30. <configuration>
  31. <archive>
  32. <manifestEntries>
  33. <Class-Path>conf/</Class-Path>
  34. </manifestEntries>
  35. </archive>
  36. </configuration>
  37. </plugin>
  38. <plugin>
  39. <groupId>org.springframework.boot</groupId>
  40. <artifactId>spring-boot-maven-plugin</artifactId>
  41. <configuration>
  42. <!--set mainclass-->
  43. <mainClass>com.qq.tars.quickstart.server.QuickStartApplication</mainClass>
  44. </configuration>
  45. <executions>
  46. <execution>
  47. <goals>
  48. <goal>repackage</goal>
  49. </goals>
  50. </executions>
  51. </plugin>

Service development

Tars interface file definition

Tars has its own interface file format. First, we need to define the Tars interface file. Create a new hello.tars file in the resources directory with the following content:

  1. module TestApp
  2. {
  3. interface Hello
  4. {
  5. string hello(int no, string name);
  6. };
  7. };
Interface file compilation

Then we need to convert the Tars interface file to the server interface code using the tars-maven-plugin. In the project root directory, execute mvn tars: tars2java to get HelloServant.java, the content is as follows:

  1. @Servant
  2. public interface HelloServant {
  3. public String hello(int no, String name);
  4. }
Interface implementation

Next we need to implement the generated server interface. Create a new HelloServantImpl.java file, implement the HelloServant.java interface, and expose the service through the @TarsServant annotation, where ‘HelloObj’ is the servant name, corresponding to the name in the web management platform.

  1. @TarsServant("HelloObj")
  2. public class HelloServantImpl implements HelloServant {
  3. @Override
  4. public String hello(int no, String name) {
  5. return String.format("hello no=%s, name=%s, time=%s", no, name, System.currentTimeMillis());
  6. }
  7. }
Tars service enabling

Finally, add @EnableTarsServer annotation in the spring boot startup class QuickStartApplication to enable Tars service:

  1. @SpringBootApplication
  2. @EnableTarsServer
  3. public class QuickStartApplication {
  4. public static void main(String[] args) {
  5. SpringApplication.run(QuickStartApplication.class, args);
  6. }
  7. }
Directory Features
net Source code implementation of Java language net framework
core Source code implementation of Java language rpc framework
tools Source code implementation of framework tools, maven plug-ins, etc
examples Sample code for the Java language framework
distributedContext Source code implementation of Java language framework’s distributed context
protobuf Source code implementation of pb protocol support
spring Source code implementation of spring framework support