【AzureRedis缓存】示例使用redisson-spring-boot-starter连接/使用AzureRedis服务

博客 动态
0 177
羽尘
羽尘 2023-03-06 20:26:27
悬赏:0 积分 收藏

【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.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

 

posted @ 2023-03-06 20:06  路边两盏灯  阅读(0)  评论(0编辑  收藏  举报
回帖
    羽尘

    羽尘 (王者 段位)

    2335 积分 (2)粉丝 (11)源码

     

    温馨提示

    亦奇源码

    最新会员