(转)简易WCF负载均衡方案
最近跟高老师讨论nginx跟tomcat集群做负载均衡方案。感觉很有意思。想到自己项目中服务用的WCF技术,于是就想WCF如何做负载均衡,Google了一会,发现wcf4.0的路由服务好像可以实现。不过在研究路由服务期间,我有了个自己的方案,哈哈。
我要在客户端跟WCF服务中间部署一台WCF平衡服务器,用来分发请求,模拟nginx的工作。

WCF平衡服务器我同样用WCF来实现,所有服务接口全部通过平衡服务区暴露给客户端。对于客户端来说,只要跟正常调用服务一样,添加平衡器的远程服务引用。
实现:
1.平衡服务类库
namespace WcfSimpleBalance
{
/// <summary>
/// 负载均衡基类
/// </summary>
/// <typeparam name="T"></typeparam>
public class WcfBalance<T>
{
private ChannelFactory<T> _channelFactory; public T BalanceProxy { get; set; } public WcfBalance(string serviceName)
{
string endpoint = EndpointBalance.GetBalanceEndpoint(serviceName);//获取随机endpoint
this._channelFactory = new ChannelFactory<T>(endpoint);
this.BalanceProxy = this._channelFactory.CreateChannel();
}
}
}
其中泛型T为协定,这样就能动态构建wcf的通道了。
namespace WcfSimpleBalance
{
internal class EndpointBalance
{
/// <summary>
/// 平衡节点配置
/// </summary>
private static List<WcfBalanceSection> _wcfBalanceCfg;
static EndpointBalance()
{
_wcfBalanceCfg = ConfigurationManager.GetSection("wcfBalance") as List<WcfBalanceSection>;
} /// <summary>
/// 随机一个Endpoint
/// </summary>
/// <param name="serviceName"></param>
/// <returns></returns>
public static string GetBalanceEndpoint(string serviceName)
{
var serviceCfg = _wcfBalanceCfg.Single(s=>s.ServiceName==serviceName);
var ran = new Random();
int i = ran.Next(0, serviceCfg.Endpoints.Count);
string endpoint = serviceCfg.Endpoints[i];
Console.WriteLine(endpoint);
return endpoint;
}
}
}
这个类提供一个静态方法可以根据服务名称从配置文件中配置的endpoint,并且从中随机一个。随机数的算法可能分布不是特别均匀,不知有什么好的办法。
namespace WcfSimpleBalance
{
/// <summary>
/// 配置模型
/// </summary>
internal class WcfBalanceSection
{
public string ServiceName { get; set; } public List<string> Endpoints { get; set; }
}
/// <summary>
/// 自定义配置处理
/// </summary>
public class WcfBalanceSectionHandler : IConfigurationSectionHandler
{
public object Create(object parent, object configContext, XmlNode section)
{
var config = new List<WcfBalanceSection>();
foreach (XmlNode node in section.ChildNodes)
{
if (node.Name != "balanceService")
throw new ConfigurationErrorsException("不可识别的配置项", node);
var item = new WcfBalanceSection();
foreach (XmlAttribute attr in node.Attributes)
{
switch (attr.Name)
{
case "ServiceName":
item.ServiceName = attr.Value;
break;
case "Endpoints":
item.Endpoints = attr.Value.Split(',').ToList();
break;
default:
throw new ConfigurationErrorsException("不可识别的配置属性", attr);
}
}
config.Add(item);
}
return config;
}
}
}
这2个是用来处理配置文件的。
2.普通的WCF服务
协定:
namespace WcfServiceContracts
{
[ServiceContract(Name = "CalculatorService")]
public interface IAdd
{
[OperationContract]
int Add(int x, int y);
}
}
一个简单的加法。
wcf服务实现:
namespace WcfService
{
public class AddServices:IAdd
{
public int Add(int x, int y)
{
return x + y;
}
}
}
3.WCF平衡器实现
同样新建一个wcf服务类库,引用同样的协定,引用上面的平衡类库
namespace WcfServiceBalance
{
public class AddServices : WcfBalance<IAdd>, IAdd
{
public AddServices()
: base("AddServices")
{
} public int Add(int x, int y)
{
return BalanceProxy.Add(x, y);
}
}
}
继承WcfBalance跟协定接口。构造函数调用基类的构造函数,传入服务名称。Add实现直接调用基类的方法。
模拟:
1.wcf服务器寄宿
WCF服务可以寄宿在多个方案下面,IIS,win服务,控制台。这里为了方便直接寄宿在控制台下。
新建2个控制台程序,一个寄宿普通的wcf服务。一个寄宿wcf平衡服务。代码不表,给出服务地址。
3个普通的服务。(把寄宿普通服务的控制台程序的bin目录复制3份,改3个端口就成了3个服务)
平衡服务
http://localhost:8088/WcfBalance
配置文件
在平衡服务器的配置文件中定义所有后台服务器的endpoint,然后在自定义wcfBalance节点中配置,服务名对应的endpoint列表用逗号分隔。
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="wcfBalance" type="WcfSimpleBalance.WcfBalanceSectionHandler, WcfSimpleBalance" />
</configSections>
<wcfBalance>
<balanceService ServiceName="AddServices" Endpoints="AddService1,AddService2,AddService3" />
</wcfBalance>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_CalculatorService" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:8081/Wcf" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_CalculatorService"
contract="WcfServiceContracts.IAdd" name="AddService1" />
<endpoint address="http://localhost:8082/Wcf" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_CalculatorService"
contract="WcfServiceContracts.IAdd" name="AddService2" />
<endpoint address="http://localhost:8083/Wcf" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_CalculatorService"
contract="WcfServiceContracts.IAdd" name="AddService3" />
</client>
</system.serviceModel>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
2.客户端调用
添加平衡服务器引用,然后用代码调用。
启动30个线程去跑服务。
namespace WcfServiceClient
{
class Program
{
static void Main(string[] args)
{
for (int i = 0; i < 30; i++)
{
var thread = new Thread(new ThreadStart(CallAdd));
thread.Start();
}
Console.Read();
} private static void CallAdd()
{
using (var proxy = new CalculatorServiceClient())
{
proxy.Add(1,2);
}
}
}
}
运行结果:

请求被分布到3个服务上面,不过貌似不太均匀,这个跟算法有关系。
通过以上我们实现了一个简单的wcf平衡服务器,这只是一个简单的方案,肯定有很多很多问题没有考虑到,希望大家指出讨论。
不过我想虽然实现了请求的分发,但是面对真正的高并发环境,平衡服务器会不会成为另外一个瓶颈。
(转)简易WCF负载均衡方案的更多相关文章
- 大数据时代下的SQL Server第三方负载均衡方案----Moebius测试
一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 架构原理(Architecture) 测试环境(Environment) 安装Moebius( ...
- Openfire 集群部署和负载均衡方案
Openfire 集群部署和负载均衡方案 一. 概述 Openfire是在即时通讯中广泛使用的XMPP协议通讯服务器,本方案采用Openfire的Hazelcast插件进行集群部署,采用Hapro ...
- Windows平台下利用APM来做负载均衡方案 - 负载均衡(下)
概述 我们在上一篇Windows平台分布式架构实践 - 负载均衡中讨论了Windows平台下通过NLB(Network Load Balancer) 来实现网站的负载均衡,并且通过压力测试演示了它的效 ...
- (转)大数据时代下的SQL Server第三方负载均衡方案----Moebius测试
一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 架构原理(Architecture) 测试环境(Environment) 安装Moebius( ...
- asp.net负载均衡方案[转]
在前面的几篇文章中,主要谈到了在Discuz!NT中的跨站缓存数据,数据库负载均衡.但如果要实现将产品分布式布置到若干机器,组成集群来共同支撑起整个业务的话,还是有一定问题的(后面会有所介绍).下面先 ...
- 高可用性的负载均衡方案之lvs+keepalived和haproxy+heartbeat区别
高可用性的负载均衡方案 目前使用比较多的就是标题中提到的这两者,其实lvs和haproxy都是实现的负载均衡的作用,keepalived和heartbeat都是提高高可用性的,避免单点故障.那么他们为 ...
- MySQL数据库读写分离、读负载均衡方案选择
MySQL数据库读写分离.读负载均衡方案选择 一.MySQL Cluster外键所关联的记录在别的分片节点中性能很差对需要进行分片的表需要修改引擎Innodb为NDB因此MySQL Cluster不适 ...
- mysql负载均衡方案
mysql负载均衡方案 一.直接连接 数据库的读写分离方案很多,这里介绍基于mysql数据库的读写分离方案. 比较常见的读写分离方案如下: 1 基于查询分离 最简单的分离方法是将读和写分发到主和从服务 ...
- 【转载】Windows平台下利用APM来做负载均衡方案 - 负载均衡(下)
概述 我们在上一篇Windows平台分布式架构实践 - 负载均衡中讨论了Windows平台下通过NLB(Network Load Balancer) 来实现网站的负载均衡,并且通过压力测试演示了它的效 ...
随机推荐
- RDLC 聚合函数按条件求和,显示"错误号"
=Sum(IIf(Fields!AValue.Value >0,Val(Fields!AValue.Value),)) 一直显示错误号 修改为 =Sum(IIf(Fields!AValue.Va ...
- 关于控制文件和redo log损坏的恢复
前段时间一朋友自己电脑上的开发测试用的数据库出了点问题,电脑操作系统是Win8,直接在Win8上安装了Oracle11g,后来系统自动升级到Win8.1,Oracle相关的服务全都不见了,想想把数据文 ...
- C#显示声名接口就是为了解决方法重名的问题
class class1 { public static void Main(string[] args) { Person ps = new Person(); ps.KouLan(); IFlya ...
- CSS学习笔记总结和技巧
跟叶老师说项目,他叫我写一个静态首页,看起来挺简单的,但是下手才发现在真的不会怎么下手啊,什么模型啊模块啊都不懂,写毛线啊!! 如图:页面下拉还有侧栏,中间内容等. 可是答应跟老师做了,不能怂啊,于是 ...
- (原)torch中微调某层参数
转载请注明出处: http://www.cnblogs.com/darkknightzh/p/6221664.html 参考网址: https://github.com/torch/nn/issues ...
- uva 12626 - I ❤ Pizza
#include <iostream> #include <cstdio> #include <string> #include <algorithm> ...
- javascript中的substr和substring
1.substr 方法 返回一个从指定位置开始的指定长度的子字符串. stringvar.substr(start [, length ]) 参数: stringvar 必选项. 要提取子字符串的字 ...
- 将含有父ID的列表转成树
我们知道数据库一般是以一个列表(id,pid)的形式保存树的.如何提取这棵树呢?最简单的方法就是根据pid循环查表.但是毫无疑问,这会产生巨大的数据库查询开销. 那么一般建议的方法是一次性将全部相关数 ...
- flask开发restful api系列(1)
在此之前,向大家说明的是,我们整个框架用的是flask + sqlalchemy + redis.如果没有开发过web,还是先去学习一下,这边只是介绍如果从开发web转换到开发移动端.如果flask还 ...
- C 语言 输入字符串 并计算输入的字符的长度
int main(void) { char a[50];int i=0;char *j;gets(a);//输入字符串j=a;while(*j!='\0'){j++;//指针指向下一个数组字符i++; ...