一、需求分析
B站視頻、淘寶、抖音等短鏈接。
好處:
- 簡單方便,利與推廣
- 長度短,便於http傳輸,有助於帶寬節約和高並發
- 防止尾巴參數泄密,不安全
二、架構設計
1、短鏈接映射算法如何編寫?
一個長鏈接URL轉換為4個短KEY:
- 將長網址 md5 生成32位簽名串,分為4段, 每段8個字節;
- 對這四段循環處理, 取8個字節, 將他看成16進制串與0x3fffffff(30位1)與操作, 即超過30位的忽略處理;
- 這30位分成6段, 每5位的數字作為字母表的索引取得特定字符, 依次進行獲得6位字符串;
- 總的md5串可以獲得4個6位串; 取里面的任意一個就可作為這個長url的短url地址;
- 當我們點擊這6個字母的鏈接后,我們又可以跳轉到原始的真實鏈接地址。
package com.redis.demo; import org.apache.commons.codec.digest.DigestUtils; /** * 新浪短連接算法 * Created by shiwn on 2022/1/29 13:16 */ public class ShortUrlUtils { // 26小寫字母 + 26大寫字母 + 10個數字 = 62 public static final String[] chars = new String[]{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}; /** * 短連接算法 */ public static String[] shortUrl(String url) { // 對傳入網址進行 MD5 加密 String sMD5EncryptResult = DigestUtils.md5Hex(url); System.out.println("---------------sMD5EncryptResult: " + sMD5EncryptResult); System.out.println(); // md5處理后是32位 String hex = sMD5EncryptResult; // 切割為4組,每組8個字符, 32 = 4 * 8 String[] resUrl = new String[4]; for (int i = 0; i < 4; i++) { // 取出8位字符串,md5 32位,按照8位一組字符,被切割為4組 String sTempSubString = hex.substring(i * 8, i * 8 + 8); System.out.println("---------------sTempSubString: " + sTempSubString); //System.out.println("-sTempSubString作為16進制的表示"+Long.parseLong(sTempSubString, 16)); // 把加密字符按照8位一組16進制與 0x3FFFFFFF 進行位與運算 // 這里需要使用 long 型來轉換,因為 Inteper .parseInt() 只能處理 31 位 , 首位為符號位 , 如果不用 long ,則會越界 long lHexLong = 0x3FFFFFFF & Long.parseLong(sTempSubString, 16); System.out.println("---------lHexLong: " + lHexLong); String outChars = ""; for (int j = 0; j < 6; j++) { // 0x0000003D它的10進制是61,61代表最上面定義的chars數組長度62的0到61的坐標。 // 0x0000003D & lHexLong進行位與運算,就是格式化為6位,即保證了index絕對是61以內的值 long index = 0x0000003D & lHexLong; System.out.println("----------index: " + index); // 按照下標index把從chars數組取得的字符逐個相加 outChars += chars[(int) index]; // 每次循環按位移5位,因為30位的二進制,分6次循環,即每次右移5位 lHexLong = lHexLong >> 5; } // 把字符串存入對應索引的輸出數組,會產生一組6位字符串 resUrl[i] = outChars; } return resUrl; } }
2 通過映射算法后,跳轉到真實地址,如何編寫?
請求重定向
3、短:長 url的映射,你覺得用redis里面的那個結構?
hash
三、編碼實現
package com.redis.demo; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @RestController public class ShortUrlController { private final static String SHORT_URL_KEY = "short:url"; @Resource private HttpServletResponse response; @Resource private RedisTemplate redisTemplate; /** * 長鏈接轉換為短鏈接 * 實現原理:長鏈接轉換為短加密串key,然后存儲在redis的hash結構中。 * http://localhost:5555/encode?longUrl=https://www.baidu.com/ */ @GetMapping(value = "/encode") public String encode(String longUrl) { // 一個長鏈接url轉換為4個短加密串key String[] keys = ShortUrlUtils.shortUrl(longUrl); // 任意取出其中一個,我們就拿第一個 String shortUrlKey = keys[0]; // 用hash存儲,key=加密串,value=原始url this.redisTemplate.opsForHash().put(SHORT_URL_KEY, shortUrlKey, longUrl); System.out.println("長鏈接: " + longUrl + "\t" + "轉換短鏈接: " + shortUrlKey); return "http://127.0.0.1:5555/" + shortUrlKey; } /** * 重定向到原始的URL啟動AB定時器計划任務淘寶聚划算功能模擬 * 實現原理:通過短加密串KEY到redis找出原始URL,然后重定向出去 * http://localhost:5555/decode/vUfUbu */ @GetMapping(value = "/decode/{shortUrlKey}") public void decode(@PathVariable String shortUrlKey) { //到redis中把原始url找出來 String url = (String) this.redisTemplate.opsForHash().get(SHORT_URL_KEY, shortUrlKey); System.out.println("----真實長地址url: " + url); try { //重定向到原始的url response.sendRedirect(url); } catch (IOException e) { e.printStackTrace(); } } }