Skip to content

Commit 6c0a420

Browse files
committed
Use the new NetServer/NetClient configuration layer in documentation
1 parent 3982f3c commit 6c0a420

2 files changed

Lines changed: 69 additions & 95 deletions

File tree

vertx-core/src/main/asciidoc/net.adoc

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ The simplest way to create a TCP server, using all default options is as follows
1313

1414
=== Configuring a TCP server
1515

16-
If you don't want the default, a server can be configured by passing in a {@link io.vertx.core.net.NetServerOptions}
16+
If you don't want the default, a server can be configured by passing in a {@link io.vertx.core.net.TcpServerConfig}
1717
instance when creating it:
1818

1919
[source,$lang]
@@ -254,7 +254,7 @@ The simplest way to create a TCP client, using all default options is as follows
254254

255255
=== Configuring a TCP client
256256

257-
If you don't want the default, a client can be configured by passing in a {@link io.vertx.core.net.NetClientOptions}
257+
If you don't want the default, a client can be configured by passing in a {@link io.vertx.core.net.TcpClientConfig}
258258
instance when creating it:
259259

260260
[source,$lang]
@@ -285,8 +285,8 @@ When running on JDK 16+, or using a <<_native_transports,native transport>>, a c
285285
=== Configuring connection attempts
286286

287287
A client can be configured to automatically retry connecting to the server in the event that it cannot connect.
288-
This is configured with {@link io.vertx.core.net.NetClientOptions#setReconnectInterval(long)} and
289-
{@link io.vertx.core.net.NetClientOptions#setReconnectAttempts(int)}.
288+
This is configured with {@link io.vertx.core.net.TcpClientConfig#setReconnectInterval(java.time.Duration)} and
289+
{@link io.vertx.core.net.TcpClientConfig#setReconnectAttempts(int)}.
290290

291291
NOTE: Currently, Vert.x will not attempt to reconnect if a connection fails, reconnect attempts and interval
292292
only apply to creating initial connections.
@@ -377,7 +377,7 @@ You should read the <<netty-logging>> section.
377377

378378
TCP server (Net/Http) can be configured with traffic shaping options to enable bandwidth limiting. Both inbound and outbound
379379
bandwidth can be limited through {@link io.vertx.core.net.TrafficShapingOptions}. For NetServer, traffic shaping options can be set
380-
through {@link io.vertx.core.net.NetServerOptions} and for HttpServer it can be set through {@link io.vertx.core.http.HttpServerOptions}.
380+
through {@link io.vertx.core.net.TcpServerConfig} and for HttpServer it can be set through {@link io.vertx.core.http.HttpServerOptions}.
381381

382382
[source,$lang]
383383
----
@@ -408,12 +408,12 @@ TCP clients and servers can be configured to use http://en.wikipedia.org/wiki/Tr
408408
- earlier versions of TLS were known as SSL.
409409

410410
The APIs of the servers and clients are identical whether SSL/TLS is used, and it's enabled by configuring
411-
the {@link io.vertx.core.net.NetClientOptions} or {@link io.vertx.core.net.NetServerOptions} instances used
411+
the {@link io.vertx.core.net.TcpClientConfig} or {@link io.vertx.core.net.TcpServerConfig} instances used
412412
to create the servers or clients.
413413

414414
==== Enabling SSL/TLS on the server
415415

416-
Server SSL/TLS is enabled with the `NetServerOptions` {@link io.vertx.core.net.NetServerOptions#setSsl(boolean) ssl} setting.
416+
Server SSL/TLS is enabled with the `TcpServerConfig` {@link io.vertx.core.net.TcpServerConfig#setSsl(boolean) ssl} setting.
417417

418418
By default, it is disabled.
419419

@@ -426,7 +426,7 @@ You can read more about <<server_ssl,SSL server configuration>>
426426

427427
==== Enabling SSL/TLS on the client
428428

429-
Client SSL/TLS is enabled with the `NetClientOptions` {@link io.vertx.core.net.NetClientOptions#setSsl(boolean) ssl} property or {@link io.vertx.core.net.ConnectOptions#setSsl(boolean) ssl} property.
429+
Client SSL/TLS is enabled with the `TcpClientConfig` {@link io.vertx.core.net.TcpClientConfig#setSsl(boolean) ssl} property or {@link io.vertx.core.net.ConnectOptions#setSsl(boolean) ssl} property.
430430

431431
The former defines the default client behavior.
432432

@@ -535,7 +535,7 @@ The engine options to use is
535535

536536
The {@link io.vertx.core.net.NetClient} supports either an HTTP/1.x _CONNECT_, _SOCKS4a_ or _SOCKS5_ proxy.
537537

538-
The proxy can be configured in the {@link io.vertx.core.net.NetClientOptions} by setting a
538+
The proxy can be configured in the {@link io.vertx.core.net.TcpClientConfig} by setting a
539539
{@link io.vertx.core.net.ProxyOptions} object containing proxy type, hostname, port and optionally username and password.
540540

541541
Here's an example:
@@ -549,7 +549,7 @@ Here's an example:
549549
The DNS resolution is always done on the proxy server, to achieve the functionality of a SOCKS4 client, it is necessary
550550
to resolve the DNS address locally.
551551

552-
You can use {@link io.vertx.core.net.NetClientOptions#setNonProxyHosts} to configure a list of host bypassing
552+
You can use {@link io.vertx.core.net.TcpClientConfig#setNonProxyHosts} to configure a list of host bypassing
553553
the proxy. The lists accepts `*` wildcard for matching domains:
554554

555555
[source,$lang]
@@ -564,7 +564,7 @@ https://www.haproxy.org/download/1.8/doc/proxy-protocol.txt[HA PROXY protocol] p
564564
information such as a client's address across multiple layers of NAT or TCP
565565
proxies.
566566

567-
HA PROXY protocol can be enabled by setting the option {@link io.vertx.core.net.NetServerOptions#setUseProxyProtocol(boolean)}
567+
HA PROXY protocol can be enabled by setting the option {@link io.vertx.core.net.TcpServerConfig#setUseProxyProtocol(boolean)}
568568
and adding the following dependency in your classpath:
569569

570570
[source,xml]

vertx-core/src/main/java/examples/NetExamples.java

Lines changed: 58 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,11 @@
1717
import io.vertx.core.VerticleBase;
1818
import io.vertx.core.Vertx;
1919
import io.vertx.core.buffer.Buffer;
20-
import io.vertx.core.http.ClientAuth;
2120
import io.vertx.core.http.HttpServer;
2221
import io.vertx.core.http.HttpServerOptions;
2322
import io.vertx.core.net.*;
2423

25-
import java.security.cert.CertificateException;
26-
import java.util.Arrays;
24+
import java.time.Duration;
2725
import java.util.concurrent.TimeUnit;
2826

2927
/**
@@ -38,8 +36,10 @@ public void example1(Vertx vertx) {
3836

3937
public void example2(Vertx vertx) {
4038

41-
NetServerOptions options = new NetServerOptions().setPort(4321);
42-
NetServer server = vertx.createNetServer(options);
39+
TcpServerConfig config = new TcpServerConfig()
40+
.setPort(4321);
41+
42+
NetServer server = vertx.createNetServer(config);
4343
}
4444

4545
public void example3(Vertx vertx) {
@@ -206,13 +206,16 @@ public void example13(Vertx vertx) {
206206

207207
public void example14(Vertx vertx) {
208208

209-
NetClientOptions options = new NetClientOptions().setConnectTimeout(10000);
210-
NetClient client = vertx.createNetClient(options);
209+
TcpClientConfig config = new TcpClientConfig()
210+
.setConnectTimeout(Duration.ofSeconds(10));
211+
212+
NetClient client = vertx.createNetClient(config);
211213
}
212214

213215
public void example15(Vertx vertx) {
214216

215-
NetClientOptions options = new NetClientOptions().setConnectTimeout(10000);
217+
TcpClientConfig options = new TcpClientConfig()
218+
.setConnectTimeout(Duration.ofSeconds(10));
216219
NetClient client = vertx.createNetClient(options);
217220
client
218221
.connect(4321, "localhost")
@@ -228,32 +231,34 @@ public void example15(Vertx vertx) {
228231

229232
public void example16(Vertx vertx) {
230233

231-
NetClientOptions options = new NetClientOptions().
234+
TcpClientConfig options = new TcpClientConfig().
232235
setReconnectAttempts(10).
233-
setReconnectInterval(500);
236+
setReconnectInterval(Duration.ofMillis(500));
234237

235238
NetClient client = vertx.createNetClient(options);
236239
}
237240

238241
public void exampleNetworkActivityLoggingOnServer(Vertx vertx) {
239242

240-
NetServerOptions options = new NetServerOptions().setLogActivity(true);
243+
TcpServerConfig options = new TcpServerConfig()
244+
.setNetworkLogging(new NetworkLogging());
241245

242246
NetServer server = vertx.createNetServer(options);
243247
}
244248

245249
public void exampleNetworkActivityLoggingFormat(Vertx vertx) {
246250

247-
NetServerOptions options = new NetServerOptions()
248-
.setLogActivity(true)
249-
.setActivityLogDataFormat(ByteBufFormat.SIMPLE);
251+
TcpServerConfig options = new TcpServerConfig()
252+
.setNetworkLogging(new NetworkLogging()
253+
.setDataFormat(ByteBufFormat.SIMPLE));
250254

251255
NetServer server = vertx.createNetServer(options);
252256
}
253257

254258
public void exampleNetworkActivityLoggingOnClient(Vertx vertx) {
255259

256-
NetClientOptions options = new NetClientOptions().setLogActivity(true);
260+
TcpClientConfig options = new TcpClientConfig()
261+
.setNetworkLogging(new NetworkLogging());
257262

258263
NetClient client = vertx.createNetClient(options);
259264
}
@@ -266,18 +271,18 @@ public void sslServerConfiguration(Vertx vertx) {
266271
setPassword("password-of-your-keystore")
267272
);
268273

269-
NetServerOptions options = new NetServerOptions()
270-
.setSsl(true)
271-
.setSslOptions(sslOptions);
274+
TcpServerConfig config = new TcpServerConfig()
275+
.setSsl(true);
272276

273-
NetServer server = vertx.createNetServer(options);
277+
NetServer server = vertx.createNetServer(config, sslOptions);
274278
}
275279

276280
public void example29(Vertx vertx) {
277-
NetClientOptions options = new NetClientOptions().
278-
setSsl(true).
279-
setTrustAll(true);
280-
NetClient client = vertx.createNetClient(options);
281+
TcpClientConfig config = new TcpClientConfig()
282+
.setSsl(true);
283+
ClientSSLOptions sslOptions = new ClientSSLOptions()
284+
.setTrustAll(true);
285+
NetClient client = vertx.createNetClient(config, sslOptions);
281286
}
282287

283288
public void sslClientConfiguration(Vertx vertx) {
@@ -287,11 +292,10 @@ public void sslClientConfiguration(Vertx vertx) {
287292
setPassword("password-of-your-truststore")
288293
);
289294

290-
NetClientOptions options = new NetClientOptions()
291-
.setSsl(true)
292-
.setSslOptions(sslOptions);
295+
TcpClientConfig config = new TcpClientConfig()
296+
.setSsl(true);
293297

294-
NetClient client = vertx.createNetClient(options);
298+
NetClient client = vertx.createNetClient(config, sslOptions);
295299
}
296300

297301
public void sslClientSocketConfiguration(Vertx vertx, int port, String host) {
@@ -301,9 +305,9 @@ public void sslClientSocketConfiguration(Vertx vertx, int port, String host) {
301305
setPassword("password-of-your-truststore")
302306
);
303307

304-
NetClientOptions options = new NetClientOptions().setSslOptions(sslOptions);
308+
TcpClientConfig config = new TcpClientConfig();
305309

306-
NetClient client = vertx.createNetClient(options);
310+
NetClient client = vertx.createNetClient(config, sslOptions);
307311

308312
Future<NetSocket> future = client.connect(new ConnectOptions()
309313
.setHost(host)
@@ -358,60 +362,53 @@ public void updateSSLOptions(HttpServer server) {
358362
public void exampleSSLEngine(Vertx vertx, JksOptions keyStoreOptions) {
359363

360364
// Use JDK SSL engine
361-
NetServerOptions options = new NetServerOptions().
362-
setSsl(true).
363-
setKeyCertOptions(keyStoreOptions);
365+
TcpServerConfig options = new TcpServerConfig().
366+
setSsl(true);
364367

365368
// Use JDK SSL engine explicitly
366-
options = new NetServerOptions().
369+
options = new TcpServerConfig().
367370
setSsl(true).
368-
setKeyCertOptions(keyStoreOptions).
369371
setSslEngineOptions(new JdkSSLEngineOptions());
370372

371373
// Use OpenSSL engine
372-
options = new NetServerOptions().
374+
options = new TcpServerConfig().
373375
setSsl(true).
374-
setKeyCertOptions(keyStoreOptions).
375376
setSslEngineOptions(new OpenSSLEngineOptions());
376377
}
377378

378-
public void example46(Vertx vertx, String verificationAlgorithm, ClientSSLOptions sslOptions) {
379-
NetClientOptions options = new NetClientOptions().
380-
setSsl(true).
381-
setSslOptions(sslOptions).
382-
setHostnameVerificationAlgorithm(verificationAlgorithm);
379+
public void example46(Vertx vertx, String verificationAlgorithm, TrustOptions trustOptions) {
380+
TcpClientConfig config = new TcpClientConfig().
381+
setSsl(true);
383382

384-
NetClient client = vertx.createNetClient(options);
383+
ClientSSLOptions sslOptions = new ClientSSLOptions()
384+
.setTrustOptions(trustOptions)
385+
.setHostnameVerificationAlgorithm(verificationAlgorithm);
386+
387+
NetClient client = vertx.createNetClient(config, sslOptions);
385388
}
386389

387390
public void example47(Vertx vertx) {
388-
NetClientOptions options = new NetClientOptions()
391+
TcpClientConfig config = new TcpClientConfig()
389392
.setProxyOptions(new ProxyOptions().setType(ProxyType.SOCKS5)
390393
.setHost("localhost").setPort(1080)
391394
.setUsername("username").setPassword("secret"));
392-
NetClient client = vertx.createNetClient(options);
395+
NetClient client = vertx.createNetClient(config);
393396
}
394397

395398
public void nonProxyHosts(Vertx vertx) {
396399

397-
NetClientOptions options = new NetClientOptions()
400+
TcpClientConfig config = new TcpClientConfig()
398401
.setProxyOptions(new ProxyOptions().setType(ProxyType.SOCKS5)
399402
.setHost("localhost").setPort(1080)
400403
.setUsername("username").setPassword("secret"))
401404
.addNonProxyHost("*.foo.com")
402405
.addNonProxyHost("localhost");
403-
NetClient client = vertx.createNetClient(options);
404-
}
405-
406-
public void example49() {
407-
NetClientOptions clientOptions = new NetClientOptions()
408-
.setSsl(true)
409-
.setTrustAll(true);
406+
NetClient client = vertx.createNetClient(config);
410407
}
411408

412409
public void example51(Vertx vertx) {
413-
NetServerOptions options = new NetServerOptions().setUseProxyProtocol(true);
414-
NetServer server = vertx.createNetServer(options);
410+
TcpServerConfig config = new TcpServerConfig().setUseProxyProtocol(true);
411+
NetServer server = vertx.createNetServer(config);
415412
server.connectHandler(so -> {
416413
// Print the actual client address provided by the HA proxy protocol instead of the proxy address
417414
System.out.println(so.remoteAddress());
@@ -421,34 +418,11 @@ public void example51(Vertx vertx) {
421418
});
422419
}
423420

424-
public void configureSNIServer(Vertx vertx) {
425-
JksOptions keyCertOptions = new JksOptions().setPath("keystore.jks").setPassword("wibble");
426-
427-
NetServer netServer = vertx.createNetServer(new NetServerOptions()
428-
.setKeyCertOptions(keyCertOptions)
429-
.setSsl(true)
430-
.setSni(true)
431-
);
432-
}
433-
434-
public void configureSNIServerWithPems(Vertx vertx) {
435-
PemKeyCertOptions keyCertOptions = new PemKeyCertOptions()
436-
.setKeyPaths(Arrays.asList("default-key.pem", "host1-key.pem", "etc..."))
437-
.setCertPaths(Arrays.asList("default-cert.pem", "host2-key.pem", "etc...")
438-
);
439-
440-
NetServer netServer = vertx.createNetServer(new NetServerOptions()
441-
.setKeyCertOptions(keyCertOptions)
442-
.setSsl(true)
443-
.setSni(true)
444-
);
445-
}
446-
447421
public void useSNIInClient(Vertx vertx, JksOptions trustOptions) {
448422

449-
NetClient client = vertx.createNetClient(new NetClientOptions()
450-
.setTrustOptions(trustOptions)
451-
.setSsl(true)
423+
NetClient client = vertx.createNetClient(
424+
new TcpClientConfig().setSsl(true),
425+
new ClientSSLOptions().setTrustOptions(trustOptions)
452426
);
453427

454428
// Connect to 'localhost' and present 'server.name' server name
@@ -465,25 +439,25 @@ public void useSNIInClient(Vertx vertx, JksOptions trustOptions) {
465439
}
466440

467441
public void configureTrafficShapingForNetServer(Vertx vertx) {
468-
NetServerOptions options = new NetServerOptions()
442+
TcpServerConfig config = new TcpServerConfig()
469443
.setHost("localhost")
470444
.setPort(1234)
471445
.setTrafficShapingOptions(new TrafficShapingOptions()
472446
.setInboundGlobalBandwidth(64 * 1024)
473447
.setOutboundGlobalBandwidth(128 * 1024));
474448

475-
NetServer server = vertx.createNetServer(options);
449+
NetServer server = vertx.createNetServer(config);
476450
}
477451

478452
public void dynamicallyUpdateTrafficShapingForNetServer(Vertx vertx) {
479-
NetServerOptions options = new NetServerOptions()
453+
TcpServerConfig config = new TcpServerConfig()
480454
.setHost("localhost")
481455
.setPort(1234)
482456
.setTrafficShapingOptions(new TrafficShapingOptions()
483457
.setInboundGlobalBandwidth(64 * 1024)
484458
.setOutboundGlobalBandwidth(128 * 1024));
485459

486-
NetServer server = vertx.createNetServer(options);
460+
NetServer server = vertx.createNetServer(config);
487461

488462
TrafficShapingOptions update = new TrafficShapingOptions()
489463
.setInboundGlobalBandwidth(2 * 64 * 1024) // twice

0 commit comments

Comments
 (0)