Hprose在Worker Role下的服务发布
跟ASP.NET Web Role下类似,首先要添加Hprose引用,这一步操作相同,这里不再重复。
之后添加一个Example.cs,内容也与ASP.NET Web Role下的Example.cs类似,仅仅是名称空间有点变化(从HproseServiceWebRole变为HproseServiceWorkerRole)。这里也就不再重复列出代码了。
下面我们看重点,那就是对WorkerRole.cs的修改,主要修改的是它的Run方法,其它方法无需改动,修改后的代码如下:
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Threading;
using Microsoft.WindowsAzure.Diagnostics;
using Microsoft.WindowsAzure.ServiceRuntime;
using Hprose.Server;
namespace HproseServiceWorkerRole {
public class WorkerRole : RoleEntryPoint {
public override void Run() {
// This is a sample worker implementation. Replace with your logic.
Trace.WriteLine("HproseServiceWorkerRole entry point called", "Information");
HproseHttpListenerServer server = new HproseHttpListenerServer("http://127.0.0.1:8010/");
server.Methods.AddInstanceMethods(new Example());
server.Start();
Trace.WriteLine("Hprose Server Started", "Information");
while (true) Thread.Sleep(0);
}
public override bool OnStart() {
// Set the maximum number of concurrent connections
ServicePointManager.DefaultConnectionLimit = 12;
DiagnosticMonitor.Start("DiagnosticsConnectionString");
// For information on handling configuration changes
// see the MSDN topic at http://go.microsoft.com/fwlink/?LinkId=166357.
RoleEnvironment.Changing += RoleEnvironmentChanging;
return base.OnStart();
}
private void RoleEnvironmentChanging(object sender, RoleEnvironmentChangingEventArgs e) {
// If a configuration setting is changing
if (e.Changes.Any(change => change is RoleEnvironmentConfigurationSettingChange)) {
// Set e.Cancel to true to restart this role instance
e.Cancel = true;
}
}
}
}
using System.Linq;
using System.Net;
using System.Threading;
using Microsoft.WindowsAzure.Diagnostics;
using Microsoft.WindowsAzure.ServiceRuntime;
using Hprose.Server;
namespace HproseServiceWorkerRole {
public class WorkerRole : RoleEntryPoint {
public override void Run() {
// This is a sample worker implementation. Replace with your logic.
Trace.WriteLine("HproseServiceWorkerRole entry point called", "Information");
HproseHttpListenerServer server = new HproseHttpListenerServer("http://127.0.0.1:8010/");
server.Methods.AddInstanceMethods(new Example());
server.Start();
Trace.WriteLine("Hprose Server Started", "Information");
while (true) Thread.Sleep(0);
}
public override bool OnStart() {
// Set the maximum number of concurrent connections
ServicePointManager.DefaultConnectionLimit = 12;
DiagnosticMonitor.Start("DiagnosticsConnectionString");
// For information on handling configuration changes
// see the MSDN topic at http://go.microsoft.com/fwlink/?LinkId=166357.
RoleEnvironment.Changing += RoleEnvironmentChanging;
return base.OnStart();
}
private void RoleEnvironmentChanging(object sender, RoleEnvironmentChangingEventArgs e) {
// If a configuration setting is changing
if (e.Changes.Any(change => change is RoleEnvironmentConfigurationSettingChange)) {
// Set e.Cancel to true to restart this role instance
e.Cancel = true;
}
}
}
}
从上面的代码中我们可以看出,在Worker Role方式下,我们是通过创建一个Hprose独立服务器来发布服务的。下面按下F5就可以调试了。因为在Worker Role方式下,浏览器不会自动打开,我们可以自己打开浏览器并键入:http://127.0.0.1:8010/,然后回车,我们会看到跟ASP.NET Web Role方式下同样的内容。使用Nepenthes调试效果也是完全相同的。但这种方式下发布的服务性能是相当高效的。
在Worker Role下除了可以发布C#编写的Hprose服务之外,你还可以通过运行Jetty服务器来发布Java编写的Hprose服务,但运行Jetty较为复杂,网上也有相应介绍,这里就不做介绍了。
上面介绍的两种方式发布的服务都是C#编写的,而下面的CGI Web Role方式下则可以发布PHP的Hprose服务。