reactor.netty.http.client.HttpClient Java Examples
The following examples show how to use
reactor.netty.http.client.HttpClient.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ClientTest.java From influx-proxy with Apache License 2.0 | 8 votes |
public static void main(String[] args) throws JsonProcessingException { // ObjectMapper objectMapper = new ObjectMapper(); // objectMapper.setSerializationInclusion(NON_NULL); // InfluxDB influxdb = InfluxDBFactory.connect("http://172.29.64.250:18086"); // QueryResult result = influxdb.query(new Query("select * from health limit 1", "micrometerDb")); // System.out.println(objectMapper.writeValueAsString(result)); // influxdb.close(); TcpClient tcpClient = TcpClient.create() .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 1000); WebClient webClient = WebClient.builder() .baseUrl("http://localhost:8086") .clientConnector(new ReactorClientHttpConnector(HttpClient.from(tcpClient))) .filter(logRequest()) .build(); System.out.println(webClient.get().uri(uriBuilder -> uriBuilder .path("query") .queryParam("db", "micrometerDb") .queryParam("q", "select * from cpu") .build() ) .exchange() .flatMap(clientResponse -> clientResponse.toEntity(String.class)) .block().getBody()); }
Example #2
Source File: HelloWorldClient.java From reactor-netty with Apache License 2.0 | 6 votes |
public static void main(String[] args) { HttpClient client = HttpClient.create() .port(PORT) .wiretap(WIRETAP) .compress(COMPRESS); if (SECURE) { client = client.secure( spec -> spec.sslContext(SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE))); } String response = client.get() .uri("/hello") .responseContent() .aggregate() .asString() .block(); System.out.println("Response: " + response); }
Example #3
Source File: HttpServerTests.java From reactor-netty with Apache License 2.0 | 6 votes |
private void doTestIssue309(String path, HttpServer httpServer) { disposableServer = httpServer.handle((req, res) -> res.sendString(Mono.just("Should not be reached"))) .bindNow(); Mono<HttpResponseStatus> status = HttpClient.create() .port(disposableServer.port()) .get() .uri(path) .responseSingle((res, byteBufMono) -> Mono.just(res.status())); StepVerifier.create(status) .expectNextMatches(HttpResponseStatus.REQUEST_ENTITY_TOO_LARGE::equals) .expectComplete() .verify(); }
Example #4
Source File: CloudFoundryAcceptanceTest.java From spring-cloud-app-broker with Apache License 2.0 | 6 votes |
private WebClient getSslIgnoringWebClient() { return WebClient.builder() .clientConnector(new ReactorClientHttpConnector(HttpClient .create() .secure(t -> { try { t.sslContext(SslContextBuilder .forClient() .trustManager(InsecureTrustManagerFactory.INSTANCE) .build()); } catch (SSLException e) { if (LOG.isDebugEnabled()) { LOG.debug("problem ignoring SSL in WebClient", e); } } }))) .build(); }
Example #5
Source File: AppServer.java From reactor-guice with Apache License 2.0 | 6 votes |
@Test public void testPostFormBean() { for (int ii=0; ii<100000; ii++) { String hhe = HttpClient.create() .post() .uri("http://127.0.0.1:8083/kreactor/test/post-bean") .sendForm((req, form) -> form.multipart(false) .attr("id", "123123121312312") .attr("account", "account") .attr("password", "password") .attr("name", "name") ) .responseSingle((res, content) -> content) .map(byteBuf -> byteBuf.toString(CharsetUtil.UTF_8)) .block(); System.out.println("" + ii + " : " + hhe); } }
Example #6
Source File: AppServer.java From reactor-guice with Apache License 2.0 | 6 votes |
@Test public void testFileUpload() { for(int ii=0; ii<10000; ii++) { String hhe = HttpClient.create() .post() // .uri("http://0.0.0.0:8085/api/login") .uri("http://127.0.0.1:8083/kreactor/test/post-bean") .sendForm((req, form) -> form.multipart(true) .attr("id", "123123121312312") .attr("account", "liuyi") .attr("password", "password") .attr("name", "name") .file("image", new File("/Users/henry/Pictures/girl.jpg")) // .file("image", new File("C:\\Users\\koocyton\\Pictures\\cloud.jpg")) // .file("image", new File("C:\\Users\\koocyton\\Pictures\\st.jpg")) // .file("image", new File("C:\\Users\\koocyton\\Pictures\\zz.txt")) ) .responseSingle((res, content) -> content) .map(byteBuf -> byteBuf.toString(CharsetUtil.UTF_8)) .block(); System.out.println("" + ii + " : " + hhe); } }
Example #7
Source File: AppServer.java From reactor-guice with Apache License 2.0 | 6 votes |
@Test public void testWebsocketClient4() throws IOException { Properties properties = new Properties(); // properties.load(new FileInputStream("D:\\project\\reactor-guice\\application.properties")); properties.load(new FileInputStream("/Users/develop/Project/reactor-guice/application.properties")); int port = Integer.valueOf(properties.getProperty("server.port", "8081")); HttpClient.create() .port(port) .wiretap(true) .websocket() .uri("ws://127.0.0.1:8083/kreactor/ws") .handle((in, out) -> in.aggregateFrames() .receive() .asString() .map(s -> { System.out.println(s); return s; })) .blockLast(); }
Example #8
Source File: HttpCompressionClientServerTests.java From reactor-netty with Apache License 2.0 | 6 votes |
@Test public void testIssue282() { DisposableServer server = HttpServer.create() .compress(2048) .port(0) .handle((req, res) -> res.sendString(Mono.just("testtesttesttesttest"))) .bindNow(); Mono<String> response = HttpClient.create() .port(server.port()) .get() .uri("/") .responseContent() .aggregate() .asString(); StepVerifier.create(response) .expectNextMatches("testtesttesttesttest"::equals) .expectComplete() .verify(); server.disposeNow(); }
Example #9
Source File: HttpMetricsHandlerTests.java From reactor-netty with Apache License 2.0 | 6 votes |
@Before public void setUp() { httpServer = customizeServerOptions( HttpServer.create() .host("127.0.0.1") .port(0) .metrics(true, s -> s) .route(r -> r.post("/1", (req, res) -> res.header("Connection", "close") .send(req.receive().retain().delayElements(Duration.ofMillis(10)))) .post("/2", (req, res) -> res.header("Connection", "close") .send(req.receive().retain().delayElements(Duration.ofMillis(10)))))); provider = ConnectionProvider.create("HttpMetricsHandlerTests", 1); httpClient = customizeClientOptions(HttpClient.create(provider) .remoteAddress(() -> disposableServer.address()) .metrics(true, s -> s)); registry = new SimpleMeterRegistry(); Metrics.addRegistry(registry); }
Example #10
Source File: HttpServerTests.java From reactor-netty with Apache License 2.0 | 6 votes |
@Test @SuppressWarnings("deprecation") public void testTcpConfiguration_2() throws Exception { CountDownLatch latch = new CountDownLatch(10); LoopResources loop = LoopResources.create("testTcpConfiguration"); ChannelGroup group = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE); doTestTcpConfiguration( HttpServer.from(configureTcpServer(TcpServer.create(), loop, group, latch)), HttpClient.from(configureTcpClient(TcpClient.create(), loop, group, latch)) ); assertThat(latch.await(30, TimeUnit.SECONDS)).isTrue(); FutureMono.from(group.close()) .then(loop.disposeLater()) .block(Duration.ofSeconds(30)); }
Example #11
Source File: HttpServerTests.java From reactor-netty with Apache License 2.0 | 6 votes |
@Test public void testIssue186() { disposableServer = HttpServer.create() .port(0) .handle((req, res) -> res.status(200).send()) .wiretap(true) .bindNow(); HttpClient client = HttpClient.create(ConnectionProvider.create("testIssue186", 1)) .remoteAddress(disposableServer::address) .wiretap(true); doTestIssue186(client); doTestIssue186(client); }
Example #12
Source File: HttpServerTests.java From reactor-netty with Apache License 2.0 | 6 votes |
private void doTestTcpConfiguration(HttpServer server, HttpClient client) { disposableServer = server.bindNow(); String response = client.post() .uri("/") .send(ByteBufFlux.fromString(Mono.just("testTcpConfiguration"))) .responseContent() .aggregate() .asString() .block(Duration.ofSeconds(30)); disposableServer.disposeNow(); assertThat(response).isEqualTo("testTcpConfiguration"); }
Example #13
Source File: CacheSample.java From micrometer with Apache License 2.0 | 6 votes |
public static void main(String[] args) { MeterRegistry registry = SampleConfig.myMonitoringSystem(); GuavaCacheMetrics.monitor(registry, guavaCache, "book.guava"); // read all of Frankenstein HttpClient.create() .baseUrl("www.gutenberg.org") .doOnRequest((req, conn) -> conn.addHandler(wordDecoder())) .get() .uri("/files/84/84-0.txt") .responseContent() .asString() .delayElements(Duration.ofMillis(10)) // one word per 10 ms .filter(word -> !word.isEmpty()) .doOnNext(word -> { if (guavaCache.getIfPresent(word) == null) guavaCache.put(word, 1); }) .blockLast(); }
Example #14
Source File: HttpServerTests.java From reactor-netty with Apache License 2.0 | 6 votes |
@Test public void releaseInboundChannelOnNonKeepAliveRequest() { disposableServer = HttpServer.create() .port(0) .handle((req, resp) -> req.receive().then(resp.status(200).send())) .wiretap(true) .bindNow(); Flux<ByteBuf> src = Flux.range(0, 3) .map(n -> Unpooled.wrappedBuffer(Integer.toString(n) .getBytes(Charset.defaultCharset()))); Flux.range(0, 100) .concatMap(n -> HttpClient.create() .port(disposableServer.port()) .noSSL() .wiretap(true) .keepAlive(false) .post() .uri("/return") .send(src) .responseSingle((res, buf) -> Mono.just(res.status().code()))) .collectList() .block(); }
Example #15
Source File: HttpServerTests.java From reactor-netty with Apache License 2.0 | 5 votes |
@Test public void contextShouldBeTransferredFromDownStreamToUpStream() { AtomicReference<Context> context = new AtomicReference<>(); disposableServer = HttpServer.create() .port(0) .handle((req, res) -> res.status(200).send()) .bindNow(); HttpClient client = HttpClient.create(ConnectionProvider.create("contextShouldBeTransferredFromDownStreamToUpStream", 1)) .remoteAddress(disposableServer::address); Mono<String> content = client.post() .uri("/") .send(ByteBufFlux.fromString(Mono.just("bodysample") .subscriberContext(c -> { context.set(c); return c; }))) .responseContent() .aggregate() .asString() .subscriberContext(c -> c.put("Hello", "World")); StepVerifier.create(content) .expectComplete() .verify(Duration.ofSeconds(30)); assertThat(context.get().get("Hello").equals("World")).isTrue(); }
Example #16
Source File: GatewayAutoConfiguration.java From spring-cloud-gateway with Apache License 2.0 | 5 votes |
@Bean public ReactorNettyWebSocketClient reactorNettyWebSocketClient( HttpClientProperties properties, HttpClient httpClient) { ReactorNettyWebSocketClient webSocketClient = new ReactorNettyWebSocketClient( httpClient); if (properties.getWebsocket().getMaxFramePayloadLength() != null) { webSocketClient.setMaxFramePayloadLength( properties.getWebsocket().getMaxFramePayloadLength()); } webSocketClient.setHandlePing(properties.getWebsocket().isProxyPing()); return webSocketClient; }
Example #17
Source File: ChannelOperationsHandlerTest.java From reactor-netty with Apache License 2.0 | 5 votes |
@Test public void testIssue196() throws Exception { ExecutorService threadPool = Executors.newCachedThreadPool(); int testServerPort = SocketUtils.findAvailableTcpPort(); TestServer testServer = new TestServer(testServerPort); Future<?> f = threadPool.submit(testServer); if(!testServer.await(10, TimeUnit.SECONDS)){ throw new IOException("Fail to start test server"); } HttpClient client = HttpClient.newConnection() .port(testServerPort) .wiretap(true); Flux.range(0, 2) .concatMap(i -> client.get() .uri("/205") .responseContent() .aggregate() .asString() .log()) .blockLast(Duration.ofSeconds(10)); testServer.close(); assertThat(f.get()).isNull(); }
Example #18
Source File: WebClientBraveTests.java From spring-cloud-sleuth with Apache License 2.0 | 5 votes |
/** * Normally, the HTTP connector would be statically initialized. This ensures the * {@link HttpClient} is configured for the mock endpoint. */ @Bean @Order(0) public WebClientCustomizer clientConnectorCustomizer(HttpClient httpClient, URI baseUrl) { return (builder) -> builder.baseUrl(baseUrl.toString()) .clientConnector(new ReactorClientHttpConnector(httpClient)); }
Example #19
Source File: HttpServerTests.java From reactor-netty with Apache License 2.0 | 5 votes |
@Test public void testCancelConnectionCloseForWebSocketClient() { MonoProcessor<WebSocketCloseStatus> statusServer = MonoProcessor.create(); MonoProcessor<WebSocketCloseStatus> statusClient = MonoProcessor.create(); disposableServer = HttpServer.create() .port(0) .handle((req, resp) -> resp.sendWebsocket((in, out) -> in.receiveCloseStatus() .subscribeWith(statusServer) .then())) .wiretap(true) .bindNow(); HttpClient.create() .port(disposableServer.port()) .wiretap(true) .websocket() .uri("/") .handle((in, out) -> { in.receiveCloseStatus() .subscribeWith(statusClient); in.withConnection(Connection::dispose); return Mono.never(); }) .subscribe(); StepVerifier.create(statusClient) .expectNext(new WebSocketCloseStatus(-1, "")) .expectComplete() .verify(Duration.ofSeconds(30)); StepVerifier.create(statusServer) .expectNext(new WebSocketCloseStatus(-1, "")) .expectComplete() .verify(Duration.ofSeconds(30)); }
Example #20
Source File: DefaultPooledConnectionProviderTest.java From reactor-netty with Apache License 2.0 | 5 votes |
@Test public void testIssue1012() throws Exception { DisposableServer server = HttpServer.create() .port(0) .wiretap(true) .route(r -> r.get("/1", (req, resp) -> resp.sendString(Mono.just("testIssue1012"))) .get("/2", (req, res) -> Mono.error(new RuntimeException("testIssue1012")))) .bindNow(); DefaultPooledConnectionProvider provider = (DefaultPooledConnectionProvider) ConnectionProvider.create("testIssue1012", 1); CountDownLatch latch = new CountDownLatch(1); HttpClient client = HttpClient.create(provider) .port(server.port()) .wiretap(true) .doOnConnected(conn -> conn.channel().closeFuture().addListener(f -> latch.countDown())); client.get() .uri("/1") .responseContent() .aggregate() .block(Duration.ofSeconds(30)); client.get() .uri("/2") .responseContent() .aggregate() .onErrorResume(e -> Mono.empty()) .block(Duration.ofSeconds(30)); assertThat(latch.await(30, TimeUnit.SECONDS)).isTrue(); provider.channelPools.forEach((k, v) -> assertThat(v.metrics().acquiredSize()).isEqualTo(0)); provider.disposeLater() .block(Duration.ofSeconds(30)); server.disposeNow(); }
Example #21
Source File: WebsocketClientTransportTest.java From rsocket-java with Apache License 2.0 | 5 votes |
@DisplayName("creates client with HttpClient and path without root") @Test void createHttpClientWithPathWithoutRoot() { assertThat(WebsocketClientTransport.create(HttpClient.create(), "test")) .isNotNull() .hasFieldOrPropertyWithValue("path", "/test"); }
Example #22
Source File: HttpServerTests.java From reactor-netty with Apache License 2.0 | 5 votes |
@Test public void flushOnComplete() { Flux<String> flux = Flux.range(0, 100) .map(n -> String.format("%010d", n)); List<String> test = flux.collectList() .block(); assertThat(test).isNotNull(); disposableServer = HttpServer.create() .port(0) .handle((req, resp) -> resp.sendString(flux.map(s -> s + "\n"))) .wiretap(true) .bindNow(); Flux<String> client = HttpClient.create() .port(disposableServer.port()) .wiretap(true) .doOnConnected(res -> res.addHandler(new LineBasedFrameDecoder(10))) .get() .uri("/") .responseContent() .asString(); StepVerifier.create(client) .expectNextSequence(test) .expectComplete() .verify(Duration.ofSeconds(30)); }
Example #23
Source File: TitusWebClientAddOns.java From titus-control-plane with Apache License 2.0 | 5 votes |
public static WebClient.Builder addTitusDefaults(WebClient.Builder clientBuilder, HttpClient httpClient, WebClientMetric webClientMetric) { HttpClient updatedHttpClient = addMetricCallbacks( addLoggingCallbacks(httpClient), webClientMetric ); return clientBuilder.clientConnector(new ReactorClientHttpConnector(updatedHttpClient)); }
Example #24
Source File: BackendService.java From influx-proxy with Apache License 2.0 | 5 votes |
public WebClient getWebClientFromCacheOrCreate(BackendNode node) { WebClient client = webClientCache.get(node.getUrl()); if (client != null) { return client; } synchronized (webClientCache) { client = webClientCache.get(node.getUrl()); if (client != null) { return client; } int queryTimeout=Optional.ofNullable(node.getQueryTimeout()).orElse(DEFAULT_QUERY_TIMEOUT); int writeTimeout=Optional.ofNullable(node.getWriteTimeout()).orElse(DEFAULT_WRITE_TIMEOUT); int timeout=Math.max(queryTimeout,writeTimeout); TcpClient tcpClient = TcpClient.create() .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, timeout) .doOnConnected(conn -> conn .addHandlerLast(new ReadTimeoutHandler(timeout)) .addHandlerLast(new WriteTimeoutHandler(timeout))); WebClient webClient = WebClient.builder() .baseUrl(node.getUrl()) .clientConnector(new ReactorClientHttpConnector(HttpClient.from(tcpClient).keepAlive(false))) .filter(logRequest()) .build(); webClientCache.put(node.getUrl(), webClient); return webClient; } }
Example #25
Source File: HttpTests.java From reactor-netty with Apache License 2.0 | 5 votes |
@Test public void httpRespondsEmpty() { DisposableServer server = HttpServer.create() .port(0) .route(r -> r.post("/test/{param}", (req, res) -> Mono.empty())) .wiretap(true) .bindNow(); HttpClient client = HttpClient.create() .port(server.port()) .wiretap(true); Mono<ByteBuf> content = client.headers(h -> h.add("Content-Type", "text/plain")) .post() .uri("/test/World") .send(ByteBufFlux.fromString(Mono.just("Hello") .log("client-send"))) .responseContent() .log("client-received") .next() .doOnError(t -> System.err.println("Failed requesting server: " + t.getMessage())); StepVerifier.create(content) .expectComplete() .verify(Duration.ofSeconds(30)); server.disposeNow(); }
Example #26
Source File: HttpTests.java From reactor-netty with Apache License 2.0 | 5 votes |
@Test public void httpRespondsToRequestsFromClients() { DisposableServer server = HttpServer.create() .port(0) .route(r -> r.post("/test/{param}", (req, res) -> res.sendString(req.receive() .asString() .log("server-received") .map(it -> it + ' ' + req.param("param") + '!') .log("server-reply")))) .wiretap(true) .bindNow(); HttpClient client = HttpClient.create() .port(server.port()) .wiretap(true); Mono<String> content = client.headers(h -> h.add("Content-Type", "text/plain")) .post() .uri("/test/World") .send(ByteBufFlux.fromString(Flux.just("Hello") .log("client-send"))) .responseContent() .aggregate() .asString() .log("client-received") .doOnError(t -> System.err.println("Failed requesting server: " + t.getMessage())); StepVerifier.create(content) .expectNextMatches(s -> s.equals("Hello World!")) .expectComplete() .verify(Duration.ofSeconds(30)); server.disposeNow(); }
Example #27
Source File: ReactorNettySenderTests.java From micrometer with Apache License 2.0 | 5 votes |
@Test void customReadTimeoutHonored(@WiremockResolver.Wiremock WireMockServer server) throws Throwable { this.httpSender = new ReactorNettySender(HttpClient.create() .tcpConfiguration(tcpClient -> tcpClient.doOnConnected(connection -> connection.addHandlerLast(new ReadTimeoutHandler(1, TimeUnit.MILLISECONDS)) .addHandlerLast(new WriteTimeoutHandler(1, TimeUnit.MILLISECONDS))))); server.stubFor(any(urlEqualTo("/metrics")).willReturn(ok().withFixedDelay(5))); assertThatExceptionOfType(ReadTimeoutException.class) .isThrownBy(() -> httpSender.post(server.baseUrl() + "/metrics").send()); }
Example #28
Source File: HttpCompressionClientServerTests.java From reactor-netty with Apache License 2.0 | 5 votes |
@Test public void serverCompressionDefault() { HttpServer server = HttpServer.create() .port(0); DisposableServer runningServer = server.handle((in, out) -> out.sendString(Mono.just("reply"))) .wiretap(true) .bindNow(Duration.ofSeconds(10)); HttpClient client = HttpClient.create() .remoteAddress(runningServer::address) .wiretap(true); Tuple2<String, HttpHeaders> resp = client.headers(h -> h.add("Accept-Encoding", "gzip")) .get() .uri("/test") .response((res, buf) -> buf.asString() .zipWith(Mono.just(res.responseHeaders()))) .blockFirst(); assertThat(resp).isNotNull(); assertThat(resp.getT2().get("content-encoding")).isNull(); Assert.assertEquals("reply", resp.getT1()); runningServer.dispose(); runningServer.onDispose() .block(); }
Example #29
Source File: DefaultEsWebClientFactory.java From titus-control-plane with Apache License 2.0 | 5 votes |
private HttpClient buildHttpClient() { return HttpClient.create().tcpConfiguration(tcpClient -> { TcpClient tcpClientWithConnectionTimeout = tcpClient.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, esClientConfiguration.getConnectTimeoutMillis()); return tcpClientWithConnectionTimeout.doOnConnected(connection -> { //TODO Investigate why WriteTimeoutHandler appears to be broken in netty-handler 4.1.36.RELEASE package. connection.addHandlerLast(new ReadTimeoutHandler(esClientConfiguration.getReadTimeoutSeconds())); }); }); }
Example #30
Source File: HttpServerTests.java From reactor-netty with Apache License 2.0 | 5 votes |
@Test public void httpServerRequestConfigInjectAttributes() { AtomicReference<Channel> channelRef = new AtomicReference<>(); AtomicReference<Boolean> validate = new AtomicReference<>(); AtomicReference<Integer> chunkSize = new AtomicReference<>(); HttpServer server = HttpServer.create() .httpRequestDecoder(opt -> opt.maxInitialLineLength(123) .maxHeaderSize(456) .maxChunkSize(789) .validateHeaders(false) .initialBufferSize(10)) .handle((req, resp) -> req.receive().then(resp.sendNotFound())) .doOnConnection(c -> { channelRef.set(c.channel()); HttpServerCodec codec = c.channel() .pipeline() .get(HttpServerCodec.class); HttpObjectDecoder decoder = (HttpObjectDecoder) getValueReflection(codec, "inboundHandler", 1); chunkSize.set((Integer) getValueReflection(decoder, "maxChunkSize", 2)); validate.set((Boolean) getValueReflection(decoder, "validateHeaders", 2)); }) .wiretap(true); disposableServer = server.bindNow(); HttpClient.create() .remoteAddress(disposableServer::address) .post() .uri("/") .send(ByteBufFlux.fromString(Mono.just("bodysample"))) .responseContent() .aggregate() .asString() .block(); assertThat(channelRef.get()).isNotNull(); assertThat(chunkSize.get()).as("line length").isEqualTo(789); assertThat(validate.get()).as("validate headers").isFalse(); }