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
1:下載redis。官網(wǎng)下載3.0.0版本,之前2.幾的版本不支持集群模式 (服務(wù)器上部署的是3.0.3)下載地址:https://github.com/antirez/redis/archive/3.0.0-rc2.tar.gz
2:上傳服務(wù)器,解壓,編譯
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:創(chuàng)建集群需要的目錄
?
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
##修改配置文件中的下面選項(xiàng)
port?7000
daemonize?yes
cluster-enabled?yes cluster-config-file?nodes.conf 這邊官方是說這樣子配置,但是最好是cluster-config-file?nodes-7000.conf 這樣子的,就是nodes-端口號(hào).conf的形式cluster-node-timeout?5000
appendonly?yes 最后配置好log的位置?logfile "/data/logs/redis.7000.log" 我是用端口號(hào)來區(qū)分,##修改完redis.conf配置文件中的這些配置項(xiàng)之后把這個(gè)配置文件分別拷貝到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參數(shù),分別改為對(duì)應(yīng)的文件夾的名稱
如果要添加監(jiān)聽sentinel.conf,方法和添加redis.conf類似? 修改的參數(shù)為 第一個(gè):port 27000 即原有的port前加個(gè)2 第二個(gè):sentinel monitor mymaster 192.168.1.223 7000 2 5:分別啟動(dòng)這6個(gè)redis實(shí)例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
?
?
##啟動(dòng)之后使用命令查看redis的啟動(dòng)情況ps?-ef|grep?redis
?
6:執(zhí)行redis的創(chuàng)建集群命令創(chuàng)建集群
| 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測(cè)試的時(shí)候,顯示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)(這個(gè)才是正確的) |
6.1執(zhí)行上面的命令的時(shí)候會(huì)報(bào)錯(cuò),因?yàn)槭菆?zhí)行的ruby的腳本,需要ruby的環(huán)境
錯(cuò)誤內(nèi)容:/usr/bin/env:?ruby:?No?such?file?or?directory
所以需要安裝ruby的環(huán)境,這里推薦使用yum?install?ruby安裝
yum install ruby
6.2然后再執(zhí)行第6步的創(chuàng)建集群命令,還會(huì)報(bào)錯(cuò),提示缺少rubygems組件,使用yum安裝如果這個(gè)時(shí)候報(bào)不能從某個(gè)IP上下載包,類似的先執(zhí)行一下這個(gè)代碼 ? sed -i 's/192.168.1.127/172.20.1.11/g' /etc/yum.repos.d/*
?錯(cuò)誤內(nèi)容:
./redis-trib.rb:24:in?`require':?no?such?file?to?load?--?rubygems?(LoadError)
from?./redis-trib.rb:24
yum?install?rubygems
錯(cuò)誤內(nèi)容:6.3再次執(zhí)行第6步的命令,還會(huì)報(bào)錯(cuò),提示不能加載redis,是因?yàn)槿鄙賠edis和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?再次執(zhí)行第6步的命令,正常執(zhí)行?
輸入yes,然后配置完成。
至此redis集群即搭建成功!
7:使用redis-cli命令進(jìn)入集群環(huán)境
redis-cli -c -p 7000
利用cluster info 查看狀態(tài)
8:如果外部訪問不了,而內(nèi)部可以訪問 處理方法一:查看iptables是否開啟端口號(hào),未打開的話,在/etc/sysconfig 中的 iptables中配置一下 處理方法二: 直接關(guān)閉?service iptables stop 其次是整合到項(xiàng)目中。springmvc框架。 第一 pom.xml 文件:<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");
轉(zhuǎn)載于:https://www.cnblogs.com/hellozli/p/5206861.html
總結(jié)
以上是生活随笔為你收集整理的redis集群安装和java应用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 从 Android 静音看正确的查找 b
- 下一篇: ThinkPHP分页实例