|
21 | 21 | */ |
22 | 22 | package com.influxdb.v3; |
23 | 23 |
|
| 24 | +import java.net.InetSocketAddress; |
| 25 | +import java.net.URI; |
24 | 26 | import java.util.UUID; |
| 27 | +import java.util.function.Supplier; |
25 | 28 | import java.util.stream.Stream; |
| 29 | +import javax.annotation.Nonnull; |
| 30 | +import javax.annotation.Nullable; |
| 31 | + |
| 32 | +import io.grpc.HttpConnectProxiedSocketAddress; |
| 33 | +import io.grpc.ProxyDetector; |
| 34 | +import io.netty.handler.proxy.HttpProxyHandler; |
26 | 35 |
|
27 | 36 | import com.influxdb.v3.client.InfluxDBClient; |
28 | 37 | import com.influxdb.v3.client.Point; |
29 | 38 | import com.influxdb.v3.client.PointValues; |
30 | 39 | import com.influxdb.v3.client.config.ClientConfig; |
| 40 | +import com.influxdb.v3.client.config.NettyHttpClientConfig; |
31 | 41 |
|
32 | 42 | public final class ProxyExample { |
33 | 43 |
|
34 | 44 | private ProxyExample() { |
35 | 45 | } |
36 | 46 |
|
37 | 47 | public static void main(final String[] args) throws Exception { |
38 | | - // Run docker-compose.yml file to start Envoy proxy |
| 48 | + // Run the docker-compose.yml file to start Envoy proxy, |
| 49 | + // or start envoy proxy directly with the command `envoy-c envoy.yaml` |
39 | 50 |
|
40 | 51 | String proxyUrl = "http://localhost:10000"; |
| 52 | + String targetUrl = "http://localhost:8086"; |
| 53 | + String username = "username"; |
| 54 | + String password = "password"; |
| 55 | + |
| 56 | + NettyHttpClientConfig nettyHttpClientConfig = new NettyHttpClientConfig(); |
| 57 | + |
| 58 | + // Set proxy for write api |
| 59 | + Supplier<HttpProxyHandler> writeApiProxy = () -> |
| 60 | + new HttpProxyHandler(new InetSocketAddress("localhost", 10000), username, password); |
| 61 | + nettyHttpClientConfig.configureChannelProxy(writeApiProxy); |
| 62 | + |
| 63 | + // Set proxy for query api |
| 64 | + ProxyDetector proxyDetector = createProxyDetector(targetUrl, proxyUrl, username, password); |
| 65 | + nettyHttpClientConfig.configureManagedChannelProxy(proxyDetector); |
| 66 | + |
41 | 67 | String sslRootsFilePath = "src/test/java/com/influxdb/v3/client/testdata/influxdb-certificate.pem"; |
42 | 68 | ClientConfig clientConfig = new ClientConfig.Builder() |
43 | 69 | .host(System.getenv("INFLUXDB_URL")) |
44 | 70 | .token(System.getenv("INFLUXDB_TOKEN").toCharArray()) |
45 | 71 | .database(System.getenv("INFLUXDB_DATABASE")) |
46 | | - .proxyUrl(proxyUrl) |
| 72 | + .nettyHttpClientConfig(nettyHttpClientConfig) |
47 | 73 | .sslRootsFilePath(sslRootsFilePath) |
48 | 74 | .build(); |
49 | 75 |
|
50 | | - InfluxDBClient influxDBClient = InfluxDBClient.getInstance(clientConfig); |
51 | | - String testId = UUID.randomUUID().toString(); |
52 | | - Point point = Point.measurement("My_Home") |
53 | | - .setTag("room", "Kitchen") |
54 | | - .setField("temp", 12.7) |
55 | | - .setField("hum", 37) |
56 | | - .setField("testId", testId); |
57 | | - influxDBClient.writePoint(point); |
| 76 | + try (InfluxDBClient influxDBClient = InfluxDBClient.getInstance(clientConfig)) { |
| 77 | + String testId = UUID.randomUUID().toString(); |
| 78 | + Point point = Point.measurement("My_Home") |
| 79 | + .setTag("room", "Kitchen") |
| 80 | + .setField("temp", 12.7) |
| 81 | + .setField("hum", 37) |
| 82 | + .setField("testId", testId); |
| 83 | + influxDBClient.writePoint(point); |
58 | 84 |
|
59 | | - String query = String.format("SELECT * FROM \"My_Home\" WHERE \"testId\" = '%s'", testId); |
60 | | - try (Stream<PointValues> stream = influxDBClient.queryPoints(query)) { |
61 | | - stream.findFirst().ifPresent(values -> { |
62 | | - assert values.getTimestamp() != null; |
63 | | - System.out.printf("room[%s]: %s, temp: %3.2f, hum: %d", |
64 | | - new java.util.Date(values.getTimestamp().longValue() / 1000000), |
65 | | - values.getTag("room"), |
66 | | - (Double) values.getField("temp"), |
67 | | - (Long) values.getField("hum")); |
68 | | - }); |
| 85 | + String query = String.format("SELECT * FROM \"My_Home\" WHERE \"testId\" = '%s'", testId); |
| 86 | + try (Stream<PointValues> stream = influxDBClient.queryPoints(query)) { |
| 87 | + stream.findFirst().ifPresent(values -> { |
| 88 | + assert values.getTimestamp() != null; |
| 89 | + System.out.printf("room[%s]: %s, temp: %3.2f, hum: %d", |
| 90 | + new java.util.Date(values.getTimestamp().longValue() / 1000000), |
| 91 | + values.getTag("room"), |
| 92 | + (Double) values.getField("temp"), |
| 93 | + (Long) values.getField("hum")); |
| 94 | + }); |
| 95 | + } |
69 | 96 | } |
70 | 97 | } |
| 98 | + |
| 99 | + public static ProxyDetector createProxyDetector(@Nonnull final String targetUrl, @Nonnull final String proxyUrl, |
| 100 | + @Nullable final String username, @Nullable final String password) { |
| 101 | + URI targetUri = URI.create(targetUrl); |
| 102 | + URI proxyUri = URI.create(proxyUrl); |
| 103 | + return (targetServerAddress) -> { |
| 104 | + InetSocketAddress targetAddress = (InetSocketAddress) targetServerAddress; |
| 105 | + if (targetUri.getHost().equals(targetAddress.getHostString()) |
| 106 | + && targetUri.getPort() == targetAddress.getPort()) { |
| 107 | + return HttpConnectProxiedSocketAddress.newBuilder() |
| 108 | + .setProxyAddress(new InetSocketAddress(proxyUri.getHost(), proxyUri.getPort())) |
| 109 | + .setTargetAddress(targetAddress) |
| 110 | + .setUsername(username) |
| 111 | + .setPassword(password) |
| 112 | + .build(); |
| 113 | + } |
| 114 | + return null; |
| 115 | + }; |
| 116 | + } |
71 | 117 | } |
72 | 118 |
|
0 commit comments