A simple short URL service system can generate new short url through RESTful API. The mapping between short URL and original URL is stored in Redis database. When users request short URL, they will be redirected to the original url.

Vert.x-web and asynchronous programming are used in the background, and communication between Web services and Redis services is via EventBus. Short URL generation uses the original url to base 62 mapping scheme.

Short URL service principle may refer to a short URL (short URL) the principle of system and its implementation | think no

Web Routing description

  • GET / : shortUrlKey | through short url, redirect to the original site

  • GET/management | management center, to see all the short url mapping, and create a new short url

  • POST/API/create {‘ srcUrl ‘:’ the source url} | return submitted to the original short url

{"shortUrl": "xxx.xx/5Fdx6l"."date": "2020-4-28" "."state": "success|fail"}
Copy the code
  • GET/API/list | returns have created all the short url
[{"shortUrl": "short url"."srcUrl": "source url"."date": "create date"}]Copy the code

Class/file brief description

  • Shorturl.com mon. Convertor is a utility class, is mainly used to turn the decimal integer 62 into the system, and 62 turn a string into the system.

  • The shorturl. Verticle package is filled with individual Verticle. RedisVerticle is used to create and manage the Redis database, as well as the corresponding Redis read and write requests through the event mechanism (asynchronous); RestVerticle is used to create Web services and provide RESTful API management interfaces and route redirection for short domain names. It asynchronously requests read and write Redis.

  • Short. Server deploys and runs the vert. X service.

  • Webroot \management.html is a front-end management interface built on Vue and Bulma for viewing created shorturl and creating new shorturl.

  • Auto-py is a Python automation program that automatically completes maven packaging -> JAR deployment server -> restart JAR applications.

Other instructions

  • Why not use ConcurrentHashMap or MySQL for data stores?

Because I want the short domain name created before to be used again after the application restarts, I must use the external database and discard ConcurrentHashMap. Because the Web layer already uses vert. X, an asynchronous, high-concurrency framework (suite of tools), and a low-performance relational database is definitely not a match, the NOSQL database Redis is used, and the data store content is also simple.

  • Is there anything that needs to be improved?

Of course there are a lot. The first easy improvement is to make the logging system print more detailed and specific content. A simple idea is to check if the same key already exists in Redis. If so, make a small change to the short domain name (such as +1). Then go to Redis to check whether there is the same key, until there is no same key. (Welcome your advice)

run

First make sure the Redis database is started with JDK Version 11 or above

  1. The IDE runs short.Server’s main method directly;

  2. Or generate a jar package, run java-jar short-url-**.jar;

  3. Visit http://localhost:8081/management to enter interface management center;

  4. Access path Host /:short-url-key Tests the short domain name service

The source code

  • github : some-project/simple-shorturl-service/

  • gitee : shorturl