1. 程式人生 > >深入理解Redis系列之SpringBoot整合Redis

深入理解Redis系列之SpringBoot整合Redis

SpringBoot環境

快速搭建一個SpringBoot工程

進入 https://start.spring.io 網站, 使用該網站初始化一個SpringBoot工程

1706159233-5bdc6766067a1.png

新增相關依賴

因為使用spring initializer已經幫我們把Redis的依賴建立好了; 但是由於我們要使用Jedis客戶端訪問Redis, 所以還需要新增Jedis的依賴;

     <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version> //版本號可以放在properties中作為屬性, 這邊用${jedis.version}來依賴     </dependency>

配置Redis節點資訊

開啟application.properties檔案, 初始化的檔案是空的; 我們將spring redis最基本的資訊加入進去

spring.redis.host=localhost
spring.redis.port=6379

將Redis資訊讀入到程式中

新建一個Java類命名為StandaloneRedisConfig.java, 放在com.xxx.example.config包下

package com.terrylmay.redis.example.config;import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.context.annotation.Configuration;@
[email protected]
(prefix = "spring.redis")@ConditionalOnProperty(name = {"spring.redis.host"})public class StandaloneRedisConfig {     String host;    int port;    public String getHost() {        return host;     }    public void setHost(String host) {        this.host = host;     }    public int getPort() {        return port;     }    public void setPort(int port) {        this.port = port;     } }

上面配置中的@ConditionalOnProperty(name = {"spring.redis.host"}) 如果只是單機的Redis則不需要新增該屬性; 但是為了後面一套程式碼相容多個Redis部署模式, 使用該屬性作為是否建立Bean的條件; 如果是叢集模式那麼就不會使用spring.redis.host來作為連線字串了;

配置Jedis的連線池

將Redis連線物件放入到Spring容器中進行管理

package com.terrylmay.redis.example;import com.terrylmay.redis.example.config.StandaloneRedisConfig;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;import org.springframework.context.annotation.Bean;import org.springframework.data.redis.connection.RedisConnectionFactory;import org.springframework.data.redis.connection.RedisStandaloneConfiguration;import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;import org.springframework.data.redis.core.StringRedisTemplate;@SpringBootApplication(scanBasePackages = {"com.terrylmay.redis.example"})public class RedisExampleApplication {    public static void main(String[] args) {
        SpringApplication.run(RedisExampleApplication.class, args);
    }    @Autowired
    StandaloneRedisConfig standaloneRedisConfig;    @Autowired
    RedisConnectionFactory redisConnectionFactory;    @Bean
    @ConditionalOnBean(value = {StandaloneRedisConfig.class})
    public RedisConnectionFactory standaloneRedisConnectionFactory() {
        JedisConnectionFactory factory = new JedisConnectionFactory(new RedisStandaloneConfiguration(standaloneRedisConfig.getHost(), standaloneRedisConfig.getPort()));        return factory;
    }    @Bean
    public StringRedisTemplate stringRedisTemplate() {        return new StringRedisTemplate(redisConnectionFactory);
    }
}

這裡的@ConditionalOnBean(value = {StandaloneRedisConfig.class})與上面的ConditionalOnProperty 是一個道理

這裡的scanBasePackages = {"com.terrylmay.redis.example"} 是為了以後將Redis的客戶端獨立出一個工程而做的, 當然獨立出來的工程base包名還要是這個才可以;

因為還沒有看Redis支援的資料結構, 那麼現在只是把Redis字串模板類放到Spring 容器中, 後續再增加其他資料型別的支援;

建立操作Redis的介面 以及實現

建立ICacheProvider.java介面:

package com.terrylmay.redis.example.provider;public interface ICacheProvider {    void setString(String key, String value);    String getString(String key);
}

Jedis版本的實現:

package com.terrylmay.redis.example.provider.impl;import com.terrylmay.redis.example.provider.ICacheProvider;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.core.StringRedisTemplate;import org.springframework.stereotype.Component;@Componentpublic class JedisCacheProvider implements ICacheProvider {    @Autowired
    StringRedisTemplate stringRedisTemplate;    @Override
    public void setString(String key, String value) {
        stringRedisTemplate.opsForValue().set(key, value);
    }    @Override
    public String getString(String key) {        return stringRedisTemplate.opsForValue().get(key);
    }
}

這樣基本上一個可以操作Redis的Java程式就已經就緒了; 那麼我們需要驗證一下, 當然如果在主工程中寫一個類去驗證也是沒有問題的, 比如建立一個Bean, 並且放到被PostContruct註解的方法裡面;

但是更加專業的做法是寫一個測試程式來測試, 下面看一下該測試程式應該怎麼寫

UT測試程式可用性

因為建立工程的時候, 就已經有一個測試類在test目錄下面了, 我們增加我們想要的功能

package com.terrylmay.redis.example;import com.terrylmay.redis.example.provider.ICacheProvider;import org.junit.Assert;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringJUnit4Cla***unner;@RunWith(SpringJUnit4Cla***unner.class)@SpringBootTest(classes = {RedisExampleApplication.class})public class RedisExampleApplicationTests {    @Autowired
    ICacheProvider jedisCacheProvider;    @Test
    public void contextLoads() {
        jedisCacheProvider.setString("name", "terrylmay");
        System.out.println(jedisCacheProvider.getString("name")); 
        Assert.assertEquals("terrylmay", jedisCacheProvider.getString("name"));
    }
}

注: 程式中不要有列印, 使用Logger或者直接斷言來處理 (本來想用markdown語法來標紅的, 但是發現簡書竟然不支援html的寫法; 沒辦法只能用``來搞定了)

開發過程中遇到的問題

一、在寫好所有的程式之後, 跑測試用例, 但是始終都是報NoSuchBeanException

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException:No qualifying bean of type 'com.terrylmay.redis.example.config.StandaloneRedisConfig' available: expected at least 1 bean which qualifies as autowire candidate. 
Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

原因共有三點:

1、寫了scanBasepackages來掃描包下面的bean, 掃描的包與類所在的包不一樣, 只有一個字元之差 com.terrylmay.redis.example 與 com.terrlmay.redis.example, 當然這時候idea會報錯, 只是我不認識那個錯而已; Idea報錯如圖所示:

image.png

2、按照網上的application.properties屬性的讀取方式, 只使用了一個註解:
@ConfigurationProperties(prefix = "spring.redis") 但是點進該註解裡面看, 它其實並沒有Component註解的功能; 所以增加了@Configuration註解

3、第三個原因不仔細斷然不會發現這個錯誤

image.png

我理解的是隻要工程裡面父工程是spring-boot-starter-parent, 那麼就不應該存在這類Jar包沒有依賴的問題, 開啟文件

image.png

依賴可貼上版:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional></dependency>

到這裡, 一個能夠使用Redis的Java工程就已經就緒了; 最終的程式碼全部在Github的spring-redis-example倉庫下, 歡迎star與PR