In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
In this article, the editor introduces in detail "how to achieve the smooth allocation of reverse proxy cluster services through Java". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to achieve smooth allocation of reverse proxy cluster services through Java" can help you solve your doubts.
1. Understand the whole process 1.1. Overview
The knowledge required.
Socket network programming
Understanding of reverse proxy
Understanding of smooth weighted polling algorithm
Understanding of thread pool
Objective: to realize the smooth weighted polling load of Socket cluster service.
Business implementation: the client queries the user information in the cluster service through the user name.
1.2. The whole process
The client initiates a Socket request to the reverse proxy Socket service (the client does not know that the server is the reverse proxy server)
The reverse proxy server received a Socket service request
The thread pool opens the service thread to process the request
The thread service finds the current downstream service with the highest weight through the smooth weighted polling algorithm.
Create a Socket request through the service node information returned by the load balancing algorithm
The reverse proxy server uses client information to initiate Socket requests to downstream services
The Socket cluster service node receives the Socket request, queries the user information, and then returns the processing result to the reverse proxy server
The reverse proxy server then returns the results to the client.
Several minutiae
Using reverse proxy service, there is no feeling to the client, and the client does not know which real server is accessed.
Each time the reverse proxy server fails to access the downstream service, it will reduce the effective weight of the downstream server; each time the access to the downstream service is successful, it will increase the effective weight of the downstream server (no more than the configured weight value).
The smooth weighted polling algorithm will reduce and increase the rights of downtime services, and have the effect of "eliminating" downtime services and buffering and restoring downtime services.
After the reverse proxy server is restarted, all configurations return to configuration parameters
The reverse proxy server publishes the Socket service using a thread pool, which supports multiple clients to request simultaneous distribution.
two。 Code implementation 2.1. Node class
Used to save information about the service node
Package com.yty.proxy.lba;public class Node implements Comparable {private String ip; private Integer port; private final Integer weight; private Integer effectiveWeight; private Integer currentWeight; / / default weight is: 1 public Node (String ip,Integer port) {this (ip,port,1);} public Node (String ip,Integer port, Integer weight) {this.ip = ip; this.port = port; this.weight = weight This.effectiveWeight = weight; this.currentWeight = weight;} public String getIp () {return ip;} public void setIp (String ip) {this.ip = ip;} public Integer getPort () {return port;} public void setPort (Integer port) {this.port = port;} public Integer getWeight () {return weight } public Integer getEffectiveWeight () {return effectiveWeight;} public void setEffectiveWeight (Integer effectiveWeight) {this.effectiveWeight = effectiveWeight;} public Integer getCurrentWeight () {return currentWeight;} public void setCurrentWeight (Integer currentWeight) {this.currentWeight = currentWeight } / / for each success, restore the effective weight 1, which does not exceed the configured starting weight public void onInvokeSuccess () {if (effectiveWeight).
< weight) effectiveWeight++; } // 每失败一次,有效权重减少1,无底线的减少 public void onInvokeFault(){ effectiveWeight--; } @Override public int compareTo(Node node) { return currentWeight >Node.currentWeight? 1: (currentWeight.equals (node.currentWeight)? 0:-1) } @ Override public String toString () {return "Node {" + "ip='" + ip +'\'+ ", port=" + port + ", weight=" + weight + ", effectiveWeight=" + effectiveWeight + ", currentWeight=" + currentWeight +'}';}} 2.2. Agent configuration class
Generally, it is configured in the configuration file, and then read the configuration file information of the specified key to complete the configuration. The simulation is written directly into the code for simplicity.
Package com.yty.proxy;import com.yty.proxy.lba.Node;import java.util.ArrayList;import java.util.List;public class ProxyConfig {private static List nodes = new ArrayList (); / / read: node collection information in the configuration file. If you are testing on the same server, match ip to the same static {nodes.add (new Node ("192.168.233.100", 8001Power2)); nodes.add (new Node ("127.0.0.1", 8002); nodes.add (new Node ("127.0.0.1", 8003));} public static List getProxyConfig () {return nodes;} 2.3. Load balancing algorithm interface package com.yty.proxy.lba;public interface Robin {Node selectNode ();} 2.4. Smooth weighted polling algorithm
You can read the first two articles about load balancing algorithms in detail.
Package com.yty.proxy.lba;import com.yty.proxy.ProxyConfig;import java.util.List;/** * weighted polling algorithm: smooth weighted polling algorithm * / public class WeightedRoundRobin implements Robin {private static List nodes; / / read configuration information static {nodes = ProxyConfig.getProxyConfig () } / * obtain IP * @ return Node * / public Node selectNode () {if (nodes = = null | | nodes.size () 0) according to the current maximum weight (currentWeight)? NodeOfMaxWeight: node;} / / smooth load balancer nodeOfMaxWeight.setCurrentWeight (nodeOfMaxWeight.getCurrentWeight ()-totalWeight); nodes.forEach (node-> node.setCurrentWeight (node.getCurrentWeight () + node.getEffectiveWeight (); return nodeOfMaxWeight;}} 2.5. Proxy service thread class
A thread class used to process proxy service requests. Different requests are created by different threads to handle
Package com.yty.proxy;import java.io.*;import java.net.Socket;import java.util.ArrayList;import java.util.List;public class ProxyServerThread implements Runnable {private Socket proxySocket; private OutputStream proxyOut; private InputStream proxyIn; private Socket socket; private OutputStream serverOut; private InputStream serverIn; public ProxyServerThread (Socket proxySocket) throws IOException {this.proxySocket = proxySocket; this.proxySocket.setSoTimeout (6000); this.proxyOut = proxySocket.getOutputStream () This.proxyIn = proxySocket.getInputStream ();} @ Override public void run () {try {this.proxyService ();} catch (IOException e) {e.printStackTrace ();} finally {this.close () } private void proxyService () throws IOException {/ / Agent receives client request byte [] proxyDataBytes = null; proxyDataBytes = getData (proxyIn); System.out.println ("Agent receives request data:" + new String (proxyDataBytes)); if (proxyDataBytes = = null) {proxyOut.write ("request content exception" .getBytes ()) } byte [] serverData = this.dispatcherService (proxyDataBytes); / / proxy response client assert serverData! = null; proxyOut.write (serverData); proxySocket.shutdownOutput (); System.out.println ("Agent response client data:" + new String (proxyDataBytes)) } private byte [] dispatcherService (byte [] proxyDataBytes) {/ / Select node: send request and receive response information Robin wrr = new WeightedRoundRobin (); Node node = wrr.selectNode (); byte [] serverData = null; try {this.socket = new Socket (node.getIp (), node.getPort ()); socket.setSoTimeout (6000) ServerIn = socket.getInputStream (); serverOut= socket.getOutputStream (); serverOut.write (proxyDataBytes); socket.shutdownOutput (); serverData = getData (serverIn); System.out.println ("Real Server response data: + new String (serverData)); node.onInvokeSuccess () / / raise weight} catch (IOException e) {node.onInvokeFault (); / / reduce weight serverData = "proxy downstream server exception" .getBytes ();} System.out.println ("load balancing to:" + node); return serverData;} private byte [] getData (InputStream in) throws IOException {List byteList = new ArrayList (); int temp =-1 While (true) {temp = in.read (); if (temp! =-1) byteList.add ((byte) temp); else break;} byte [] bytes = new byte [byteList.size ()]; for
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.