How to open a JChannel (JGroups) using Openshift Wildfly 8 Cartridge

时光毁灭记忆、已成空白 提交于 2019-12-24 14:34:46

问题


We are developing a Java-EE Application running on Wildfly 8 in the Wildfly-Openshift-Cartridge. The Openshift Application is set to scaled, therefore, more than one JVM node will be running as a cluster.

The communicate between the different nodes, I like to use JGroups. However, I fail to open a new JChannel even though the JGroups itself seems to work. The application itself does also work, if I deploy the application locally to e.g. 3 wildfly standalone instances.

The default standalone.xml of the wildfly-cartrige contains already a configuration for JGroups:

....
<subsystem xmlns="urn:jboss:domain:jgroups:2.0" default-stack="tcp">
    <stack name="tcp">
        <transport type="TCP" socket-binding="jgroups-tcp">
            <property name="external_addr">${env.OPENSHIFT_GEAR_DNS}</property>
            <property name="external_port">${env.OPENSHIFT_WILDFLY_CLUSTER_PROXY_PORT}</property>
            <property name="bind_port">${env.OPENSHIFT_WILDFLY_CLUSTER_PORT}</property>
            <property name="bind_addr">${env.OPENSHIFT_WILDFLY_IP}</property>
            <property name="defer_client_bind_addr">true</property>
        </transport>
        <protocol type="TCPPING">
            <property name="timeout">30000</property>
            <property name="initial_hosts">${env.OPENSHIFT_WILDFLY_CLUSTER}</property>
            <property name="port_range">0</property>
            <property name="num_initial_members">1</property>
        </protocol>
        <protocol type="MERGE2"/>
        <protocol type="FD"/>
        <protocol type="VERIFY_SUSPECT"/>
        <protocol type="BARRIER"/>
        <protocol type="pbcast.NAKACK"/>
        <protocol type="UNICAST2"/>
        <protocol type="pbcast.STABLE"/>
        <protocol type="AUTH">
            <property name="auth_class">org.jgroups.auth.MD5Token</property>
            <property name="token_hash">SHA</property>
            <property name="auth_value">${env.OPENSHIFT_SECRET_TOKEN}</property>
        </protocol>
        <protocol type="pbcast.GMS"/>
        <protocol type="UFC"/>
        <protocol type="MFC"/>
        <protocol type="FRAG2"/>
        <!--protocol type="pbcast.STATE_TRANSFER"/>
        <protocol type="pbcast.FLUSH"/-->
    </stack>
</subsystem>
....

According this, the default-stack shall be tcp - which makes sense, since UDP multicasting is not allowed in the OpenShift environment.

If I deploy the application to OpenShift (scaled, with two running gears), the JGroups itself seems to communicate between the JVMs: Output from wildfly/log/server.log:

....
INFO  [org.infinispan.remoting.transport.jgroups.JGroupsTransport] (ServerService Thread Pool -- 59) ISPN000078: Starting JGroups Channel
INFO  [stdout] (ServerService Thread Pool -- 59)
INFO  [stdout] (ServerService Thread Pool -- 59) -------------------------------------------------------------------
INFO  [stdout] (ServerService Thread Pool -- 59) GMS: address=xxx.rhcloud.com/ejb, cluster=ejb, physical address=172.30.0.163:62417
....
INFO  [org.infinispan.remoting.transport.jgroups.JGroupsTransport] (Incoming-5,shared=tcp) ISPN000094: Received new cluster view: [xxx.rhcloud.com/ejb|4] (1) xxx.rhcloud.com/ejb]
....
INFO  [org.infinispan.remoting.transport.jgroups.JGroupsTransport] (Incoming-6,shared=tcp) ISPN000094: Received new cluster view: [yyyy.rhcloud.com/ejb|5] (2) [xxx.rhcloud.com/ejb, 55b89cd750044609f0000043-xx.rhcloud.com/ejb]
...

This works out of the box, without any own programming.

But now, I want to open a JChannel to send messages from one JVM to all others.

...
import org.jgroups.*;
...
public class MyNotifier extends ReceiverAdapter
{
    ...

    JChannel channel;

    public void start()
    {
        try
        {
            channel = new JChannel();
            channel.setReceiver(this);
            channel.connect("MyNotifier");
        }
        catch (ChannelException e)
        {
            log.error("JGroups: Error starting channel.", e);
        }
    }

    public void receive(Message msg)
    {
        // process message
    }

    public void send(DataObject data)
    {
        Message msg = new Message(null, null, data);

        try
        {
            channel.send(msg);
        }
        catch (Exception e)
        {
            log.error("JGroups: Error sending message.", e);
        }
    }

    public void viewAccepted(View new_view)
    {
        log.info("JGroups: View accepted: " + new_view );
    }
}

An exception is raised during channel = new JChannel():

INFO  [stdout] (ServerService Thread Pool -- 82) 72190 [ERROR] MyNotifier: JGroups: Error starting channel.
INFO  [stdout] (ServerService Thread Pool -- 82) org.jgroups.ChannelException: failed to start protocol stack
INFO  [stdout] (ServerService Thread Pool -- 82)    at org.jgroups.JChannel.startStack(JChannel.java:1787) ~[jgroups-2.12.1.Final.jar:2.12.1.Final]
INFO  [stdout] (ServerService Thread Pool -- 82)    at org.jgroups.JChannel.connect(JChannel.java:413) ~[jgroups-2.12.1.Final.jar:2.12.1.Final]
INFO  [stdout] (ServerService Thread Pool -- 82)    at org.jgroups.JChannel.connect(JChannel.java:388) ~[jgroups-2.12.1.Final.jar:2.12.1.Final]
INFO  [stdout] (ServerService Thread Pool -- 82)    at xxxxx.MyNotifier.start(MyNotifier.java:41) [classes:?]
INFO  [stdout] (ServerService Thread Pool -- 82)    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_45]
INFO  [stdout] (ServerService Thread Pool -- 82)    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_45]
INFO  [stdout] (ServerService Thread Pool -- 82)    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_45]
INFO  [stdout] (ServerService Thread Pool -- 82)    at java.lang.reflect.Method.invoke(Method.java:497) ~[?:1.8.0_45]
INFO  [stdout] (ServerService Thread Pool -- 82)    at org.jboss.as.ee.component.ManagedReferenceMethodInterceptor.processInvocation(ManagedReferenceMethodInterceptor.java:52) [wildfly-ee-8.2.0.Final.jar!/:8.2.0.Final]
...
INFO  [stdout] (ServerService Thread Pool -- 82) Caused by: java.lang.Exception: problem creating sockets (bind_addr=localhost/127.0.0.1, mcast_addr=null)
INFO  [stdout] (ServerService Thread Pool -- 82)    at org.jgroups.protocols.UDP.start(UDP.java:238) ~[jgroups-2.12.1.Final.jar:2.12.1.Final]
INFO  [stdout] (ServerService Thread Pool -- 82)    at org.jgroups.stack.ProtocolStack.startStack(ProtocolStack.java:990) ~[jgroups-2.12.1.Final.jar:2.12.1.Final]
INFO  [stdout] (ServerService Thread Pool -- 82)    at org.jgroups.JChannel.startStack(JChannel.java:1784) ~[jgroups-2.12.1.Final.jar:2.12.1.Final]
INFO  [stdout] (ServerService Thread Pool -- 82)    ... 136 more
INFO  [stdout] (ServerService Thread Pool -- 82) Caused by: java.lang.IllegalArgumentException: port out of range:65536
INFO  [stdout] (ServerService Thread Pool -- 82)    at java.net.InetSocketAddress.checkPort(InetSocketAddress.java:143) ~[?:1.8.0_45]
INFO  [stdout] (ServerService Thread Pool -- 82)    at java.net.InetSocketAddress.<init>(InetSocketAddress.java:188) ~[?:1.8.0_45]
INFO  [stdout] (ServerService Thread Pool -- 82)    at java.net.DatagramSocket.<init>(DatagramSocket.java:299) ~[?:1.8.0_45]
INFO  [stdout] (ServerService Thread Pool -- 82)    at org.jgroups.util.DefaultSocketFactory.createDatagramSocket(DefaultSocketFactory.java:65) ~[jgroups-2.12.1.Final.jar:2.12.1.Final]
INFO  [stdout] (ServerService Thread Pool -- 82)    at org.jgroups.protocols.UDP.createEphemeralDatagramSocket(UDP.java:458) ~[jgroups-2.12.1.Final.jar:2.12.1.Final]
INFO  [stdout] (ServerService Thread Pool -- 82)    at org.jgroups.protocols.UDP.createSockets(UDP.java:342) ~[jgroups-2.12.1.Final.jar:2.12.1.Final]
INFO  [stdout] (ServerService Thread Pool -- 82)    at org.jgroups.protocols.UDP.start(UDP.java:234) ~[jgroups-2.12.1.Final.jar:2.12.1.Final]
INFO  [stdout] (ServerService Thread Pool -- 82)    at org.jgroups.stack.ProtocolStack.startStack(ProtocolStack.java:990) ~[jgroups-2.12.1.Final.jar:2.12.1.Final]
INFO  [stdout] (ServerService Thread Pool -- 82)    at org.jgroups.JChannel.startStack(JChannel.java:1784) ~[jgroups-2.12.1.Final.jar:2.12.1.Final]
INFO  [stdout] (ServerService Thread Pool -- 82)    ... 136 more

It seems that JChannel tries to use even UDP. But how can I bring it to use what is setup as subsystem in the standalone.xml?

Some more information: As I wrote on top, JChannel is successfully created, if I deploy the application to some local running wildfly instances using UDP Multicasting. I then tried to apply locally the same stack configuration, as on OpenShift with the result that it still worked but even by UDP. So it seems that my channel = new JChannel(); does not use the settings from the subsystem configuration. But why?

EDIT:

I understood that the subsystem configuration i.e. the default-stack set in the subsystem has nothing to do with what new JChannel() will use; therefore, I try to create now my own tcp.xml. However, the first problem is how to use environment variables in this config (which expands to their value) e.g. I need something like <TCP bind_addr=${env.OPENSHIFT_WILDFLY_IP}/>. How can I achieve that?

For the first step, I tried to insert the fix correct values:

<config xmlns="urn:org:jgroups"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="urn:org:jgroups http://www.jgroups.org/schema/JGroups-3.3.xsd">
    <TCP
            external_addr="backend-evendingtest.rhcloud.com"
            external_port="62417"
            bind_port="7600"
            bind_addr="127.11.122.129"
            defer_client_bind_addr="true"/>

    <TCPPING timeout="3000"
             initial_hosts="backend-evendingtest.rhcloud.com[62417],55b89cd750044609f0000043-evendingtest.rhcloud.com[56432]"
             port_range="0"
             num_initial_members="1"/>
    <MERGE2/>
    <FD/>
    <VERIFY_SUSPECT/>
    <BARRIER/>
    <pbcast.NAKACK2
            use_mcast_xmit="false"/>
    <UNICAST3/>
    <pbcast.STABLE/>
    <AUTH auth_class="org.jgroups.auth.MD5Token"
          token_hash="SHA"
          auth_value="2QVXd4rUYmxOtZNlxsJ7fBbWb4J8eFJup39PSC1IZck1JHDr_y4qUzFZVqUzLVZ7rDHnZWPtOpyY4pTrPAZ__D06GHHz5Uq9D5ZPMtyHPkZuvRh3cWchUX3JxA5Qsq4B"/>
    <pbcast.GMS/>
    <MFC/>
    <FRAG2/>

...but I fail with an Exception:

java.net.BindException: Permission denied
      at java.net.PlainDatagramSocketImpl.bind0(Native Method)
      at java.net.AbstractPlainDatagramSocketImpl.bind(AbstractPlainDatagramSocketImpl.java:94)
      at java.net.DatagramSocket.bind(DatagramSocket.java:392)
      at java.net.MulticastSocket.<init>(MulticastSocket.java:172)
      at java.net.MulticastSocket.<init>(MulticastSocket.java:136)
      at org.jgroups.util.DefaultSocketFactory.createMulticastSocket(DefaultSocketFactory.java:70)
      at org.jgroups.stack.DiagnosticsHandler.start(DiagnosticsHandler.java:83)
      at org.jgroups.protocols.TP.start(TP.java:1222)
      at org.jgroups.protocols.TCP.start(TCP.java:82)
      at org.jgroups.stack.ProtocolStack.startStack(ProtocolStack.java:952)
      at org.jgroups.JChannel.startStack(JChannel.java:864)

Why is there somehting like 'createMulticastSocket' in this trace? - I do not want to use UDP / Multicasting...

Thanks in advance for help! - badera


回答1:


new JChannel() uses the default config, which is udp.xml (in the JGroups JAR). The error message you're getting is because JGroups can't bind to 127.0.0.1:0 (ephemeral port).

I suggest reuse the channel already provided by Wildfly, see [1] for details. IIRC, Wildfly 9 actually exposes the JGroups channel, but this may not yet be in 8...

[1] https://developer.jboss.org/wiki/ClusteringChangesInWildFly8



来源:https://stackoverflow.com/questions/31922483/how-to-open-a-jchannel-jgroups-using-openshift-wildfly-8-cartridge

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!