This document is a continuation of the Gist AzureEventHubsJavaSDKCustomEndpointWebSockets.md, which demonstrated front-ending Event Hubs with a custom endpoint (endpoint as AppGateway).
As the next step, we'll place an "HTTP Proxy" between the Event Hubs SDK running in the client machine and the host (AppGateway) that the custom endpoint addresses.
The blog explains in detail how to run the popular HTTP Proxy, Squid, on a docker container in a client machine. To simplify the setup process, follow the steps below:
c:\squid
|
|-- docker-compose.yaml
|-- password
|-- squid.conf
- From the terminal, switch to the extracted directory
squid
. - In the terminal, run the command
docker-compose up
, to start the docker container running the Squid Proxy.
The Proxy uses container port 3128, which is mapped to the client machine port 8080.
- The proxy_host = "127.0.0.1"
- The proxy_port = "8080"
- The proxy_username = "anu"
- The proxy_password = "1StepAhead!"
- Install apache2-utils
sudo apt install apache2-utils
- Generate a file
usrpwd
with the new proxy credentials- Run the command
htpasswd -c usrpwd
<user-name>
- Enter the new password when prompted
- A file named
usrpwd
will be created with the user name and encoded password.
- Run the command
- Copy the content of the file
usrpwd
, and paste it as the content ofpassword
file in the extractedsquid
directory. - (Stop proxy container if already running and), start the Proxy container by running
docker-compose up
At this point, the java code can connect to Event Hubs using custom endpoint with a domain through Squid Proxy.
List<EventData> telemetryEvents = Arrays.asList(
new EventData("Roast beef".getBytes(UTF_8)),
new EventData("Cheese".getBytes(UTF_8)),
new EventData("Tofu".getBytes(UTF_8)),
new EventData("Turkey".getBytes(UTF_8)));
AzureNamedKeyCredential credential = new AzureNamedKeyCredential("RootManageSharedAccessKey",
"<Shared Access Policy Primary or Secondary Key from the Portal>");
ProxyOptions proxyOptions = new ProxyOptions(
ProxyAuthenticationType.BASIC,
new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 8080)),
"anu", "1StepAhead!");
EventHubProducerClient producer = new EventHubClientBuilder()
.proxyOptions(proxyOptions)
.credential("<eventubs-namespace>.servicebus.windows.net", "<eventhubs-name>", credential)
.customEndpointAddress("https://<custom-domain-app-gateway>") // e.g. "https://apg-eh.anuchan.us"
.transportType(AmqpTransportType.AMQP_WEB_SOCKETS)
.buildProducerClient();
EventDataBatch currentBatch = producer.createBatch();
for (EventData event : telemetryEvents) {
if (currentBatch.tryAdd(event)) {
continue;
}
producer.send(currentBatch);
currentBatch = producer.createBatch();
if (!currentBatch.tryAdd(event)) {
System.err.printf("Event is too large for an empty batch. Skipping. Max size: %s. Event: %s%n",
currentBatch.getMaxSizeInBytes(), event.getBodyAsString());
}
}
producer.send(currentBatch);