【问题标题】:Grails ehcache and externalizing configurationGrails ehcache 和外部化配置
【发布时间】:2014-08-23 17:55:51
【问题描述】:

我正在考虑在我们的 Grails 应用程序中为 ehcache 外部化某些配置参数,但我遇到了文档声称应该解决的问题。

我可能缺少一些东西。

我将 grails ehcache 插件版本 1.0.1 与 Grails 2.4.0 和 grails 缓存插件 1.1.7 一起使用。我正在使用休眠插件 3.6.10.16。

这是我的 CacheConfig.groovy 配置中的内容...

...
cacheManagerPeerProviderFactory {
    peerDiscovery 'automatic'
    factoryType 'rmi'
    multicastGroupAddress '${ehcacheMulticastGroupAddress}'
    multicastGroupPort '${ehcacheMulticastGroupPort}'
    timeToLive 'site'
}

我已打开调试级日志记录,因此我可以查看它生成的 XML。这是相关的sn-p:

<cacheManagerPeerProviderFactory class='net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory'
    properties="peerDiscovery=automatic,multicastGroupAddress=${ehcacheMulticastGroupAddress},multicastGroupPort=${ehcacheMulticastGroupPort},timeToLive=32"
    propertySeparator=','
/>

grails ehcache 插件文档有以下注释,我希望“证明”...

(note that ${ehcacheMulticastGroupAddress} and ${ehcacheMulticastGroupPort} are an Ehcache feature that lets you use system property names as variables to be resolved at runtime)

太好了。除了我启动应用程序时它不起作用。由于以下原因,它无法创建CacheManagerPeerProvider

...
Caused by UnknownHostException: ${ehcacheMulticastGroupAddress}
->>  901 | lookupAllHostAddr        in java.net.InetAddress$1
...

我有一个 myApplication-config.groovy 文件位于可访问区域中,我在为 Config.groovy 中的 grails.config.locations 分配值时指向该文件。但我不确定它是否正在努力真正插入该值。

我尝试了双引号,但它们也是一个坏主意——在解释 CacheConfig.groovy 时,它看不到我放入 myApplication-config.groovy 的配置。我确实知道它在某个时候成功地读取了该文件,因为我成功地使用它来驱动一些 Quartz 作业逻辑,因此该配置文件的位置可能不是问题。

【问题讨论】:

  • 我成功地将我的Config.groovy 外部化到grails.config.locations。这个文件包含来自另一个配置的一些配置部分(例如来自DataSource.groovy)。所以,你可以尝试用同样的方式外部化你的 EhCache 设置。
  • 您甚至可以在grails.config.locations 中指定外部CacheConfig.groovy 即可。
  • 感谢您的建议。但我意识到我做错了什么……问题在于我的阅读理解能力。 Ehcache 正在寻找 system properties 而不是上下文配置的东西。

标签: caching grails config ehcache externalizing


【解决方案1】:

答案是我需要设置 SYSTEM PROPERTIES 才能让 ehcache 找到。使用myApplication-config.groovy等Grails配置文件是完全错误的。

CacheConfig.groovy 文件是正确的,它生成的 XML 也是正确的。那么问题就变成了,它所寻找的属性首先是如何正确设置的?

我正在部署到 Tomcat。对于 Tomcat,在 setenv.bat 文件(或 *nix 上的 setenv.sh)中设置系统属性最有意义。

我创建了setenv.bat,将以下内容放入其中

set CATALINA_OPTS=%CATALINA_OPTS% -DehcacheMulticastGroupAddress=230.0.0.1 -DehcacheMulticastGroupPort=4446 -DehcachePeerListenerPort=40001

...它奏效了。 Ehcache 能够找到系统属性并正确启动一切。

tl;dr: 系统属性!= grails 应用程序配置

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-01
    • 1970-01-01
    • 2014-08-07
    • 2016-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多