JAVA和Nginx 教程大全

网站首页 > 精选教程 正文

springboot2.2.X手册:构建全局唯一的短链接数据中心

wys521 2024-11-05 12:50:37 精选教程 24 ℃ 0 评论

溪云阁:专注编程教学,架构,JAVA,Python,微服务,机器学习等领域,欢迎关注,一起学习。

什么是短链接

短链接,其实这个东西很常见,最早开始的时候小编是在微博上看见的,然后就很好奇这是个什么东西,怎么点进去后网址又辣么长?

其实这个东西就必须按照字面来理解,就是短的链接,通过短的链接去数据库或者缓存中找到长的链接,映射出来即可。

今天我们用redis来实现短链接服务,想换存储介质的同学自行更换。


有哪些场景

1、微博,短信等那些有限制字数的文案需求

2、用户角度上来考虑,短链接更适合现在的快餐文化

加载包体

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.boots</groupId>
        <artifactId>boots</artifactId>
        <version>1.1.0.RELEASE</version>
    </parent>
    <artifactId>boots-shorturl</artifactId>
    <name>boots-shorturl</name>
    <url>http://maven.apache.org</url>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>

        <!-- 公共组件:swagger服务+入参出参+统一异常拦截 -->
        <dependency>
            <groupId>com.boots</groupId>
            <artifactId>module-boots-api</artifactId>
            <version>2.0.0.RELEASE</version>
        </dependency>

        <!-- 公共组件:redis缓存包 -->
        <dependency>
            <groupId>com.boots</groupId>
            <artifactId>module-boots-redis</artifactId>
            <version>1.1.0.RELEASE</version>
        </dependency>

    </dependencies>
</project>

编写短链接工具类

/**
 * All rights Reserved, Designed By 林溪
 * Copyright:    Copyright(C) 2016-2020
 * Company       溪云阁 .
 */

package com.boots.shorturl.common.utils;

import java.util.Random;
import java.util.UUID;

/**
 * 短链接工具类
 * @author:溪云阁
 * @date:2020年6月11日
 */
public class ShortUrlUtils {

    /**
     * 获取短链接
     * @author 溪云阁
     * @param host 请求的URL解析出来的域名信息
     * @param length 自定义你需要的短链接长度
     * @return String 返回的短链接
     */
    public static String getShortUr(int length) {
        // 定义偏移量,每6个生成一个字符
        final int PER_VARCHAR = 6;
        // 定义数字+大小写字母的字符数组
        final char[] c = new char[] { '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',
                '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' };
        // 2的35次方,每五位一个字符,可生成7个字符
        final long long16 = (long) Math.pow(2, PER_VARCHAR * length) - 1;
        // 生成的随机数里面存在-的话直接用空空字符串替代
        final String a = UUID.randomUUID().toString().replace("-", "");
        // 生成随机数,使之成为35长度
        final Random random = new Random();
        // 每8字符=32位,加3位=111
        final int nextInt = random.nextInt(8);
        // 偏移起始位置
        int subIndexStart = 0;
        // 定义需要返回的短链接后缀
        final StringBuffer sb = new StringBuffer();
        while (subIndexStart < a.length()) {
            // 8位一组,使用16进行转换,可转换成 4*8=32长度二进制
            final String substring = a.substring(subIndexStart, subIndexStart += 8);
            final long parseLong = Long.parseLong(nextInt + substring, 16);
            long x = long16 & parseLong;
            for (int j = 0; j < length; j++) {
                final long x2 = c.length - 1 & x;
                sb.append(c[(int) x2]);
                x = x >> PER_VARCHAR;
            }
            return sb.toString();
        }
        return null;
    }

}

编写URL工具类

/**
 * All rights Reserved, Designed By 林溪
 * Copyright:    Copyright(C) 2016-2020
 * Company       溪云阁 .
 */

package com.boots.shorturl.common.utils;

import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.module.boots.exception.CommonRuntimeException;

/**
 * 校验工具类
 * @author:溪云阁
 * @date:2020年6月11日
 */
public class UrlsUtils {

    /**
     * 验证是否是URL
     * @param url
     * @return
     */
    public static boolean verifyUrl(String url) {
        // URL验证规则
        final String regEx = "[a-zA-z]+://[^\\s]*";
        // 编译正则表达式
        final Pattern pattern = Pattern.compile(regEx);
        // 忽略大小写的写法
        // Pattern pat = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
        final Matcher matcher = pattern.matcher(url);
        // 字符串是否与正则表达式相匹配
        final boolean rs = matcher.matches();
        return rs;
    }

    /**
     * 获取域名
     * @author 溪云阁
     * @param address 网址
     * @return String
     * @throws URISyntaxException
     */
    public static String getHost(String address) {
        String host = "";
        try {
            final URL url = new URL(address);
            final URI uri = url.toURI();
            host = uri.getScheme() + "://" + url.getHost();
        }
        catch (final Exception e) {
            throw new CommonRuntimeException(e.fillInStackTrace());
        }
        return host;
    }

}

编写服务接口

/**
 * All rights Reserved, Designed By 林溪
 * Copyright:    Copyright(C) 2016-2020
 * Company       溪云阁 .
 */

package com.boots.shorturl.view.shorturl.view;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.boots.shorturl.common.utils.ShortUrlUtils;
import com.boots.shorturl.common.utils.UrlsUtils;
import com.module.boots.api.message.ResponseMsg;
import com.module.boots.api.utils.MsgUtils;
import com.module.boots.exception.CommonRuntimeException;
import com.module.boots.redis.RedisUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;

/**
 * 短链接服务
 * @author:溪云阁
 * @date:2020年6月11日
 */
@Api(tags = { "web服务:短链接服务接口" })
@RestController
@RequestMapping("view/shortUrl")
public class ShortUrlView {

    @Autowired
    private RedisUtils redisUtils;

    /**
     * 生成短链接
     * @author 溪云阁
     * @param url 长连接地址
     * @return
     * @throws Exception ResponseMsg<String>
     */
    @ApiOperation(value = "生成短链接")
    @PostMapping(value = "/generateShortUrl")
    @SneakyThrows(CommonRuntimeException.class)
    public ResponseMsg<String> generateShortUrl(@RequestParam("url") String url) {
        // 判断是否为网址
        if (UrlsUtils.verifyUrl(url)) {
            // 生成6位数的随机短链接后缀
            final String suffix = ShortUrlUtils.getShortUr(6);
            // 生成完整的短链接
            final String shortUrl = UrlsUtils.getHost(url) + "/" + suffix;
            // 把短链接设置到Redis,这里为了方便,直接把短链接作为KEY,也可以把后缀作为key
            redisUtils.set(shortUrl, url);
            // 返回短链接地址
            return MsgUtils.buildSuccessMsg(shortUrl);

        } else {
            return MsgUtils.buildFailureMsg("网址不合法");
        }
    }

    /**
     * 获取长连接
     * @author 溪云阁
     * @param url
     * @return ResponseMsg<Object>
     */
    @ApiOperation(value = "获取长链接")
    @PostMapping(value = "/getLongUrl")
    @SneakyThrows(CommonRuntimeException.class)
    public ResponseMsg<Object> getLongUrl(@RequestParam("url") String url) {
        // 判断是否为网址
        if (UrlsUtils.verifyUrl(url)) {
            // 返回长链接地址
            return MsgUtils.buildSuccessMsg(redisUtils.get(url));

        } else {
            return MsgUtils.buildFailureMsg("网址不合法");
        }
    }

}

配置redis地址

# redis地址
spring.redis.host: 127.0.0.1
# redis端口号
spring.redis.port: 6379
# redis密码,如果没有不用填写,建议还是得有
spring.redis.password: 123456
# 最大活跃连接数,默认是8
spring.redis.lettuce.pool.maxActive: 100
# 最大空闲连接数 ,默认是8
spring.redis.lettuce.pool.maxIdle: 100
# 最小空闲连接数 ,默认是0
spring.redis.lettuce.pool.minIdle: 0

测试

调用生成短链接接口


获取长链接接口

查看redis中数据


从测试结果上看,短链接按照我们的需求已经生成,并且存到redis中去,一个简单的短链接服务完成。

后续的拓展各位同学可以根据需要进行拓展,这里不做标准。

拓展

1、目前采用6位数的短链接,可以产生56800235584个短链接,不够的话,可以采用7位的,62的7次方个

2、目前采用redis存储,用set存储的话,最多只能存储40多亿个,可以根据不同需求加入不同过期时间

3、目前按照6位数的生成方案,按照一个数据或者字符为1个字节来算,一个短链接就有6个字节,短链接单量的话接近2T数据,再加上长链接的数据,在增长到一定程度后,需要引入大数据存储,一般情况下,超过500万的数据就要开始引入。


--END--

作者:@溪云阁

原创作品,抄袭必究

如需要源码,转发,关注后私信我

部分图片或代码来源网络,如侵权请联系删除,谢谢!


历史文章:

springboot2.2.X手册:放弃fastdfs,整合Minio做文件服务器真香

springboot2.2.X手册:分布式系统下,重复提交的解决方案

springboot2.2.X手册:Easypoi导出excel,最新版的手感香不香?

springboot2.2.X手册:项目从100M瘦身到100K,部署省事多了!

springboot2.2.X手册:redis的7种类型100个方法全解析

springboot2.2.X手册:是时候用Lettuce替换Jedis操作Redis缓存了

springboot2.2.X手册:构建多元化的API接口,我们这样子设计

springboot2.2.X手册:基于Jasypt的JavaConfig方式敏感信息加密

springboot2.2.X手册:整合最新版MybatisPlus 3.3.1版本

springboot2.2.X手册:对象复制哪种最快?7种复制方式性能对比

springboot2.2.X手册:基于OSS解决文件存储(一年9元^^,赚了)

springboot2.2.X手册:36个注解详细解析,一目了然

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表