我在找一个使用redis sentinel的jedis的例子。我无法使用jedis设施进行定位,也找不到任何好的示例或文档。寻找与哨兵Jedis的例子
13
A
回答
17
好的,这里是解决方案!
这很容易,我有点尴尬,但真正的jedis库是一个糟糕的文档解决方案。
所以,
@Autowired private JedisSentinelPool pool;
public void mymethod() {
Jedis jedis = null;
try {
jedis = pool.getResource();
jedis.hset(....
} catch (JedisException je) {
throw je;
} finally {
if (jedis != null) pool.returnResource(jedis);
}
}
由于我使用Spring,你需要:
<bean id="redisSentinel" class="redis.clients.jedis.JedisSentinelPool">
<constructor-arg index="0" value="mymaster" />
<constructor-arg index="1">
<set>
<value>hostofsentinel:26379</value>
</set>
</constructor-arg>
<constructor-arg index="2" ref="jedisPoolConfig"/>
</bean>
1
你试过Redisson?它提供了哨兵自动主/从/哨兵发现和拓扑更新,你不需要处理连接,数据编码......这一切都是由雷森森完成的。下面是代码示例:
Config config = new Config();
config.useSentinelServers()
.setMasterName("mymaster")
.addSentinelAddress("127.0.0.1:26389", "127.0.0.1:26379")
RedissonClient redisson = Redisson.create(config);
RMap<MyKey, MyValue> map = redisson.getMap("myMap");
map.put(new MyKey(), new MyValue());
+0
除了put和get之外,我怎样才能应用不同的Redis操作? – Veswanth
+1
@VeswanthRaju下面是Redis命令如何映射到Redisson对象的文档https://github.com/redisson/redisson/wiki/11.-Redis-commands-mapping –
1
,当你不使用Spring,需要通过Jedis通过Redis的管理的Redis的主/从设定的简单连接哨兵
public class JedisTestSentinelEndpoint {
private static final String MASTER_NAME = "mymaster";
public static final String PASSWORD = "foobared";
private static final Set sentinels;
static {
sentinels = new HashSet();
sentinels.add("mymaster-0.servers.example.com:26379");
sentinels.add("mymaster-1.servers.example.com:26379");
sentinels.add("mymaster-2.servers.example.com:26379");
}
public JedisTestSentinelEndpoint() {
}
private void runTest() throws InterruptedException {
JedisSentinelPool pool = new JedisSentinelPool(MASTER_NAME, sentinels);
Jedis jedis = null;
try {
printer("Fetching connection from pool");
jedis = pool.getResource();
printer("Authenticating...");
jedis.auth(PASSWORD);
printer("auth complete...");
Socket socket = jedis.getClient().getSocket();
printer("Connected to " + socket.getRemoteSocketAddress());
printer("Writing...");
jedis.set("java-key-999", "java-value-999");
printer("Reading...");
jedis.get("java-key-999");
} catch (JedisException e) {
printer("Connection error of some sort!");
printer(e.getMessage());
Thread.sleep(2 * 1000);
} finally {
if (jedis != null) {
jedis.close();
}
}
}
...
}
来源下面是一个例子:连接到Redis Sentinels上的This blog post。
相关问题
- 1. 哨兵与Python 3
- 2. 哨兵与传递计数
- 3. 使用两个cartalyst哨兵实例2
- 4. 虽然与哨兵循环的Java
- 5. 误差积分哨兵与log4j的
- 6. C + +的哨兵while循环
- 7. ImproperlyConfigured安装Django的哨兵
- 8. Redis的哨兵认证
- 9. AWS EMR中的哨兵
- 10. Laravel的哨兵使用
- 11. 与亚马逊SES哨兵配置
- 12. for循环发生器与哨兵
- 13. 哨兵与OAuth2用户服务器
- 14. 用PHP设置哨兵
- 15. Laravel 4播种类哨兵
- 16. 哨兵忽略--config路径
- 17. 做哨兵不工作(java)
- 18. Django哨兵默认标签
- 19. 哨兵投掷误差5.1
- 20. 获取redis主控哨兵
- 21. NSMutableArray额外零哨兵
- 22. Laravel哨兵捕捉错误
- 23. 哨兵升级错误
- 24. 解读哨兵频率图
- 25. 哨兵2 laravel 4 ACL
- 26. 哨兵使用等级
- 27. 如何在cartalyst哨兵
- 28. Laravel 5哨兵/注册
- 29. 龙卷风哨兵集成
- 30. Hive哨兵用户伪装
如何将此用于RedisTemplate? – pavan
您可以使用RedisTemplate作为本文共享的示例 –
有人可以解释为什么JedisSentinelPool.getResource()不返回JedisSentinel,而是返回Jedis? –