問題描述
在 Spring Boot 專案中,使用 Redisson 連線 Azure Redis 服務,如下是詳細的操作步驟(專案原始碼文末可下載)
示例步驟
第一步: 在 Spring Boot 的專案中,新增 redisson-spring-boot-starter 依賴
在專案的pom.xml檔案中新增 redisson-spring-boot-starter 依賴,根據Spring Boot 的版本來選擇 Redisson 的版本。
<dependency> <groupId>org.redisson</groupId> <artifactId>redisson-spring-boot-starter</artifactId> <version>3.16.8</version> </dependency>
(Maven Source: https://mvnrepository.com/artifact/org.redisson/redisson-spring-boot-starter)
(注:全部 pom.xml 請見附件 )
第二步: 新增 RedissonAutoConfiguration 類, RedissonAutoConfigurationCustomizer 類,和 RedissonProperties 類
從Github中獲取 redisson-spring-boot-starter Auto Configuration 類的內容,新增在自己的測試專案中。下載地址:https://github.com/redisson/redisson/tree/master/redisson-spring-boot-starter/src/main/java/org/redisson/spring/starter
本地專案中,新增完成後的檔案結構為:
本次實驗,採用的是最基本方式:把Redis 伺服器的引數設定在 application.yml 檔案中
application.yml
spring:
redis:
host: xxxxxxxxxxxxxxxxxxx.redis.cache.chinacloudapi.cn
port: 6380
database: 0
password: *****************************************************
ssl: true
abortConnect: false
timeout: 1000000ms
第三步: 在 Application Main 函式類中新增 Get 方法,在其中測試Set, Get Redis Key
新增 RedissonClient 物件,然後定義一個GET請求:SetGetRedis,請求URL為:/redis/{key},專案執行後,訪問的URL為:http://localhost:8080/redis/keytest1
package com.example.demokeyvault; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.data.redis.core.RedisTemplate; import org.redisson.api.RSet; import org.redisson.api.RedissonClient; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @RestController public class DemokeyvaultApplication { @Autowired private RedissonClient redisson; public static void main(String[] args) { SpringApplication.run(DemokeyvaultApplication.class, args); System.out.println("Hello World!"); } @RequestMapping("/redis/{key}") String SetGetRedis(@PathVariable String key){ //Set the Key to Redis Service. RSet<String> set = redisson.getSet(key); set.add("Test value from Redisson Application ... "); //Return the Vlaue to Page return set.toString(); } }
執行效果如下:
附錄:pom.xml
<?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 https://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.9.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>demokeyvault</artifactId> <version>0.1</version> <name>demokeyvault</name> <description>Demo project for Spring Boot</description> <properties> <java.version>8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.redisson</groupId> <artifactId>redisson-spring-boot-starter</artifactId> <version>3.16.8</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.azure</groupId> <artifactId>azure-security-keyvault-secrets</artifactId> <version>4.2.3</version> </dependency> <dependency> <groupId>com.azure</groupId> <artifactId>azure-identity</artifactId> <version>1.2.0</version> </dependency> <dependency> <groupId>io.projectreactor</groupId> <artifactId>reactor-core</artifactId> <version>3.4.19</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
原始檔下載地址:https://files.cnblogs.com/files/lulight/redissondemo.zip?t=1678104057
或
Github 地址:https://github.com/LuBu0505/My-Code/blob/main/redissondemo.zip