1、概述
Spring Data Redis提供了一種與Redis實(shí)例集成的簡單方法。
但是,在某些情況下,使用嵌入式服務(wù)器比使用真實(shí)服務(wù)器創(chuàng)建開發(fā)和測試環(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)行集成測試的各種依賴。
此外,embedded-redis包含我們將使用的嵌入式服務(wù)器。
3、設(shè)置
添加依賴項(xiàng)后,我們應(yīng)該定義Redis服務(wù)器和我們的應(yīng)用程序之間的連接設(shè)置。
讓我們首先創(chuàng)建一個(gè)類來保存我們的屬性:
@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
}
接下來,我們應(yīng)該創(chuàng)建一個(gè)配置類來定義連接并使用我們的屬性:
@Configuration
@EnableRedisRepositories
public class RedisConfiguration {
@Bean
public LettuceConnectionFactory redisConnectionFactory(
RedisProperties redisProperties) {
return new LettuceConnectionFactory(
redisProperties.getRedisHost(),
redisProperties.getRedisPort());
}
@Bean
public RedisTemplate?, ?> redisTemplate(LettuceConnectionFactory connectionFactory) {
RedisTemplatebyte[], byte[]> template = new RedisTemplate>();
template.setConnectionFactory(connectionFactory);
return template;
}
}
配置非常簡單。這樣我們的嵌入式服務(wù)器可以在其他的端口上運(yùn)行。
4、嵌入式Redis服務(wù)器
現(xiàn)在,我們將配置嵌入式服務(wù)器并在我們的一項(xiàng)測試中使用它。
首先,讓我們在測試的資源目錄(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ù)我們在屬性中定義的端口運(yùn)行在我們的機(jī)器上。有了它,我們現(xiàn)在可以在不停止實(shí)際Redis服務(wù)器的情況下運(yùn)行測試了。
理想情況下,我們希望在隨機(jī)可用端口上啟動(dòng)它,但嵌入式Redis尚不具備此功能。我們現(xiàn)在可以做的是通過ServerSocket API 獲取隨機(jī)端口。
此外,當(dāng)上下文停止,服務(wù)器也跟著停止。
服務(wù)器也可以由我們自己的可執(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)建一個(gè)使用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服務(wù)器。
此外,我們必須手動(dòng)將TestRedisConfiguration添加到SpringBootTest。正如我們之前所說,服務(wù)器在測試之前啟動(dòng)并在測試之后停止。
5、結(jié)論
嵌入式Redis服務(wù)器是在測試環(huán)境中替換實(shí)際服務(wù)器的完美工具。我們已經(jīng)看到了如何配置它以及如何在我們的測試中使用它。
到此這篇關(guān)于嵌入式Redis服務(wù)器在Spring Boot測試中的使用的文章就介紹到這了,更多相關(guān)Redis Spring Boot使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- springboot2整合redis使用lettuce連接池的方法(解決lettuce連接池?zé)o效問題)
- 基于SpringBoot2.0默認(rèn)使用Redis連接池的配置操作
- SpringBoot中使用Redis的完整實(shí)例
- springboot+redis過期事件監(jiān)聽實(shí)現(xiàn)過程解析
- Spring boot+redis實(shí)現(xiàn)消息發(fā)布與訂閱的代碼
- SpringBoot結(jié)合Redis哨兵模式的實(shí)現(xiàn)示例