1 Star 3 Fork 1

liuxingyu01 / tiny-url

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

tiny-url

在线短链接生成平台

SpringBoot star fork

已实现功能

1、将长链接转换成短链接,访问短链接时,302重定向至原始长链接

2、支持设置短链的有效期限,生成短链访问二维码

3、支持记录访问次数,访问日志

4、使用布隆过滤器优化短链冲突问题,优化高并发下的性能表现

5、支持多租户管理功能,租户注册后可拥有独立的后台数据管理功能(包括仪表盘、短链管理、访问日志管理、数据统计等)

6、支持租户通过API调用方式生成短链接、禁用/启用短链接、更新短链有效期限、查看短链接信息等功能;提供成熟的API接口文档和鉴权方案(access_key + access_key_secret)

计划实现功能

1、超级管理员系统管理功能(用于管理租户)

2、带访问密码的短链,访问时重定向到输入密码的页面,输入密码后方可访问

3、带最大可用次数的短链,如设置访问限制100次,超出后则禁止访问

部分界面展示

首页 首页

api接口文档页 api接口文档页

关于我们页 关于我们页

短链寻址失败时-404页 404页

短链寻址失败时-失效页 失效页

租户登录页 租户登录

租户注册页 租户注册

租户后台数据管理 租户首页 仪表盘 短链列表 数据统计 访问日志

主要技术选型

依赖 说明
SpringBoot 3.1.9 基础框架
Hikari 高性能数据库连接池
MyBatis-Plus MyBatis的增强ORM
Redis 业务缓存、会话共享
Guava Hash算法、布隆过滤器
Layui 前端模板

运行环境

  • Jdk17
  • MySQL5.6+
  • Redis3.0+

启动部署教程

  1. 新建MySQL数据库并导入resources/init文件夹下的数据库脚本
  2. 修改配置文件中application.yml数据库Redis连接信息
  3. 运行启动类TinyUrlApplication,即可正常启动项目
  4. 平台首页地址:http://localhost:9999/
  5. 租户登录地址:http://localhost:9999/page/tenant/login.html 账户密码 zhangsan / 123456

实现逻辑

1、使用 MurmurHash 算法将原始长链接 hash 为 32 位散列值,将散列值转为 BASE62 编码 ,即为短链接。

2、用户访问短链接时,在 Redis 中查找是否存在缓存,存在则刷新缓存时间,302 重定向至原始长链接,并自增短链接访问量,记录访问日志; 缓存中不存在,则去数据库查找,查找成功则添加到 Redis 缓存,302 重定向至原始长链接,并自增短链接访问量,记录访问日志; 数据库中若也不存在,则跳转到404页面;

3、具体实现参考文章:掘金-高性能短链设计

部分技术介绍

1、MurmurHash:MurmurHash 是一种非加密型哈希函数,适用于一般的哈希检索操作。 与其它流行的MD5等哈希函数相比,对于规律性较强的 key,MurmurHash 的随机分布特征表现更良好。 非加密意味着着相比 MD5,SHA 这些函数它的性能肯定更高(实际上性能是 MD5 等加密算法的十倍以上)。 MurmurHash 有 32 bit、64 bit、128 bit 的实现,32 bit 已经足够表示近 43 亿个短链接。 使用 Java 的话,在 Google 的 guavahutool 中有相应实现,这里选择 guava 的实现。

2、base62:MurmurHash 生成的哈希值最长有 10 位十进制数,为了进一步缩短短链接长度,可以将哈希值转为 base62 编码,这样最长就只有 6 个字符了。

3、Redis:生成短链接后,通常在后续一段时间内此短链接的访问频率较高,则向 Redis 中添加带过期时间的缓存来减轻数据库压力。

4、301 和 302 重定向的区别

  • 301,代表 永久重定向,也就是说第一次请求拿到长链接后,下次浏览器再去请求短链的话,不会向短网址服务器请求了,而是直接从浏览器的缓存里拿,这样在 server 层面就无法获取到短网址的点击数了,如果这个链接刚好是某个活动的链接,也就无法分析此活动的效果。所以我们一般不采用 301。
  • 302,代表 临时重定向,也就是说每次去请求短链都会去请求短网址服务器(除非响应中用 Cache-Control 或 Expired 暗示浏览器缓存),这样就便于 server 统计点击数,所以虽然用 302 会给 server 增加一点压力,但在数据异常重要的今天,这点代码是值得的,所以推荐使用 302!

5、多租户方案介绍: 多租户技术,或称多重租赁技术,是一种软件架构技术。它探讨的是如何于多用户的环境下共用相同的系统或者程序组件,并且确保各个用户数据的隔离性。在云计算迅速发展的今天,多租户技术被广为运用于开发云各式服务,不论是 IaaS、PaaS 还是 SaaS,都可以看到多租户技术的影子,SaaS 应用与其它应用最大的差异特征就是多租户。

多租户软件架构的设计目标:

  • 保证多个租户使用统一的SaaS云平台,同时对多个租户进行良好的隔离;
  • 能够保证SaaS云平台进行统一的升级,又不破坏各自的应用逻辑;
  • 支持业务数据的横向扩展,并能够支持某个租户单独扩展资源的能力;

在多租户架构中,根据数据隔离情况可以分为以下三种情况:

  • 数据库分离
  • 共享数据库,schema分离
  • 共享数据库,共享schema

多租户架构类型对比:

多租户架构类型 优点 缺点
数据库分离 为不同的租户提供独立的数据库,有助于简化数据模型的扩展设计,满足不同租户的独特需求;如果出现故障,恢复数据比较简单 增多了数据库的安装数量,随之带来维护成本和购置成本的增加
共享数据库,Schema独立 为安全性要求较高的租户提供了一定程度的逻辑数据隔离,并不是完全隔离;每个数据库可支持更多的租户数量 如果出现故障,数据恢复比较困难,因为恢复数据库将牵涉到其他租户的数据;如果需要跨租户统计数据,存在一定困难
共享数据库,共享schema 维护和购置成本最低,允许每个数据库支持的租户数量最多 隔离级别最低,安全性最低,需要在设计开发时加大对安全的开发量; 数据备份和恢复最困难,需要逐表逐条备份和还原

本项目属于简单实现,故选用第三种方案

Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

简介

📚tiny-turl 是一个基于 SpringBoot3 +Java17 开发的在线短链生成系统,支持Sass多租户模式管理,支持rest api远程调用生成短链,以及短链访问日志,访问量统计等数据分析功能。 展开 收起
JavaScript 等 4 种语言
Apache-2.0
取消

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/leisureLXY/tiny-url.git
git@gitee.com:leisureLXY/tiny-url.git
leisureLXY
tiny-url
tiny-url
master

搜索帮助

53164aa7 5694891 3bd8fe86 5694891