redis集群安装和java应用
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了redis集群安装和java应用相关的知识,希望对你有一定的参考价值。
127.0.0.1:7001
127.0.0.1:7002
127.0.0.1:7003
127.0.0.1:7004
127.0.0.1:7005
下载地址:https://github.com/antirez/redis/archive/3.0.0-rc2.tar.gz
2:上传服务器,解压,编译
tar -zxvf redis-3.0.0-rc2.tar.gz
mv redis-3.0.0-rc2.tar.gz redis3.0
cd /usr/local/redis3.0
make
make install
3:创建集群需要的目录
mkdir -p /usr.local/cluster
cd /usr.local/cluster
mkdir 7000
mkdir 7001
mkdir 7002
mkdir 7003
mkdir 7004
mkdir 7005
4:修改配置文件redis.conf
cp /usr/local/redis3.0/redis.conf /usr.local/cluster
vi redis.conf
##修改配置文件中的下面选项
port 7000
daemonize yes
cluster-node-timeout 5000
##修改完redis.conf配置文件中的这些配置项之后把这个配置文件分别拷贝到7000/7001/7002/7003/7004/7005目录下面
cp /usr/local/cluster/redis.conf /usr/local/cluster/7000
cp /usr/local/cluster/redis.conf /usr/local/cluster/7001
cp /usr/local/cluster/redis.conf /usr/local/cluster/7002
cp /usr/local/cluster/redis.conf /usr/local/cluster/7003
cp /usr/local/cluster/redis.conf /usr/local/cluster/7004
cp /usr/local/cluster/redis.conf /usr/local/cluster/7005
##注意:拷贝完成之后要修改7001/7002/7003/7004/7005目录下面redis.conf文件中的port参数,分别改为对应的文件夹的名称
cd /usr/local/cluster/7000
redis-server redis.conf
cd /usr/local/cluster/7001
redis-server redis.conf
cd /usr/local/cluster/7002
redis-server redis.conf
cd /usr/local/cluster/7003
redis-server redis.conf
cd /usr/local/cluster/7004
redis-server redis.conf
cd /usr/local/cluster/7005
redis-server redis.conf
##启动之后使用命令查看redis的启动情况ps -ef|grep redis
6:执行redis的创建集群命令创建集群
cd /usr/local/redis3.0/src
./redis-trib.rb create --replicas 1 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005(这里java测试的时候,显示too many redirection,解决把127.0.0.1改为ip,./redis-trib.rb create --replicas 1 192.168.1.223:7000 192.168.1.223:7001 192.168.1.223:7002 192.168.1.223:7003 192.168.1.223:7004 192.168.1.223:7005)(这个才是正确的)
|
6.1执行上面的命令的时候会报错,因为是执行的ruby的脚本,需要ruby的环境
错误内容:/usr/bin/env: ruby: No such file or directory
所以需要安装ruby的环境,这里推荐使用yum install ruby安装
yum install ruby
6.2然后再执行第6步的创建集群命令,还会报错,提示缺少rubygems组件,使用yum安装如果这个时候报不能从某个IP上下载包,类似的先执行一下这个代码 sed -i ‘s/192.168.1.127/172.20.1.11/g‘ /etc/yum.repos.d/*
错误内容:
./redis-trib.rb:24:in `require‘: no such file to load -- rubygems (LoadError)
from ./redis-trib.rb:24
yum install rubygems
错误内容:6.3再次执行第6步的命令,还会报错,提示不能加载redis,是因为缺少redis和ruby的接口,使用gem 安装
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require‘: no such file to load -- redis (LoadError)
from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require‘
from ./redis-trib.rb:25
gem install redis
6.4 再次执行第6步的命令,正常执行
输入yes,然后配置完成。
至此redis集群即搭建成功!
7:使用redis-cli命令进入集群环境
redis-cli -c -p 7000
利用cluster info 查看状态
<spring.version>4.2.3.RELEASE</spring.version> 要4.1以上的版本才支持 spring-data-redis 1.7.0的
<jackson.version>2.6.4</jackson.version> 支持spring4.2.3
<jackson-dataformat-xml.version>2.6.3</jackson-dataformat-xml.version>支持spring4.2.3
<jedis.version>2.8.0</jedis.version> 目前支持redis集群比较新的版本 (https://github.com/xetorthio/jedis)
<springdataredis.version>1.7.0.M1</springdataredis.version> 支持redis集群(http://projects.spring.io/spring-data-redis/)
第二 applicationContext-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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<constructor-arg name="clusterConfig" ref="redisClusterConfiguration"></constructor-arg>
</bean>
<bean id="clusterRedisNodes1" class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg value="${redis.ip}" />
<constructor-arg value="${redis.port}" type="int" />
</bean>
<!--setter方式注入-->
<bean id="redisClusterConfiguration" class="org.springframework.data.redis.connection.RedisClusterConfiguration">
<property name="clusterNodes">
<set>
<ref bean="clusterRedisNodes1"/>
</set>
</property>
</bean>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory"></property>
<property name="keySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
<property name="hashKeySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
<property name="valueSerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
<property name="hashValueSerializer">
<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
</property>
</bean>
</beans>
第三
在类中
@Autowired
protected RedisTemplate redisTemplate;
然后 redisTemplate.opsForValue().set("foo", "test");
以上是关于redis集群安装和java应用的主要内容,如果未能解决你的问题,请参考以下文章
Redis单节点安装主从复制java连接redis和Redis集群安装
Redis单节点安装主从复制java连接redis和Redis集群安装