简体   繁体   English

如何在 Java 服务器中共享套接字连接会话

[英]How to share socket connection sessions in java servers

If you are familiar with XMPP servers , then you must know openfire.如果你熟悉 XMPP 服务器,那么你一定知道 openfire。 im planning to write a JSON version of the openfire (imo xml is outdated and json is alot faster,and openfire is slower than its mate, Tigase, but they both have issues), but the only part that i'm stuck at is, when clustering (for example using HazzelCast) and a contact sends a message to an instance of server, but the target is on another instance, then i either need to have the connection shared or i should use a sort of memory based queue to send the packet to the target.我打算写一个 JSON 版本的 openfire(imo xml 已经过时了,json 快了很多,而且 openfire 比它的伙伴 Tigase 慢,但它们都有问题),但我唯一遇到的问题是,当集群(例如使用 HazzelCast)和联系人向服务器实例发送消息,但目标在另一个实例上时,那么我要么需要共享连接,要么我应该使用一种基于内存的队列来发送包到目标。

I tried to read the openfire source but it was too complicated , so i decided to share my question here:我试图阅读 openfire 源代码,但它太复杂了,所以我决定在这里分享我的问题:

How can serveral clustered servers use a single TCP connetion to send packet?多个集群服务器如何使用单个 TCP 连接发送数据包?

I'm going to use netty for a network framework.我将使用 netty 作为网络框架。

A TCP connection it's an abstraction of two pairs of IP_ADDR + PORT, one belonging to the server and the other to the client (more precisely, it's the pair of channels associated with the endpoint). TCP 连接是两对 IP_ADDR + PORT 的抽象,一对属于服务器,另一对属于客户端(更准确地说,它是与端点关联的一对通道)。 Given this premise, you cannot have a TCP connection shared amongst servers having different ip addresses.鉴于此前提,您不能在具有不同 IP 地址的服务器之间共享 TCP 连接。

In Java 9 SO_REUSEPORT will be supported and you will be able to have multiple server sockets sharing the same pair IP_ADDR + PORT.在 Java 9 中将支持 SO_REUSEPORT,您将能够让多个服务器套接字共享同一对 IP_ADDR + PORT。 As far as I know Netty already does that for you.据我所知,Netty 已经为你做到了。

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

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