1. 程式人生 > >SpringBoot整合Redis(單機/叢集)

SpringBoot整合Redis(單機/叢集)

前言

  前面redis弄了那麼多, 就是為了在專案中使用. 

  那這裡, 就分別來看一下, 單機版和叢集版在springboot中的使用吧.  在裡面, 我會同時貼出Jedis版, 作為比較.

單機版

1. pom.xml 

<!-- 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 

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

這裡為redis設定了一個密碼, 可以在 redis.conf 檔案中設定: requirepass 密碼

3. controller

複製程式碼

@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. Jedis

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

複製程式碼

  @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存入的資料, 讀取出來. 

這裡有一點要注意以下, 如果步驟3用的不是StringRedisTemplate, 而是RedisTemplate, 那麼通過步驟4是讀取不出來的. 

如果你裝了 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 就不需要考慮那麼多了, 確實方便許多.