Call a special web farm

Some background. There is currently an asp.net website, which is hosted on two web servers that are load balanced, i.e. Web farm.

I would like to have some code that allows me to call a specific server and execute a method on it. I WANT to do this so that I can get all web servers to update their cache via the web page on the site.

Use case: the Admin user logs on to the site and makes changes to the cached parameter, and then clicks the "Update Web Server Cache" button, which then calls the method for caching updates on each server. this means that I do not have to restart the application pool every time I change the cache settings.

+3
source share
2 answers

The Pit Answer is correct; each machine still has its own IP address. I use the ASHX handler to receive requests to reset the cache. Any computer in a web farm can initiate a request.

This is a fairly complete example, but there are some helper methods and configuration settings that I have not included.

   <!-- 

    URL of user web application that can be used to clear caches.  
    Delimit multiple URLS with pipes: url1|url2|url3 

    -->
    <add key="UserServers" value="http://123.456.789.001/|http://123.456.789.002" />

Here is the code to actually call the handler on each site that executes the reset cache. I suggest using some kind of common password between the machines and separately protecting the handler so that it cannot be accessed publicly.

        /// <summary>
        /// Calls for a reset of caches on one or more user sites serving reports. 
        /// Allows for multiple urls to be processed; configure the reset targets
        /// using AppSettings["UserCacheResetUrl"], delimited with pipes if there
        /// are multiple sites. 
        /// </summary>
        public static void ClearAllUserCaches()
        {
            //
            // clear local user caches
            ClearUserCaches();

            //
            // clear out of process user caches

            string[] urls = AppSettings.UserServers;

            for( int i = 0; i < urls.Length; i++ )
            {
                string url = urls[i] + AppSettings.UserCacheResetPath + "&sharedPassword=" + AppSettings.SharedPassword;

                WebRequest request = null;
                HttpWebResponse response = null;

                try
                {
                    request = WebRequest.Create( url );
                    response = (HttpWebResponse)request.GetResponse();
                }
                catch( WebException ex )
                {
                    Log.LogException( ex );
                }
                finally
                {
                    request = null;
                }

                if( response == null || response.StatusCode != HttpStatusCode.OK )
                {
                    if( response != null )
                    {
                        response.Close();
                        response = null;
                    }
                }
            }
        }

The handler code itself (sorry for the length).

    /// <summary>
    /// Exposes an interface for trusted callers to request that this
    /// instance of the application perform an action.
    /// </summary>
    public class AdministrationRequestHandler : IHttpHandler
    {
        /// <summary>
        /// Processes an incoming request and performs an action specified via the "action"
        /// parameter passed on the query string.  Only local callers will be allowed, and
        /// only callers who pass a shared password via the "sharedPassword" query string
        /// parameter.
        /// </summary>
        /// <param name="context"></param>
        public void ProcessRequest( HttpContext context )
        {
            //
            // get the action from the query string, and check that
            // it actually contains a value.
            string action = context.Request.QueryString["action"].ToSafeString().ToUpper( CultureInfo.InvariantCulture );

            if( string.IsNullOrEmpty( action ) )
            {
                //
                // Dump out an error message and return--we can't do anything
                // without an action and this request may have malicious
                // origins.
                context.Response.Write( "Missing action." );
                return;
            }

            //
            // Now validate the shared password that all web applications in this
            // solution should be using.  This password will NEVER be placed on a user's
            // query string or ever passed over a public network.
            string sharedPassword = context.Request.QueryString["sharedPassword"].ToSafeString();

            if( string.IsNullOrEmpty( sharedPassword ) )
            {
                context.Response.Write( "Missing shared password." );
                return;
            }

            //
            // check that the shared password is actually valid
            if( sharedPassword != AppSettings.SharedPassword )
            {
                context.Response.Write( "Invalid shared password." );
                return;
            }

            //
            // perform the specified action
            if( action == "CLEAR_CACHE" )
            {
                AppContext.ClearUserCaches();
            }
        }

        /// <summary>
        /// Specifies whether or not the instance is reusable.
        /// </summary>
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
+4
source

- - IP-, ; IP- ( DNS), -.

, -, IP-, .

+3

Source: https://habr.com/ru/post/1767193/


All Articles