我在我的项目中使用hazelcast,并希望将hazelcast host:port信息转移到环境变量中。在此之前,我有默认配置,即:
<hazelcast-client xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.hazelcast.com/schema/client-config
http://www.hazelcast.com/schema/client-config/hazelcast-client-config-3.8.xsd"
xmlns="http://www.hazelcast.com/schema/client-config">
<network>
<connection-timeout>3000</connection-timeout>
<connection-attempt-period>1000</connection-attempt-period>
<connection-attempt-limit>259200</connection-attempt-limit>
</network>
</hazelcast-client>我发现有可能在<network>中添加<cluster-members>标签来为hazelcast实例提供自定义<address>。我已经将我的hazelcast.xml文件修改为:
<network>
<cluster-members>
<address>${HAZELCAST_URL}</address>
</cluster-members>
...但每当我启动我的应用程序时,它就会显示:
2017-11-10 17:55:45 [service,,,] WARN c.h.c.s.i.ClusterListenerSupport hz.client_0 [dev] [3.8.5] Exception during initial connection to ${HAZELCAST_URL}:5701, exception java.lang.IllegalArgumentException: Can't resolve address: ${HAZELCAST_URL}:5701
2017-11-10 17:55:45 [service,,,] WARN c.h.c.s.i.ClusterListenerSupport hz.client_0 [dev] [3.8.5] Exception during initial connection to ${HAZELCAST_URL}:5702, exception java.lang.IllegalArgumentException: Can't resolve address: ${HAZELCAST_URL}:5702这意味着它仍然尝试连接到默认端口,变量未被解析。有什么方法可以配置它吗?
发布于 2017-11-12 21:14:52
您可以将java.util.Properties传递给客户端配置构建器。您所需要做的就是从Spring的环境中构建它。
@Bean
public ClientConfig clientConfig(Environment environment) throws Exception {
Properties properties = new Properties();
String HAZELCAST_URL = "HAZELCAST_URL";
properties.put(HAZELCAST_URL, environment.getProperty(HAZELCAST_URL));
XmlClientConfigBuilder xmlClientConfigBuilder = new XmlClientConfigBuilder("hazelcast-client.xml");
xmlClientConfigBuilder.setProperties(properties);
return xmlClientConfigBuilder.build();
}
@Bean
public HazelcastInstance hazelcastInstance(ClientConfig clientConfig) {
return HazelcastClient.newHazelcastClient(clientConfig);
}注意,有更好的方法可以做到这一点,上面的只是一个尽可能简单的解决方案。
https://stackoverflow.com/questions/47226694
复制相似问题