【Azure Redis 缓存】示例使用 redisson-spring-boot-starter 连接/使用 Azure Redis 服务
问题描述
在 Spring Boot 项目中,使用 Redisson 连接 Azure Redis 服务,如下是详细的操作步骤(项目源代码文末可下载)
示例步骤
第一步: 在 Spring Boot 的项目中,添加 redisson-spring-boot-starter 依赖
在项目的pom.xml文件中添加 redisson-spring-boot-starter 依赖,根据Spring Boot 的版本来选择 Redisson 的版本。
dependency>
groupId>org.redissongroupId>
artifactId>redisson-spring-boot-starterartifactId>
version>3.16.8version>
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. RSetset = 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.0modelVersion> parent> groupId>org.springframework.bootgroupId> artifactId>spring-boot-starter-parentartifactId> version>2.0.9.RELEASEversion> relativePath /> parent> groupId>com.examplegroupId> artifactId>demokeyvaultartifactId> version>0.1version> name>demokeyvaultname> description>Demo project for Spring Bootdescription> properties> java.version>8java.version> properties> dependencies> dependency> groupId>org.springframework.bootgroupId> artifactId>spring-boot-starter-webartifactId> dependency> dependency> groupId>org.redissongroupId> artifactId>redisson-spring-boot-starterartifactId> version>3.16.8version> dependency> dependency> groupId>org.springframework.bootgroupId> artifactId>spring-boot-starter-testartifactId> scope>testscope> dependency> dependency> groupId>com.azuregroupId> artifactId>azure-security-keyvault-secretsartifactId> version>4.2.3version> dependency> dependency> groupId>com.azuregroupId> artifactId>azure-identityartifactId> version>1.2.0version> dependency> dependency> groupId>io.projectreactorgroupId> artifactId>reactor-coreartifactId> version>3.4.19version> dependency> dependencies> build> plugins> plugin> groupId>org.springframework.bootgroupId> artifactId>spring-boot-maven-pluginartifactId> 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
当在复杂的环境中面临问题,格物之道需:浊而静之徐清,安以动之徐生。 云中,恰是如此!
文章来源于互联网:【Azure Redis 缓存】示例使用 redisson-spring-boot-starter 连接/使用 Azure Redis 服务