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

嵌入式Redis服務(wù)器在Spring Boot測(cè)試中的使用教程

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

1、概述

Spring Data Redis提供了一種與Redis實(shí)例集成的簡(jiǎn)單方法。

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

因此,我們將學(xué)習(xí)如何設(shè)置和使用嵌入式Redis服務(wù)器。

2、依賴

讓我們首先添加必要的依賴項(xiàng):

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

這個(gè)spring-boot-starter-test包含我們需要運(yùn)行集成測(cè)試的各種依賴。

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

3、設(shè)置

添加依賴項(xiàng)后,我們應(yīng)該定義Redis服務(wù)器和我們的應(yīng)用程序之間的連接設(shè)置。

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

@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
}

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

@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;
    }
}

配置非常簡(jiǎn)單。這樣我們的嵌入式服務(wù)器可以在其他的端口上運(yùn)行。

4、嵌入式Redis服務(wù)器

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

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

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

之后,我們將創(chuàng)建一個(gè)@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();
    }
}

當(dāng)context上下文啟動(dòng),服務(wù)器就跟著啟動(dòng)。它根據(jù)我們?cè)趯傩灾卸x的端口運(yùn)行在我們的機(jī)器上。有了它,我們現(xiàn)在可以在不停止實(shí)際Redis服務(wù)器的情況下運(yùn)行測(cè)試了。

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

此外,當(dāng)上下文停止,服務(wù)器也跟著停止。

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

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

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

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)建一個(gè)使用TestRedisConfiguration類的測(cè)試吧:

@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服務(wù)器。

此外,我們必須手動(dòng)將TestRedisConfiguration添加到SpringBootTest。正如我們之前所說(shuō),服務(wù)器在測(cè)試之前啟動(dòng)并在測(cè)試之后停止。

5、結(jié)論

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

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

相關(guān)文章

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

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

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

    Redis?key-value亂碼的解決

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

    RedisDesktopManager?連接redis的方法

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

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

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

    Redis的過(guò)期鍵刪除策略原理說(shuō)明

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

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

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

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

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

    Linux 下redis5.0.0安裝教程詳解

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

    深入理解redis分布式鎖和消息隊(duì)列

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

    分割超大Redis數(shù)據(jù)庫(kù)例子

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

最新評(píng)論