-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AB#154 feat: add http request forwaring in EventToHttpConverter job
- Loading branch information
1 parent
7be0999
commit 6bcf141
Showing
7 changed files
with
169 additions
and
10 deletions.
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
.../dev-external-deps/apache-flink/app/src/main/java/it/giovannibaratta/AsyncHttpClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package it.giovannibaratta; | ||
|
||
import java.net.URI; | ||
import java.net.http.HttpClient; | ||
import java.net.http.HttpRequest; | ||
import java.net.http.HttpResponse; | ||
import java.util.Collections; | ||
import java.util.Map; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.function.Supplier; | ||
|
||
import org.apache.flink.configuration.Configuration; | ||
import org.apache.flink.streaming.api.functions.async.ResultFuture; | ||
import org.apache.flink.streaming.api.functions.async.RichAsyncFunction; | ||
|
||
class AsyncHttpClient extends RichAsyncFunction<KafkaEvent, KafkaEvent> { | ||
|
||
private Map<String, String> eventRouting; | ||
private HttpClient client; | ||
|
||
public AsyncHttpClient(Map<String, String> eventRouting) { | ||
this.eventRouting = eventRouting; | ||
} | ||
|
||
@Override | ||
public void open(Configuration parameters) throws Exception { | ||
client = HttpClient.newHttpClient(); | ||
} | ||
|
||
@Override | ||
public void asyncInvoke(KafkaEvent event, final ResultFuture<KafkaEvent> resultFuture) throws Exception { | ||
|
||
String destination = eventRouting.get(event.topic); | ||
|
||
HttpRequest request = HttpRequest.newBuilder() | ||
.POST(HttpRequest.BodyPublishers.ofString(event.value)) | ||
.uri(URI.create(destination)) | ||
.build(); | ||
|
||
CompletableFuture<HttpResponse<String>> responseFuture = client.sendAsync(request, | ||
HttpResponse.BodyHandlers.ofString()); | ||
|
||
CompletableFuture.supplyAsync(new Supplier<KafkaEvent>() { | ||
@Override | ||
public KafkaEvent get() { | ||
try { | ||
HttpResponse<String> result = responseFuture.get(); | ||
System.out.println("Sent request to " + destination + ". Result status code " + result.statusCode()); | ||
return event; | ||
} catch (InterruptedException | ExecutionException e) { | ||
System.out.println("Error while performing request"); | ||
return null; | ||
} | ||
} | ||
}).thenAccept((KafkaEvent value) -> { | ||
resultFuture.complete(Collections.singleton(value)); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
...ev-external-deps/apache-flink/app/src/main/java/it/giovannibaratta/KafkaDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package it.giovannibaratta; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
|
||
import org.apache.flink.api.common.typeinfo.TypeInformation; | ||
import org.apache.flink.connector.kafka.source.reader.deserializer.KafkaRecordDeserializationSchema; | ||
import org.apache.kafka.clients.consumer.ConsumerRecord; | ||
|
||
public class KafkaDeserializer implements | ||
KafkaRecordDeserializationSchema<KafkaEvent> { | ||
|
||
@Override | ||
public TypeInformation<KafkaEvent> getProducedType() { | ||
return TypeInformation.of(KafkaEvent.class); | ||
} | ||
|
||
@Override | ||
// Given a Kafka record, extract the topic and the value and build a KafkaEvent | ||
public void deserialize(ConsumerRecord<byte[], byte[]> record, org.apache.flink.util.Collector<KafkaEvent> out) { | ||
String topic = record.topic(); | ||
String value = new String(record.value(), StandardCharsets.UTF_8); | ||
KafkaEvent event = new KafkaEvent(topic, value); | ||
out.collect(event); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
service/dev-external-deps/apache-flink/app/src/main/java/it/giovannibaratta/KafkaEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package it.giovannibaratta; | ||
|
||
public class KafkaEvent { | ||
|
||
public final String topic; | ||
public final String value; | ||
|
||
public KafkaEvent(String topic, String value) { | ||
this.topic = topic; | ||
this.value = value; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "KafkaEvent{" + | ||
"topic='" + topic + '\'' + | ||
", value='" + value + '\'' + | ||
'}'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
server { | ||
listen 9000; | ||
server_name backend-proxy; | ||
location / { | ||
# host.docker.internal points to the host machine from within a docker container | ||
proxy_pass http://host.docker.internal:3000; | ||
proxy_http_version 1.1; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters