Redis安装/使用
一、基于Docker安装/不带配置文件:
1)拉取镜像:docker pull redis;
2)运行:docker run -it -p 6379:6379 redis bash
3)使用:
1.默认进入目录是:/data
2.需要进入到:/usr/local/bin
3.启动:./redis-cli
//指定IP和端口运行:./redis-cli -h 127.0.0.1 -p 6379
4.关闭:./redis-cli shutdown
二、带自定义配置文件:
1)从官网下载配置文件:redis.conf;
2)运行:
docker run -v /usr/local/docker/redis/conf/redis.conf:/usr/local/etc/redis/redis.conf --name myredis -p 6379:6379 redis redis-server /usr/local/etc/redis/redis.conf
3)重建一个连接进入:docker exec -it myredis bash
三、配置文件redis.conf:
1)解除本地绑定:注释掉bind 127.0.0.1
2)设置密码:requirepass java1234
四、redis的操作方式:
1)在linux系统内使用redis-cli工具;
2)使用redisdesktopmanager来连接redis服务器;
3)使用使用jedis:java连接redis的工具;
1.导包:
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency>
2.测试:
@Test public void test(){ Jedis jedis = new Jedis("192.168.80.135",6379); jedis.auth("java1234"); jedis.set("name","zs"); String name = jedis.get("name"); System.out.println(name); }
3.连接池:
public class JedisPoolFactory { private static JedisPoolConfig config = new JedisPoolConfig(); private static JedisPool pool; static { //最大空闲数 config.setMaxIdle(50); //最大连接数 config.setMaxTotal(100); //最大等待毫秒数 config.setMaxWaitMillis(20000); } public static Jedis getJedisByPool() { if (pool == null) { synchronized (JedisPoolFactory.class) { if (pool == null) { pool = new JedisPool(config, "192.168.80.135", 6379, 2000, "java1234"); } } } return pool.getResource(); } }
4)使用springdata-redis框架来操作redis;
1、导入依赖:
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.8.8.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.3.8.RELEASE</version> </dependency>
2、编写配置文件:redis.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 配置redis连接池对象 --> <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"> <!-- 最大空闲数 --> <property name="maxIdle" value="50" /> <!-- 最大连接数 --> <property name="maxTotal" value="100" /> <!-- 最大等待时间 --> <property name="maxWaitMillis" value="20000" /> </bean> <!-- stringRedisSerializer序列化器 --> <bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/> <!-- 配置redis连接工厂 --> <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <!-- 连接池配置 --> <property name="poolConfig" ref="poolConfig" /> <!-- 连接主机 --> <property name="hostName" value="192.168.80.135" /> <!-- 端口 --> <property name="port" value="6379" /> <!-- 密码 --> <property name="password" value="java1234" /> </bean> <!-- 配置redis模板对象 --> <bean class="org.springframework.data.redis.core.RedisTemplate"> <!-- 配置连接工厂 --> <property name="connectionFactory" ref="connectionFactory" /> <!--配置序列化器--> <property name="keySerializer" ref="stringRedisSerializer"/> <!--<property name="valueSerializer" ref="stringRedisSerializer"/>--> </bean> </beans>
3、测试类:
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:redis.xml") public class RedisTest { @Autowired private RedisTemplate redisTemplate; @Test public void test01(){ ValueOperations ops = redisTemplate.opsForValue(); ops.set("name", "zs"); System.out.println(ops.get("name")); } }
五、springBoot整合redis:
1)导入依赖 :
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
2)
spring: redis: host: 192.168.2.147 port: 6379 password: java1902 jedis: pool: max-active: 100