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. oracle select in超过1000条报错解决方法

    本博客介绍oracle select in超过1000条数据的解决方法,java框架是采用mybatis的,这可以说是一种比较常见的错误:select * from A where id in(... ...

  2. jQuery实现遮罩层

    1.1 背景半透明遮罩层样式 需要一个黑色(当然也可以其他)背景,且须设置为绝对定位,以下是项目中用到的css样式: /* 半透明的遮罩层 */ #overlay { background: #000 ...

  3. Python find函数用法和概念

    概念: Python find() 方法检测字符串中是否包含子字符串 str ,如果指定 beg(开始) 和 end(结束) 范围,则检查是否包含在指定范围内,如果包含子字符串返回开始的索引值,否则返 ...

  4. th:标签

    https://blog.csdn.net/xxb5502296/article/details/78319898(挺全的) https://blog.csdn.net/qq_43279637/art ...

  5. 设置python环境变量

    原始环境变量 /> python Python |Anaconda (-bit)| (default, May , ::) [GCC (Red Hat -)] on linux Type &qu ...

  6. Oracle客户端连接数据库配置

    配置文件和路径 配置文件:tnsnames.ora 默认路径:%ORACLE_HOME%\network\admin\tnsnames.ora,%ORACLE_HOME%通常在环境变量中使用. 我的路 ...

  7. linux sar 命令详解(历史资源查看,如内存、CUP等等)

    sar(System Activity Reporter系统活动情况报告)是目前 Linux 上最为全面的系统性能分析工具之一,可以从多方面对系统的活动进行报告,包括:文件的读写情况.系统调用的使用情 ...

  8. python的pyspider框架下爬虫

    1.将框架下载好之后,控制台运行pyspider 2.浏览器打开http://localhost:5000 3.创建项目 页面区域介绍: 整个页面分为两栏,左边是爬取页面预览区域,右边是代码编写区域. ...

  9. 网络编程第三讲UDP编写

    网络编程第三讲UDP编写 一丶UDP简介 UDP是面向无连接的.就是说数据传输会丢掉.网络延时比较大的情况下.会早上丢包.例如视频通话.就是UDP UDP不需要建立建立. 下面有UDP编写流程图 下图 ...

  10. SLAM入门之视觉里程计(3):两视图对极约束 基础矩阵

    在上篇相机模型中介绍了图像的成像过程,场景中的三维点通过"小孔"映射到二维的图像平面,可以使用下面公式描述: \[ x = MX \]其中,\(c\)是图像中的像点,\(M\)是一 ...