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); } }

springboot 开发 Tars的更多相关文章

  1. 基于SpringBoot开发一个Restful服务,实现增删改查功能

    前言 在去年的时候,在各种渠道中略微的了解了SpringBoot,在开发web项目的时候是如何的方便.快捷.但是当时并没有认真的去学习下,毕竟感觉自己在Struts和SpringMVC都用得不太熟练. ...

  2. SpringBoot开发案例之多任务并行+线程池处理

    前言 前几篇文章着重介绍了后端服务数据库和多线程并行处理优化,并示例了改造前后的伪代码逻辑.当然了,优化是无止境的,前人栽树后人乘凉.作为我们开发者来说,既然站在了巨人的肩膀上,就要写出更加优化的程序 ...

  3. SpringBoot开发案例从0到1构建分布式秒杀系统

    前言 ​最近,被推送了不少秒杀架构的文章,忙里偷闲自己也总结了一下互联网平台秒杀架构设计,当然也借鉴了不少同学的思路.俗话说,脱离案例讲架构都是耍流氓,最终使用SpringBoot模拟实现了部分秒杀场 ...

  4. 记一次SpringBoot 开发中所遇到的坑和解决方法

    记一次SpringBoot 开发中所遇到的坑和解决方法 mybatis返回Integer为0,自动转型出现空指针异常 当我们使用Integer去接受数据库中表的数据,如果返回的数据中为0,那么Inte ...

  5. SpringBoot系列四:SpringBoot开发(改变环境属性、读取资源文件、Bean 配置、模版渲染、profile 配置)

    声明:本文来源于MLDN培训视频的课堂笔记,写在这里只是为了方便查阅. 1.概念 SpringBoot 开发深入 2.具体内容 在之前已经基本上了解了整个 SpringBoot 运行机制,但是也需要清 ...

  6. SpringBoot开发(改变环境属性、读取资源文件、Bean 配置、模版渲染、profile 配置)

    1.概念 SpringBoot 开发深入 2.具体内容 在之前已经基本上了解了整个 SpringBoot 运行机制,但是也需要清楚的认识到以下的问题,在实际的项目开发之中,尤其是 Java 的 MVC ...

  7. 我的spring-boot开发环境

    我的spring-boot开发环境,目的方便我快速搭建开发环境,同时可以最佳实践.使用spring-boot 2.1.x. 代码地址:GitHub my-springboot-examples 目的是 ...

  8. springboot项目--传入参数校验-----SpringBoot开发详解(五)--Controller接收参数以及参数校验----https://blog.csdn.net/qq_31001665/article/details/71075743

    https://blog.csdn.net/qq_31001665/article/details/71075743 springboot项目--传入参数校验-----SpringBoot开发详解(五 ...

  9. SpringBoot开发mockserver及生成swagger接口文档

    通过springboot开发mock server,包含get及post接口,用于练习接口自动化及jmeter很方便 当然,也为后面jenkins持续集成做基础(开发push代码后  → jenkin ...

随机推荐

  1. C#遍历文件夹下面所有指定格式文件

    C#遍历指定文件夹中的所有文件 DirectoryInfo TheFolder=new DirectoryInfo(folderFullName);//遍历文件夹foreach(DirectoryIn ...

  2. pycharm注册码(不断更新)

    2019.3.13测试可用 MTW881U3Z5-eyJsaWNlbnNlSWQiOiJNVFc4ODFVM1o1IiwibGljZW5zZWVOYW1lIjoiTnNzIEltIiwiYXNzaWd ...

  3. 如何使用 GDB

    前期准备 启动GDB方法 设置运行参数 查看源码 断点break 使用 运行程序 查看运行时数据 查看内存数据 分割窗口 问题汇总 参考文献 GDB, The GNU Project debugger ...

  4. 使用QNetworkAccessManager实现Qt的FTP下载服务

    从Qt5开始,官方推荐使用QNetworkAccessManager进行Ftp和http的上传和下载操作:Qt4中使用的QtFtp模块即作为独立模块,需要自己从github上进行下载编译后使用(官方地 ...

  5. mybatis框架(3)---SqlMapConfig.xml解析

    SqlMapConfig.xml SqlMapConfig.xml是Mybatis的全局配置参数,关于他的具体用的有专门的MyBatis - API文档,这里面讲的非常清楚,所以我这里就挑几个讲下: ...

  6. C++版-剑指offer 面试题6:重建二叉树(Leetcode105. Construct Binary Tree from Preorder and Inorder Traversal) 解题报告

    剑指offer 重建二叉树 提交网址: http://www.nowcoder.com/practice/8a19cbe657394eeaac2f6ea9b0f6fcf6?tpId=13&tq ...

  7. Android--多线程之进程与线程

    前言 对于Android程序中,使用多线程的技术是必不可少的,就拿之前最简单的例子来说明,对于Android4.0+的应用而言,访问网络必须另起线程才可以访问.本片博客介绍Android下进程和线程, ...

  8. java设定一个日期时间,加几分钟(小时或者天)后得到新的日期

    //返回的是字符串型的时间,输入的 //是String day, int x public static String addDateMinut(String day, int x){ SimpleD ...

  9. 【ASP.NET MVC系列】浅谈ASP.NET 页面之间传值的几种方式

    ASP.NET MVC系列文章 [01]浅谈Google Chrome浏览器(理论篇) [02]浅谈Google Chrome浏览器(操作篇)(上) [03]浅谈Google Chrome浏览器(操作 ...

  10. mysql服务器架构

    mysql是最广泛使用的开源数据库之一,作为后端开发人员,或多或少都会和mysql打交道,本篇文章会从sql查询语句的执行过程,来介绍mysql的服务器架构, 查询的过程大致分为从客户端到服务器,在服 ...