Nginx WCF做负载均衡

Nginx WCF做负载均衡

  1. 创建3个WCF服务,修改app.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>

   <appSettings>
   	<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
   </appSettings>
   <system.web>
   	<compilation debug="true" targetFramework="4.7.2" />
   	<httpRuntime targetFramework="4.7.2"/>
   </system.web>
   <system.serviceModel>
   	<behaviors>
   		<serviceBehaviors>
   			<behavior>
   				<!-- 为避免泄漏元数据信息,请在部署前将以下值设置为 false -->
   				<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
   				<!-- 要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息 -->
   				<serviceDebug includeExceptionDetailInFaults="false"/>
   			</behavior>
   		</serviceBehaviors>
   	</behaviors>
   	<protocolMapping>
   		<add binding="basicHttpsBinding" scheme="https" />
   	</protocolMapping>
   	<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
   	<services>
   		<service name="WindowsFormsApp2.user">
   			<endpoint address="/user" binding="basicHttpBinding" contract="WindowsFormsApp2.Iuser">
   				<identity>
   					<dns value="localhost" />
   				</identity>
   			</endpoint>
   			<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
   			<host>
   				<baseAddresses>
   					<add baseAddress="http://192.168.99.38:8889" /> 
   				</baseAddresses>
   			</host>
   		</service>
   	</services>
   </system.serviceModel>
   <system.webServer>
   	<modules runAllManagedModulesForAllRequests="true"/>
   	<!--
       若要在调试过程中浏览 Web 应用程序根目录,请将下面的值设置为 True。
       在部署之前将该值设置为 False 可避免泄露 Web 应用程序文件夹信息。
     -->
   	<directoryBrowse enabled="true"/>
   </system.webServer>


</configuration>

我这里三个服务是在同一台主机上,所以端口不一样 分别是 8888 8889 8890

  1. 创建form 用于开启和停止wcf
    在这里插入图片描述
private void button1_Click(object sender, EventArgs e)
        {
           
            try
            {
                host = new ServiceHost(typeof(WindowsFormsApp2.user));
                host.Open();
                MessageBox.Show("服务已启动:" + host.State.ToString() + " | " + host.BaseAddresses[0].AbsoluteUri);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                host.Close();
                MessageBox.Show("服务已停止:" + host.State.ToString() + " | " + host.BaseAddresses[0].AbsoluteUri);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
  1. 修改下DoWork方法
    1111
    8888 对应服务1 8889 对应服务2 8890 对应 我是备份

  2. 开启三个wcf
    在这里插入图片描述

  3. 修改nginx配置


#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


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;
    upstream abc{

        server 192.168.99.38:8888 max_fails=6 fail_timeout=5s;
        server 192.168.99.38:8889 max_fails=2 fail_timeout=2s;
        server 192.168.99.38:8890 backup;
    }
    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
            proxy_pass   http://abc;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.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;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    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;
    #    }
    #}

}

  1. 开启 nginx
  2. 客户端测试
    新建一个form 添加服务引用
    在这里插入图片描述
    修改 客户端app.config
    注意 这里需要将自动生成的app.config做下修改,将address修改为 nginx服务器ip+wcf服务名
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
    </startup>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_Iuser" />
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://192.168.70.130/user" binding="basicHttpBinding"
                bindingConfiguration="BasicHttpBinding_Iuser" contract="ServiceReference1.Iuser"
                name="BasicHttpBinding_Iuser" />
        </client>
    </system.serviceModel>
</configuration>

在这里插入图片描述
点击开始
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值