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代码
- <dependency>
- <groupId>redis.clients</groupId>
- <artifactId>jedis</artifactId>
- <version>2.7.2</version>
- </dependency>
2.applicationContext.xml中的配置
Java代码
- <!-- jedis cluster config -->
- <bean name="genericObjectPoolConfig" class="org.apache.commons.pool2.impl.GenericObjectPoolConfig" >
- <property name="maxWaitMillis" value="-1" />
- <property name="maxTotal" value="1000" />
- <property name="minIdle" value="8" />
- <property name="maxIdle" value="100" />
- </bean>
- <bean id="jedisCluster" class="com.besttone.subscribe.util.JedisClusterFactory">
- <property name="addressConfig">
- <value>classpath:redis-config.properties</value>
- </property>
- <property name="addressKeyPrefix" value="address" />
- <property name="timeout" value="300000" />
- <property name="maxRedirections" value="6" />
- <property name="genericObjectPoolConfig" ref="genericObjectPoolConfig" />
- </bean>
3.JedisClusterFactory实现类
Java代码
- public class JedisClusterFactory implements FactoryBean<JedisCluster>, InitializingBean {
- private Resource addressConfig;
- private String addressKeyPrefix ;
- private JedisCluster jedisCluster;
- private Integer timeout;
- private Integer maxRedirections;
- private GenericObjectPoolConfig genericObjectPoolConfig;
- private Pattern p = Pattern.compile("^.+[:]\\d{1,5}\\s*$");
- @Override
- public JedisCluster getObject() throws Exception {
- return jedisCluster;
- }
- @Override
- public Class<? extends JedisCluster> getObjectType() {
- return (this.jedisCluster != null ? this.jedisCluster.getClass() : JedisCluster.class);
- }
- @Override
- public boolean isSingleton() {
- return true;
- }
- private Set<HostAndPort> parseHostAndPort() throws Exception {
- try {
- Properties prop = new Properties();
- prop.load(this.addressConfig.getInputStream());
- Set<HostAndPort> haps = new HashSet<HostAndPort>();
- for (Object key : prop.keySet()) {
- if (!((String) key).startsWith(addressKeyPrefix)) {
- continue;
- }
- String val = (String) prop.get(key);
- boolean isIpPort = p.matcher(val).matches();
- if (!isIpPort) {
- throw new IllegalArgumentException("ip 或 port 不合法");
- }
- String[] ipAndPort = val.split(":");
- HostAndPort hap = new HostAndPort(ipAndPort[0], Integer.parseInt(ipAndPort[1]));
- haps.add(hap);
- }
- return haps;
- } catch (IllegalArgumentException ex) {
- throw ex;
- } catch (Exception ex) {
- throw new Exception("解析 jedis 配置文件失败", ex);
- }
- }
- @Override
- public void afterPropertiesSet() throws Exception {
- Set<HostAndPort> haps = this.parseHostAndPort();
- jedisCluster = new JedisCluster(haps, timeout, maxRedirections,genericObjectPoolConfig);
- }
- public void setAddressConfig(Resource addressConfig) {
- this.addressConfig = addressConfig;
- }
- public void setTimeout(int timeout) {
- this.timeout = timeout;
- }
- public void setMaxRedirections(int maxRedirections) {
- this.maxRedirections = maxRedirections;
- }
- public void setAddressKeyPrefix(String addressKeyPrefix) {
- this.addressKeyPrefix = addressKeyPrefix;
- }
- public void setGenericObjectPoolConfig(GenericObjectPoolConfig genericObjectPoolConfig) {
- this.genericObjectPoolConfig = genericObjectPoolConfig;
- }
- }
4.redis-config.properties文件
这是一个集群环境,六个节点(不同端口),三个master ,三个slaver
Java代码
- address1=192.168.30.139:7000
- address2=192.168.30.139:7001
- address3=192.168.30.139:7002
- address4=192.168.30.139:7003
- address5=192.168.30.139:7004
- address6=192.168.30.139:7005
原文地址:https://www.cnblogs.com/lyzjavajiagou/p/9927552.html