redis的安装以及在springboot中的配置

Redis的安装

首先从官网下载redis,目前的redis都是装在linux系统中,在官网中并没有提供windows版本

然后通过rz上传到服务器上的opt文件夹下,之后需要安装c语言环境,可以通过yum安装

也可以只安装gcc环境(可以通过gcc --version查看是否有gcc),如果显示没有则通过yum install gcc来进行安装

然后对刚刚上传的redis进行解压:tar -zxvf redis-6.2.6.tar.gz

解压完进入目录:redis-6.2.6

在redis-6.2.6目录下进行编译,通过make命令,最后通过make install进行安装,默认安装到 /usr/local/bin 目录下

然后进入目录,会发现redis安装成功

 redis-sentinel:redis集群使用

redis-server:redis服务器启动命令

redis-cli:客户端,操作入口

redis-benchmark:性能测试工具

redis-check-aof:修复有问题的aof文件

redis-check-dump:修复有问题的dump.rdb文件

 这个时候就安装完成了

启动方式

前台启动(不推荐)

命令行窗口不能关闭,否则服务器停止

redis-server

 此时这个窗口无法再进行其他操作,所以不推荐,关闭窗口则断开连接

使用ctrl C停止redis

后端启动

首先进入目录 redis-6.2.6 下,这里将redis.conf复制到etc下(也可以不复制)

 cp redis.conf /etc/redis.conf

 然后将后台启动设置daemonize no改成yes(修改etc下的redis.conf)

vi redis.conf

 完成修改后保存退出

然后就可以启动了,进入:cd /usr/local/bin 目录下启动

redis-server /etc/redis.conf

查看进程:

 ps -ef | grep redis

 客户端访问:redis-cli

 输入ping,如果显示pong则正常连接

关闭:通过shutdown或通过kill进程号

配置文件

此时只允许linux本地访问,将该行加注释,让他支持远程连接

将保护模式(防火墙)模式关闭,即将yes改为no

超时时间,0为永不超时

 然后可以设置密码

SpringBoot整合Redis

添加依赖

        <!-- redis -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <!--连接池 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
            <version>2.6.0</version>
        </dependency>

application.properties配置

spring.redis.host=101.200.239.97
spring.redis.port= 6379
#数据库索引
spring.redis.database=0
#连接超时时间
spring.redis.timeout=18000000
#连接池最大连接数(负数代表无限制)
spring.redis.lettuce.pool.max-active=20
#最大阻塞时间(负数代表无限制)
spring.redis.lettuce.pool.max-wait=-1
#连接池的最大空闲连接
spring.redis.lettuce.pool.max-idle=5
#连接池的最小空闲连接
spring.redis.lettuce.pool.min-idle=0
#密码
spring.redis.password=xxxxxx
package com.example.redistest.config;


import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.time.Duration;

/**
 * 如果使用redis作为任务队列则启用这两个标注
 */
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        RedisSerializer<String> redisSerializer = new StringRedisSerializer();
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
        template.setConnectionFactory(factory);
        template.setKeySerializer(redisSerializer);
        template.setValueSerializer(jackson2JsonRedisSerializer);
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        return template;
    }

    @Bean
    public CacheManager cacheManager(RedisConnectionFactory factory){
        RedisSerializer<String> redisSerializer = new StringRedisSerializer();
        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);
        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
                .entryTtl(Duration.ofSeconds(600))
                .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer))
                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer))
                .disableCachingNullValues();
        RedisCacheManager cacheManager = RedisCacheManager.builder(factory)
                .cacheDefaults(config).build();

        return cacheManager;
    }

}

测试

package com.example.redistest.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("test")
public class TestController {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @RequestMapping("redis")
    public String testRedis(){

        String test=redisTemplate.opsForValue().get("test");
        System.out.println(test);
        return test;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

过街的老鼠

感谢你对诗仙女的打赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值