zoukankan      html  css  js  c++  java
  • Spring Boot系列教程十:Spring boot集成Sentinel Redis

    前言

    上一篇文章介绍了spring boot集成单点的redis,然而实际生产环境使用单点的redis风险很高,一旦宕机整个服务将无法使用,这篇文章介绍如何使用基于sentinel的redis高可用方案。
    哨兵sentinel的地址如下:
    192.168.12.194:26379
    192.168.12.194:36379
    192.168.12.194:46379
     
    Redis的地址如下:
    192.168.12.194:6379
    192.168.12.194:6380
    192.168.12.194:6381
     

    实现:

    properties配置文件中添加配置信息:
     1 spring.redis.database=0
     2 spring.redis.password=123456
     3 
     4 # pool settings ...池配置  
     5 spring.redis.pool.max-idle=8  
     6 spring.redis.pool.min-idle=0  
     7 spring.redis.pool.max-active=8  
     8 spring.redis.pool.max-wait=-1 
     9 
    10 #哨兵监听redis server名称
    11 spring.redis.sentinel.master=mymaster
    12 #哨兵的配置列表
    13 spring.redis.sentinel.nodes=192.168.12.194:26379,192.168.12.194:36379,192.168.12.194:46379

    创建RedisComponent类

     1 package com.woniu.RedisComponent;
     2 
     3 import java.io.UnsupportedEncodingException;
     4 
     5 import org.springframework.beans.factory.annotation.Autowired;
     6 import org.springframework.data.redis.core.RedisTemplate;
     7 import org.springframework.data.redis.core.StringRedisTemplate;
     8 import org.springframework.data.redis.core.ValueOperations;
     9 import org.springframework.stereotype.Component;
    10 import org.springframework.stereotype.Service;
    11 
    12 import com.woniu.bean.User;
    13 
    14 
    15 @Component
    16 public class RedisComponent {
    17     
    18     @Autowired
    19     //操作字符串的template,StringRedisTemplate是RedisTemplate的一个子集
    20     private StringRedisTemplate stringRedisTemplate;
    21     
    22     @Autowired
    23     // RedisTemplate,可以进行所有的操作  
    24     private RedisTemplate<Object,Object> redisTemplate;
    25     
    26     public void set(String key, String value){
    27         ValueOperations<String, String> ops = this.stringRedisTemplate.opsForValue();
    28         boolean bExistent = this.stringRedisTemplate.hasKey(key);
    29         if (bExistent) {
    30             System.out.println("this key is bExistent!");
    31         }else{
    32             ops.set(key, value);
    33         }
    34     }
    35     
    36     public String get(String key){
    37         return this.stringRedisTemplate.opsForValue().get(key);
    38     }
    39     
    40     public void del(String key){
    41         this.stringRedisTemplate.delete(key);
    42     }
    43     
    44     public void sentinelSet(User user){
    45         String key = null;
    46         try {
    47             key = new String(user.getId().getBytes("gbk"),"utf-8");
    48         } catch (UnsupportedEncodingException e) {
    49             // TODO Auto-generated catch block
    50             e.printStackTrace();
    51         }
    52 
    53         System.out.println(key);
    54         redisTemplate.opsForValue().set(key, user.toString());
    55     }
    56     
    57     public String sentinelGet(String key){
    58         return stringRedisTemplate.opsForValue().get(key);
    59     }
    60 }
    添加测试类的测试代码
     
     1 package com.woniu;
     2 
     3 import org.junit.Test;
     4 import org.junit.runner.RunWith;
     5 import org.springframework.beans.factory.annotation.Autowired;
     6 import org.springframework.boot.test.context.SpringBootTest;
     7 import org.springframework.test.context.junit4.SpringRunner;
     8 
     9 import com.woniu.RedisComponent.RedisComponent;
    10 import com.woniu.bean.User;
    11 
    12 @RunWith(SpringRunner.class)
    13 @SpringBootTest
    14 public class SpringbootSentinelredisApplicationTests {
    15 
    16     @Autowired
    17     private RedisComponent redisComponet;
    18     
    19     @Test
    20     public void sentinelSet(){
    21         User user = new User();
    22         user.setId("001");
    23         user.setAge("30");
    24         user.setName("wangpengfei");
    25         
    26         redisComponet.sentinelSet(user);
    27     }
    28 
    29     @Test
    30     public void sentinelGet(){
    31         String str = redisComponet.sentinelGet("001");
    32         System.out.println(str);
    33     }
    34 }
     
  • 相关阅读:
    Android——ArrayList 、LinkList、List 区别 & 迭代器iterator的使用 & HashMap、Hashtable、LinkedHashMap、TreeMap
    Android--List与ArrayList区别(转)
    android——inflater 用法(转)
    Android——列表视图 ListView(二)SimpleAdapter
    Odoo 仓库扫码打包方案
    Odoo 仓库调拨移动过程中 单位不允许错误的分析及解决方案
    Ubuntu FTP 配置
    attrs 中的 uid
    GitLab安装手记
    [转载]How To Add Swap on Ubuntu 12.04
  • 原文地址:https://www.cnblogs.com/wdpnodecodes/p/7406976.html
Copyright © 2011-2022 走看看