简体   繁体   English

我们可以对 REST 服务进行 UDP 调用吗

[英]Can we make a UDP call to REST service

I have a REST endpoint and I want to access it using UDP for example Java Datagram.我有一个 REST 端点,我想使用 UDP 访问它,例如 Java 数据报。 I know its not a best practice to try even but my recent project I have some hardware limitations.我知道尝试甚至不是最佳实践,但我最近的项目有一些硬件限制。 Hardware can make UDP calls only and my existing services are over REST ie HTTP/HTTPS.硬件只能进行 UDP 调用,而我现有的服务基于 REST,即 HTTP/HTTPS。
I am looking for any way I can reuse my existing services.我正在寻找可以重用现有服务的任何方式。 I have tried following code but received UnknownHostException .我尝试了以下代码,但收到了UnknownHostException

public class UDPClinet {
    public static void main(String[] args) {
        String hostname = "https://jsonplaceholder.typicode.com/posts/1";
        int port = 80;
        try {
            InetAddress address = InetAddress.getByName(hostname);
            DatagramSocket socket = new DatagramSocket();
            while (true) {
                DatagramPacket request = new DatagramPacket(new byte[1], 1, address, port);
                socket.send(request);
                byte[] buffer = new byte[512];
                DatagramPacket response = new DatagramPacket(buffer, buffer.length);
                socket.receive(response);
                String quote = new String(buffer, 0, response.getLength());
                System.out.println(quote);
                System.out.println();
                Thread.sleep(10000);
            }
        } catch (SocketTimeoutException ex) {
            System.out.println("Timeout error: " + ex.getMessage());
            ex.printStackTrace();
        } catch (IOException ex) {
            System.out.println("Client error: " + ex.getMessage());
            ex.printStackTrace();
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
    }
}

Is it possible to implement a RESTful service that can be called via UDP.是否可以实现一个可以通过 UDP 调用的 RESTful 服务。

Yes.是的。 (See below) (见下文)

Can you call your existing RESTful service via UDP?您可以通过 UDP 调用现有的 RESTful 服务吗?

Probably no.可能没有。 And certainly not without a lot of work.当然不是没有很多工作。


Typical RESTful services are in fact implemented using HTTP or HTTPS over TCP/IP connections.典型的 RESTful 服务实际上是通过 TCP/IP 连接使用 HTTP 或 HTTPS 实现的。 It is not possible to talk directly to an TCP-based service using UDP.不可能使用 UDP 直接与基于 TCP 的服务对话。 The IP-level packets will have the wrong protocol family and the service's OS won't route them to the service. IP 级数据包将具有错误的协议族,服务的操作系统不会将它们路由到服务。

However, it is possible (technically speaking) to implement RESTful services over any transport that is capable of sending messages.但是,(从技术上讲)可以通过任何能够发送消息的传输实现 RESTful 服务。 REST principles are agnostic of the transport protocol. REST 原则与传输协议无关。

The problem will be finding a service framework that support RESTful UDP and (conventional) RESTful HTTP at the same time .该问题将被找到服务框架支持的RESTful UDP(常规)的RESTful HTTP同时

There are a couple of other practical problems:还有一些其他的实际问题:

  • UDP is unreliable, and this is exacerbated if you send datagrams that won't fit into a packet with the default MTU (1500 bytes). UDP 是不可靠的,如果您发送的数据报不适合具有默认 MTU(1500 字节)的数据包,则情况会更加严重。 So if you want to implement a RESTful service over UDP, you will need to play close attention to the size of request and response payloads.因此,如果您想通过 UDP 实现 RESTful 服务,则需要密切注意请求和响应负载的大小。

  • HTTPS uses TLS so that the client is able to validate the server's authenticity and then send data encrypted. HTTPS 使用 TLS,以便客户端能够验证服务器的真实性,然后发送加密数据。 TLS over UDP is possible (it is calledDTLS ) and supported by JCSE, but using it in a typical RESTful / HTTP framework may be challenging. UDP 上的 TLS 是可能的(称为DTLS )并由 JCSE 支持,但在典型的 RESTful / HTTP 框架中使用它可能具有挑战性。

If you want to pursue this, look for a RESTful framework that implements CoAP (Constrained Application Protocol - RFC 7252) and DTLS.如果您想追求这一点,请寻找实现CoAP (受限应用程序协议 - RFC 7252)和 DTLS 的 RESTful 框架。

You may have a look at available CoAP implementations, see coap.technology for an overview.您可以查看可用的 CoAP 实现,请参阅coap.technology以获得概述。 Though I'm a committer of Eclipse/Californium , a CoAP/DTLS implementation in java, I would recommend to try that out.尽管我是Eclipse/Californium (Java 中的 CoAP/DTLS 实现)的提交者,但我建议您尝试一下。

CoAP also defines Cross Proxies to HTTP. CoAP 还定义了 HTTP 的交叉代理。 There is one example available in the google cloud tutorials . 谷歌云教程中提供了一个示例。 It's using a older Californium version as base.它使用较旧的 Californium 版本作为基础。 Californium itself has an update proxy exmaple (but not out of the box usable for the google cloud), see Californium - Proxy2 . Californium 本身有一个更新代理示例(但不是开箱即用的可用于谷歌云),请参阅Californium - Proxy2

Yes, there's a internet standard now for REST over UDP, it's goes by the name the Constrained Application Protocoal - CoAP .是的,现在有一个基于 UDP 的 REST 的互联网标准,它的名称是受约束的应用程序协议 - CoAP CoApp is defined in a series of internet standards starting with RFC 7252 . CoApp 是在一系列互联网标准中定义的,从RFC 7252开始。

CoAP addresses concerns raised in earlier answers to this question, including using retransmissions over UDP to make it reliable, and providing for security using DTLS. CoAP 解决了早先对该问题的回答中提出的问题,包括使用 UDP 上的重传使其可靠,并使用 DTLS 提供安全性。

CoAP has been designed to work on microcontrollers with as low as 10 KiB of RAM and 100 KiB of code space ( RFC 7228 ). CoAP 设计用于低至 10 KiB RAM 和 100 KiB 代码空间的微控制器 ( RFC 7228 )。 Furthermore it adds very little overhead to the UDP packets, using a binary fixed header of 4 bytes, and variable length options (like optional HTTP headers)此外,它使用 4 字节的二进制固定标头和可变长度选项(如可选的 HTTP 标头)为 UDP 数据包增加了很少的开销

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

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