刘华
实验室:JS测试器
分类专栏
hadoop2篇
mysql1篇
nginx8篇
java5篇
架构设计3篇
信息安全6篇
前端开发2篇
数据同步6篇
AI7篇
其他9篇

实现两个或多个站点的负载均衡

liuhua-2018/11/7 19:27:57

实现两个或多个站点的负载均衡,可以用nginx实现。实现方式如下:

1、在nginx配置中通过upstream指定要负载均衡的站点:

upstream testcluster{       
         server   localhost:8021 max_fails=0 fail_timeout=10s;
         server   localhost:8022 max_fails=0 fail_timeout=10s;
keepalive 20000;
            }

通过Server指定代理端口和转发:

server {
        listen       8020;
        server_name  localhost;

        location / {
            proxy_pass http://testcluster;
        }
    }

2、将站点的会话状态可以保存在独立的stateServer中,在web.Config中指定:

<sessionState mode="StateServer" cookieless="false" timeout="20" stateConnectionString="tcpip=127.0.0.1:42424" stateNetworkTimeout="60"></sessionState>

3、StateServer要开启远程访问:

启动 Asp.Net Session Service 服务,在注册表 [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\aspnet_state\Parameters] 配置端口(Port,默认是42424),和允许远程连接(AllowRemoteConnection,默认是0,0-不允许,1-允许)

4、此外,还需要保证负载均衡的IIS站点的标识一样。

(1)可以用以下脚本修改站点的标识:

----------

Dim WebService
Dim oldstr
Dim newstr
Dim args
Set args = WScript.Arguments
If args.Count < 1 Then
    Wscript.Echo "Must have original instance id and new instance id" &     chr(10) & chr(13) & _
    "usage:  moveinstance.vbs 1 5"  & chr(10) & chr(13) & _
"Moves instance 1 to instance 5"
    WScript.Quit()
End If
Set WebService = GetObject("IIS://LocalHost/W3SVC")
oldstr = args(0) 'old instance
newstr = args(1) 'new instance
WebService.MoveHere oldstr,newstr
WebService.SetInfo
Set WebService = nothing
Set args=nothing
WScript.echo "DONE"

------------------

保存为Moveinstance.vbs,执行命令为:Moveinstance.vbs 1 5

(2)也可以在Global类中用以下代码设置AppName为同样的:

public override void Init()
        {
            base.Init();
            foreach (string moduleName in this.Modules)
            {
                string appName = "APPNAME";
                IHttpModule module = this.Modules[moduleName];
                SessionStateModule ssm = module as SessionStateModule;
                if (ssm != null)
                {
                    FieldInfo storeInfo = typeof(SessionStateModule).GetField("_store", BindingFlags.Instance | BindingFlags.NonPublic);
                    SessionStateStoreProviderBase store = (SessionStateStoreProviderBase)storeInfo.GetValue(ssm);
                    if (store == null)//In IIS7 Integrated mode, module.Init() is called later
                    {
                        FieldInfo runtimeInfo = typeof(HttpRuntime).GetField("_theRuntime", BindingFlags.Static | BindingFlags.NonPublic);
                        HttpRuntime theRuntime = (HttpRuntime)runtimeInfo.GetValue(null);
                        FieldInfo appNameInfo = typeof(HttpRuntime).GetField("_appDomainAppId", BindingFlags.Instance | BindingFlags.NonPublic);
                        appNameInfo.SetValue(theRuntime, appName);
                    }
                    else
                    {
                        Type storeType = store.GetType();
                        if (storeType.Name.Equals("OutOfProcSessionStateStore"))
                        {
                            FieldInfo uribaseInfo = storeType.GetField("s_uribase", BindingFlags.Static | BindingFlags.NonPublic);
                            uribaseInfo.SetValue(storeType, appName);
                        }
                    }
                }
            }
        }

参考链接:

https://www.cnblogs.com/w3live/p/5570406.html


https://www.cnblogs.com/ryhan/p/3748976.html



小钉 京ICP备16032583号-1