目录结构 E:\redis\redis64-3.0.501-6379 在此目录下 新建一个txt @echo off redis-server.exe redis.windows.conf @pause 重命名为startRedisServer.bat bat结尾的是windows可识别的批处理程序 能够直接执行命令窗口的命令 @echo off DOS批处理中的, 不想显示器显示 dos批处理中 的 每条命令 , 加 echo off “echo off”也是命令,它自己也会显示,若是连这条也不显示,就在前面加个“@”。 @自己就是一条指令,意思是跟在它后面的指令的执行及结果都不会在DOS界面上显示出来 pause暂停命令 运行该命令时,将显示消息:请按任意键继续 . . .,通常用于看清楚屏幕上显示的内容 而后新建一个txt,在E:\redis目录下 @echo off cd redis64-3.0.501-6379 startRedisServer.bat 重命名为start6379.cmd cmd开启客户端 @echo off cd redis64-3.0.501-6379 redis-cli @pause
设置服务命令 redis-server --service-install redis.windows-service.conf --loglevel verbose 卸载服务:redis-server --service-uninstall 开启服务:redis-server --service-start 中止服务:redis-server --service-stop
经过右击计算机---计算机管理--服务和应用程序--服务可查看多出了一个redis服务
首先在pom.xml文件添加依赖,由于我用的spring框架,因此导了一个spring-data-redis,jackson包是为了能保持对象
<dependencies>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.7.1</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.6.2.RELEASE</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.1.0</version>
</dependency>
</dependencies>
#访问地址 redis.host=127.0.0.1 #访问端口 redis.port=6379 #注意,若是没有password,此处不设置值,但这一项要保留 redis.password= #最大空闲数,数据库链接的最大空闲时间。超过空闲时间,数据库链接将被标记为不可用,而后被释放。设为0表示无限制。 redis.maxIdle=300 #链接池的最大数据库链接数。设为0表示无限制 redis.maxActive=600 #最大创建链接等待时间。若是超过此时间将接到异常。设为-1表示无限制。 redis.maxWait=1000 #在borrow一个jedis实例时,是否提早进行alidate操做;若是为true,则获得的jedis实例均是可用的; redis.testOnBorrow=true
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd "> <!-- 若是在多个spring配置文件中引入<context:property-placeholder .../>标签, 最后须要加上ignore-unresolvable="true",不然会报错。 ignore-unresolvable="true" 在加载第一个property-placeholder时出现解析不了的占位符进行忽略掉 --> <!-- 链接池基本参数配置,相似数据库链接池 --> <context:property-placeholder location="classpath:redis.properties" ignore-unresolvable="true" /> <!-- redis链接池 --> <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxTotal" value="${redis.maxActive}" /> <property name="maxIdle" value="${redis.maxIdle}" /> <property name="testOnBorrow" value="${redis.testOnBorrow}" /> </bean> <!-- 链接池配置,相似数据库链接池 --> <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <property name="hostName" value="${redis.host}"></property> <property name="port" value="${redis.port}"></property> <!-- <property name="password" value="${redis.pass}"></property> --> <property name="poolConfig" ref="poolConfig"></property> </bean> <!--redis操做模版,使用该对象能够操做redis --> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" > <property name="connectionFactory" ref="jedisConnectionFactory" /> <!--若是不配置Serializer,那么存储的时候缺省使用String,若是用User类型存储,那么会提示错误User can't cast to String!! --> <property name="keySerializer" > <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" /> </property> <property name="valueSerializer" > <bean class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer" /> </property> <property name="hashKeySerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/> </property> <property name="hashValueSerializer"> <bean class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"/> </property> <!--开启事务 --> <property name="enableTransactionSupport" value="true"></property> </bean > <!-- 下面这个是整合Mybatis的二级缓存使用的 <bean id="redisCacheTransfer" class="cn.qlq.jedis.RedisCacheTransfer"> <property name="jedisConnectionFactory" ref="jedisConnectionFactory" /> </bean>--> </beans>
package com.one.redis; import java.io.InputStream; import java.util.*; import javax.annotation.Resource; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import redis.clients.jedis.Jedis; @RunWith(SpringJUnit4ClassRunner.class) // @ContextConfiguration("classpath:applicationContext-*.xml") @ContextConfiguration(locations = {"classpath:applicationContext.xml"}) // 加载配置文件 @SuppressWarnings("all") public class RedisTest { // @Autowired // private RedisUtil redisUtil; // 1.导包 // applicationContext.xml <import resource="classpath*:conf/beans-*.xml"/> // beans-redis.xml // <bean id="redisTemplate" // class="org.springframework.data.redis.core.RedisTemplate" > @Resource(name = "redisTemplate") private RedisTemplate redisTemplate; @Test public void testSpringRedis() { // stringRedisTemplate的操做 // String读写 redisTemplate.delete("myStr"); redisTemplate.opsForValue().set("myStr", "skyLine"); System.out.println(redisTemplate.opsForValue().get("myStr")); System.out.println("---------------"); // org.springframework.data.redis.RedisConnectionFailureException: // Cannot get Jedis connection; nested exception is // redis.clients.jedis.exceptions.JedisConnectionException: Could not // get a resource from the pool // List读写 redisTemplate.delete("myList"); redisTemplate.opsForList().rightPush("myList", "T"); redisTemplate.opsForList().rightPush("myList", "L"); redisTemplate.opsForList().leftPush("myList", "A"); List<String> listCache = redisTemplate.opsForList().range("myList", 0, -1); for (String s : listCache) { System.out.println(s); } System.out.println("---------------"); // Set读写 redisTemplate.delete("mySet"); redisTemplate.opsForSet().add("mySet", "A"); redisTemplate.opsForSet().add("mySet", "B"); redisTemplate.opsForSet().add("mySet", "C"); redisTemplate.opsForSet().add("mySet", "C"); Set<String> setCache = redisTemplate.opsForSet().members("mySet"); for (String s : setCache) { System.out.println(s); } System.out.println("---------------");// ABC // Hash读写 redisTemplate.delete("myHash"); redisTemplate.opsForHash().put("myHash", "BJ", "北京"); redisTemplate.opsForHash().put("myHash", "SH", "上海"); redisTemplate.opsForHash().put("myHash", "HN", "河南"); Map<String, String> hashCache = redisTemplate.opsForHash().entries( "myHash"); for (Map.Entry entry : hashCache.entrySet()) { System.out.println(entry.getKey() + " - " + entry.getValue()); } System.out.println("---------------"); // Redis Incrby 命令将 key 中储存的数字加上指定的增量值,若是 key 不存在,那么 key 的值会先被初始化为 0 // ,而后再执行 INCR 操做 double stringValueDouble = redisTemplate.opsForValue().increment( "doubleValue", 5); System.out.println("经过increment(K key, double delta)方法以增量方式存储double值:" + stringValueDouble); // incrBy:将 key 所储存的值加上给定的增量值(increment) 。 Jedis jedis = new Jedis("127.0.0.1", 6379); jedis.incrBy("key3", 5); String v3 = jedis.get("key3"); System.out.println("结果:" + v3); } @Test public void delete() { redisTemplate.delete("myStr"); redisTemplate.delete("mySet"); redisTemplate.delete("myHash"); redisTemplate.delete("key3"); String str = "string";// 1.字符串 redisUtil.set("str", str); System.out.println(redisTemplate.opsForValue().get("str")); } @Autowired private RedisUtil redisUtil; @Test public void testSpringRedis2() { String str = "string";// 1.字符串 List<String> list = new ArrayList<String>();// list list.add("0"); list.add("中国"); list.add("2"); Set<String> set = new HashSet<String>();// set set.add("0"); set.add("中国"); set.add("2"); Map<String, Object> map = new HashMap();// map map.put("key1", "str1"); map.put("key2", "中国"); map.put("key3", "str3"); redisUtil.del("myStr", "str");// 删除数据 // 1.字符串操做 System.out.println(str); redisUtil.set("str", str); redisUtil.expire("str", 120);// 指定失效时间为2分钟 String str1 = (String) redisUtil.get("str"); System.out.println(redisTemplate.opsForValue().get("str")); System.out.println("str1= " + str1); // 2.list操做 redisUtil.lSet("list", list); redisUtil.expire("list", 120);// 指定失效时间为2分钟 List<Object> list1 = redisUtil.lGet("list", 0, -1); System.out.println(list1); // 3.set操做 redisUtil.sSet("set", set); redisUtil.expire("set", 120);// 指定失效时间为2分钟 Set<Object> set1 = redisUtil.sGet("set"); System.out.println(set1); // 3.map操做 redisUtil.hmset("map", map); redisUtil.expire("map", 120);// 指定失效时间为2分钟 Map<Object, Object> map1 = redisUtil.hmget("map"); System.out.println(map1); } // TODO 哨兵集群 // TODO CDN }
<properties> <junit.version>4.9</junit.version> <spring-version>3.1.2.RELEASE</spring-version> </properties> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring-version}</version> </dependency>
参考https://blog.csdn.net/u010648555/article/details/79427606 这么操做 1.先将E:\redis\redis64-3.0.501复制三份,分别命名为redis64-3.0.501-6379,redis64-3.0.501-6379, redis64-3.0.501-6381 2.修改6380 6381下的redis.windows.conf文件,改为各自对应的端口号,都作6379的从节点 port 6380 # slaveof <masterip> <masterport> slaveof 127.0.0.1 6379 3.经过上面介绍的cmd脚本形式启动6379, 而后启动6380,而后启动6379客户端,看下图 命令info replication
能够在redis解压目录下 经过redis-server.exe redis.windows.conf redis-server.exe sentinel.conf --sentinel 命令, 先启动Redis集群,再启动哨兵实例 也能够像以前那样,写个脚本执行 示例文件作了详细的配置说明,启动不了,估计是里面哪里配置有误,懒得找了,用下面的简化版sentinel.conf port 26379 sentinel monitor mymaster 127.0.0.1 6379 2 sentinel down-after-milliseconds mymaster 3000 sentinel failover-timeout mymaster 10000 sentinel config-epoch mymaster 0 执行后你会发现该文件有变化,写入了从服务器的信息 # Generated by CONFIG REWRITE dir "E:\\redis\\redis64-3.0.501-6379" sentinel leader-epoch mymaster 0 sentinel known-slave mymaster 127.0.0.1 6380 sentinel known-slave mymaster 127.0.0.1 6381 sentinel current-epoch 0 若是报Invalid argument during startup: Failed to open the .conf file: 通常都是配置文件的问题,把它从其余地方关闭