Ensure the uniqueness of objects-singleton mode (2)

Source: Internet
Author: User
3.3 Design and Implementation of Server Load balancer

Sunny software company undertakes the development of a server load balance software, which runs on a server Load balancer server, concurrent access and data traffic can be distributed to multiple devices in the server cluster for concurrent processing, improving the overall processing capability of the system and shortening the response time. Because the servers in the cluster need to be dynamically deleted and client requests must be distributed in a unified manner, the uniqueness of the Server Load balancer must be ensured. Only one Server Load balancer can be used to manage servers and distribute requests, otherwise, Server Status Inconsistency and request allocation conflicts may occur. How to Ensure the uniqueness of the Server Load balancer is the key to the success of the software.

Through analysis and trade-offs, sunny developers decided to design the Server Load balancer using the singleton mode, as shown in Figure 3-3:

In Figure 3-3, the Load balancer is designed as a singleton class, which contains a collection of server information serverlist. Each time a server is randomly selected in the serverlist to respond to client requests, the implementation code is as follows:

Import Java. util. *; // Load balancer: single-instance class. In a real environment, this class is very complex, including a large number of initialization work and business methods. Considering the readability and comprehensibility of the Code, only list some of the core code class loadbalancer {// Private Static member variables related to the mode, store the unique instance Private Static loadbalancer instance = NULL; // server set Private list serverlist = NULL; // private constructor private loadbalancer () {serverlist = new arraylist ();} // public static member method, return the unique instance public static loadbalancer getloadbalancer () {If (instance = NULL) {instance = new loadbalancer ();} return instance;} // Add public void addserver (string server) {serverlist. add (server) ;}// Delete the public void removeserver (string server) {serverlist. remove (server) ;}// use the random class to randomly retrieve the server Public String getserver () {random = new random (); int I = random. nextint (serverlist. size (); Return (string) serverlist. get (I );}}
Write the following client test code:
Class client {public static void main (string ARGs []) {// create four loadbalancer objects: loadbalancer balancer1, balancer2, balancer3, balancer4; balancer1 = loadbalancer. getloadbalancer (); balancer2 = loadbalancer. getloadbalancer (); balancer3 = loadbalancer. getloadbalancer (); balancer4 = loadbalancer. getloadbalancer (); // determine whether the Server Load balancer is the same if (balancer1 = balancer2 & balancer2 = balancer3 & balancer3 = balancer4) {syst Em. Out. println ("Server Load balancer is unique! ");} // Add Server balancer1.addserver (" Server 1 "); balancer1.addserver (" Server 2 "); balancer1.addserver (" Server 3 "); balancer1.addserver (" Server 4 "); // simulate the distribution of client requests (INT I = 0; I <10; I ++) {string Server = balancer1.getserver (); system. out. println ("Distribution Request to server:" + server );}}}
Compile and run the program. The output result is as follows:

Server Load balancer is unique!

Distribute requests to server: Server 1

Distribute requests to server: Server 3

Distribute requests to the server: Server 4

Distribute requests to the server: Server 2

Distribute requests to server: Server 3

Distribute requests to the server: Server 2

Distribute requests to server: Server 3

Distribute requests to the server: Server 4

Distribute requests to the server: Server 4

Distribute requests to server: Server 1

Although four loadbalancer objects are created, they are actually the same object. Therefore, the uniqueness of the loadbalancer object can be ensured through the singleton mode.

[Author: Liu Wei http://blog.csdn.net/lovelion]

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.