Thank you for reading. This article is copyrighted by Yang Bin. If reproduced, please indicate the source: Yang bin’s blog (y0ngb1n. Making. IO/a/build – a – c…

The project has been hosted on GitHub: y0ngb1n/spring-boot-samples. Welcome to Star, Fork 😘


The preparatory work

  • Spring Boot 2.1.0 +
  • Redis
  • Lombok
  • Guava 28.0
  • Common Validator 1.6

Adding dependencies

pom.xml

<dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>

    <dependency>
      <groupId>commons-validator</groupId>
      <artifactId>commons-validator</artifactId>
    </dependency>

    <dependency>
      <groupId>com.google.guava</groupId>
      <artifactId>guava</artifactId>
    </dependency>
</dependencies>
Copy the code

application.yml

spring:
  # Redis Config
  redis:
    url: 127.0. 01.
    port: 6379
    password: your_password

logging:
  level:
    io.github.y0ngb1n.*: debug
Copy the code

The core code

/**
 * 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); }}Copy the code

use

Step 0: Install and start Redis

# on Windows
scoop install redis
redis-server

# on Mac
brew install redis
redis-server
Copy the code

Step 1: starturl-shortenerservice

$ mvn install. [INFO] BUILD SUCCESS ...$ mvn spring-boot:run. The 21:03:50 2019-08-21. 10244-215 the INFO [main] O.S.B.W.E mbedded. Tomcat. TomcatWebServer: tomcat started on the port (s) : 8080 (HTTP) with the context path '21:03:50 2019-08-21. 10244-219 the INFO [main] I.G.Y.S.U.U rlShortenerApplication: Started UrlShortenerApplication in 6.01 seconds (JVM Running for 12.165)Copy the code

Step 2: Generate short chains

$Curl -X POST http://127.0.0.1:8080/v1 \
  -H 'Content-Type: text/plain' \
  -d https://y0ngb1n.github.io
515bbe2b
Copy the code

Step 3: Restore the short chain

$The curl -x GET http://127.0.0.1:8080/v1/515bbe2b
https://y0ngb1n.github.io
Copy the code

See the log

. The 2019-08-21 21:42:26. 10244-788 the DEBUG [nio - 8080 - exec - 2] I.G.Y.S.U.C.U rlShortenerController: Id generated URL: 515 bbe2b 21:42:40 2019-08-21. 10244-748 the DEBUG [nio - 8080 - exec - 3] I.G.Y.S.U.C.U rlShortenerController: urls Retrieved: https://y0ngb1n.github.ioCopy the code

The resources

  • youtu.be/Zr0E2VP24w8
  • En.wikipedia.org/wiki/Murmur…
  • Github.com/google/guav…
  • www.flyml.net/2016/09/05/…
  • The Principle and Implementation of Short URL System, by Fangyun Hu
  • How to quickly determine whether a URL is in a 2 billion URL collection?, by Zhang Zhenwei
    • Application Scenarios:The blacklist,The URL to heavy,Spell check,Key-value Specifies the Key check of the cache system,ID verification, such as the order system to check whether an order ID exists, if not directly returned
  • Talk about globally unique ID generation methods
  • Leaf — Meituan-Dianping distributed ID generation system