Router and load balancing and Implementation ideas of Service hosts (1)

Source: Internet
Author: User

Router and load balancing and Implementation ideas of Service hosts (1)

Router introduction and demonstration

The routing function of the WCF middleware is to add an intermediary service between the client and the server to forward messages between them. To Implement Message forwarding, you can modify the content of the WCF Service message header and re-specify the service address. Then, you need to configure the server address directory for the router where the service address is specified for the message header, the router and the server are certainly not one-to-one. The router can specify multiple servers, and the router specifies the server to which the client is connected. Here there is an algorithm, the advantages and disadvantages of algorithms determine the middleware Load Balancing Capability.

The following demonstrates the middleware routing function. Six copies of the program in the Out directory are copied and renamed as follows: three clients, one with middleware and two with service middleware, you also need to modify the configuration of each program. In this way, the routing middleware Router, the Service middleware WCFservser1, and WCFServer2 are started, and then two client programs are started respectively, the connection information of the client is displayed in the routing middleware and service middleware. Two of the three clients are allocated to one service middleware, and one client is allocated to another service middleware. It does not mean that all three clients are allocated to one service middleware, this is determined by the load balancing algorithm of routing middleware;

Implementation of the routing function

The framework adds a routing service object Router that can be used to intercept messages sent by clients. The interception method is ProcessMessage (Message requestMessage );

First, obtain the endpointAddress Based on the routing directory and the load balancing algorithm, then create a WCF channel, bind a new service address, and call the method of the server;

 
 
  1. /// <Summary>
  2. /// Intercept the message sent from the Client and forward it to the target endpoint and obtain the return value to the Client.
  3. /// </Summary>
  4. /// <Param name = "requestMessage"> </param>
  5. /// <Returns> </returns>
  6. Public Message ProcessMessage (Message requestMessage)
  7. {
  8. // Binding binding = null;
  9. EndpointAddress endpointAddress = null;
  10. GetServiceEndpoint (requestMessage, out endpointAddress );
  11. IDuplexRouterCallback callback = OperationContext. Current. GetCallbackChannel <IDuplexRouterCallback> ();
  12. NetTcpBinding tbinding = new NetTcpBinding ("netTcpExpenseService_ForSupplier ");
  13. Using (DuplexChannelFactory <IRouterService> factory = new DuplexChannelFactory <IRouterService> (new InstanceContext (null, new DuplexRouterCallback (callback), tbinding, endpointAddress ))
  14. {
  15.  
  16. Factory. Endpoint. Behaviors. Add (new MustUnderstandBehavior (false ));
  17. IRouterService proxy = factory. CreateChannel ();
  18.  
  19. Using (proxy as IDisposable)
  20. {
  21. // Request message record
  22. IClientChannel clientChannel = proxy as IClientChannel;
  23. // Console. writeLine (String. format ("Request received ed at {0}, to {1} \ r \ n \ tAction: {2}", DateTime. now, clientChannel. remoteAddress. uri. absoluteUri, requestMessage. headers. action ));
  24. If (Convert. ToInt32 (HostSettingConfig. GetValue ("debug") = 1)
  25. HostwcfMsg (DateTime. Now, String. Format ("route request message sending: {0}", clientChannel. RemoteAddress. Uri. AbsoluteUri ));
  26. // Call the service method of the bound endpoint
  27. Message responseMessage = proxy. ProcessMessage (requestMessage );
  28.  
  29. // Response Message record
  30. // Console. writeLine (String. format ("Reply replicated ed at {0} \ r \ n \ tAction: {1}", DateTime. now, responseMessage. headers. action ));
  31. // Console. WriteLine ();
  32. // HostwcfMsg (DateTime. Now, String. Format ("Response Message: {0}", responseMessage. Headers. Action ));
  33. Return responseMessage;
  34. }
  35. }
  36. }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.