springboot 开发 Tars

时间:2023-12-11 15:08:38

1,创建 springboot 项目,并在启动类添加 @EnableTarsServer 注解

@SpringBootApplication
@EnableTarsServer
public class GlobalIdApplication { public static void main(String[] args) {
SpringApplication.run(GlobalIdApplication.class, args);
}
}

2,pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.ictpaas</groupId>
<artifactId>GlobalId</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>GlobalId</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<!-- tars 版本 -->
<tars.version>1.6.0</tars.version>
<!-- springboot 版本 -->
<spring-boot.version>2.0.3.RELEASE</spring-boot.version>
<spirngmvc.version>4.3.14.RELEASE</spirngmvc.version>
</properties> <dependencies>
<dependency>
<groupId>com.tencent.tars</groupId>
<artifactId>tars-spring-boot-starter</artifactId>
<version>${tars.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.tencent.tars</groupId>
<artifactId>tars-server</artifactId>
<version>${tars.version}</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- spring mvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>
<!-- springboot 启动类 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.ictpaas.GlobalIdApplication</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- tars 文件位置 -->
<plugin>
<groupId>com.tencent.tars</groupId>
<artifactId>tars-maven-plugin</artifactId>
<version>1.6.0</version>
<configuration>
<tars2JavaConfig>
<!-- 项目中 tars 文件位置 -->
<tarsFiles>
<tarsFile>${basedir}/src/main/resources/GlobalId.tars</tarsFile>
</tarsFiles>
<tarsFileCharset>UTF-8</tarsFileCharset>
<!-- 生成接口,true 服务端,false 客户端 -->
<servant>false</servant>
<charset>UTF-8</charset>
<srcPath>${basedir}/src/main/java</srcPath>
<!-- 生成源代码包前缀 -->
<packagePrefixName>com.ictpaas.tars.client</packagePrefixName>
</tars2JavaConfig>
</configuration>
</plugin>
</plugins>
</build> </project>

3,创建 tars 文件,生成接口文件

分两次生成接口,一次生成服务端接口(一个服务肯定要有服务端接口,除了 http 服务),一次生成客户端接口(客户端是为了方便别的服务来调用当前服务)

4,实现服务端接口,重写接口方法并暴露服务

@TarsServant("GlobalIdObj") 表示 obj 名是 GlobalIdObj。如果不使用 springboot ,暴露接口需要在 xml 文件中配置
@TarsServant("GlobalIdObj")
public class GlobalIdServantImpl implements GlobalIdServant { @Override
public String getGlobalId(String pre) {
// TODO Auto-generated method stub // 获取当前年月日
if(pre.length() != 5) {
return "10001";
}
String[] strNow = new SimpleDateFormat("yyyy-MM-dd").format(new Date()).toString().split("-");
String currYear = strNow[0];
String currMonth = strNow[1];
String currDay = strNow[2]; // 17 位随机数,来自 UUID 保留 机器识别规则
String uuidRandom = UUID.randomUUID().toString().substring(15).replace("-", ""); return pre.concat(currYear).concat(currMonth).concat(currDay).concat(uuidRandom); } }