问题
I'm trying to integrate redis cache to JHipster generator following this pull request on Github: https://github.com/jhipster/generator-jhipster/pull/10057/commits/cd2f2865d35dfd77624dd3a38ed32822e895539d#
I receive this error while building my project:
[ERROR] symbol: method getRedis()
[ERROR] location: class io.github.jhipster.config.JHipsterProperties.Cache
[ERROR] ../config/CacheConfiguration.java:[61,139] cannot find symbol
The method getRedis() is undefined for the type JHipsterProperties.CacheJava(67108964)
Where is getRedis() defined?
CacheConfiguration method in CacheConfiguration.java:
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);
}
Am I missing some dependencies for getRedis()?
Note: I left out this in build.gradle.ejs; would this be causing the problem?
<%_ if (cacheProvider === 'redis') { _%>
implementation "org.redisson:redisson"
<%_ if (enableHibernateCache) { _%>
implementation "org.hibernate:hibernate-jcache"
<%_ } _%>
<%_ } _%>
Solution?:
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:
You are missing the respective changes in the JHipster library which are not released yet (located in this pull request).
My advice (until it's released) would be to copy the changes (the Redis class and values) from JhipsterProperties.java to your ApplicationProperties.java.
Then if you need to configure the values to a non-default value, you can do so in your application.yml under the application: key.
Lastly add ApplicationProperties applicationProperties
to the constructor in CacheConfiguration.java
next to JhipsterProperties and reference getRedis()
from there.
I believe the reddison
dependency is also needed.
来源:https://stackoverflow.com/questions/57062875/integrate-redis-to-jhipster-cacheconfiguration-error