Categories: 分布式
REmote DIctionary Server(Redis) 是一个由Salvatore Sanfilippo写的key-value存储系统。
下载
官网地址:https://redis.io/download
本地下载:redis-3.2.12.tar.gz
常用命令
安装
上传、解压略
进入解压目录 编译
make && make install
安装
make install PREFIX=/usr/local/software/redis
注:
安装时提示没有gcc环境时,需要先安装gcc
yum install make cmake gcc gcc-c++
执行make时报错#error "Newer version of jemalloc required",执行
make MALLOC=libc
开启远程访问
复制redis源码包下的redis.conf到安装目录下
cp /usr/local/software/redis-3.2.11/redis.conf /usr/local/software/redis/bin
修改redis/bin/redis.conf
注释:
修改:
在redis/bin/redis.conf最后一行添加
requirepass 123456
开启、关闭
以下操作在bin目录进行
启动:./redis-server 默认启动时ssh窗口关闭服务也会停止,且窗口会无响应(Ctrl+c可解除)
指定配置文件启动:./redis-server [config file path]
连接:./redis-cli
连接指定IP和端口:./redis-cli -h 127.0.0.1 -p 6379 -a 123456(-a为密码) -c(-c为集群连接)
停止: ./redis-cli shutdown
通过密码启动时,停止同样需要密码:./redis-cli -a 123456 shutdown
java使用redis
测试代码
/** * 单redis无密码,原生测试连接 * * @throws Exception */ @Test public void testJedis() throws Exception { // 第一步:创建一个Jedis对象。需要指定服务端的ip及端口。 Jedis jedis = new Jedis("10.0.107.157", 6379); System.out.println(jedis.ping()); // 第二步:使用Jedis对象操作数据库,每个redis命令对应一个方法。 //jedis.set("hello","123"); System.out.println(jedis.get("hello")); // 第三步:关闭Jedis jedis.close(); } /** * 单redis有密码,原生测试连接 * * @throws Exception */ @Test public void testJedisWithPassword() throws Exception { // 第一步:创建一个Jedis对象。需要指定服务端的ip及端口。 Jedis jedis = new Jedis("10.0.107.157", 6379); jedis.auth("123456"); System.out.println(jedis.ping()); // 第二步:使用Jedis对象操作数据库,每个redis命令对应一个方法。 //jedis.set("hello","123"); System.out.println(jedis.get("hello")); // 第三步:关闭Jedis jedis.close(); } /** * 单redis无密码,spring测试连接 * * @throws Exception */ @Test public void testSpringJedis() throws Exception { ApplicationContext ac = new ClassPathXmlApplicationContext("spring-redis.xml"); //获取连接池对象 JedisPool jedisPool = (JedisPool) ac.getBean("jedisPool"); //从连接池中获取连接 Jedis jedis = jedisPool.getResource(); System.out.println(jedis.ping()); System.out.println(jedis.exists("hello")); jedis.close();//关闭连接 jedisPool.close();//关闭连接池 } /** * 单redis有密码,spring测试连接 * * @throws Exception */ @Test public void testSpringJedisWidthPassword() throws Exception { ApplicationContext ac = new ClassPathXmlApplicationContext("spring-redis.xml"); //获取连接池对象 JedisPool jedisPool = (JedisPool) ac.getBean("jedisPoolPwd"); //从连接池中获取连接 Jedis jedis = jedisPool.getResource(); System.out.println(jedis.ping()); System.out.println(jedis.exists("hello")); jedis.close();//关闭连接 jedisPool.close();//关闭连接池 }
spring-redis.xml 注:两个JedisPool不能同时开启
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
</bean>
<!--单redis,无密码-->
<bean id="jedisPool" class="redis.clients.jedis.JedisPool">
<constructor-arg name="host" value="10.0.107.157"></constructor-arg>
<constructor-arg name="port" value="6379"></constructor-arg>
</bean>
<!--单redis,有密码-->
<bean id="jedisPoolPwd" class="redis.clients.jedis.JedisPool">
<constructor-arg name="host" value="10.0.107.157"></constructor-arg>
<constructor-arg name="port" value="6379"></constructor-arg>
<constructor-arg name="poolConfig" ref="jedisPoolConfig"></constructor-arg>
<constructor-arg name="password" value="123456"></constructor-arg>
<constructor-arg name="timeout" value="30000"></constructor-arg>
</bean>
pom.xml
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!--Jedis的jar-->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<!--Spring基础jar包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.11.RELEASE</version>
</dependency>