1、安装redis 集群,1主1从 也可以是多主多从 redis 详细今后会在以后的文章中写出
配置redis-master 的配置文件 redis.conf
配置代码
* port 6379
* daemonize yes
* #protected-mode no
* dbfilename "1.db"
* bind 0.0.0.0
配置 redis-slave 的配置文件 redis.conf
配置代码
* port 6380
* daemonize yes
* dbfilename "2.db"
* bind 0.0.0.0
* #这里的IP必需通过程序可以访问到的IP地址
* slaveof 10.0.0.12 6379
配置哨兵配置文件 sentinel.conf
配置代码
* port 6388
* #protected-mode no
* bind 0.0.0.0
* daemonize yes
* logfile "/var/log/sentinel_6388.log"
* sentinel myid 818ae715a423ace06ab54a81bb4dac66de338377
* # 这里的IP,必需是通过程序可以访问的IP
* sentinel monitor master 10.0.0.12 6379 1
* sentinel down-after-milliseconds master 5000
* sentinel failover-timeout master 18000
* #如果有密码,这里就需要密码
* #sentinel auth-pass master 123456
启动redis
Shell代码
* bin/redis-server redis.conf 主和从
启动哨兵
Shell代码
* bin/redis-sentinel sentinel.conf
通过Java程序访问
Java代码 <>
* import java.util.HashSet;
* import java.util.Set;
*
* import redis.clients.jedis.Jedis;
* import redis.clients.jedis.JedisSentinelPool;
*
* public class RedisSentinelTest {
*
* public static void main(String[] args) {
* Set<String> sentinels = new HashSet<String>();
* String hostAndPort1 = "10.0.0.12:6388";
* sentinels.add(hostAndPort1);
* String clusterName = "master" ;
* JedisSentinelPool redisSentinelJedisPool = new
JedisSentinelPool(clusterName,sentinels);
* Jedis jedis = null;
* try {
* jedis = redisSentinelJedisPool.getResource();
* //jedis.set("key", "aaa");
* System.out.println(jedis.get("key"));
* System.out.println(jedis.get("bbb"));
* } catch (Exception e) {
* e.printStackTrace();
* } finally {
* jedis.close();
* }
* redisSentinelJedisPool.close();
* }
* }
热门工具 换一换