wcf高并发 mysql_使用nginx搭建高可用,高并发的wcf集群

本文介绍了如何利用Nginx作为负载均衡器,结合Zookeeper实现对WCF服务的复杂均衡。尽管Zookeeper对C#支持不够友好,但在需求不那么精细的情况下,Nginx足以胜任。文中详细展示了在Windows和CentOS环境下,配置WCF服务和Nginx的步骤,以及客户端的调用方式,通过1:5的权重比例展示了负载均衡的效果。
摘要由CSDN通过智能技术生成

很多情况下基于wcf的复杂均衡都首选zookeeper,这样可以拥有更好的控制粒度,但zk对C# 不大友好,实现起来相对来说比较麻烦,实际情况下,如果

你的负载机制粒度很粗糙的话,优先使用nginx就可以搞定,既可以实现复杂均衡,又可以实现双机热备,以最小的代码量实现我们的业务,下面具体分享下。

一:准备的材料

1. 话不多说,一图胜千言,图中的服务器都是采用vmware虚拟化,如下图:

65e82355b83ba12e26cb0fa3c19f014e.png

《1》 三台windows机器 ,两个WCF的windows服务器承载(192.168.23.187,192.168.23.188),一台Client的服务器(192.168.23.1)

《2》 一台Centos机器,用来承载web复杂均衡nginx(192.168.23.190)。

《3》在所有的Client的Hosts文件中增加host映射:【192.168.23.190 cluster.com】,方便通过域名的形式访问nginx所在服务器的ip地址。

二:环境搭建

1. WCF程序

既然是测试,肯定就是简单的程序,代码就不完全给出了。

《1》 HomeService实现类代码如下(输出当前server的ip地址,方便查看):

1 public classHomeService : IHomeService2 {3 public string DoWork(stringmsg)4 {5 var ip = Dns.GetHostAddresses(Dns.GetHostName()).FirstOrDefault(i => i.AddressFamily ==

6 AddressFamily.InterNetwork).ToString();7

8 return string.Format("当前 request 由 server={0} 返回", ip);9 }10 }

《2》 App.Config代码

1 <?xml version="1.0" encoding="utf-8"?>

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

因为windows的两台机器的ip地址是192.168.23.187,192.168.23.188,所以部署的时候注意一下config中的baseAddress地址。

2. centos上的nginx搭建

nginx我想大家用的还是比较多的,去官网下载最新的就好【

[root@localhost nginx-1.13.6]# yum install -y gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel openssl openssl-devel

[root@localhost nginx-1.13.6]# ./configure --prefix=/usr/myapp/nginx

[root@localhost nginx-1.13.6]# make && make install

然后在nginx的安装目录下面找到conf文件,修改里面的nginx.conf 配置。

[root@localhost nginx]# cd conf

[root@localhost conf]# ls

fastcgi.conf koi-utf nginx.conf uwsgi_params

fastcgi.conf.default koi-win nginx.conf.default uwsgi_params.defaultfastcgi_params mime.types scgi_params win-utf

fastcgi_params.default mime.types.default scgi_params.default[root@localhost conf]# vim nginx.conf

详细配置如下,注意下面“标红”的地方,权重按照1:5的方式进行调用,关于其他的配置,大家可以在网上搜一下就可以了。

#user nobody;

worker_processes1;

#error_log logs/error.log;

#error_log logs/error.log notice;

#error_log logs/error.log info;

#pid logs/nginx.pid;

events {

worker_connections1024;

}

http {

include mime.types;

default_type application/octet-stream;

#log_format main'$remote_addr - $remote_user [$time_local] "$request"'#'$status $body_bytes_sent "$http_referer"'#'"$http_user_agent" "$http_x_forwarded_for"';

#access_log logs/access.log main;

sendfile on;

#tcp_nopush on;

#keepalive_timeout0;

keepalive_timeout65;

#gzip on;

upstream cluster.com{

server192.168.23.187:8733 weight=1;

server 192.168.23.188:8733 weight=5;

}

server {

listen80;

server_name localhost;

#charset koi8-r;

#access_log logs/host.access.log main;

location/ {

root html;

index index.html index.htm;

proxy_pass http://cluster.com;

#设置主机头和客户端真实地址,以便服务器获取客户端真实IP

proxy_set_header X-Forwarded-Host $host;

proxy_set_header X-Forwarded-Server $host;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_set_header X-Real-IP $remote_addr;

}

#error_page404 /404.html;

# redirect server error pages to thestatic page /50x.html

#

error_page500 502 503 504 /50x.html;

location= /50x.html {

root html;

}

# proxy the PHP scripts to Apache listening on127.0.0.1:80#

#location~\.php$ {

# proxy_pass http://127.0.0.1;

#}

# pass the PHP scripts to FastCGI server listening on127.0.0.1:9000#

#location~\.php$ {

# root html;

# fastcgi_pass127.0.0.1:9000;

# fastcgi_index index.php;

# fastcgi_param SCRIPT_FILENAME/scripts$fastcgi_script_name;

# include fastcgi_params;

#}

# deny access to .htaccess files,if Apache's document root

# concurs with nginx's one

#

#location~ /\.ht {

# deny all;

#}

}

# anothervirtual host using mix of IP-, name-, and port-based configuration

#

#server {

# listen8000;

# listen somename:8080;

# server_name somename alias another.alias;

# location/{

# root html;

# index index.html index.htm;

# }

#}

# HTTPS server

#

#server {

# listen443ssl;

# server_name localhost;

# ssl_certificate cert.pem;

# ssl_certificate_key cert.key;

# ssl_session_cache shared:SSL:1m;

# ssl_session_timeout 5m;

# ssl_ciphers HIGH:!aNULL:!MD5;

# ssl_prefer_server_ciphers on;

# location/{

# root html;

# index index.html index.htm;

# }

#}

}

3. client端的程序搭建

《1》 第一件事就是将 192.168.23.190 映射到本机的host中去,因为服务不提供给第三方使用,所以加host还是很轻松的。

192.168.23.190 cluster.com

21e626fda46d8250007f50710b877b38.png

《2》 然后就是client端程序添加服务引用,因为添加了host映射,所以服务引用地址就是"http://cluster.com"。 代码如下:

77fd0789ea7369e8345c433261aa2b17.png

1 classProgram2 {3 static void Main(string[] args)4 {5 for (int i = 0; i < 1000; i++)6 {7 HomeServiceClient client = newHomeServiceClient();8

9 var info = client.DoWork("hello world!");10

11 Console.WriteLine(info);12

13 System.Threading.Thread.Sleep(1000);14 }15

16 Console.Read();17 }18 }

最后来执行以下程序,看看1000次循环中,是不是按照权重1:5 的方式对后端的wcf进行调用的???

4e9ba24dd9c1a2b84da978545553f08b.png

924c1432ae1b97171b8db8d5af690ed3.png

看到没有,是不是很牛逼,我只需要通过cluster.com进行服务访问,nginx会自动给我复杂均衡,这就是我们开发中非常简单化的wcf复杂均衡。

希望本篇对你有帮助~~~~   附完整源代码:cluster.zip

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值