springboot整合redis(單機)(springboot整合redis)


springboot整合redis(單機)springboot整合redisCluster集群參考https://www.cnblogs.com/super-chao/p/15143411.html

1.引入springboot和redis的相關jar包:

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.3.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
            <exclusions>
                <exclusion>
                    <artifactId>spring-aop</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <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>
            <exclusions>
                <exclusion>
                    <artifactId>spring-aop</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

2.創建application.properties或者application.yml配置文件並配置redis:

########################################################
###Redis (RedisConfiguration)
########################################################
spring.redis.database=0
spring.redis.host=127.0.0.1
#原來不是集群,我自己設置的集群
spring.redis.nodes=127.0.0.1:6379,127.0.0.1:6380,127.0.0.1:6381,127.0.0.1:6382,127.0.0.1:6383,127.0.0.1:6384
spring.redis.port=6385
#spring.redis.password=123456
spring.redis.password=
spring.redis.pool.max-idle=8
spring.redis.pool.min-idle=0
spring.redis.pool.max-active=8
spring.redis.pool.max-wait=-1
spring.redis.timeout=5000

如果項目中沒有集群,將spring.redis.nodes=127.0.0.1:6379,127.0.0.1:6380,127.0.0.1:6381,127.0.0.1:6382,127.0.0.1:6383,127.0.0.1:6384注釋掉。只使用redis單機版。

3.redis工具類,RedisService:

package com.itmayiedu.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class RedisService {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
//    @SuppressWarnings("rawtypes")
//    @Autowired
//    private RedisTemplate redisTemplate;

    public void setStr(String key, String value) {
        setStr(key, value, null);
    }

    public void setStr(String key, String value, Long time) {
        stringRedisTemplate.opsForValue().set(key, value);
        if (time != null){
            stringRedisTemplate.expire(key, time, TimeUnit.SECONDS);
        }
    }

    public Object getKey(String key) {
        return stringRedisTemplate.opsForValue().get(key);
    }

    public void delKey(String key) {
        stringRedisTemplate.delete(key);
    }
}

4.controller類,IndexController:

package com.itmayiedu.controller;



import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.itmayiedu.redis.RedisService;

@RestController
public class IndexController {
    @Autowired
    private RedisService redisService;

    @RequestMapping("/setRedis")
    public String setRedis(String key, String value) {
//        redisService.setStr(key, value,20L);
        redisService.setStr(key, value);
        return "success";
    }
    
    @RequestMapping("/delStringKey")
    public String reStrRedis(String key){
        redisService.delKey(key);
        return "success";
    }

    @RequestMapping("/getKey")
    public Object getKey(String key){
        Object result = redisService.getKey(key);
        return result == null ? "緩存沒有該數據" : result;
    }

}

5.springboot的啟動類,App:

package com.itmayiedu.app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;

@ComponentScan("com.itmayiedu.**")
@EnableAutoConfiguration
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}

6.測試:

瀏覽器測試:

 

 

 

cmd窗口查看:

 

 

redisClient工具查看:

 

 

 

至此,springboot整合redis(單機)完成。項目中可直接使用工具類RedisService(可自行重構)對redis操作。

 

 

 

 

spring.redis.nodes=127.0.0.1:6379,127.0.0.1:6380,127.0.0.1:6381,127.0.0.1:6382,127.0.0.1:6383,127.0.0.1:6384


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM