前言
以前对比过enginx和其他几个web服务器(IIS,Apache,lighttpd)的处理静态文件的能力,enginx是最好的,甚至超过其他的几倍。
虽说enginx官方声明在Windows上的表现不如Linux等OS,经过测试enginx的异步处理和响应真出乎意料。
正文
测试的目的和思路:
后台接口用wcf,json作为请求和响应的类型,HTTP协议。
纯前端用angular及路由,enginx作为web服务器。
凡请求后台接口enginx都代理转发到wcf处理。
测试上下文:
Firebird3.0.3标准版、ORM微框架FluentData、VS2015、WebStorm2017.
数据库访问:
1 class DbCxt 2 { 3 private static string _connStr = ""; 4 5 static DbCxt() 6 { 7 FbConnectionStringBuilder connBuilder = new FbConnectionStringBuilder(); 8 connBuilder.DataSource = "localhost"; 9 connBuilder.UserID = "sysdba"; 10 connBuilder.Password = "1234"; 11 connBuilder.Database = "wms"; 12 connBuilder.Charset = "utf8"; 13 connBuilder.Dialect = 3; 14 connBuilder.ServerType = FbServerType.Default; 15 16 _connStr = connBuilder.ConnectionString; 17 18 } 19 20 public static IDbContext NewDbContext() 21 { 22 return new DbContext().ConnectionString(_connStr, new DB2Provider() 23 , FirebirdClientFactory.Instance); 24 } 25 26 public static string ConnStr() 27 { 28 return _connStr; 29 } 30 }
wcf接口:
1 [ServiceContract] 2 public interface IWmsService 3 { 4 [OperationContract] 5 [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] 6 DtoResult<DtoUser> AddUser(DtoUser usr); 7 }
接口实现:
1 public class WmsService : IWmsService 2 { 3 public DtoResult<DtoUser> AddUser(DtoUser usr) 4 { 5 if (usr != null) 6 { 7 DateTime creTime = DateTime.Now; 8 DateTime.TryParse(usr.CreTime, out creTime); 9 using (var db = DbCxt.NewDbContext()) 10 { 11 int count = db.Insert("m_user") 12 .Column("code", usr.Code) 13 .Column("label", usr.Label) 14 .Column("cre_time", creTime) 15 .Column("remark", usr.Remark) 16 .Execute(); 17 return new DtoResult<DtoUser>() {State = 1, Data = usr}; 18 } 19 } 20 return new DtoResult<DtoUser>() {State = 0, Msg = "用户不能为空"}; 21 } 22 }
wcf宿主:
1 static void Main(string[] args) 2 { 3 ServiceHost host = new ServiceHost(typeof(Wms.Service.WmsService)); 4 host.Open(); 5 Console.WriteLine("已启动"); 6 Console.WriteLine("回车键退出"); 7 Console.ReadLine(); 8 }
wcf配置:
1 <system.serviceModel> 2 <services> 3 4 <service name="Wms.Service.WmsService" behaviorConfiguration="http" > 5 <host> 6 <baseAddresses> 7 <add baseAddress="http://localhost:20001/WmsService"/> 8 </baseAddresses> 9 </host> 10 <endpoint binding="webHttpBinding" contract="Wms.Service.IWmsService" behaviorConfiguration="web"></endpoint> 11 </service> 12 </services> 13 14 <behaviors> 15 <serviceBehaviors> 16 <behavior name="mex"> 17 <serviceMetadata/> 18 </behavior> 19 <behavior name="http"> 20 <serviceMetadata httpGetEnabled="true"/> 21 <serviceDebug includeExceptionDetailInFaults="true" httpHelpPageEnabled="true"/> 22 </behavior> 23 </serviceBehaviors> 24 <endpointBehaviors> 25 <behavior name="web"> 26 <webHttp helpEnabled="true"/> 27 </behavior> 28 </endpointBehaviors> 29 </behaviors> 30 </system.serviceModel>
前端页面:
js提交代码:Note 请求URL最前的/不能省,因为后面enginx配置代理时不允许前缀后面加/
1 $scope.data = { 2 Code: '', Label: '', CreTime: new Date().Format('yyyy-MM-dd hh:mm:ss'), Remark: '', Pwd:'123456' 3 }; 4 5 $scope.save = function () { 6 $http.post('/wmsService/addUser', $scope.data).then(function (value) { 7 console.log(value); 8 }) 9 };
enginx代理转发部分配置:~*表示URL不区分大小写匹配,因为默认wcf也不区分。
1 location ~* /WmsService/ { 2 proxy_pass http://127.0.0.1:20001; 3 }
enginx其他配置都取默认,除端口改为10005以外。