https://github.com/TheLandscapeBe/tinyurl.git
项目地址
短连接生成就是把普通网址转换成比较换的地址。
例如:一个很长的链接地址:https://developer.aliyun.com/article/829833?spm=a2c6h.13148508.0.0.67134f0ewKO0ri需要发发布给用户,而用户不能接收这么长的内容,那么就需要将这个很长的链接地址转换为用户能够接收的长度,这就是短连接生成,其实就是一个转换。
#算法对比
1 ID自增算法
目前ID自增算法基于MySQL ID自增算法,不利于扩展,适用于小型应用
2 snowflake算法
snowflake算法为twitter设计,特点ID有序且为数字,适合中大型应用
1 选取字母表ALPHABET=[0-9a-zA-Z],字母表长度ALPHABET_LENGTH=62,所有使用62进制,
private static String encode(long number) {
StringBuilder chip = new StringBuilder(9);
while (number > 0) {
int mod = (int)(number % ALPHABET_LENGTH);
chip.append(ALPHABET.charAt(mod));
number -= mod;
number /= ALPHABET_LENGTH;
}
return chip.reverse().toString();
}
private static long decode(String key) {
long number = 0L;
for (int i = 0; i < key.length(); i++) {
long pow = pow(key, i);
number += pow * ALPHABET.indexOf(key.charAt(i));
}
return number;
}
private static long pow(String key, int i) {
long pow = 1L;
for (int j = 0; j < key.length() - i - 1; j++) {
pow *= ALPHABET_LENGTH;
}
return pow;
}
数据库初始化脚本
CREATE DATABASE `tiny_urldb` CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci;
CREATE TABLE `application` (
`id` int NOT NULL AUTO_INCREMENT,
`access_key` varchar(256) NOT NULL DEFAULT '' COMMENT '密钥',
`name` varchar(256) NOT NULL DEFAULT '' COMMENT '应用名称',
`create_time` datetime NOT NULL COMMENT '创建时间',
`app_id` varchar(256) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL DEFAULT '' COMMENT '应用ID',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='应用';
CREATE TABLE `domain` (
`id` int NOT NULL AUTO_INCREMENT,
`domain` varchar(256) NOT NULL DEFAULT '' COMMENT '域名',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='域名';
CREATE TABLE `url` (
`id` bigint NOT NULL AUTO_INCREMENT,
`origin_url` varchar(5000) NOT NULL DEFAULT '' COMMENT '原始URL',
`hash` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL DEFAULT '' COMMENT '原始URL MD5哈希值',
`create_time` datetime NOT NULL,
`expire_time` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='URL记录';
增加测试应用
INSERT INTO `application` VALUES ('1', '1594708959736', '测试应用', '2020-07-14 14:43:12', '1594708959736');
增加测试域名
INSERT INTO `domain` VALUES ('1', 'e.example.com');
服务数据可以使用jar部署,docker镜像部署
git clone https://github.com/fofcn/tinyurl.git
1.2 代码打包为jar
cd tinyurl
mvn clean package -Dmaven.test.skip=true
cd target
java -jar -Xms128M -Xmx256M tinyurl.jar
git clone https://github.com/fofcn/tinyurl.git
2.2 代码打包为镜像
docker login
docker build -t tinyurl .
docker tag tinyurl ${registry}/name/image-name:${tag}
docker push ${registry}/name/image-name:${tag}
#例如在我的环境中执行命令
docker login
docker build -t tinyurl .
docker tag tinyurl fofcn/tinyurl:v0.2.0
docker push fofcn/tinyurl
2.3 使用docker镜像部署 (目前docker镜像已经上传到doker hub,直接使用fofcn/tinyurl获取镜像即可)
2.3.1 编写docker-compose.yml
# MAINTAINER: [email protected]
version: '3'
networks:
your-network-name:
driver: bridge
services:
tinyurl:
container_name: tinyurl
image: fofcn/tinyurl:v1.3.0
restart: always
ports:
- "53000:53000"
volumes:
- /etc/localtime:/etc/localtime:ro
- /etc/timezone:/etc/timezone:ro
- /app/applog/tinyurl:/app/applog/tinyurl
networks:
your-network-name:
environment:
- SERVER_PORT=53000
# 目前支持dev:开发人员使用,test:测试人员使用,prod:生产环境使用
- SPRING_PROFILES_ACTIVE=dev
- SPRING_SHARDINGSPHERE_DATASOURCE_MASTER_URL=jdbc:mysql://localhost:3306/tiny_urldb?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8
- SPRING_SHARDINGSPHERE_DATASOURCE_MASTER_USERNAME=tinyurl_user
- SPRING_SHARDINGSPHERE_DATASOURCE_MASTER_PASSWORD=Yy123456.
- SPRING_SHARDINGSPHERE_DATASOURCE_SLAVE0_URL=jdbc:mysql://localhost:3306/tiny_urldb?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8
- SPRING_SHARDINGSPHERE_DATASOURCE_SLAVE0_USERNAME=tinyurl_user
- SPRING_SHARDINGSPHERE_DATASOURCE_SLAVE0_PASSWORD=Yy123456.
# v1.3.0新增配置
# dataCenterId与WorkerId在多节点部署时必须指定且组合唯一
- TINYURL_SNOWFLAKE_DATACENTERID=0
- TINYURL_SNOWFLAKE_WORKERID=0
# 配置域名http或https
- TINYURL_HTTPSCHEME=https
2.3.2 docker-compose启动
docker-compose up -d
生成短链接
打开短链接