• 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
  • 相关阅读:
    php写的几种常见算法
    无状态登陆:JWT
    boostrap中日期控件使用
    boostrap中文件上传使用组件fileinput
    ubuntu环境下homestead安装运行nsq
    Ubuntu 下修改mysqlroot密码
    从git到lnmp代码发布
    maven的xml中报错:org.apache.maven.archiver.MavenArchiver.getManifest(org.apache.maven.project.MavenProject, org.apache.maven.archiver.MavenArchiveConfiguration)
    给mysql查询添加序号列
    maven jar包下载不下来
  • 原文地址:https://www.cnblogs.com/super-chao/p/15229835.html
Copyright © 2020-2023  润新知