将Redis集成到JHipster CacheConfiguration错误

时间:2019-07-16 17:47:55

标签: java redis jhipster

在Github上的以下拉取请求后,我正在尝试将Redis缓存集成到JHipster生成器:https://github.com/jhipster/generator-jhipster/pull/10057/commits/cd2f2865d35dfd77624dd3a38ed32822e895539d#

我在构建项目时收到此错误:

[ERROR]   symbol:   method getRedis()
[ERROR]   location: class io.github.jhipster.config.JHipsterProperties.Cache
[ERROR] ../config/CacheConfiguration.java:[61,139] cannot find symbol

JHipsterProperties.CacheJava(67108964)类型的getRedis()方法未定义

getRedis()在哪里定义?

CacheConfiguration.java中的

CacheConfiguration方法:

private final javax.cache.configuration.Configuration<Object, Object> jcacheConfiguration;

            public CacheConfiguration(JHipsterProperties jHipsterProperties) {
                MutableConfiguration<Object, Object> jcacheConfig = new MutableConfiguration<>();
                Config config = new Config();
                config.useSingleServer()
                .setAddress(jHipsterProperties.getCache().getRedis().getServer())
                .setSubscriptionConnectionMinimumIdleSize(1)
                .setSubscriptionConnectionPoolSize(50)
                .setConnectionMinimumIdleSize(24)
                .setConnectionPoolSize(64)
                .setDnsMonitoringInterval(5000)
                .setIdleConnectionTimeout(10000)
                .setConnectTimeout(10000)
                .setTimeout(3000)
                .setRetryAttempts(3)
                .setRetryInterval(1500)
                .setDatabase(0)
                .setPassword(null)
                .setSubscriptionsPerConnection(5)
                .setClientName(null)
                .setSslEnableEndpointIdentification(true)
                .setSslProvider(SslProvider.JDK)
                .setSslTruststore(null)
                .setSslTruststorePassword(null)
                .setSslKeystore(null)
                .setSslKeystorePassword(null)
                .setPingConnectionInterval(0)
                .setKeepAlive(false)
                .setTcpNoDelay(false);
            jcacheConfig.setStatisticsEnabled(true);
            jcacheConfig.setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(new Duration(TimeUnit.SECONDS, jHipsterProperties.getCache().getRedis().getExpiration())));
            jcacheConfiguration = RedissonConfiguration.fromInstance(Redisson.create(config), jcacheConfig);
        }

我是否缺少getRedis()的某些依赖项?

注意:我在build.gradle.ejs中忽略了这个;这会导致问题吗?

  <%_ if (cacheProvider === 'redis') { _%>
    implementation "org.redisson:redisson"
        <%_ if (enableHibernateCache) { _%>
    implementation "org.hibernate:hibernate-jcache"
        <%_ } _%>
    <%_ } _%>

解决方案?:

ApplicationProperties.java:

@ConfigurationProperties(prefix = "application", ignoreUnknownFields = false)
public class ApplicationProperties {

     private final Redis redis = new Redis();

     public Redis getRedis() {
          return redis;
      }

      public static class Redis {
          private String server = JHipsterDefaults.Cache.Redis.server;
          private int expiration = JHipsterDefaults.Cache.Redis.expiration;

          public String getServer() {
              return server;
          }

          public void setServer(String server) {
              this.server = server;
          }

          public int getExpiration() {
              return expiration;
          }

          public void setExpiration(int expiration) {
              this.expiration = expiration;
          }
      }
}

CacheConfiguration.java

 <%_ if (cacheProvider === 'redis') { _%>
        private final javax.cache.configuration.Configuration<Object, Object> jcacheConfiguration;

            public CacheConfiguration(JHipsterProperties jHipsterProperties, ApplicationProperties applicationProperties) {

            MutableConfiguration<Object, Object> jcacheConfig = new MutableConfiguration<>();
            Config config = new Config();
            config.useSingleServer()
                .setAddress(applicationProperties.getRedis().getServer());
                .setSubscriptionConnectionMinimumIdleSize(1)
                .setSubscriptionConnectionPoolSize(50)
                .setConnectionMinimumIdleSize(24)
                .setConnectionPoolSize(64)
                .setDnsMonitoringInterval(5000)
                .setIdleConnectionTimeout(10000)
                .setConnectTimeout(10000)
                .setTimeout(3000)
                .setRetryAttempts(3)
                .setRetryInterval(1500)
                .setDatabase(0)
                .setPassword(null)
                .setSubscriptionsPerConnection(5)
                .setClientName(null)
                .setSslEnableEndpointIdentification(true)
                .setSslProvider(SslProvider.JDK)
                .setSslTruststore(null)
                .setSslTruststorePassword(null)
                .setSslKeystore(null)
                .setSslKeystorePassword(null)
                .setPingConnectionInterval(0)
                .setKeepAlive(false)
                .setTcpNoDelay(false);
            jcacheConfig.setStatisticsEnabled(true);
            jcacheConfig.setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(new Duration(TimeUnit.SECONDS, applicationProperties.getRedis().getExpiration())));
            jcacheConfiguration = RedissonConfiguration.fromInstance(Redisson.create(config), jcacheConfig);
        }

application.yml.ejs

# ===================================================================
# Application specific properties
# Add your own application properties here, see the ApplicationProperties class
# to have type-safe configuration, like in the JHipsterProperties above
#
# More documentation is available at:
# https://www.jhipster.tech/common-application-properties/
# ===================================================================

# application:
    application.redis.server: redis://localhost:6379
    application.redis.expiration: 300

1 个答案:

答案 0 :(得分:1)

您遗漏了JHipster库中尚未发布的相应更改(位于此pull request中)。

我的建议(直到发布)是将JhipsterProperties.java中的更改(Redis类和值)复制到您的ApplicationProperties.java中。

然后,如果需要将值配置为非默认值,则可以在application.yml under the application: key中进行配置。

最后将ApplicationProperties applicationProperties添加到JhipsterProperties旁边的CacheConfiguration.java中的构造函数中,并从那里引用getRedis()

我相信也需要reddison依赖项。