springboot 開發 Tars

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 服務),一次生成客戶端接口(客戶端是爲了方便別的服務來調用當前服務)java

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); } }
相關文章
相關標籤/搜索