欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

嵌入式Redis服務器在Spring Boot測試中的使用教程

 更新時間:2021年07月19日 08:40:31   作者:碼農熊貓  
這篇文章主要介紹了嵌入式Redis服務器在Spring Boot測試中的使用,本文通過實例代碼場景分析給大家介紹的非常詳細,需要的朋友參考下吧

1、概述

Spring Data Redis提供了一種與Redis實例集成的簡單方法。

但是,在某些情況下,使用嵌入式服務器比使用真實服務器創(chuàng)建開發(fā)和測試環(huán)境更方便。

因此,我們將學習如何設置和使用嵌入式Redis服務器。

2、依賴

讓我們首先添加必要的依賴項:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

<dependency>
  <groupId>it.ozimov</groupId>
  <artifactId>embedded-redis</artifactId>
  <version>0.7.2</version>
  <scope>test</scope>
</dependency>

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-test</artifactId>
  <scope>test</scope>
</dependency>

這個spring-boot-starter-test包含我們需要運行集成測試的各種依賴。

此外,embedded-redis包含我們將使用的嵌入式服務器。

3、設置

添加依賴項后,我們應該定義Redis服務器和我們的應用程序之間的連接設置。

讓我們首先創(chuàng)建一個類來保存我們的屬性:

@Configuration
public class RedisProperties {
    private int redisPort;
    private String redisHost;

    public RedisProperties(
      @Value("${spring.redis.port}") int redisPort, 
      @Value("${spring.redis.host}") String redisHost) {
        this.redisPort = redisPort;
        this.redisHost = redisHost;
    }

    // getters
}

接下來,我們應該創(chuàng)建一個配置類來定義連接并使用我們的屬性:

@Configuration
@EnableRedisRepositories
public class RedisConfiguration {

    @Bean
    public LettuceConnectionFactory redisConnectionFactory(
      RedisProperties redisProperties) {
        return new LettuceConnectionFactory(
          redisProperties.getRedisHost(), 
          redisProperties.getRedisPort());
    }

    @Bean
    public RedisTemplate<?, ?> redisTemplate(LettuceConnectionFactory connectionFactory) {
        RedisTemplate<byte[], byte[]> template = new RedisTemplate<>();
        template.setConnectionFactory(connectionFactory);
        return template;
    }
}

配置非常簡單。這樣我們的嵌入式服務器可以在其他的端口上運行。

4、嵌入式Redis服務器

現(xiàn)在,我們將配置嵌入式服務器并在我們的一項測試中使用它。

首先,讓我們在測試的資源目錄(src/test/resources)中創(chuàng)建一個application.properties文件:

spring.redis.host=localhost
spring.redis.port=6370

之后,我們將創(chuàng)建一個@TestConfiguration注解的配置類:

@TestConfiguration
public class TestRedisConfiguration {

    private RedisServer redisServer;

    public TestRedisConfiguration(RedisProperties redisProperties) {
        this.redisServer = new RedisServer(redisProperties.getRedisPort());
    }

    @PostConstruct
    public void postConstruct() {
        redisServer.start();
    }

    @PreDestroy
    public void preDestroy() {
        redisServer.stop();
    }
}

當context上下文啟動,服務器就跟著啟動。它根據我們在屬性中定義的端口運行在我們的機器上。有了它,我們現(xiàn)在可以在不停止實際Redis服務器的情況下運行測試了。

理想情況下,我們希望在隨機可用端口上啟動它,但嵌入式Redis尚不具備此功能。我們現(xiàn)在可以做的是通過ServerSocket API 獲取隨機端口。

此外,當上下文停止,服務器也跟著停止。

服務器也可以由我們自己的可執(zhí)行文件來提供:

this.redisServer = new RedisServer("/path/redis", redisProperties.getRedisPort());

此外,可執(zhí)行文件可以按不同的操作系統(tǒng)來定義:

RedisExecProvider customProvider = RedisExecProvider.defaultProvider()
.override(OS.UNIX, "/path/unix/redis")
.override(OS.Windows, Architecture.x86_64, "/path/windows/redis")
.override(OS.MAC_OS_X, Architecture.x86_64, "/path/macosx/redis");

this.redisServer = new RedisServer(customProvider, redisProperties.getRedisPort());

最后,讓我們創(chuàng)建一個使用TestRedisConfiguration類的測試吧:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestRedisConfiguration.class)
public class UserRepositoryIntegrationTest {

    @Autowired
    private UserRepository userRepository;

    @Test
    public void shouldSaveUser_toRedis() {
        UUID id = UUID.randomUUID();
        User user = new User(id, "name");

        User saved = userRepository.save(user);

        assertNotNull(saved);
    }
}

這樣用戶保存就到了我們的嵌入式Redis服務器。

此外,我們必須手動將TestRedisConfiguration添加到SpringBootTest。正如我們之前所說,服務器在測試之前啟動并在測試之后停止。

5、結論

嵌入式Redis服務器是在測試環(huán)境中替換實際服務器的完美工具。我們已經看到了如何配置它以及如何在我們的測試中使用它。

到此這篇關于嵌入式Redis服務器在Spring Boot測試中的使用的文章就介紹到這了,更多相關Redis Spring Boot使用內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • redis.conf中使用requirepass不生效的原因及解決方法

    redis.conf中使用requirepass不生效的原因及解決方法

    本文主要介紹了如何啟用requirepass,以及啟用requirepass為什么不會生效,從代碼層面分析了不生效的原因,以及解決方法,需要的朋友可以參考下
    2023-07-07
  • Redis?key-value亂碼的解決

    Redis?key-value亂碼的解決

    本文主要介紹了Redis?key-value亂碼的解決,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • RedisDesktopManager?連接redis的方法

    RedisDesktopManager?連接redis的方法

    這篇文章主要介紹了RedisDesktopManager?連接redis,需要的朋友可以參考下
    2023-08-08
  • Redis和Nginx實現(xiàn)限制接口請求頻率的示例

    Redis和Nginx實現(xiàn)限制接口請求頻率的示例

    限流就是限制API訪問頻率,當訪問頻率超過某個閾值時進行拒絕訪問等操作,本文主要介紹了Redis和Nginx實現(xiàn)限制接口請求頻率的示例,具有一定的參考價值,感興趣的可以了解一下
    2024-02-02
  • Redis的過期鍵刪除策略原理說明

    Redis的過期鍵刪除策略原理說明

    這篇文章主要介紹了Redis的過期鍵刪除策略原理說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • Redis中Redisson紅鎖(Redlock)使用原理

    Redis中Redisson紅鎖(Redlock)使用原理

    本文主要介紹了Redis中Redisson紅鎖(Redlock)使用原理,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-08-08
  • 詳解centos7 yum安裝redis及常用命令

    詳解centos7 yum安裝redis及常用命令

    這篇文章主要介紹了centos7 yum安裝redis及常用命令,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-09-09
  • Linux 下redis5.0.0安裝教程詳解

    Linux 下redis5.0.0安裝教程詳解

    這篇文章主要介紹了Linux 下redis5.0.0安裝教程,本文圖文并茂給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-10-10
  • 深入理解redis分布式鎖和消息隊列

    深入理解redis分布式鎖和消息隊列

    本篇文章主要介紹了深入理解redis分布式鎖和消息隊列,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-07-07
  • 分割超大Redis數據庫例子

    分割超大Redis數據庫例子

    這篇文章主要介紹了分割超大Redis數據庫例子,本文講解了分割的需求、分割的思路及分割實例,需要的朋友可以參考下
    2015-03-03

最新評論