簡體   English   中英

調用特定的Web場

[英]Call Particular Web farm

一些背景。 目前有一個asp.net網站,它包含在2個負載均衡的Web服務器上,即Web場。

我想有一些代碼可以讓我調用一個特定的服務器並在其上執行一個方法。 我想這樣做,以便我可以強制所有Web服務器通過網站上的網頁刷新緩存。

用例是:管理員用戶登錄到站點並對已緩存的設置進行更改,然后單擊“刷新Web服務器緩存”按鈕,然后在每個服務器上調用更新緩存方法。 這是為了防止每次更改緩存設置時都必須重新啟動應用程序池。

傑米克的回答是正確的; 每台機器仍然有自己的IP。 我使用ASHX處理程序來接收重置緩存的請求。 Web場中的任何計算機都可以發起請求。

這是一個相當完整的示例,但是我沒有包含一些輔助方法和配置設置。

   <!-- 

    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" />

這是在每個執行緩存重置的站點上實際調用處理程序的代碼。 我建議在機器之間使用某種共享密碼,並單獨保護處理程序,以便不能公開訪問它。

        /// <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;
                    }
                }
            }
        }

處理程序代碼本身(抱歉長度)。

    /// <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;
            }
        }
    }

負載均衡的Web服務器仍然有自己的IP地址,要清楚; 負載均衡器具有第三個IP地址(DNS條目通常指向該地址),它只是通過一些內部邏輯決定將請求轉發到一個或另一個Web服務器。

因此,您通常仍然可以使用其IP地址而不是負載均衡器來訪問特定的Web服務器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM