Try to connect from the list of multiple configuration servers
In fact, it is relatively simple. There are mainly two important variables, one is the Server Index and the other is the number of attempts to connect. Of course, these variables can be set by yourself.
The following is a column program I wrote. It was involved in my previous project. I simplified it and discussed it with you.
Public Bool Conn ( Ref Mcservices. Services MS)
{
// Conncount connection attempts
// Server Index configured by serverindex
Try
{
System. Threading. thread. Sleep ( 3000 ); // Blocked for 3 seconds
MS = New Mcservices. Services ();
Ms. url = Configurationsettings. deleettings [ " Server " + Serverindex];
If (Ms. checkservices ())
{
Conncount = 0 ;
Serverindex = 1 ;
Console. writeline (Ms. url + " Connection successful! " );
Return True ;
}
}
Catch (Exception E) // If the connection fails, an exception is thrown.
{
If (Conncount > = 3 )
{
Conncount = 0 ;
If (Serverindex > 3 )
{< br> console. writeline ( " connection attempts on all servers failed! " );
return false ;
}
Else
{Serverindex++;}
Return Conn ( Ref MS );
}
Else
{
Console. writeline (Ms. url + " Connection attempt failed " + (Conncount + 1 ) + " Time, wait 3 seconds to try to connect! " );
Conncount ++ ;
Return Conn ( Ref MS );
}
}
Return False ;
}
Of course, you can also add backend threads to check whether a thread is suspended, connected, or connected successfully. All of these functions can be used freely.