經過一個簡單的例子使用Springboot 鏈接並使用Redis。 本文假設已經安裝好Redis。java
1.首先將URL轉換爲一個ID ,並使用 StringRedisTemplate 將ID 和 URL 保存到Redisweb
2. 根據ID 從Redis中獲取對應的URLredis
項目結構以下spring
POM文件apache
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.1.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>redis-shorterurl</artifactId> <version>0.0.1-SNAPSHOT</version> <name>redis-shorterurl</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/commons-validator/commons-validator --> <dependency> <groupId>commons-validator</groupId> <artifactId>commons-validator</artifactId> <version>1.6</version> </dependency> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>18.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
以及Java文件app
package com.example.redisshorterurl; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class RedisShorterurlApplication { public static void main(String[] args) { SpringApplication.run(RedisShorterurlApplication.class, args); } }
package com.example.redisshorterurl; import java.nio.charset.StandardCharsets; import org.apache.commons.validator.routines.UrlValidator; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; import com.google.common.hash.Hashing; @RestController public class URLShorterResource { @Autowired StringRedisTemplate redisTemplate; @GetMapping("URL/{id}") public String getUrl(@PathVariable String id) { String url = redisTemplate.opsForValue().get(id); System.out.println("URL Retrieved: " + url); if (url == null) { throw new RuntimeException("There is no shorter URL for : " + id); } return url; } @PostMapping("/create") 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(); System.out.println("URL ID generated:" + id); redisTemplate.opsForValue().set(id, url); return id; } throw new RuntimeException("URL Invalid: " + url); } }
application.properties ssh
以後啓動Redis servermaven
啓動Springboot Application.spring-boot
此時,能夠用Postman測試效果。測試
首先, 使用 HTTP POST method 根據URL 生成一個 ID
以後,使用 ID 獲取對應的 URL