栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

redis安装部署与jedis

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

redis安装部署与jedis

redis安装部署与jedis
  • Redis安装
    • 下载安装包
    • 解压安装
    • 编辑配置文件
    • 启动
    • 控制台
  • springboot整合Jedis
    • 添加依赖
    • 配置文件
    • Jedis配置
    • Jedis工具类
    • Jedis使用

Redis安装 下载安装包

http://download.redis.io/releases/
或者去官网下载

解压安装
tar -zxvf redis-3.2.0.tar.gz

cd redis-3.2.0

make

make PREFIX=/usr/local/redis install

cp redis.conf /usr/local/redis/bin/

编辑配置文件
vim redis.conf

#修改
#此处并非限制IP访问,而是监听IP地址,与服务器网卡有关
bind 0.0.0.0

#默认开启
protected-mode yes

#后台启动
daemonize yes

#密码设置
requirepass password

启动
./redis-server redis.conf
控制台

使用控制台可进行相关数据操作

./redis-cli -h 127.0.0.1 -p 6379
127.0.0.1:6379> auth password
OK
127.0.0.1:6379>
springboot整合Jedis 添加依赖

在pom.xml中添加依赖

        
            redis.clients
            jedis
        
配置文件
spring.redis.password=password
spring.redis.host=10.18.224.102
spring.redis.port=6379
spring.redis.jedis.pool.max-idle=10
spring.redis.jedis.pool.max-active=8
spring.redis.jedis.pool.min-idle=0
spring.redis.timeout=2000
Jedis配置
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

@Configuration
public class JedisConfig {


    @Value("${spring.redis.host}")
    private String host;

    @Value("${spring.redis.port}")
    private int port;

    @Value("${spring.redis.password}")
    private String password;
    @Value("${spring.redis.timeout}")
    private int timeout;

    @Value("${spring.redis.jedis.pool.max-active}")
    private int maxActive;

    @Value("${spring.redis.jedis.pool.max-idle}")
    private int maxIdle;

    @Value("${spring.redis.jedis.pool.min-idle}")
    private int minIdle;



    @Bean
    public JedisPool jedisPool(){
        JedisPoolConfig jedisPoolConfig=new JedisPoolConfig();
        jedisPoolConfig.setMaxIdle(maxIdle);
        jedisPoolConfig.setMinIdle(minIdle);
        jedisPoolConfig.setMaxTotal(maxActive);

        JedisPool jedisPool=new JedisPool(jedisPoolConfig,host,port,timeout,password);

        return jedisPool;
    }


}

Jedis工具类
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

@Component
public class JedisUtils {

    @Autowired
    private JedisPool jedisPool;

    
    public Jedis getJedis(){
        return jedisPool.getResource();
    }
    
    public void close(Jedis jedis){
        if(jedis!=null){
            jedis.close();
        }
    }

}
Jedis使用
        Jedis jedis= jedisUtils.getJedis();
        jedis.select(1);
        jedis.set("1","a");
        String ret = jedis.get("1");
        System.out.println(ret);

        jedis.select(2);
        jedis.set("1","b");
        String ret2 = jedis.get("1");
        System.out.println(ret2);

        jedis.select(1);
        String ret3 = jedis.get("1");
        System.out.println(ret3);
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1000037.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号