Spring整合redis,通過sentinel進行主從切換
實現功能描述:
redis伺服器進行Master-slaver-slaver-....主從配置,通過2臺sentinel進行failOver故障轉移,自動切換,採用該程式碼完全可以直接用於實際生產環境。
題外話:
一般來說這樣的部署足以支援數以百萬級的使用者,但如果數量實在是太高,此時redis的Master-Slaver主從不一定能夠滿足,因此進行redis的分片。
本文不講解redis的分片,但如果你使用了,需要注意的按照另一篇文章的介紹:Sentinel&Jedis看上去是個完美的解決方案,這句話只說對了一半,
在無分片的情況是這樣,但我們的應用使用了資料分片-sharing,資料被平均分佈到4個不同的例項上,每個例項以主從結構部署,Jedis沒有提供
基於Sentinel的ShardedJedisPool,也就是說在4個分片中,如果其中一個分片發生主從切換,應用所使用的ShardedJedisPool無法獲得通知,所有
該程式碼模擬多執行緒向redis中set/get。
1、maven依賴配置
- <dependency>
-
<groupId>org.springframework.data
- <artifactId>spring-data-redis</artifactId>
- <version>1.4.1.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>redis.clients</groupId>
- <artifactId>jedis</artifactId>
-
<version
- </dependency>
- <dependency>
- <groupId>org.apache.commons</groupId>
- <artifactId>commons-lang3</artifactId>
- <version>3.3.2</version>
- </dependency>
2、redis.properties
- # Redis settings
- #sentinel1的IP和埠
- im.hs.server.redis.sentinel1.host=192.168.62.154
- im.hs.server.redis.sentinel1.port=26379
- #sentinel2的IP和埠
- im.hs.server.redis.sentinel2.host=192.168.62.153
- im.hs.server.redis.sentinel2.port=26379
- #sentinel的鑑權密碼
- im.hs.server.redis.sentinel.masterName=155Master
- im.hs.server.redis.sentinel.password=hezhixiong
- #最大閒置連線數
- im.hs.server.redis.maxIdle=500
- #最大連線數,超過此連線時操作redis會報錯
- im.hs.server.redis.maxTotal=5000
- im.hs.server.redis.maxWaitTime=1000
- im.hs.server.redis.testOnBorrow=true
- #最小閒置連線數,spring啟動的時候自動建立該數目的連線供應用程式使用,不夠的時候會申請。
- im.hs.server.redis.minIdle=300
3、spring-redis.xml
- <?xmlversion="1.0"encoding="UTF-8"?>
- <beansxmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:jee="http://www.springframework.org/schema/jee"xmlns:tx="http://www.springframework.org/schema/tx"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
- <!-- Spring自動將該包目錄下標記為@Service的所有類作為spring的Bean -->
- <context:component-scanbase-package="com.gaojiasoft.test.redis"/>
- <context:property-placeholderlocation="classpath:conf/redis/redis.properties"/>
- <beanid="poolConfig"class="redis.clients.jedis.JedisPoolConfig">
- <propertyname="maxTotal"value="${im.hs.server.redis.maxTotal}"/>
- <propertyname="minIdle"value="${im.hs.server.redis.minIdle}"/>
- <propertyname="maxWaitMillis"value="${im.hs.server.redis.maxWaitTime}"/>
- <propertyname="maxIdle"value="${im.hs.server.redis.maxIdle}"/>
- <propertyname="testOnBorrow"value="${im.hs.server.redis.testOnBorrow}"/>
- <propertyname="testOnReturn"value="true"/>
- <propertyname="testWhileIdle"value="true"/>
- </bean>
- <beanid="sentinelConfiguration"
- class="org.springframework.data.redis.connection.RedisSentinelConfiguration">
- <propertyname="master">
- <beanclass="org.springframework.data.redis.connection.RedisNode">
- <propertyname="name"value="${im.hs.server.redis.sentinel.masterName}"></property>
- </bean>
- </property>
- <propertyname="sentinels">
- <set>
- <beanclass="org.springframework.data.redis.connection.RedisNode">
-
相關推薦
Spring整合redis,通過sentinel進行主從切換。(何志雄)
實現功能描述: redis伺服器進行Master-slaver-slaver-....主從配置,通過2臺sentinel進行failOver故障轉移,自動切換,採用該程式碼完全可以直接用於實際生產環境。 題外話: 一般來說這樣的部署
Spring整合redis,通過sentinel進行主從切換
實現功能描述: redis伺服器進行Master-slaver-slaver-....主從配置,通過2臺sentinel進行failOver故障轉移,自動切換,採用該程式碼完全可以直接用於實際生產環境。 題外話:
11、Spring技術棧-整合Redis,通過Redis的Master-Slave實現快取資料讀寫分離
1、Redis主從複製(Master-Salve Replication)簡介 Redis 支援簡單且易用的主從複製(master-slave replication)功能, 該功能可以讓從伺服器(slave server)成為主伺服器(master serv
spring 整合 redis,以及spring的RedisTemplate如何使用
需要的jar包 spring-data-redis-1.6.2.RELEASE.jar jedis-2.7.2.jar(依賴 commons-pool2-2.3.jar) commons-poo
Redis的哨兵模式第一次主從切換成功,再次進行主從切換就不行了,怎麼破?
第一次主掛掉,將從切為主成功;然後重啟掛掉的主,哨兵提示被識別為從;然後再次關閉現在的主,從庫切換就失敗了,一直提示no good slave,請問要如何設定,讓第二次主從也能順利切換? 我在cen
Redis 和 spring整合時,Jar包要匹配
<dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-red
Spring整合Redis快取,提高查詢效率
整合redis快取後可以將資料庫的查詢介面,序列化到redis中,key就是註解中的引數,例如@Cacheable(“findUsers”): 存在redis中的key就是findUsers。快取了這個結果之後再次請求這個方法就不會去資料庫中查,而是從redis快取中讀取資料,這樣就減少
spring整合redis(叢集、主從)
使用maven整合 整合環境ssm: spring 4.2.6.RELEASE Mybatis 3.2.6 注意事項: spring、redis和spring-redis的版本,開始用spring4.0整合不成功,使用4.2整合成功 pom.xml檔案: <
Spring Boot整合redis,key自定義生成
redis key生成策略程式碼: import org.springframework.cache.CacheManager; import org.springframework.cache.annotation.CachingConfigurerSupport; im
spring boot 整合 redis,使用@Cacheable,@CacheEvict,@CachePut,jedisPool操作redis資料庫
好久沒寫文章了,最近換了個公司,入職差不多一個半月了,接觸了不少沒玩過的新東西,這裡放個 spring boot 整合 redis的demo吧。 先看一下demo目錄: 如何建立spring boot專案我就不說了很簡單,不會百度一大把。 先看一下p
springboot整合redis,用spring-data-redis2.0的同時用jedis
最新版本的spring-data-redis很煩,很多以前用起來很方便的方法過時了。下面是2.0這個版本的搭建過程:首先要了解這個版本 (1)spring-data-redis是預設是用lettuce的,要用jedis需要單獨出來配置 (2)Jed
spring,springmvc,mybatis整合redis,redis作為快取使用
環境 1,windows7 2,mysql 3,eclipse 4,redis 5,tomcat7 注意:啟動redis的方式,已經把redis做成windows服務,以windows服務的方式啟動 把redis做成windows服務的命令列 redis-s
spring整合redis
data sch throws ack xid .org source protect pid 1,利用spring-data-redis整合 項目使用的pom.xml: <project xmlns="http://maven.apache.org/POM/4.0
Spring整合Redis做數據緩存(Windows環境)
端口號 init 技術分享 factory redis-cli @service tab long 配置 當我們一個項目的數據量很大的時候,就需要做一些緩存機制來減輕數據庫的壓力,提升應用程序的性能,對於java項目來說,最常用的緩存組件有Redis、Ehcache和Mem
Spring整合Redis(spring-data-redis)
nds 獲取 可能 div 普通 工具 long red 等待 歷經幾天看了大量的博客資料,差不多算是搞定了,目前只是針對單個數據源,集群暫時沒研究 maven依賴 <properties> <!-- redis 版本 --> &l
關於頁面元素不能定位,通過Js進行處理
pass timeunit nds def 行處理 static second tex new public class Test { public static void main(String[] args) throws Exception { File file
網站性能優化小結和spring整合redis
remove utf ota turn tor package process 基本屬性 version 現在越來越多的地方需要非關系型數據庫了,最近網站優化,當然從頁面到服務器做了相應的優化後,通過在線網站測試工具與之前沒優化對比,發現有顯著提升。 服務器優化目前主要優化
訪問dubbo沒有權限,通過ip進行跳轉服務器,並通過有權限服務器代理訪問
service _for dnat asq tro -- 編譯 tar conf #啟動ip跳轉 echo 1 > /proc/sys/net/ipv4/ip_forward vi /etc/sysctl.conf net.ipv4.ip_forward =1
spring 整合 redis的配置
index row val return created log idea pass truct 廢話不說,直接上代碼: 1、先添加依賴包 <!--redis--> <dependency>
springboot2整合redis,並自定義@Cache
1.maven依賴 <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId>