Short link generation

Posted by jgh84 on Sat, 26 Feb 2022 19:42:35 +0100

Project address
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: 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

  1. 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.
  2. 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

  1. 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:
  2. 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);
            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` (
  `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` (
  `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';

  `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', '');

Service deployment

Service data can be deployed using jar and docker image

  1. Deploy using jar package
    1.1 code cloning
git clone

1.2 package the code into jar

cd tinyurl
mvn clean package -Dmaven.test.skip=true
cd target
java -jar -Xms128M -Xmx256M tinyurl.jar
  1. Compiling docker image deployment
    2.1 code download
git clone

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

version: '3'

    driver: bridge

    container_name: tinyurl
    image: fofcn/tinyurl:v1.3.0
    restart: always
      - "53000:53000"
      - /etc/localtime:/etc/localtime:ro
      - /etc/timezone:/etc/timezone:ro
      - /app/applog/tinyurl:/app/applog/tinyurl

      - SERVER_PORT=53000
      # Currently, dev: used by developers, test: used by testers, and prod: used by production environment 
      - SPRING_SHARDINGSPHERE_DATASOURCE_MASTER_URL=jdbc:mysql://localhost:3306/tiny_urldb?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8
      - SPRING_SHARDINGSPHERE_DATASOURCE_SLAVE0_URL=jdbc:mysql://localhost:3306/tiny_urldb?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8
      # v1.3.0 new configuration
      # Datacenter ID and WorkerId must be specified and combined uniquely during multi node deployment
      # Configure domain name http or https

2.3.2 docker compose startup

docker-compose up -d

Interface list

  1. Generate short links

  2. Open short link

Topics: data structure url