[BAEL-3079]: Embedded Redis Server Article (#7368)

This commit is contained in:
Lukasz Rys 2019-07-21 14:52:37 +02:00 committed by Grzegorz Piwowarek
parent f0c877bf0c
commit 85d3960cf8
9 changed files with 158 additions and 0 deletions

View File

@ -15,6 +15,10 @@
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
@ -33,6 +37,13 @@
<scope>test</scope>
</dependency>
<!-- Embedded Redis Server -->
<dependency>
<groupId>it.ozimov</groupId>
<artifactId>embedded-redis</artifactId>
<version>0.7.2</version>
<scope>test</scope>
</dependency>
<!-- Spock & Spring -->
<dependency>
<groupId>org.spockframework</groupId>

View File

@ -0,0 +1,24 @@
package com.baeldung.boot.embeddedRedis.configuration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories;
@Configuration
@EnableRedisRepositories
public class RedisConfiguration {
@Bean
public LettuceConnectionFactory redisConnectionFactory(final RedisProperties redisProperties) {
return new LettuceConnectionFactory(redisProperties.getRedisHost(), redisProperties.getRedisPort());
}
@Bean
public RedisTemplate<?, ?> redisTemplate(final LettuceConnectionFactory connectionFactory) {
RedisTemplate<byte[], byte[]> template = new RedisTemplate<>();
template.setConnectionFactory(connectionFactory);
return template;
}
}

View File

@ -0,0 +1,23 @@
package com.baeldung.boot.embeddedRedis.configuration;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RedisProperties {
private final int redisPort;
private final String redisHost;
public RedisProperties(@Value("${spring.redis.port}") final int redisPort, @Value("${spring.redis.host}") final String redisHost) {
this.redisPort = redisPort;
this.redisHost = redisHost;
}
public int getRedisPort() {
return redisPort;
}
public String getRedisHost() {
return redisHost;
}
}

View File

@ -0,0 +1,25 @@
package com.baeldung.boot.embeddedRedis.domain;
import org.springframework.data.annotation.Id;
import org.springframework.data.redis.core.RedisHash;
import java.util.UUID;
@RedisHash("user")
public class User {
@Id private UUID id;
private String name;
public User(UUID id, String name) {
this.id = id;
this.name = name;
}
public UUID getId() {
return id;
}
public String getName() {
return name;
}
}

View File

@ -0,0 +1,10 @@
package com.baeldung.boot.embeddedRedis.domain.repository;
import com.baeldung.boot.embeddedRedis.domain.User;
import org.springframework.data.repository.CrudRepository;
import java.util.UUID;
public interface UserRepository extends CrudRepository<User, UUID> {
}

View File

@ -1,3 +1,6 @@
# embedded redis
spring.redis.host= localhost
spring.redis.port= 6379
# security
spring.security.user.name=john

View File

@ -0,0 +1,28 @@
package com.baeldung.boot.embeddedRedis;
import com.baeldung.boot.embeddedRedis.configuration.RedisProperties;
import org.springframework.boot.test.context.TestConfiguration;
import redis.embedded.RedisServer;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
@TestConfiguration
public class TestRedisConfiguration {
private final RedisServer redisServer;
public TestRedisConfiguration(final RedisProperties redisProperties) {
this.redisServer = new RedisServer(redisProperties.getRedisPort());
}
@PostConstruct
public void postConstruct() {
redisServer.start();
}
@PreDestroy
public void preDestroy() {
redisServer.stop();
}
}

View File

@ -0,0 +1,31 @@
package com.baeldung.boot.embeddedRedis.domain.repository;
import com.baeldung.boot.embeddedRedis.TestRedisConfiguration;
import com.baeldung.boot.embeddedRedis.domain.User;
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.SpringRunner;
import java.util.UUID;
import static org.junit.Assert.assertNotNull;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestRedisConfiguration.class)
public class UserRepositoryIntegrationTest {
@Autowired
private UserRepository userRepository;
@Test
public void shouldSaveUser_toRedis() {
final UUID id = UUID.randomUUID();
final User user = new User(id, "name");
final User saved = userRepository.save(user);
assertNotNull(saved);
}
}

View File

@ -1,3 +1,6 @@
#embedded redis
spring.redis.host= localhost
spring.redis.port= 6370
# security
spring.security.user.name=john
spring.security.user.password=123