简体   繁体   English

如何在Netty服务器中使用Netty客户端

[英]How to use Netty clients within Netty server

I'm going to create an authentication server which itself interacts with a set of different Oauth2.0 servers. 我将创建一个身份验证服务器,该服务器本身会与一组不同的Oauth2.0服务器交互。 Netty seems to be a good candidate to implement network part here. Netty似乎是在此处实施网络部分的一个不错的选择。 But before start I need to clear some details about netty as I'm new to it. 但是在开始之前,我需要清除一些有关netty的细节,因为我是新手。 The routine will be as follows: 该例程将如下所示:

  1. The server accepts an HTTPS connection from a client. 服务器接受来自客户端的HTTPS连接。

  2. Then, not closing this first connection, it makes another connection via HTTPS to a remote Oauth2.0 server and gets data 然后,不关闭第一个连接,而是通过HTTPS与远程Oauth2.0服务器建立另一个连接并获取数据

  3. After all, the server sends the result back to the client which is supposed to keep the connection alive. 毕竟,服务器会将结果发送回客户端,该客户端应该保持连接处于活动状态。

How to implement this scenario with Netty? 如何用Netty实施此方案? Do I have to create a new netty client and/or reconnect it each time I need to connect to a remote Oauth2.0 server? 每当我需要连接到远程Oauth2.0服务器时,是否必须创建一个新的netty客户端和/或重新连接它? If so, I'll have to create a separate thread for every outgoing connection which will drastically reduce performance. 如果是这样,我将必须为每个传出连接创建一个单独的线程,这将大大降低性能。 Another scenario is to create a sufficient number of Netty clients within a server at the beginning (when server starts) and keep them constantly connected to the Oauth2.0 servers via HTTPS. 另一种情况是在服务器开始时(服务器启动时)在服务器中创建足够数量的Netty客户端,并使它们通过HTTPS持续连接到Oauth2.0服务器。

That's easily done with Netty. 使用Netty可以轻松完成。 First you set up your Netty server using the ServerBootstrap and then in a ChannelHandler that handles your connection from the client you can use eg the client Bootstrap to connect to the OAuth server and fetch the data. 首先,您使用ServerBootstrap设置Netty服务器,然后在处理来自客户端的连接的ChannelHandler ,可以使用例如客户端Bootstrap连接到OAuth服务器并获取数据。 You don't need to worry about creating threads or similar. 您无需担心创建线程或类似问题。 You can do it all in a non-blocking fashion. 您可以以非阻塞方式完成所有操作。 Take a look at and try to understand how this example works: 看一下并尝试了解此示例的工作方式:

https://github.com/netty/netty/blob/master/example/src/main/java/io/netty/example/proxy/HexDumpProxyFrontendHandler.java#L44 . https://github.com/netty/netty/blob/master/example/src/main/java/io/netty/example/proxy/HexDumpProxyFrontendHandler.java#L44

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

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