简体   繁体   English

Spring,Infinispan和JGroups-以编程方式进行配置

[英]Spring, Infinispan and JGroups - configuration programmatically

I am using Spring 4.3, Infinispan 9.11 and JGroups 4.0.6. 我正在使用Spring 4.3,Infinispan 9.11和JGroups 4.0.6。 For JGroups I am using xml configuration, in which I have: 对于JGroups,我使用的是xml配置,其中有:

<TCPPING async_discovery="true"
         initial_hosts="${jgroups.tcpping.initial_hosts: HOSTS}"
(...)

Let's say I want to keep configuration in xml, however, I need to apply a list of hosts from another (yml) configuration file. 假设我想将配置保留在xml中,但是,我需要应用另一个(yml)配置文件中的主机列表。 A way to go might be to read yml properties from Java (I have that part) and set them somehow to the JGroups configuration. 一种可能的方法是从Java读取yml属性(我有那部分)并将它们以某种方式设置为JGroups配置。

This is what I've tried so far: 到目前为止,这是我尝试过的:

EmbeddedCacheManager cacheManager = new DefaultCacheManager(
            GlobalConfigurationBuilder.defaultClusteredBuilder()
                    .transport()
                    .nodeName(nodeName)
                    .addProperty(JGroupsTransport.CONFIGURATION_FILE, "tcp.xml")
                    .addProperty(JGroupsTransport.CONFIGURATION_STRING, "jgroups.tcpping.initial_hosts: HOSTS")
                    .build(),
            new ConfigurationBuilder()
                    ...
                    .build()
    );

However, configuration string doesn't do the job. 但是,配置字符串不起作用。

UPDATE. UPDATE。 Another attempt: 另一尝试:

JGroupsTransport transport = (JGroupsTransport)(cacheManager.getCacheManagerConfiguration().transport().transport());
    TCPPING ping = transport.getChannel().getProtocolStack().findProtocol(TCPPING.class);
    ping.setPortRange(1);
    ping.setInitialHosts(Arrays.asList(new IpAddress("HOST1:PORT1"), new IpAddress("HOST2:PORT2")));

That doesn't seem to be working neither. 这似乎也不起作用。

如果您具有JGroups JChannel,则可以像这样获取TCPPING: channel.getProtocolStack().findProtocol(TCPPING.class) ,然后调用设置器在其上设置initial_hosts。

As Bela Ban and Altanis indicated, the problem is that Infinispan is calling JChannel.connect() before I am able to modify the transport properties. 正如Bela Ban和Altanis所指出的那样,问题在于Infinispan在调用JChannel.connect()之前才能够修改传输属性。 I've found a workaround though: 我找到了一种解决方法:

public class JGroupsChannelLookupImpl implements JGroupsChannelLookup {

    @Override
    public JChannel getJGroupsChannel(Properties p) {
        JChannel channel = null;
        try {
            channel = new JChannel("tcp.xml");
            TCPPING ping = channel.getProtocolStack().findProtocol(TCPPING.class);
            ping.setInitialHosts(Arrays.asList(HOST1, HOST2, HOST3, ...));
        }
        catch (Exception ex) {
            // do sth with the ex
        }
        Objects.requireNonNull(channel);
        return channel;
    }

(...)

}

Then when creating DefaultCacheManager, instead of loading the config directly from xml, just load channel lookup: 然后,在创建DefaultCacheManager时,无需直接从xml加载配置,而只需加载通道查找:

new DefaultCacheManager(
            GlobalConfigurationBuilder.defaultClusteredBuilder()
                    .transport()
                    .nodeName(nodeName)
                    .addProperty(JGroupsTransport.CHANNEL_LOOKUP, JGroupsChannelLookupImpl.class.getName())
                    .build(),
           (...)

Works as expected! 可以正常工作!

Before digging into Infinispan internals, let me try the easy path. 在深入研究Infinispan内部之前,让我尝试一下简单的方法。

Do you know all the hosts on application boot time? 您是否知道应用程序启动时的所有主机? If so, you can use environmental variable (or a system property) to inject the list of hosts. 如果是这样,则可以使用环境变量(或系统属性)来注入主机列表。 Just execute java myApp -Djgroups.tcpping.initial_hosts=host1.. . 只需执行java myApp -Djgroups.tcpping.initial_hosts=host1..

The other way to do it is to dig into Infinispan internals just like you did. 另一种方法是像您一样深入研究Infinispan内部。 The only problem is that your implementation does this too late (JGroups channel is already initialized at that point). 唯一的问题是您的实现太晚了(此时JGroups通道已被初始化)。 So you would need to inject a view (probably using an INJECT_VIEW protocol, see the manual ) 因此,您需要注入一个视图(可能使用INJECT_VIEW协议,请参见手册

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM