Expand my Community achievements bar.

Enhance your AEM Assets & Boost Your Development: [AEM Gems | June 19, 2024] Improving the Developer Experience with New APIs and Events
SOLVED

Is there anyway to route requests to a specific publisher instance in AEM

Avatar

Level 1

Hi Team,

We have 4 publisher instances and some times we observed content in all publishers are not in sync, is there any way to route requests to a specific publisher instance in AEM for testing purpose from live site.

For an example :-
When i load a page in live url (www.example.com/home.html) the request should fetch content only from publisher 2.

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Level 9

Hi @Karthikachery ,

Routing requests to a specific publisher instance in Adobe Experience Manager (AEM) for testing purposes can be useful when dealing with synchronization issues or when you need to validate specific content on a particular instance. Here are some ways to achieve this:

Option 1: Use a Dispatcher Filter

If you are using AEM Dispatcher, you can configure a Dispatcher filter to route requests to a specific publisher instance. This can be done by modifying the dispatcher.any configuration file. Here’s how:

  1. Modify dispatcher.any:
    • Locate and open your dispatcher.any configuration file.
    • Add a new farm configuration specifically for testing that routes to the desired publisher instance.

 

/dispatcher
{
    /farms
    {
        /specific-publisher-instance
        {
            /clientheaders
            {
                "host"
                "referer"
                "user-agent"
                "authorization"
                "cookie"
            }
            /virtualhosts
            {
                "www.example.com"
            }
            /filters
            {
                # Allow all requests for testing
                "/0001 { /type "allow" /glob "*" }"
            }
            /cache
            {
                /docroot "/path/to/cache/root"
                /rules
                {
                    "/0000 { /glob "*" /type "allow" }"
                }
            }
            /stats
            {
                "/categories
                {
                    "/html { /glob "*.html" }"
                }
            }
            /renders
            {
                /specific-render
                {
                    /hostname "publisher2.example.com"
                    /port "4503"
                }
            }
        }
    }
}
​

 

  1. Apply Dispatcher Configuration:

    • Ensure the Dispatcher configuration is reloaded. This might require restarting the web server or using specific commands to reload the configuration.
  2. Use a Custom Header or Parameter:

    • Modify your request to include a custom header or query parameter that your Dispatcher filter can detect to route the request to the specific publisher.

Option 2: DNS or Load Balancer Configuration

If your infrastructure includes a DNS service or a load balancer (e.g., F5, AWS ELB, etc.), you can temporarily configure it to route traffic to a specific publisher instance.

  1. DNS Configuration:

    • Modify the DNS entry for a subdomain (e.g., test.example.com) to point directly to the IP address of Publisher 2.
  2. Load Balancer Configuration:

    • Update your load balancer configuration to direct requests with specific headers or parameters to Publisher 2.

For example, in AWS ELB, you could create a listener rule that checks for a specific query parameter or header and routes to a specific target group that contains Publisher 2.

Option 3: Direct URL to Publisher

For quick and manual testing, you can directly access the publisher instance via its internal URL. This does not require any configuration changes but is less ideal for testing live URLs.

  • Access Publisher 2 directly

 

http://publisher2.internal.example.com:4503/home.html
​

 

 

Option 4: Custom Servlet Filter in AEM

As a more advanced solution, you can implement a custom servlet filter in AEM that detects a specific query parameter or header and routes the request accordingly. This requires development effort and careful deployment.

  1. Create a Custom Servlet Filter:
    • Develop an OSGi service that implements a Servlet Filter.

 

import javax.servlet.*;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.ConfigurationPolicy;
import org.osgi.framework.Constants;

@Component(
    service = Filter.class,
    property = {
        Constants.SERVICE_DESCRIPTION + "=Custom Routing Filter",
        "sling.filter.scope=request"
    },
    configurationPolicy = ConfigurationPolicy.OPTIONAL
)
public class CustomRoutingFilter implements Filter {
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        String targetPublisher = httpRequest.getParameter("targetPublisher");

        if ("publisher2".equals(targetPublisher)) {
            RequestDispatcher dispatcher = request.getRequestDispatcher("http://publisher2.internal.example.com:4503" + httpRequest.getRequestURI());
            dispatcher.forward(request, response);
        } else {
            chain.doFilter(request, response);
        }
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void destroy() {
    }
}
​

 

  1. Deploy and Configure the Filter:
    • Deploy the custom filter bundle to your AEM instances.
    • Configure the filter if necessary through the OSGi console.

By using any of these methods, you can route requests to a specific publisher instance for testing purposes. The choice of method depends on your infrastructure setup and the level of control you have over the various components in your deployment.

View solution in original post

3 Replies

Avatar

Correct answer by
Level 9

Hi @Karthikachery ,

Routing requests to a specific publisher instance in Adobe Experience Manager (AEM) for testing purposes can be useful when dealing with synchronization issues or when you need to validate specific content on a particular instance. Here are some ways to achieve this:

Option 1: Use a Dispatcher Filter

If you are using AEM Dispatcher, you can configure a Dispatcher filter to route requests to a specific publisher instance. This can be done by modifying the dispatcher.any configuration file. Here’s how:

  1. Modify dispatcher.any:
    • Locate and open your dispatcher.any configuration file.
    • Add a new farm configuration specifically for testing that routes to the desired publisher instance.

 

/dispatcher
{
    /farms
    {
        /specific-publisher-instance
        {
            /clientheaders
            {
                "host"
                "referer"
                "user-agent"
                "authorization"
                "cookie"
            }
            /virtualhosts
            {
                "www.example.com"
            }
            /filters
            {
                # Allow all requests for testing
                "/0001 { /type "allow" /glob "*" }"
            }
            /cache
            {
                /docroot "/path/to/cache/root"
                /rules
                {
                    "/0000 { /glob "*" /type "allow" }"
                }
            }
            /stats
            {
                "/categories
                {
                    "/html { /glob "*.html" }"
                }
            }
            /renders
            {
                /specific-render
                {
                    /hostname "publisher2.example.com"
                    /port "4503"
                }
            }
        }
    }
}
​

 

  1. Apply Dispatcher Configuration:

    • Ensure the Dispatcher configuration is reloaded. This might require restarting the web server or using specific commands to reload the configuration.
  2. Use a Custom Header or Parameter:

    • Modify your request to include a custom header or query parameter that your Dispatcher filter can detect to route the request to the specific publisher.

Option 2: DNS or Load Balancer Configuration

If your infrastructure includes a DNS service or a load balancer (e.g., F5, AWS ELB, etc.), you can temporarily configure it to route traffic to a specific publisher instance.

  1. DNS Configuration:

    • Modify the DNS entry for a subdomain (e.g., test.example.com) to point directly to the IP address of Publisher 2.
  2. Load Balancer Configuration:

    • Update your load balancer configuration to direct requests with specific headers or parameters to Publisher 2.

For example, in AWS ELB, you could create a listener rule that checks for a specific query parameter or header and routes to a specific target group that contains Publisher 2.

Option 3: Direct URL to Publisher

For quick and manual testing, you can directly access the publisher instance via its internal URL. This does not require any configuration changes but is less ideal for testing live URLs.

  • Access Publisher 2 directly

 

http://publisher2.internal.example.com:4503/home.html
​

 

 

Option 4: Custom Servlet Filter in AEM

As a more advanced solution, you can implement a custom servlet filter in AEM that detects a specific query parameter or header and routes the request accordingly. This requires development effort and careful deployment.

  1. Create a Custom Servlet Filter:
    • Develop an OSGi service that implements a Servlet Filter.

 

import javax.servlet.*;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.ConfigurationPolicy;
import org.osgi.framework.Constants;

@Component(
    service = Filter.class,
    property = {
        Constants.SERVICE_DESCRIPTION + "=Custom Routing Filter",
        "sling.filter.scope=request"
    },
    configurationPolicy = ConfigurationPolicy.OPTIONAL
)
public class CustomRoutingFilter implements Filter {
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        String targetPublisher = httpRequest.getParameter("targetPublisher");

        if ("publisher2".equals(targetPublisher)) {
            RequestDispatcher dispatcher = request.getRequestDispatcher("http://publisher2.internal.example.com:4503" + httpRequest.getRequestURI());
            dispatcher.forward(request, response);
        } else {
            chain.doFilter(request, response);
        }
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void destroy() {
    }
}
​

 

  1. Deploy and Configure the Filter:
    • Deploy the custom filter bundle to your AEM instances.
    • Configure the filter if necessary through the OSGi console.

By using any of these methods, you can route requests to a specific publisher instance for testing purposes. The choice of method depends on your infrastructure setup and the level of control you have over the various components in your deployment.

Avatar

Community Advisor

Hi @Karthikachery 
If you are on AMS, you can talk to your Adobe CSE,

CSE will do this from load balancer, they can create a target group and add a testing dispatcher to that group and based on some cookies , the traffic can be redirected to test dispatcher.



Arun Patidar