Java Socket分發服務負載均衡


  1 1、 設備請求分發服務器,分發服務器返回有效的socket服務器ip與port,然后斷開連接。
  2 a) 設備與服務器建立連接。
  3 b) 服務器接收到連接請求后,立即將分配好的socket服務器ip與port信息響應給設備。
  4 c) 服務器主動斷開socket連接。
  5 2、 設備得到ip與port以后,設備去連接socket服務器,然后與其進行協議通訊。
  6 a) 設備連接到socket服務器。
  7 b) socket服務器響應連接成功響應信息。
  8 c) 設備與socket服務器保持長鏈接通訊。
  9 
 10 *. 若設備未收到連接成功響應則再次嘗試連接,若三次請求依舊沒有成功建立連接,那么設備需要去請求分發服務器然后再重新上述操作。
 11 *. 當設備在異常情況下鏈接不上socket服務器時,依舊嘗試三次若不能成功,則直接請求分發服務器,然后再重復上述操作。
 12 
 13 分發服務器代碼:
 14 分發服務與客戶端是短鏈接:
 15 package com.easygo.server;
 16 
 17 import java.io.IOException;
 18 import java.net.ServerSocket;
 19 import java.net.Socket;
 20 import java.util.concurrent.ExecutorService;
 21 import java.util.concurrent.Executors;
 22 
 23 public class CenterServer implements Runnable{
 24     ServerSocket serverSocket=null;
 25     static volatile String server_ip="127.0.0.1";
 26     static volatile int serverport1=8085;
 27     static volatile int serverport2=8086;
 28     public static void main(String[] args) {
 29         new Thread(new CenterServer()).start();
 30         
 31     }
 32 
 33     @Override
 34     public void run() {
 35          ExecutorService cachedThreadPool = null;//線程池
 36             cachedThreadPool = Executors.newCachedThreadPool();//線程池
 37             try {
 38                 serverSocket=new ServerSocket(9999);
 39             } catch (IOException e1) {
 40                 // TODO Auto-generated catch block
 41                 e1.printStackTrace();
 42             }
 43         while(true){
 44             try {
 45                 
 46                 cachedThreadPool.execute(new ServerRun(serverSocket.accept()));
 47                 System.out.println("有一個客戶端連進來了..");
 48             } catch (IOException e) {
 49                 // TODO Auto-generated catch block
 50                 e.printStackTrace();
 51             }
 52             
 53         }
 54     }
 55 
 56     
 57     
 58 }
 59 class ServerRun implements Runnable{
 60     Socket socket=null;
 61     ServerRun(Socket socket){
 62         this.socket=socket;
 63         
 64     }
 65     @Override
 66     public void run() {
 67         
 68         receive();
 69         
 70         
 71     }
 72     public void send(String message){
 73         try {
 74             socket.getOutputStream().write(message.getBytes());
 75             socket.getOutputStream().flush();
 76         } catch (IOException e) {
 77             // TODO Auto-generated catch block
 78             e.printStackTrace();
 79         }
 80         
 81         
 82     }
 83     public void receive(){
 84         
 85         byte[]tmp=null;
 86         String message=null;
 87         
 88         boolean runs=true;
 89         while(runs){
 90             if(null!=socket){
 91                 tmp=new byte[30];
 92                 
 93                 @SuppressWarnings("unused")
 94                 int count =0;
 95                 try {
 96                     while((count=socket.getInputStream().read(tmp))!=-1){
 97                         message=new String(tmp);
 98                         System.out.println("Clent:"+message.trim());
 99                         if(message.trim().split(",")[0].equals("login")&&message.trim().split(",")[1].equals("1")){
100                             
101                             send("login,"+CenterServer.server_ip+","+CenterServer.serverport1);
102                         }else if(message.trim().split(",")[0].equals("login")&&message.trim().split(",")[1].equals("2")){
103                             
104                             send("login,"+CenterServer.server_ip+","+CenterServer.serverport2);
105                         }else if(message.equals("0")){
106                             if(socket!=null){
107                                 socket.close();
108                                 
109                             }
110                             
111                             
112                         }
113                         if(socket.isClosed()){
114                             runs=false;
115                         }
116                         tmp=null;
117                         message=null;
118                         tmp=new byte[30];
119                     }
120                     
121                     
122                 } catch (IOException e) {
123                     
124                 }
125                 
126             }
127             
128             
129         }
130     }
131     
132 }
133 
134 
135 
136 客戶端代碼案例:
137 
138 package com.easygo.server;
139 
140 
141 import java.io.IOException;
142 import java.net.Socket;
143 
144 
145 public class Client implements Runnable{
146     Socket socket=null;
147     final int CENTER_SERVER_PORT=9999;//分發服務器端口
148     final String CENTER_SERVER_IP="127.0.0.1";
149      int CENTER_SERVER_PORTS;
150      String CENTER_SERVER_IPS;
151     boolean conn=false;
152     volatile boolean receive;
153 public static void main(String[] args) {
154     new Thread(new Client()).start();
155 }
156 
157 @Override
158 public void run() {
159     try {
160         connection();
161     } catch (Exception e) {
162         // TODO Auto-generated catch block
163         e.printStackTrace();
164     }
165     
166     
167 }
168 
169 public void connection() throws Exception{
170     
171     while(true){
172         if(null==socket&&conn==false){//登入分發服務器
173             socket=new Socket(CENTER_SERVER_IP, CENTER_SERVER_PORT);
174             receive=true;
175             
176             send("login,1");//login,登入分發服務器,1指定登入服務
177                     
178             
179             new Thread(){
180                 public void run(){
181                     try {
182                         receiveX();
183                     } catch (IOException e) {
184                         // TODO Auto-generated catch block
185                         e.printStackTrace();
186                     }
187                     
188                 }
189                 
190             }.start();
191         }else if(null==socket&&conn==true){//登入指定服務
192             socket=new Socket(CENTER_SERVER_IPS, CENTER_SERVER_PORTS);
193             receive=true;
194             new Thread(){
195                 @Override
196                 public void run(){
197                     while(true){
198                         send("message,我是客戶端");
199                         
200                         try {
201                             Thread.sleep(1000);
202                         } catch (InterruptedException e) {
203                             // TODO Auto-generated catch block
204                             e.printStackTrace();
205                         }
206                     }
207                     
208                     
209                 }
210                 
211             }.start();
212             new Thread(){
213                 public void run(){
214                     try {
215                         receiveX();
216                     } catch (IOException e) {
217                         // TODO Auto-generated catch block
218                         e.printStackTrace();
219                     }
220                     
221                 }
222                 
223             }.start();
224         }
225         
226         Thread.sleep(1000);
227         
228     }
229 }
230 
231 public void send(String message){
232     System.out.println("send:"+message);
233     try {
234         socket.getOutputStream().write(message.getBytes());
235         socket.getOutputStream().flush();
236     } catch (IOException e) {
237     
238     }
239     
240 }
241 
242 public void receiveX() throws IOException{
243     byte[]tmp=null;
244     String messaage=null;
245     while(receive){
246         if(null!=socket){
247             tmp=new byte[50];
248             @SuppressWarnings("unused")
249             int count=0;
250             while((count=socket.getInputStream().read(tmp))!=-1){
251                 messaage=new String(tmp);
252                 System.out.println("result:"+messaage);
253                 if(messaage.trim().contains("login")){
254                     String[]arr=messaage.trim().split(",");
255                     System.out.println(":"+arr[1]+":"+arr[2]);
256                     CENTER_SERVER_IPS=arr[1];
257                     CENTER_SERVER_PORTS=Integer.parseInt(arr[2]);
258                     
259                     
260                     conn=true;
261                     //send("0");
262                     closeXX();//獲取到對應服務ip和端口后斷開連接
263                     receive=false;//
264                     
265                     
266                 }else if(messaage.trim().contains("message")){
267                     send(messaage);
268                     
269                 }
270                 tmp=null;
271                 tmp=new byte[50];
272             }
273             
274             
275             
276             
277         }
278         tmp=null;
279         messaage=null;
280     }
281     
282 }
283 public void closeXX(){
284     if(this.socket!=null){
285         try {
286             socket.close();
287             socket=null;
288         } catch (IOException e) {
289             // TODO Auto-generated catch block
290             e.printStackTrace();
291         }
292         
293     }
294 }
295 }
296 
297 
298 指定登入處理客戶端服務代碼
299 package com.easygo.server;
300 
301 import java.io.IOException;
302 import java.net.ServerSocket;
303 import java.net.Socket;
304 import java.util.concurrent.ExecutorService;
305 import java.util.concurrent.Executors;
306 
307 public class Server implements Runnable{
308     volatile int port=8086;
309     ServerSocket serverSocket=null;
310     public static void main(String[] args) {
311         new Thread(new Server()).start();
312     }
313 
314     @Override
315     public void run() {
316         int count=0;
317         try {
318             serverSocket=new ServerSocket(port);
319             ExecutorService cachedThreadPool = null;//線程池
320             cachedThreadPool = Executors.newCachedThreadPool();//線程池
321             
322             while(true){
323                 cachedThreadPool.execute(new ServerRunS(serverSocket.accept()));
324                 System.out.println("a client conn:"+count++);
325                 
326             }
327         } catch (IOException e) {
328             // TODO Auto-generated catch block
329             e.printStackTrace();
330         }
331         
332     }
333 
334 }
335 class ServerRunS implements Runnable{
336     Socket socket=null;
337     ServerRunS(Socket socket){
338         
339         this.socket=socket;
340     }
341     @Override
342     public void run() {
343         byte[]tmp=null;
344         
345         while(true){
346             
347             if(socket!=null){
348                 tmp=new byte[30];
349                 @SuppressWarnings("unused")
350                 int count=0;
351                 try {
352                     while((count=socket.getInputStream().read(tmp))!=-1){
353                     System.out.println("客戶端發來的消息:"+new String(tmp));
354                         
355                     }
356                 } catch (IOException e) {
357                     // TODO Auto-generated catch block
358                     e.printStackTrace();
359                 }
360                 
361             }
362             tmp=null;
363         }
364     }
365     
366     
367     
368 }
369 
370 流程:
371 1啟動server
372 2啟動CenterServer服務
373 3啟動客戶端一,參數(login,1374 4啟動客戶端二,參數(login,2375 
376 運行結果:
377 客戶端1
378 send:login,1
379 result:login,127.0.0.1,8085
380 send:message,我是客戶端
381 send:message,我是客戶端
382 send:message,我是客戶端
383 send:message,我是客戶端
384 send:message,我是客戶端
385 send:message,我是客戶端
386 
387 客戶端2
388 send:login,2
389 result:login,127.0.0.1,8086
390 send:message,我是客戶端
391 send:message,我是客戶端
392 send:message,我是客戶端
393 send:message,我是客戶端
394 send:message,我是客戶端
395 send:message,我是客戶端
396 
397 分發服務器:
398 有一個客戶端連進來了
399 UUlogin,2
400 有一個客戶端連進來了
401 UUlogin,1
402 
403 服務器1(8085端口)
404 8085
405 客戶端發來的消息:message,我是客戶端
406 客戶端發來的消息:message,我是客戶端
407 .
408 .
409 .
410 服務器1(8086端口)
411 8086
412 客戶端發來的消息:message,我是客戶端
413 客戶端發來的消息:message,我是客戶端
414 .
415 .
416 .

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM