springboot redis(單機/叢集)

來源:互聯網
上載者:User

標籤:result   bubuko   repos   jedis   mapping   tco   hash   cep   對象   

前言

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

springboot redis(單機/叢集)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.