Applications running inside Azure Spring Cloud may need to make outbound calls to targets outside the Azure Spring Cloud. Sometimes the application may encounter network connection issue for different reasons. If we can run DNS resolve and TCP ping test from inside the Azure Spring Cloud to the outside target, it should be helpful for us to quickly narrow down network connection related issues.
This blog shows how to build a very simple App for DNS resolve and TCP ping tests. After we deploy this App inside the existing Azure Spring Cloud service. We can run quick test to verify the availability of network connection to a specific target.
Note:
Azure Spring Apps published a new Connect Feature that allows you access the shell environment inside your application instances to do advanced troubleshooting.
For more details, please refer to: Investigate Azure Spring Apps Networking Issue with the new Connect feature
Step 1: Create a simple Spring Web application with DNS resolve and TCP ping feature
Step 2: Deploy it to your Azure Spring Cloud
Step 3: Run DNS/Tcpping tests inside your Azure Spring Cloud instance
My test tool source code and compiled jar file can be found in:
https://github.com/renhanli/AzureSpringCloud_connectiontest/tree/main/src/main/java/com/network/conn...
https://github.com/renhanli/AzureSpringCloud_connectiontest/blob/main/target/connectiontest-0.0.1-SN...
You can use the following steps to build your own test application:
1. Go to Spring initializr to create a Spring Web template.
Select Generate when all the dependencies are set.
2. Download and unpack the package, then create a network test controller for a simple web application by adding the file \src\main\java\com\network\connectiontest\NetworkTestController.java with the following contents:
package com.network.connectiontest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.net.Socket;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.List;
import org.xbill.DNS.ResolverConfig;
@RestController
public class NetworkTestController {
@GetMapping("/")
public String index() {
return "Run DNS resovle and Tcpping tests!";
}
@GetMapping("/dns")
public String dns(@RequestParam String hostname) {
String result = "Resolve name for "+ hostname + ". </br>";
try {
List<InetSocketAddress> dnsServers = ResolverConfig.getCurrentConfig().servers();
for (InetSocketAddress nameServer : dnsServers)
{
result += "Using system selected DNS server:"+ "</br>";
result += nameServer.getAddress() + "</br>";
}
InetAddress addr = InetAddress.getByName(hostname);
result += addr + "</br>";
} catch (Exception e) {
result += "DNS resolve Failed! </br>";
StringWriter writer = new StringWriter();
PrintWriter printWriter = new PrintWriter( writer );
e.printStackTrace( printWriter );
printWriter.flush();
result += writer.toString().replaceAll("(\r\n|\n)", "<br />");
}
return result;
}
@GetMapping("/tcpping")
public String tcpping(@RequestParam String target, @RequestParam int port) {
String result = "Tcpping "+ target + ":"+ port + ".</br>";
Socket socket = null;
try
{
socket = new Socket(target, port);
socket.setSoTimeout(2*1000);
result += "Successfully Connected. </br>";
socket.close();
result += "Disconnected. </br>";
}
catch(Exception e) {
result += "Connection Failed! </br>";
StringWriter writer = new StringWriter();
PrintWriter printWriter = new PrintWriter( writer );
e.printStackTrace( printWriter );
printWriter.flush();
result += writer.toString().replaceAll("(\r\n|\n)", "<br />");
}
return result;
}
}
3. Build the project using Maven
mvn clean package
4. Run local test
java -jar target\connectiontest-0.0.1-SNAPSHOT.jar
After you bring up the application in your local machine, you should be able to access http://localhost:8080
We can run DNS resolve test by providing the hostname
http://localhost:8080/dns?hostname=google.com
We can run tcpping test by providing the target IP/domain and port number
http://localhost:8080/tcpping?target=google.com&port=443
http://localhost:8080/tcpping?target=142.250.76.174&port=443
Assume you already have your Azure Spring Cloud service created. If you do not have any Azure Spring Cloud instance created, please create one according to https://docs.microsoft.com/en-us/azure/spring-cloud/quickstart?tabs=Azure-CLI&pivots=programming-lan...
Use Azure CLI to create the network test App and deploy the .jar file to it.
1. Install the Azure CLI version 2.0.67 or higher and the Azure Spring Cloud extension with the command:
az extension add --name spring-cloud
2. Login your account
az login
az account set --subscription <Name or ID of a subscription>
3. Create the test app with endpoint enabled
az spring-cloud app create -n networktest -s <spring cloud instance name> -g <resource group name> --assign-endpoint true
4. Deploy the jar file to the app
az spring-cloud app deploy -n networktest -s <spring cloud instance name> -g <resource group name> --artifact-path <jar file path>/connectiontest-0.0.1-SNAPSHOT.jar
Go to the networktest app we just created, use the URL provided in Overview Portal to run the test
For succeed dns resolve test, we can see the following output.
For failed dns resolve, we can see detailed error message and call stacks.
For succeed tcpping, we can see the following output.
For failed tcpping, we can see detailed error message and call stacks.
To help you get started, we have monthly FREE grants on all tiers – 50 vCPU Hours and 100 memory GB Hours per tier. These are the number of FREE hours per month BEFORE any usage is billed.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.