LINUX下安装redis及spring boot整合测试

第一步下载Redis:

下载地址:http://redis.io/download,下载最新文档版本。

第二步上传到linux并进行解压编译:

$ tar xzf redis-2.8.17.tar.gz
$ cd redis-2.8.17
$ make

第三步通过vi编辑器修改访问IP允许其他设备访问:

$ vi redis.conf

将:bind 127.0.0.1 改为:bind 0.0.0.0

第四步启动redis:(记得加载配置文件)

./redis-server ../redis.conf

第五步:spring boot整合引入依赖包 pom.xm:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

第六步进行application.properties相关配置:

#################redis基础配置#################
spring.redis.database=5 
spring.redis.host=10.0.0.224
#spring.redis.password=admin.1231
spring.redis.port=6379
# 连接超时时间 单位 ms(毫秒)
spring.redis.timeout=3000

#################redis线程池设置#################
# 连接池中的最大空闲连接,默认值也是8spring.redis.pool.max-idle=500
#连接池中的最小空闲连接,默认值也是0spring.redis.pool.min-idle=50
# 如果赋值为-1,则表示不限制;如果pool已经分配了maxActivejedis实例,则此时pool的状态为exhausted(耗尽)spring.redis.pool.max-active=2000
# 等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待时间,则直接抛出JedisConnectionException
spring.redis.pool.max-wait=1000

第七步进行redis相关配置:

@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {

    @Bean
    public KeyGenerator keyGenerator() {
        return new KeyGenerator() {
            @Override
            public Object generate(Object target, Method method, Object... params) {
                StringBuilder sb = new StringBuilder();
                sb.append(target.getClass().getName());
                sb.append(method.getName());
                for (Object obj : params) {
                    sb.append(obj.toString());
                }
                return sb.toString();
            }
        };
    }

    @SuppressWarnings("rawtypes")
    @Bean
    public CacheManager cacheManager(RedisTemplate redisTemplate) {
        RedisCacheManager rcm = new RedisCacheManager(redisTemplate);
        //设置缓存过期时间
        //rcm.setDefaultExpiration(60);//        return rcm;
    }

    @Bean
    public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
        StringRedisTemplate template = new StringRedisTemplate(factory);
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        template.setValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }
}

第八步进行代码测试:

@Controller
@RequestMapping("/dome")
public class Dome {

    /**
     * 获得用户获得令牌工具类
     */
  
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping
    @ResponseBody
    public String test(String c){

        ValueOperations<String, String> stringStringValueOperations = stringRedisTemplate.opsForValue();
        stringStringValueOperations.set("name",c);
        System.out.println("返回的值是:"+stringStringValueOperations.get("name"));
        redisTemplate.opsForValue().set("name", c);
        VideoPacket videoPacket=new VideoPacket();
        videoPacket.setId("123");
        videoPacket.setName("李四");
        redisTemplate.opsForHash().put("test","name",videoPacket.getName());
        return redisTemplate.opsForValue().get("name").toString();
    }


@Controller
@RequestMapping("/redis")
public class Test {

    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping
    @ResponseBody
    public String test(){
        ValueOperations valueOperations = redisTemplate.opsForValue();
        HashOperations hashOperations = redisTemplate.opsForHash();
        return  valueOperations.get("name").toString()+hashOperations.get("test","name");
    }
}


阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xm526489770/article/details/79960397
文章标签: redis
上一篇springboot反射工厂模式练习案例
下一篇MySql学习笔记
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭