A Simple Network Connection Test Tool for Azure Spring Cloud
Published Apr 13 2022 07:51 PM 3,074 Views
Microsoft

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.

 

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

 

 

Step 1: Create a simple Spring Web application with DNS resolve and TCP ping feature

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.

Hanli_Ren_0-1649152020123.png

 

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.util.List;

import sun.net.dns.ResolverConfiguration;

@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<String> nameServers = ResolverConfiguration.open().nameservers();
            for (String nameServer : nameServers)
            {
                result += "Using system selected DNS server:"+ "</br>";
                result += nameServer + "</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

Hanli_Ren_0-1649152339699.png

We can run DNS resolve test by providing the hostname
http://localhost:8080/dns?hostname=google.com

Hanli_Ren_1-1649152393066.png


We can run tcpping test by providing the target IP/domain and port number
http://localhost:8080/tcpping?target=google.com&port=443 

Hanli_Ren_2-1649152418388.png

http://localhost:8080/tcpping?target=142.250.76.174&port=443 

Hanli_Ren_3-1649152418390.png

 

 

Step 2: Deploy it to your Azure Spring Cloud

 

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

 

 

 

Step 3: Run DNS/Tcpping tests inside your Azure Spring Cloud instance

 

Go to the networktest app we just created, use the URL provided in Overview Portal to run the test

Hanli_Ren_0-1649152791397.png

 

Hanli_Ren_1-1649152813545.png

 

For succeed dns resolve test, we can see the following output.

Hanli_Ren_2-1649152934136.png

 

For failed dns resolve, we can see detailed error message and call stacks.

Hanli_Ren_4-1649152966568.png

 

For succeed tcpping, we can see the following output.

Hanli_Ren_5-1649153020522.png

 

For failed tcpping, we can see detailed error message and call stacks.

Hanli_Ren_6-1649153053665.png

 

 

1 Comment
Co-Authors
Version history
Last update:
‎Apr 05 2022 03:08 AM
Updated by: