组通信之jgroups篇----Adress,View,Message,Event

3.3. Address
Each member of a group has an address, which uniquely identifies the member. The interface for such an address is Address, which requires concrete implementations to provide methods for comparison and sorting of addresses, and for determination whether the address is a multicast address. JGroups addresses have to implement the following interface:

            public interface Address extends Externalizable, Comparable, Cloneable {
                boolean isMulticastAddress();
                int compareTo(Object o) throws ClassCastException;
                boolean equals(Object obj);
                int hashCode();
                String toString();
            }
Please never use implementations of Address directly; Address should always be used as an opaque identifier of a cluster node !

Actual implementations of addresses are often generated by the bottommost protocol layer (e.g. UDP or TCP). This allows for all possible sorts of addresses to be used with JGroups, e.g. ATM.

In JChannel, it is the IP address of the host on which the stack is running and the port on which the stack is receiving incoming messages; it is represented by the concrete class org.jgroups.stack.IpAddress. Instances of this class are only used within the JChannel protocol stack; users of a channel see addresses (of any kind) only as Addresses. Since an address uniquely identifies a channel, and therefore a group member, it can be used to send messages to that group member, e.g. in Messages (see next section).

In 2.8, the default implementation of Address was changed from IpAddress to org.jgroups.util.UUID.

组内每个成员都有一个地址,它唯一标志该成员.其接口即为Address.它需要具体实现如下的一些方法.

不要直接使用Adress的实现(对象),应将Adress当作集群节点不透明标志符使用.

地址的具体实现一般在最底层协议生成,它允许JGroups使用所有可能的地址类型.

在JChannel层,使用主机的IP地址,它由org.jgroups.stack.IpAddress类实现.此类的对象只在协议栈中使用,用户通过channel得到的仍然是Adress对象,Adress唯一代表了channel,代表了组成员.

在2.8版本中,Adress的默认实现已由IpAddress转变为org.jgroups.util.UUID.

 

3.5. View
A View ( View ) is a list of the current members of a group. It consists of a ViewId , which uniquely identifies the view(see below), and a list of members. Views are set in a channel automatically by the underlying protocol stack whenever a new member joins or an existing one leaves (or crashes). All members of a group see the same sequence of views.

Note that there is a comparison function which orders all the members of a group in the same way. Usually, the first member of the list is the coordinator (the one who emits new views). Thus, whenever the membership changes, every member can determine the coordinator easily and without having to contact other members.

The code below shows how to send a (unicast) message to the first member of a view (error checking code omitted):

            View view=channel.getView();
            Address first=view.getMembers().first();
            Message msg=new Message(first, null, "Hello world");
            channel.send(msg);
Whenever an application is notified that a new view has been installed (e.g. by Receiver.viewAccepted(), the view is already set in the channel. For example, calling Channel.getView() in a viewAccepted() callback would return the same view (or possibly the next one in case there has already been a new view !).

3.5.1. ViewId
The ViewId is used to uniquely number views. It consists of the address of the view creator and a sequence number. ViewIds can be compared for equality and put in a hashtable as they implement equals() and hashCode() methods.

3.5.2. MergeView
Whenever a group splits into subgroups, e.g. due to a network partition, and later the subgroups merge back together, a MergeView instead of a View will be received by the application. The MergeView class is a subclass of View and contains as additional instance variable the list of views that were merged. As an example if the group denoted by view V1:(p,q,r,s,t) split into subgroups V2:(p,q,r) and V2:(s,t) , the merged view might be V3:(p,q,r,s,t) . In this case the MergeView would contains a list of 2 views: V2:(p,q,r) and V2:(s,t) .

view指的是某个组的当前所有成员列表,它包含有viewID,它唯一标志组.还包含成员列表.当有成员加入,退出,异常时,channel中的view会被底层协议栈自动赋值.每个成员的view是一致有序的.

有比较函数可以将组内成员以相同的方式排序.一般的,第一个成员是主,不管组成员关系什么时候发生变化,每个成员都不需要跟其他成员通信就能决定出新的主.

下面的代码显示了如何单播发送消息到view中的第一个成员.

无论何时应用程序被通知有新的view产生,channel中的view都已经被设置.

3.5.1. ViewId

包含一个序列号和该view的创建者,viewId可以被比较

3.5.2. MergeView

当一个组被切分成几个子组,如网络分区,随后,多个子组又重新组合到一起.应用程序会收到MergeView而不是View.MergeView是View的子类,且包含要被组合的视图列表.举例:如果某个组的视图V1:(p,q,r,s,t) 被分成两个组 V2:(p,q,r) 和 V2:(s,t),组合后的试图将会是V3:(p,q,r,s,t),

MergeView将包含有一个列表,该列表含有V2:(p,q,r) 和 V2:(s,t).

 

3.4. Message

Data is sent between members in the form of messages ( org.jgroups.Message ). A message can be sent by a member to a single member , or to all members of the group of which the channel is an endpoint.

A message contains 5 fields:

Destination address

The address of the receiver. If null , the message will be sent to all current group members

Source address

The address of the sender. Can be left null , and will be filled in by the transport protocol (e.g. UDP) before the message is put on the network

Flags

This is one byte used for flags. The currently recognized flags are OOB, LOW_PRIO and HIGH_PRIO. See the discussion on the concurrent stack for OOB.

Payload

The actual data (as a byte buffer). The Message class contains convenience methods to set a serializable object and to retrieve it again, using serialization to convert the object to/from a byte buffer.

Headers

A list of headers that can be attached to a message. Anything that should not be in the payload can be attached to a message as a header. Methods putHeader() , getHeader() and removeHeader() of Message can be used to manipulate headers.

A message is similar to an IP packet and consists of the payload (a byte buffer) and the addresses of the sender and receiver (as Addresses). Any message put on the network can be routed to its destination (receiver address), and replies can be returned to the sender's address.

A message usually does not need to fill in the sender's address when sending a message; this is done automatically by the protocol stack before a message is put on the network. However, there may be cases, when the sender of a message wants to give an address different from its own, so that for example, a response should be returned to some other member.

The destination address (receiver) can be an Address, denoting the address of a member, determined e.g. from a message received previously, or it can be null , which means that the message will be sent to all members of the group. A typical multicast message, sending string "Hello" to all members would look like this:

            Message msg=new Message(null, null, "Hello".getBytes());

            channel.send(msg);

数据以消息的形式在成员间发送.消息可以发送到一个成员或发送到当前组内所有成员.

1个消息包含5个部分:

目的地址:接收端地址,如果为空,则发送到组内所有成员.

源端地址:发送端地址,可以为空,将在传输层填充地址,然后将消息发送到网络上.

标志位:1个字节的标志位,现在能支持的有 OOB, LOW_PRIO和HIGH_PRIO.

有效负载:真实的数据.消息类有方便的方法去设置一个流式对象或得到一个流式对象.

头:头队列可以加到消息内,所有不在有效负载内的信息都可以加到头中.

消息类似于一个IP包,包含有效负载,发送端地址和接收端地址,网络上的消息可以到达它的目的端,同时回应消息也能返回到发送端.

消息通常不需要填充发送端地址,这将自动在协议栈中处理,然后消息才会发送到网络上.但如果想给此消息设置一个非本地的地址,那回应消息则发到另一个成员上了.

接受端地址一般可以为一个Address,表示一个成员的地址(该地址通常由上次接收到的消息所决定),也可以为空,表示此消息发送到组内所有成员.一个典型的组播消息,发送"你好"到所有成员如下.

 

 

1.4. Header

A header is a custom bit of information that can be added to each message. JGroups uses headers extensively, for example to add sequence numbers to each message (NAKACK and UNICAST), so that those messages can be de-

 

报文头是约定的一些少量信息,可以加在每个消息上.JGroups广泛使用报文头,比如增加序列号到消息里,可以保证消息传递的有序性.

 

1.5. Event

Events are means by which JGroups protcols can talk to each other. Contrary to Messages, which travel over the network between group members, events only travel up and down the stack.

 

事件保证JGroups各协议可以互相交流.不同于消息,它在网络中组成员中传递,而事件只在协议栈中上下传输.

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值