All Products
Search
Document Center

Enterprise Distributed Application Service:How do I obtain the real IP address of the client when I access an EDAS application by using SLB?

Last Updated:May 25, 2023

This topic describes the solutions for obtaining the real IP address of the client from code when you access an Enterprise Distributed Application Service (EDAS) application by using Server Load Balancer (SLB).

Solutions

ECS application

  • Solution 1: Run the following command to obtain the real IP address of the client from the Java code in the backend:

    String client_ip = request.getHeader("x-forwarded-for");
  • Solution 2: Query the values of the x-real-ip and wl-proxy-client-ip parameters from the HTTP header to obtain the real IP address of the client.

Kubernetes application

  1. Log on to the EDAS console.

  2. In the left-side navigation pane, choose Application Management > Applications. In the top navigation bar, select a region. In the upper part of the page, select a microservice namespace. From the Cluster Type drop-down list, select Kubernetes Cluster. Then, find the application that you want to manage and click the name in the Application Name column.

  3. In the Access configuration section of the application overview page, click the Image - Plus icon or Image - Edit icon on the right of SLB (Public Network) or SLB (Private Network).

  4. In the SLB (Public Network) or SLB (Private Network) dialog box, change External Traffic Policy from Cluster to Local, and click OK.

  5. Run the following command to obtain the real IP address of the client from the Java code in the backend:

    String client_ip = request.getRemoteAddr();

You can use ternary operators to combine the preceding solutions that are used for applications separately deployed in an Elastic Compute Service (ECS) cluster and a Kubernetes cluster as a line of code. The new code is compatible with different types of EDAS applications.

String client_ip = request.getHeader("x-forwarded-for") == null ? request.getRemoteAddr() : request.getHeader("x-forwarded-for");