`
hpgary
  • 浏览: 78678 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Java 通过哨兵链接Redis

 
阅读更多

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

 配置哨兵配置文件

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 

bin/redis-server redis.conf 主和从

 启动哨兵

bin/redis-sentinel sentinel.conf

 

通过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();
    }
}

 spring boot 配置文件

spring.redis.sentinel.nodes=10.0.0.12:6388
#master 是哨兵监控的名
spring.redis.sentinel.master=master

jar包:

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-redis</artifactId>
</dependency>

 

 使用

RedisService redisService = run.getBean(RedisService.class);
//redisService.set("ddd", "MyLove");	
String deserialize = stringRedisTemplate.getStringSerializer().deserialize("ddd".getBytes()) ;
System.out.println(deserialize);

 

 redis 哨兵集群:

http://wosyingjun.iteye.com/blog/2289593

redis 集群

http://wosyingjun.iteye.com/blog/2289220

0
1
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics