简体   繁体   English

Java服务器到服务器接口

[英]Java server-to-server interface

I have to design a (java) interface for server-to-server communication, but I have no experience what so ever in this field. 我必须设计用于服务器到服务器通信的(java)接口,但是我对此领域没有任何经验。

My Server fetches information from different sources. 我的服务器从不同来源获取信息。 Other servers shall be able to connect to the server and get the current information as well as receive changes via push notifications. 其他服务器应能够连接到该服务器并获取当前信息,并通过推送通知接收更改。 The interface should not depend on certain programming languages or operating systems for those other servers. 该接口不应依赖于其他服务器的某些编程语言或操作系统。 My first thought was to use sockets and leave the socket open to transmit changes. 我的第一个想法是使用套接字并使套接字保持打开状态以传输更改。 Is there a better way to this? 有更好的方法吗?

I guess working in a Java EE environment the easiest would be using web services. 我猜最容易在Java EE环境中使用Web服务。 But if this is not the case and/or you need pushing, I would recommend using some library. 但是,如果不是这种情况,并且/或者您需要推送,我建议您使用一些库。 For example xSocket that uses non-blocking IO sockets and can send and receive any string messages asynchronously. 例如,使用无阻塞IO套接字的xSocket可以异步发送和接收任何字符串消息。 From there on you can use for example JAXB binders to send XML messages or any other format. 从那里开始,您可以使用例如JAXB活页夹发送XML消息或任何其他格式。

Due to your requirement to be able to push data to the client, while this is obviously possible with sockets, they do introduce other complexities and so, imho, the easiest solution would be to use a message queue. 由于您要求能够将数据推送到客户端,尽管套接字显然可以做到这一点,但它们确实带来了其他复杂性,因此,恕我直言,最简单的解决方案是使用消息队列。

To use a message queue, the client would post a message to a known queue that the server is listening to. 要使用消息队列,客户端会将消息发布到服务器正在侦听的已知队列中。 This message should contain a replyTo destination (typically a temporary queue owned by the client). 此消息应包含一个replyTo目标(通常是客户端拥有的临时队列)。 On receiving a message, the server should register the replyTo destination, sending any notifications to this replyTo destination. 接收到消息后,服务器应注册replyTo目标,并将所有通知发送到该replyTo目标。

I've normally found it easier to use the notification mechanism for all data - both the initial load and the updates - as it prevents the client from having to support two mechanisms to obtain data. 通常,我发现对所有数据使用通知机制(包括初始加载和更新)更加容易,因为它避免了客户端必须支持两种机制来获取数据。 This is very easy to do, as your notification messages will need to indicate what type of event is occurring (add, update, delete for example) and you just need to introduce an initialize type to this list. 这很容易做到,因为您的通知消息将需要指示正在发生的事件类型(例如,添加,更新,删除),而您只需要在此列表中引入一个初始化类型即可。

If you require good performance with real-time data, you can have a look at redis which is a nosql database all-in-memory that supports publish/subscribe and has many clients for all major languages. 如果您需要实时数据的良好性能,则可以查看redis,它是一种nosql数据库全内存,支持发布/预订,并具有许多支持所有主要语言的客户端。

Your publishing server will listen to events and push data into redis thanks to jedis (redis client for java) for instance, then thanks to the publish/subscribe support of redis, your other servers that subscribed to the redis channel will get updates. 例如,由于使用jedis(Java的redis客户端),您的发布服务器将侦听事件并将数据推送到Redis中,然后由于对Redis的发布/订阅的支持,订阅了Redis通道的其他服务器将获得更新。 Basically you use redis as a message broker, and it works well. 基本上,您将redis用作消息代理,并且效果很好。 If you don't want to add intelligence on reads, redis is definitely a good choice. 如果您不想增加读取智能,那么redis绝对是一个不错的选择。

You will however need to deploy it on a linux server (no windows for production, but you can use a windows port of redis for developping). 但是,您需要将其部署在linux服务器上(没有用于生产的Windows,但是您可以使用redis的Windows端口进行开发)。

Redis adds complexity but also has many great features (zsets are wonderful for stocking rankings of data) and as it is all in memory (you will have to check if you have enough RAM though on your server), the performance is very great. Redis增加了复杂性,但又具有许多出色的功能(zset非常适合存储数据排名),并且全部存储在内存中(您必须检查服务器上是否有足够的RAM),因此性能非常好。

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

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