springboot redis(單機/集羣)

前言

  前面redis弄了那麼多, 就是爲了在項目中使用. node

  那這裏, 就分別來看一下, 單機版和集羣版在springboot中的使用吧.  在裏面, 我會同時貼出Jedis版, 做爲比較.redis

  

單機版

1. pom.xml spring

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-redis -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
    <version>1.5.9.RELEASE</version>
</dependency>

 

2. application.yml springboot

spring:
  redis:
    port: 6379
    host: 127.0.0.1
    password: redis

這裏爲redis設置了一個密碼, 能夠在 redis.conf 文件中設置: requirepass 密碼服務器

 

3. controllerapp

@RestController
@RequestMapping("simple")
public class SimpleController {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @GetMapping("set")
    public void set(){

        ValueOperations<String, String> operations = stringRedisTemplate.opsForValue();
        operations.set("1", "1a");
        operations.set("2", "2b");
        operations.set("3", "3c");
        operations.set("4", "4d");
        operations.set("5", "5e");
        operations.set("elvin", "elvin");
        operations.set("abc", "abc");
        operations.set("xingming", "xingming");
    }
}

 

4. Jedisspring-boot

來看一下單機版redis下, Jedis是怎麼玩的.ui

  @Test
    public void testJedisPool() throws Exception {
        // 第一步:建立一個JedisPool對象。須要指定服務端的ip及端口。
        JedisPool jedisPool = new JedisPool("127.0.0.1", 6379);
        // 第二步:從JedisPool中得到Jedis對象。
        Jedis jedis = jedisPool.getResource();
        jedis.auth("redis");
        // 第三步:使用Jedis操做redis服務器。
        String result = jedis.get("abc");
        System.out.println(result);
        // 第四步:操做完畢後關閉jedis對象,鏈接池回收資源。
        jedis.close();
        // 第五步:關閉JedisPool對象。
        jedisPool.close();
    }

結果我就不展現了, 經過以上步驟, 能把controller存入的數據, 讀取出來. spa

這裏有一點要注意如下, 若是步驟3用的不是StringRedisTemplate, 而是RedisTemplate, 那麼經過步驟4是讀取不出來的. code

若是你裝了 redis desktop manager , 可使用這個去看一下, 就會知道爲啥讀不出來. 

具體爲啥會產生這樣的狀況呢?

能夠看一下RedisTemplate的源碼:

看得出來, 這裏使用了 JdkSerializationRedisSerializer 來序列化 key 和 value.

直觀點的話, 能夠看下圖:

 

so, 這裏就能看出來, 爲啥用abc直接去查, 是查不到想要的結果的.

 

集羣版

 在集羣裏面, 若是你使用的是 spring-boot-starter-data-redis 的話, 就會發現, 超方便, 只要改一下配置文件就能夠了, 其餘的均可以不改.

1. application.yml

spring:
  redis:
    cluster:
      nodes:
       - 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
       - 127.0.0.1:7006
    password: 123456

在application裏面配置集羣節點.

 

2. controller

controller裏面的方法不變, 仍是用那個. 直接用 Terminal 操做查看:

確實存進去了. 

 

3. Jedis

  @Test
    public void testJedisCluster() throws Exception {
        // 第一步:使用JedisCluster對象。須要一個Set<HostAndPort>參數。Redis節點的列表。
        Set<HostAndPort> nodes = new HashSet<>();
        nodes.add(new HostAndPort("127.0.0.1", 7001));
        nodes.add(new HostAndPort("127.0.0.1", 7002));
        nodes.add(new HostAndPort("127.0.0.1", 7003));
        nodes.add(new HostAndPort("127.0.0.1", 7004));
        nodes.add(new HostAndPort("127.0.0.1", 7005));
        nodes.add(new HostAndPort("127.0.0.1", 7006));
        JedisCluster jedisCluster = new JedisCluster(nodes, 2000, 5, 8, "123456", new GenericObjectPoolConfig());
        // 第二步:直接使用JedisCluster對象操做redis。在系統中單例存在。
        String result = jedisCluster.get("abc");
        // 第三步:打印結果
        System.out.println(result);
        // 第四步:系統關閉前,關閉JedisCluster對象。
        jedisCluster.close();
    }

這裏有個比較蛋疼的事情就是, 若是集羣設置了密碼, 並不能經過jedisCluster.auth()方式來輸入密碼

剛開始, 我還覺得這是不推薦使用的, 誰知道, 這tm是不能用啊. 過度了, 簡直.

經過Jedis的代碼, 能夠發現, 單機版和集羣版, 操做的對象是不同的, 那麼在開發的過程當中, 怎麼來統一呢?(開發的時候, 不須要使用redis集羣, 上線的時候, 直接切換過去就能夠了)

那麼想要解決這個問題, 能夠經過策略模式來解決, 定義一個操做接口, 在接口中定義方法, 我管你單機仍是集羣, 都要來實現這個接口. 那麼在操做的過程當中, 就統一到接口了. 剩下來的就是賦值和切換了.

而使用  spring-boot-starter-data-redis 就不須要考慮那麼多了, 確實方便許多.

相關文章
相關標籤/搜索