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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM