java 短链接服务_利用SpringBoot&Redis实现短链接服务

springboot技术内幕架构设计与实现

53.1元

包邮

(需用券)

去购买 >

940fafc1c5689762200b13267e0e004b.png

1460000020253412

感谢您的阅读,本文由 杨斌的博客 版权所有。

如若转载,请注明出处:杨斌的博客(https://y0ngb1n.github.io/a/b...)

项目已托管于 GitHub:y0ngb1n/spring-boot-samples,欢迎 Star, Fork

准备工作

Spring Boot 2.1.0+

Redis

Lombok

Guava 28.0

Common Validator 1.6

添加依赖项

pom.xml

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-data-redis

commons-validator

commons-validator

com.google.guava

guava

application.yml

spring:

# Redis Config

redis:

url: 127.0.0.1

port: 6379

password: your_password

logging:

level:

io.github.y0ngb1n.*: debug

核心代码

/**

* URL Shortener Resource

*

* @author yangbin

*/

@Slf4j

@RestController

@RequestMapping(path = "/v1")

public class UrlShortenerController {

@Autowired

StringRedisTemplate redisTemplate;

@GetMapping(path = "/{id}")

public String getUrl(@PathVariable String id) {

+ String url = redisTemplate.opsForValue().get(id);

log.debug("URL Retrieved: {}", url);

return url;

}

@PostMapping

public String create(@RequestBody String url) {

UrlValidator urlValidator = new UrlValidator(

new String[]{"http", "https"}

);

if (urlValidator.isValid(url)) {

- String id = Hashing.murmur3_32().hashString(url, StandardCharsets.UTF_8).toString();

log.debug("URL Id generated: {}", id);

+ redisTemplate.opsForValue().set(id, url);

return id;

}

throw new RuntimeException("URL Invalid: " + url);

}

}

使用方式

Step 0: 安装并启动 Redis

# on Windows

scoop install redis

redis-server

# on Mac

brew install redis

redis-server

Step 1: 启动 url-shortener 服务

$ mvn install

...

[INFO] BUILD SUCCESS

...

$ mvn spring-boot:run

...

2019-08-21 21:03:50.215 INFO 10244 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''

2019-08-21 21:03:50.219 INFO 10244 --- [ main] i.g.y.s.u.UrlShortenerApplication : Started UrlShortenerApplication in 6.01 seconds (JVM running for 12.165)

Step 2: 生成短链

$ curl -X POST http://127.0.0.1:8080/v1 \

-H 'Content-Type: text/plain' \

-d https://y0ngb1n.github.io

515bbe2b

Step 3: 还原短链

$ curl -X GET http://127.0.0.1:8080/v1/515bbe2b

https://y0ngb1n.github.io

查看日志

...

2019-08-21 21:42:26.788 DEBUG 10244 --- [nio-8080-exec-2] i.g.y.s.u.c.UrlShortenerController : URL Id generated: 515bbe2b

2019-08-21 21:42:40.748 DEBUG 10244 --- [nio-8080-exec-3] i.g.y.s.u.c.UrlShortenerController : URL Retrieved: https://y0ngb1n.github.io

参考资料

https://youtu.be/Zr0E2VP24w8

https://en.wikipedia.org/wiki...

https://github.com/google/gua...

https://www.flyml.net/2016/09...

短网址(Short URL)系统的原理及其实现,by 胡方运

如何快速判断某 URL 是否在一个 20 亿的网址 URL 集合中?,by 张振伟

应用场景:黑名单、URL 去重、单词拼写检查、Key-Value 缓存系统的 Key 校验、ID 校验,比如订单系统查询某个订单 ID 是否存在,如果不存在就直接返回

谈谈全局唯一 ID 生成方法

Leaf — 美团点评分布式 ID 生成系统

原文链接:https://segmentfault.com/a/1190000020253409

java 11官方入门(第8版)教材

79.84元

包邮

(需用券)

去购买 >

f0f3f55624fb396b1764d42d6df88864.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值