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. 从PMP培训归来,跟大家聊聊做项目的套路

    管理也是一些套路的传承,很多人说不去学专门的管理,照样把工作做得很好.是的,不是散打乱打就不能赢,只是会吃点亏而已.如果你有了套路在心中,那么必定会让自己车到山前开路,让事情更好办. 所以,我去学了几 ...

  2. Python之父:为什么操作符很有用?

    这是我在python-ideas上发布的一些东西,但我认为这些很有趣,应该分享给更多的人. 最近有很多关于合并两个dict(词典)的运算符的讨论. 这促使我思考为什么有些人喜欢运算符,我想起了30多年 ...

  3. Python开发之---PyCharm初体验

    PyCharm 的初始设置(知道) 目标 恢复 PyCharm 的初始设置 第一次启动 PyCharm 新建一个 Python 项目 设置 PyCharm 的字体显示 PyCharm 的升级以及其他 ...

  4. .net core dump分析

    服务器上如果出现cpu内存饱满,找不到原因,那么dump文件分析必不可少. 起初是想在linux下调试.net core 的dump,但是环境一直无法安装 搞了许久没搞出来,其次文章太少了,googl ...

  5. Java核心技术及面试指南 2.3.6 String相关的面试题答案

    2.3.6.1 String是最基本的数据类型吗?能不能被继承? String不能被继承,它是一个对象 2.3.6.2 String s = new String("xyz");创 ...

  6. SkyWalking-netcore

    详细安装步骤:https://www.jianshu.com/p/3ddd986c7581?from=groupmessage SkyWalking-netcore 官网:https://github ...

  7. MFC控件GDI编程

    MFC控件GDI编程 一丶学习内容 1.了解常用的GDI函数绘图. 2.使用常用的画笔画刷. 二丶常用的GDI函数绘图 上方则为我们常用的GDI函数了. 画线 矩形. 以及圆 等等. 2.1 画线代码 ...

  8. 网络协议抓包分析——IP互联网协议

    前言 IP协议是位于OSI模型的第三层协议,其主要目的就是使得网络间可以相互通信.在这一层上运行的协议不止IP协议,但是使用最为广泛的就是互联网协议. 什么是IP数据报 TCP/IP协议定义了一个在因 ...

  9. The 15th Zhejiang Provincial Collegiate Programming Contest(部分题解)

    ZOJ 4024 Peak 题意 给出n和n个数,判断该数列是否是凸形的. 解题思路 从前往后第一对逆序数,和从后往前第一队逆序数,如果都非零而且相邻,证明该数组是凸形的. 代码 #include & ...

  10. Spring Cloud Stream如何消费自己生产的消息?

    在上一篇<Spring Cloud Stream如何处理消息重复消费>中,我们通过消费组的配置解决了多实例部署情况下消息重复消费这一入门时的常见问题.本文将继续说说在另外一个被经常问到的问 ...