Project address
https://github.com/TheLandscapeBe/tinyurl.git
Project address
Short connection generation service
What is short connection generation?
Short connection generation is to convert the ordinary web address into a relatively new address.
For example: a long link address: https://developer.aliyun.com/article/829833?spm=a2c6h.13148508.0.0.67134f0ewKO0ri If you need to send and publish to users, but users cannot receive such long content, you need to convert this long link address into the length that users can receive. This is short connection generation, which is actually a conversion.
Applicable scenario
- short message
The link in the short message cannot be too long, because it may be truncated into several short messages. Converting the original long link into a short connection can display all files and connection contents to users in one short message. - QR code
Link to generate QR code. If the connection is too long, the scanning speed and success rate will be reduced. Converting long links into short links and generating QR codes will greatly improve the scanning speed and success rate.
Short link generation algorithm
- ID auto increment algorithm
The ID auto increment algorithm is currently implemented using the MySQL database auto increment ID, so the generated short connections will show the characteristics of sequential increase:
For example:
According to the request order, the short connection generation will be the following addresses:
https://xxx.xx.com/a
https://xxx.xx.com/b
https://xxx.xx.com/c
... - snowflake algorithm
After the ID is generated, the short link key is obtained by encoding in binary 62. Generally, a 9-bit string is obtained
#Algorithm comparison
1 ID auto increment algorithm
At present, the ID auto increment algorithm is based on MySQL ID auto increment algorithm, which is not conducive to expansion and is suitable for small applications
2. Snowflake algorithm
The snowflake algorithm is designed for twitter. Its ID is orderly and digital. It is suitable for medium and large-scale applications
Binary algorithm
Coding core computing
1 select alphabet ALPHABET=[0-9a-zA-Z], alphabet length ALPHABET_LENGTH=62, all use base 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(); }
Decoding core computing
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; }
How to use?
database initialized
Database initialization script
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 'secret key', `name` varchar(256) NOT NULL DEFAULT '' COMMENT 'apply name', `create_time` datetime NOT NULL COMMENT 'Creation time', `app_id` varchar(256) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL DEFAULT '' COMMENT 'application ID', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='application'; CREATE TABLE `domain` ( `id` int NOT NULL AUTO_INCREMENT, `domain` varchar(256) NOT NULL DEFAULT '' COMMENT 'domain name', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='domain name'; CREATE TABLE `url` ( `id` bigint NOT NULL AUTO_INCREMENT, `origin_url` varchar(5000) NOT NULL DEFAULT '' COMMENT 'original URL', `hash` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL DEFAULT '' COMMENT 'original URL MD5 Hash value', `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 record';
Add test application
INSERT INTO `application` VALUES ('1', '1594708959736', 'Test application', '2020-07-14 14:43:12', '1594708959736');
Add test domain name
INSERT INTO `domain` VALUES ('1', 'e.example.com');
Service deployment
Service data can be deployed using jar and docker image
- Deploy using jar package
1.1 code cloning
git clone https://github.com/fofcn/tinyurl.git
1.2 package the code into jar
cd tinyurl mvn clean package -Dmaven.test.skip=true cd target java -jar -Xms128M -Xmx256M tinyurl.jar
- Compiling docker image deployment
2.1 code download
git clone https://github.com/fofcn/tinyurl.git
2.2 code packaged as image
docker login docker build -t tinyurl . docker tag tinyurl ${registry}/name/image-name:${tag} docker push ${registry}/name/image-name:${tag} #For example, execute commands in my environment docker login docker build -t tinyurl . docker tag tinyurl fofcn/tinyurl:v0.2.0 docker push fofcn/tinyurl
2.3 deploy using the docker image (currently, the docker image has been uploaded to the doker hub, and you can directly use fofcn/tinyurl to obtain the image)
2.3.1 write docker compose yml
# MAINTAINER: errorfatal89@gmail.com 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 # Currently, dev: used by developers, test: used by testers, and prod: used by production environment - 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 new configuration # Datacenter ID and WorkerId must be specified and combined uniquely during multi node deployment - TINYURL_SNOWFLAKE_DATACENTERID=0 - TINYURL_SNOWFLAKE_WORKERID=0 # Configure domain name http or https - TINYURL_HTTPSCHEME=https
2.3.2 docker compose startup
docker-compose up -d
Interface list
-
Generate short links
-
Open short link