精品专区-精品自拍9-精品自拍三级乱伦-精品自拍视频-精品自拍视频曝光-精品自拍小视频

網站建設資訊

NEWS

網站建設資訊

Spring-data-redis如何操作rediscluster

小編給大家分享一下Spring-data-redis如何操作redis cluster,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!

公司主營業務:成都網站制作、網站建設、外貿網站建設、移動網站開發等業務。幫助企業客戶真正實現互聯網宣傳,提高企業的競爭能力。創新互聯是一支青春激揚、勤奮敬業、活力青春激揚、勤奮敬業、活力澎湃、和諧高效的團隊。公司秉承以“開放、自由、嚴謹、自律”為核心的企業文化,感謝他們對我們的高要求,感謝他們從不同領域給我們帶來的挑戰,讓我們激情的團隊有機會用頭腦與智慧不斷的給客戶帶來驚喜。創新互聯推出夏縣免費做網站回饋大家。

一、利用Jedis來實現

通過Jedis操作Redis Cluster的模型可以參考Redis官網,具體如下:

Set jedisClusterNodes = new HashSet();

     //Jedis Cluster will attempt to discover cluster nodes automatically

     jedisClusterNodes.add(new HostAndPort("10.96.5.183",9001));

     jedisClusterNodes.add(new HostAndPort("10.96.5.183",9002));

     jedisClusterNodes.add(new HostAndPort("10.96.5.183",9003));

    JedisCluster jc = new JedisCluster(jedisClusterNodes);

    jc.set("foo","bar");

    jc.get("foo");

二、利用spring-data-redis來實現

目前spring-data-redis已發布的主干版本都不能很好的支持Redis Cluster的新特性。為了解決此問題spring-data-redis開源項目組單獨拉了一個315分支,但截止到目前尚未發布。下面在分析spring-data-redis源碼的基礎上配置spring實現操作Redis Cluster.下面分別針對XML和注入的方式進行說明。

315分支gitHub下載路徑如下:https://github.com/spring-projects/spring-data-redis

315分支源碼下載路徑:http://maven.springframework.org/snapshot/org/springframework/data/spring-data-redis/1.7.0.DATAREDIS-315-SNAPSHOT/

(1)采用setClusterNodes屬性方式構造RedisClusterConfiguration

代碼目錄結構如下所示:

 src
       com.example.bean 
       com.example.repo
       com.example.repo.impl
       resources

A.在resources目錄下增加spring-config.xml配置,配置如下:

   

    

      

      

   

   ....

  

  

     

       

            

            

            

       

    

   



    

  

 

  

      

      

      

  

 

      

      

 

 





    

注:加載lua文件



  

 

在com.example.repo.impl下增加PersonRepoImpl,主要包括屬性private RedisTemplate  redisTemplate(該屬性存在setterXXX方法,對應property屬性);

利用redisTemplate.opsForHash().put()即可完成對Redis Cluster的操作。

 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new ClassPathResource("resources/spring-config.xml").getPath());

 Repo repo =(Repo)context.getBean("personRepo");

(2)采用RedisClusterConfiguration(PropertySource propertySource)方式構造RedisClusterConfiguration

  代碼目錄結構如下所示:

 src
       com.redis.cluster.support.config
            MonitorConfig
       resources
           spring-config.xml
            redis.properties

A.在resources目錄下增加spring-config.xml配置,配置如下:

  

   

  

   

  

   

   

   

B.添加redis.properties文件

spring.redis.cluster.nodes=10.48.193.201:7389,10.48.193.201:7388
spring.redis.cluster.timeout=2000
spring.redis.cluster.max-redirects=8

C.編寫初始化JedisConnectionFactory連接工廠的java類

  @Configuration

  public class MonitorConfig {

    @Value("${spring.redis.cluster.nodes}")

    private String clusterNodes;

    @Value("${spring.redis.cluster.timeout}")

    private Long timeout;

   @Value("${spring.redis.cluster.max-redirects}")

    private int redirects;

    @Bean

    public RedisClusterConfiguration getClusterConfiguration() {

      Map source = new HashMap();

      source.put("spring.redis.cluster.nodes", clusterNodes);

      source.put("spring.redis.cluster.timeout", timeout);

      source.put("spring.redis.cluster.max-redirects", redirects);

      return new RedisClusterConfiguration(new MapPropertySource("RedisClusterConfiguration", source));

     }

    @Bean

    public JedisConnectionFactory getConnectionFactory() {

      return new JedisConnectionFactory(getClusterConfiguration());

     }

   @Bean

    public JedisClusterConnection getJedisClusterConnection() {

      return (JedisClusterConnection) getConnectionFactory().getConnection();

     }

    @Bean

    public RedisTemplate getRedisTemplate() {

      RedisTemplate clusterTemplate = new RedisTemplate();

      clusterTemplate.setConnectionFactory(getConnectionFactory());

      clusterTemplate.setKeySerializer(new DefaultKeySerializer());

      clusterTemplate.setDefaultSerializer(new GenericJackson2JsonRedisSerializer());

      return clusterTemplate;

     }

    }

D.通過注解方式使用JedisClusterConnection和RedisTemplate

@Autowired

    JedisClusterConnection clusterConnection;

   @Autowired

   RedisTemplate redisTemplate;

三、簡單集成Spring

自己編寫jedisCluster的工廠類JedisClusterFactory,然后通過Spring注入的方式獲取jedisCluster,實現客戶端使用Redis3.0版本的集群特性。

請參考:https://www.jb51.net/article/128895.htm

使用時,直接通過注解或者XML注入即可,如下所示:

   @Autowired
   JedisCluster jedisCluster;

  或者

  

    

  
 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new ClassPathResource("resources/spring-config.xml").getPath());

 TestRedis testRedis=(TestRedis)context.getBean("testRedis");

四、Redis Cluster調試中常見錯誤

(1)當客戶端與集群服務器不在同一臺服務器上時,有如下錯誤Could not get a resource from the Cluster

一般當客戶端與集群服務器在同一臺服務器上時,操作Redis Cluster正常; 當二者不在同一臺服務器上時報如上錯誤,可能是clusterTimeOut時間設置過小;

(2)操作Redis時報Too many cluster redirections

初始化JedisCluster時,設定JedisCluster的maxRedirections.

JedisCluster(Set jedisClusterNode, int timeout, int maxRedirections) ;
JedisCluster jc = new JedisCluster(jedisClusterNodes,5000,1000);

(3)Redis Cluster數據寫入慢

檢查在通過./redis-trib命令建立集群時,如果是通過127.0.0.1的方式建立的集群,那么在往Redis Cluster中寫入數據時寫入速度比較慢。可以通過配置真實的IP來規避此問題。

看完了這篇文章,相信你對“Spring-data-redis如何操作redis cluster”有了一定的了解,如果想了解更多相關知識,歡迎關注創新互聯行業資訊頻道,感謝各位的閱讀!


網頁標題:Spring-data-redis如何操作rediscluster
分享鏈接:http://m.jcarcd.cn/article/iggcoo.html
主站蜘蛛池模板: 国产人成亚 | 国产乱妇乱子在 | 日本成人免费观看 | 三区在线观看 | 欧美日韩中文国 | 国精品99久9在线 | 日韩精品在线第一页 | 日本到成人免费网站 | 韩国主播| 92午夜福利影院 | 国产亚洲成aⅴ | 成人视屏一区 | 国产日本韩国视频 | 国产综合精品国 | 国产精品久免 | 91人人澡人人| 久涩导航 | 日本在线免费 | 精品一级无| 国产白虎不卡在线 | 国产一区自拍视频 | 日韩a级片视频 | 国产白丝在线 | 人妖二区 | 91午夜福利电影 | 成人一区视频入口 | 国产亚洲观看日韩 | 国产精品00| 日本高清色 | 韩国床戏激情戏裸戏 | 国产性色惰视频 | 国产对白合 | 国产一区二区三区四 | 日韩高清专区 | 午夜理论剧 | 果冻剧精品传媒入口 | 国产剧视频在线播放 | 国产日韩欧美高清 | 国产精国产精品 | 日本亚欧在线观看 | 国产高清在线看 |