Jhipster使用Redis连接构建Docker镜像错误

问题描述

我是Jhipster社区的新手,开发平台看起来非常好! 但是我在为本地开发构建docker映像时遇到了问题。

我成功生成了jhipster应用程序,然后按照文档进行操作,并尝试使用(maven)构建docker映像

/mvnw package -Pprod verify jib:dockerBuild

我一直在遍历这个错误(大约一千万,之后停止并说构建失败)

2020-08-24 15:48:55.693  WARN 8388 --- [           main] o.s.w.c.s.GenericWebApplicationContext   : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'cacheManager' defined in class path resource [org/springframework/boot/autoconfigure/cache/jcacheCacheConfiguration.class]: Unsatisfied dependency expressed through method 'cacheManager' parameter 1; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jcacheCacheManager' defined in class path resource [org/springframework/boot/autoconfigure/cache/jcacheCacheConfiguration.class]: Bean instantiation via factory method Failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.cache.CacheManager]: Factory method 'jcacheCacheManager' threw exception; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'cacheManagerCustomizer' defined in class path resource [com/carcrypto/api/config/CacheConfiguration.class]: Unsatisfied dependency expressed through method 'cacheManagerCustomizer' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jcacheConfiguration' defined in class path resource [com/carcrypto/api/config/CacheConfiguration.class]: Bean instantiation via factory method Failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.cache.configuration.Configuration]: Factory method 'jcacheConfiguration' threw exception; nested exception is org.redisson.client.RedisConnectionException: Unable to connect to Redis server: localhost/127.0.0.1:6379
2020-08-24 15:48:55.698 ERROR 8388 --- [           main] o.s.boot.SpringApplication               : Application run Failed

我们清楚地看到了这一点:RedisConnectionException: Unable to connect to Redis server: localhost/127.0.0.1:6379

同时我可以看到redis容器在docker中运行,但没有端口6379,它在端口32789上(看起来像docker的认可用端口)...

我在Windows 10上,已经有人遇到了这个问题?

预先感谢

解决方法

在6.10.1中是a,但是。它将在即将发布的6.10.2版本(尚未发布)中修复。在此之前,您需要更改RedisTestContainerExtension的实现。

当前应如下所示:

public class RedisTestContainerExtension implements BeforeAllCallback {

    private static AtomicBoolean started = new AtomicBoolean(false);

    private static GenericContainer redis;

    @DynamicPropertySource
    static void redisProperties(DynamicPropertyRegistry registry) {
        registry.add("jhipster.cache.redis.server",() -> "redis://" + redis.getContainerIpAddress() + ":" + redis.getMappedPort(6379));
    }

    @Override
    public void beforeAll(ExtensionContext extensionContext) throws Exception {
        if (!started.get()) {
            redis = new GenericContainer("redis:6.0.4").withExposedPorts(6379);
            redis.start();
            started.set(true);
        }
    }
}

问题是DynamicPropertySource仅在测试类中有效,而在junit扩展中无效。因此,您需要删除它并设置系统属性,以使用容器中的正确端口覆盖redis连接,如下所示:

public class RedisTestContainerExtension implements BeforeAllCallback {

    private static AtomicBoolean started = new AtomicBoolean(false);

    private static GenericContainer redis = new GenericContainer("redis:6.0.4").withExposedPorts(6379);

    @Override
    public void beforeAll(ExtensionContext extensionContext) throws Exception {
        if (!started.get()) {
            redis.start();
            System.setProperty(
                "jhipster.cache.redis.server","redis://" + redis.getContainerIpAddress() + ":" + redis.getMappedPort(6379)
            );
            started.set(true);
        }
    }
}