Foo*_*bar 7 .net c# wcf windows-services
我在三台不同的机器上平衡了WCF服务负载.
假设这些服务可以处理类型A,B和C的请求.处理A或B没有限制.但是,我们一次只能处理5个类型为C的请求,因此如果C类的第6个请求进入,则必须等到之前的一个请求完成.
如何确保在所有三台计算机上只处理5个C类型的请求?
听起来你需要跨机器信号量,你可以使用像memcached这样的分布式缓存解决方案实现一个.或者,您可以在一台机器上运行另一个WCF服务,该服务器管理负载平衡服务的信号量.
所以新服务可能看起来像这样:
[ServiceContract]
public interface ISemaphorService
{
[OperationContract]
void Acquire();
[OperationContract]
void Release();
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class SemaphoreService
{
private readonly static Semaphore Pool = new Semaphore(5, 5);
public void Acquire()
{
Pool.WaitOne();
}
public void Release()
{
Pool.Release();
}
}
Run Code Online (Sandbox Code Playgroud)
在真实世界的应用程序中,您可能希望在配置或其他内容中配置信号量的数量并放入一些超时并设置机制以确保信号量及时发布和/或客户端崩溃时:
// on the client side (service C)
var client = new SemaphoreServiceClient();
try
{
// acquire the semaphore before processing the request
client.Acquire();
// process request
...
}
finally
{
// always remember the release the semaphore
client.Release();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
805 次 |
| 最近记录: |