java操作 redis集群

1.redis服务端集群搭建步骤:

1.下载redis安装包,进行解压安装

2.安装ruby、rubygems install ruby ,安装ruby的原因是,在进行集群的时候,使用的是ruby语言工具实现的,所以在集群之前首先需要搭建ruby的环境

3.在上述步骤完成之后,便可以搭建集群环境,redis提供了两种集群搭建方法,执行脚本方法(安装包下面的util包中)和手动搭建。

注意:

1.在集群的时候,如果是远端客户端访问redis服务端,那么在分片的时候,需要使用Ip进行分片,下面会详细说

2.在创建每个节点的时候,不要只用redis-server ,使用绝对路径下的redis-server xxx

具体的安装步骤如下:http://blog.csdn.net/xu470438000/article/details/42971091

附件:在安装ruby的时候,需要gemredis,下载地址在下面。

2.客户端(java):

注意:

1.本文的客户端使用的是java,官网中对于java客户端也提供了不少的client,但是本文使用的是官方推荐的jedis。

2.在项目开发中,一般情况下都会用到spring来管理应用,本文也是如此,spring 本身也提供了对redis的集成支持,具体的网址:http://projects.spring.io/spring-data-redis,

但是好像目前spring-data-redis不提供集群的功能,所以本文没有使用它,而是使用了原装的jedis来进行开发,如果在项目中没有用到集群的功能,则可以使用spirng-data-redis。

下面是具体的代码实现

1.maven依赖

Java代码  

  1. <dependency>
  2. <groupId>redis.clients</groupId>
  3. <artifactId>jedis</artifactId>
  4. <version>2.7.2</version>
  5. </dependency>

2.applicationContext.xml中的配置

Java代码  

  1. <!-- jedis cluster config -->
  2. <bean name="genericObjectPoolConfig" class="org.apache.commons.pool2.impl.GenericObjectPoolConfig" >
  3. <property name="maxWaitMillis" value="-1" />
  4. <property name="maxTotal" value="1000" />
  5. <property name="minIdle" value="8" />
  6. <property name="maxIdle" value="100" />
  7. </bean>
  8. <bean id="jedisCluster" class="com.besttone.subscribe.util.JedisClusterFactory">
  9. <property name="addressConfig">
  10. <value>classpath:redis-config.properties</value>
  11. </property>
  12. <property name="addressKeyPrefix" value="address" />
  13. <property name="timeout" value="300000" />
  14. <property name="maxRedirections" value="6" />
  15. <property name="genericObjectPoolConfig" ref="genericObjectPoolConfig" />
  16. </bean>

3.JedisClusterFactory实现类

Java代码  

  1. public class JedisClusterFactory implements FactoryBean<JedisCluster>, InitializingBean {
  2. private Resource addressConfig;
  3. private String addressKeyPrefix ;
  4. private JedisCluster jedisCluster;
  5. private Integer timeout;
  6. private Integer maxRedirections;
  7. private GenericObjectPoolConfig genericObjectPoolConfig;
  8. private Pattern p = Pattern.compile("^.+[:]\\d{1,5}\\s*$");
  9. @Override
  10. public JedisCluster getObject() throws Exception {
  11. return jedisCluster;
  12. }
  13. @Override
  14. public Class<? extends JedisCluster> getObjectType() {
  15. return (this.jedisCluster != null ? this.jedisCluster.getClass() : JedisCluster.class);
  16. }
  17. @Override
  18. public boolean isSingleton() {
  19. return true;
  20. }
  21. private Set<HostAndPort> parseHostAndPort() throws Exception {
  22. try {
  23. Properties prop = new Properties();
  24. prop.load(this.addressConfig.getInputStream());
  25. Set<HostAndPort> haps = new HashSet<HostAndPort>();
  26. for (Object key : prop.keySet()) {
  27. if (!((String) key).startsWith(addressKeyPrefix)) {
  28. continue;
  29. }
  30. String val = (String) prop.get(key);
  31. boolean isIpPort = p.matcher(val).matches();
  32. if (!isIpPort) {
  33. throw new IllegalArgumentException("ip 或 port 不合法");
  34. }
  35. String[] ipAndPort = val.split(":");
  36. HostAndPort hap = new HostAndPort(ipAndPort[0], Integer.parseInt(ipAndPort[1]));
  37. haps.add(hap);
  38. }
  39. return haps;
  40. } catch (IllegalArgumentException ex) {
  41. throw ex;
  42. } catch (Exception ex) {
  43. throw new Exception("解析 jedis 配置文件失败", ex);
  44. }
  45. }
  46. @Override
  47. public void afterPropertiesSet() throws Exception {
  48. Set<HostAndPort> haps = this.parseHostAndPort();
  49. jedisCluster = new JedisCluster(haps, timeout, maxRedirections,genericObjectPoolConfig);
  50. }
  51. public void setAddressConfig(Resource addressConfig) {
  52. this.addressConfig = addressConfig;
  53. }
  54. public void setTimeout(int timeout) {
  55. this.timeout = timeout;
  56. }
  57. public void setMaxRedirections(int maxRedirections) {
  58. this.maxRedirections = maxRedirections;
  59. }
  60. public void setAddressKeyPrefix(String addressKeyPrefix) {
  61. this.addressKeyPrefix = addressKeyPrefix;
  62. }
  63. public void setGenericObjectPoolConfig(GenericObjectPoolConfig genericObjectPoolConfig) {
  64. this.genericObjectPoolConfig = genericObjectPoolConfig;
  65. }
  66. }

4.redis-config.properties文件

这是一个集群环境,六个节点(不同端口),三个master ,三个slaver

Java代码  

  1. address1=192.168.30.139:7000
  2. address2=192.168.30.139:7001
  3. address3=192.168.30.139:7002
  4. address4=192.168.30.139:7003
  5. address5=192.168.30.139:7004
  6. address6=192.168.30.139:7005

原文地址:https://www.cnblogs.com/lyzjavajiagou/p/9927552.html

时间: 2024-07-30 16:00:00

java操作 redis集群的相关文章

java操作redis集群配置[可配置密码]和工具类

java操作redis集群配置[可配置密码]和工具类 <dependency>   <groupId>redis.clients</groupId>   <artifactId>jedis</artifactId>   <version>2.9.0</version>   </dependency>   <dependency>   <groupId>org.apache.commons

Java操作 Redis 集群

// 连接redis集群 @Test public void testJedisCluster() { JedisPoolConfig config = new JedisPoolConfig(); // 最大连接数 config.setMaxTotal(30); // 最大连接空闲数 config.setMaxIdle(2); //集群结点 Set<HostAndPort> jedisClusterNode = new HashSet<HostAndPort>(); jedisC

突破Java面试-Redis集群模式的原理

1 面试题 Redis集群模式的工作原理说一下?在集群模式下,key是如何寻址的?寻址都有哪些算法?了解一致性hash吗? 2 考点分析 Redis不断在发展-Redis cluster集群模式,可以做到在多台机器上,部署多个实例,每个实例存储一部分的数据,同时每个实例可以带上Redis从实例,自动确保说,如果Redis主实例挂了,会自动切换到redis从实例顶上来. 现在新版本,大家都是用Redis cluster的,也就是原生支持的集群模式,那么面试官肯定会就redis cluster对你来

python 操作redis集群

redis集群 cd /usr/local/redis3.0/src ./redis-trib.rb  create --replicas 1 ip1:7000 ip1:7001 cluster info/nodes redis-cli -c -h yourhost -p yourpost https://github.com/andymccurdy/redis-py pip install redis-py-cluster pip升级pip-9.0.1.tar.gz # !/usr/bin/e

java 连接 redis集群时报错:Could not get a resource from the pool

由于弄这个的时候浪费了太多的时间,所以才记录下这个错,给大伙参考下 检查了一下,配置啥的都没问题的,但在redis集群机器上就可以,错误如下: Exception in thread "main" redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool 原因: 是因为我在创集群的时候的ip地址是:127.0.0.1,不是本机的电脑访问的话是不能访问的,

15.9,python操作redis集群

上代码 1.对redis的单实例进行连接操作 python3 >>>import redis >>>r = redis.StrictRedis(host='localhost', port=6379, db=0,password='root') >>>r.set('lufei', 'guojialei') True >>>r.get('lufei') 'bar' -------------------- 2.sentinel集群连接并

java连接redis集群

http://m.blog.csdn.net/koushr/article/details/50956870 连接redis数据库(非集群模式) public static void main(String[] args) { JedisPoolConfig poolConfig = new JedisPoolConfig(); // 最大连接数 poolConfig.setMaxTotal(2); // 最大空闲数 poolConfig.setMaxIdle(2); // 最大允许等待时间,如

Java集成redis集群

spring-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

redis--(六)java操作redis

Java操作redis集群