嵌入式Redis用于弹簧启动

Gur*_*der 29 java redis spring-boot

我在我的机器上使用我的本地redis-server帮助运行我的Integration Test案例和Spring启动.

但我想要一个嵌入式Redis服务器,它不依赖于任何服务器,可以在任何环境中运行,比如H2内存数据库.我该怎么做?

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@IntegrationTest("server.port:0")
@SpringApplicationConfiguration(classes = Application.class) 
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
public class MasterIntegrationTest {

}
Run Code Online (Sandbox Code Playgroud)

Séb*_*mer 44

您可以使用嵌入式Redis,如https://github.com/kstyrc/embedded-redis

  1. 将依赖项添加到pom.xml
  2. 调整集成测试的属性以指向嵌入式redis,例如:

    spring:
      redis:
        host: localhost
        port: 6379
    
    Run Code Online (Sandbox Code Playgroud)
  3. 将嵌入式redis服务器安装在仅在测试中定义的组件中:

    @Component
    public class EmbededRedis {
    
        @Value("${spring.redis.port}")
        private int redisPort;
    
        private RedisServer redisServer;
    
        @PostConstruct
        public void startRedis() throws IOException {
            redisServer = new RedisServer(redisPort);
            redisServer.start();
        }
    
        @PreDestroy
        public void stopRedis() {
            redisServer.stop();
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)

  • 该项目似乎不再积极维护.最近一次发布接近两年前. (6认同)
  • https://github.com/ozimov/embedded-redis是https://github.com/kstyrc/embedded-redis的继承者,我使用ozimov/embedded-redis添加了额外的答案. (6认同)
  • @Renjith关于密码,我能够使用 `.setting("requirepass " + redisPassword)` 成功设置它。这是 ozimov 版本的 (2认同)

Mar*_*lte 21

您可以使用ozimov/embedded-redis作为Maven(-test)-dependency(这是kstyrc/embedded-redis的后继者).

  1. 将依赖项添加到pom.xml

    <dependencies>
      ...
      <dependency>
        <groupId>it.ozimov</groupId>
        <artifactId>embedded-redis</artifactId>
        <version>0.7.1</version>
        <scope>test</scope>
      </dependency>
    
    Run Code Online (Sandbox Code Playgroud)
  2. 调整集成测试的应用程序属性

    spring.redis.host=localhost
    spring.redis.port=6379
    
    Run Code Online (Sandbox Code Playgroud)
  3. 测试配置中使用嵌入式redis服务器

    @TestConfiguration
    public static class EmbededRedisTestConfiguration {
    
      private final redis.embedded.RedisServer redisServer;
    
      public EmbededRedisTestConfiguration(@Value("${spring.redis.port}") final int redisPort) throws IOException {
        this.redisServer = new redis.embedded.RedisServer(redisPort);
      }
    
      @PostConstruct
      public void startRedis() {
        this.redisServer.start();
      }
    
      @PreDestroy
      public void stopRedis() {
        this.redisServer.stop();
      }
    }
    
    Run Code Online (Sandbox Code Playgroud)

  • 这也没有维护!( (6认同)
  • 放弃嵌入式 Redis 是唯一明智的做法,因为它不受维护。建议使用 testcontainers 的另一个响应现在应该是可接受的解决方案 /sf/answers/3545210581/ (2认同)
  • [codemonstur/embedded-redis](https://github.com/codemonstur/embedded-redis)、[ponfee/embedded-redis](https://github.com/ponfee/embedded-redis)、[simbahebinbo/embedded -redis](https://github.com/simbahebinbo/embedded-redis) 和 [signalapp/embedded-redis](https://github.com/signalapp/embedded-redis) 是嵌入式 redis 最近更新的分支。它们之间有一些区别,例如只有前两个支持Windows。 (2认同)

mag*_*ter 5

另一种简洁的方法是使用testcontainers可以运行Docker容器中可以运行的任何类型的应用程序的库,Redis也不例外。我最喜欢的是它与Spring Test生态系统紧密结合。

Maven的依赖:

<dependency>
    <groupId>org.testcontainers</groupId>
    <artifactId>testcontainers</artifactId>
    <version>${testcontainers.version}</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

简单集成测试:

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties = {"management.port=0"})
@ContextConfiguration(initializers = AbstractIntegrationTest.Initializer.class)
@DirtiesContext
public abstract class AbstractIntegrationTest {

    private static int REDIS_PORT = 6379;

    @ClassRule
    public static GenericContainer redis = new GenericContainer("redis:3.0.6").withExposedPorts(REDIS_PORT);

    public static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
        @Override
        public void initialize(ConfigurableApplicationContext ctx) {
            // Spring Boot 1.5.x
            TestPropertySourceUtils.addInlinedPropertiesToEnvironment(ctx,
                "spring.redis.host=" + redis.getContainerIpAddress(),
                "spring.redis.port=" + redis.getMappedPort(REDIS_PORT));

            // Spring Boot 2.x.
            TestPropertyValues.of(
                "spring.redis.host:" + redis.getContainerIpAddress(),
                "spring.redis.port:" + redis.getMappedPort(REDIS_PORT))
                .applyTo(ctx);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)