avatar Logo,nginx 负载均衡算法及原理
protected String configFile = "conf/server.xml";
protected ClassLoader parentClassLoader = Catalina.class.getClassLoader();
protected Server server = null;
protected boolean useShutdownHook = true;
protected Thread shutdownHook = null;
protected boolean useNaming = true;
private static final Log log = LogFactory.getLog(Catalina.class);
public Catalina()
{
setSecurityProtection();
}
public void setConfigFile(String file)
{
this.configFile = file;
}
public String getConfigFile()
{
return this.configFile;
}
public void setUseShutdownHook(boolean useShutdownHook)
{
this.useShutdownHook = useShutdownHook;
}
public boolean getUseShutdownHook()
{
return this.useShutdownHook;
}
public void setParentClassLoader(ClassLoader parentClassLoader)
{
this.parentClassLoader = parentClassLoader;
}
public ClassLoader getParentClassLoader() {
if (this.parentClassLoader != null) {
return this.parentClassLoader;
}
return ClassLoader.getSystemClassLoader();
}
public void setServer(Server server) {
this.server = server;
}
public Server getServer()
{
return this.server;
}
public boolean isUseNaming()
{
return this.useNaming;
}
public void setUseNaming(boolean useNaming)
{
this.useNaming = useNaming;
}
public void setAwait(boolean b) {
this.await = b;
}
public boolean isAwait() {
return this.await;
}
protected boolean arguments(String[] args)
{
boolean isConfig = false;
if (args.length < 1) {
usage();
return false;
}
for (int i = 0; i < args.length; i++)
if (isConfig) {
this.configFile = args[i];
isConfig = false;
} else if (args[i].equals("-config")) {
isConfig = true;
} else if (args[i].equals("-nonaming")) {
setUseNaming(false); } else {
if (args[i].equals("-help")) {
usage();
return false;
}if (!args[i].equals("start"))
{
if (!args[i].equals("configtest"))
{
if (!args[i].equals("stop"))
{
usage();
return false;
}
}
}
}
return true;
}
protected File configFile()
{
File file = new File(this.configFile);
if (!file.isAbsolute()) {
file = new File(Bootstrap.getCatalinaBase(), this.configFile);
}
return file;
}
protected Digester createStartDigester()
{
long t1 = System.currentTimeMillis();
Digester digester = new Digester();
digester.setValidating(false);
digester.setRulesValidation(true);
HashMap fakeAttributes = new HashMap();
ArrayList attrs = new ArrayList();
attrs.add("className");
fakeAttributes.put(Object.class, attrs);
digester.setFakeAttributes(fakeAttributes);
digester.setUseContextClassLoader(true);
digester.addObjectCreate("Server", "org.apache.catalina.core.StandardServer", "className");
digester.addSetProperties("Server");
digester.addSetNext("Server", "setServer", "org.apache.catalina.Server");
digester.addObjectCreate("Server/GlobalNamingResources", "org.apache.catalina.deploy.NamingResourcesImpl");
digester.addSetProperties("Server/GlobalNamingResources");
digester.addSetNext("Server/GlobalNamingResources", "setGlobalNamingResources", "org.apache.catalina.deploy.NamingResourcesImpl");
digester.addObjectCreate("Server/Listener", null, "className");
digester.addSetProperties("Server/Listener");
digester.addSetNext("Server/Listener", "addLifecycleListener", "org.apache.catalina.LifecycleListener");
digester.addObjectCreate("Server/Service", "org.apache.catalina.core.StandardService", "className");
digester.addSetProperties("Server/Service");
digester.addSetNext("Server/Service", "addService", "org.apache.catalina.Service");
digester.addObjectCreate("Server/Service/Listener", null, "className");
digester.addSetProperties("Server/Service/Listener");
digester.addSetNext("Server/Service/Listener", "addLifecycleListener", "org.apache.catalina.LifecycleListener");
digester.addObjectCreate("Server/Service/Executor", "org.apache.catalina.core.StandardThreadExecutor", "className");
digester.addSetProperties("Server/Service/Executor");
digester.addSetNext("Server/Service/Executor", "addExecutor", "org.apache.catalina.Executor");
digester.addRule("Server/Service/Connector", new ConnectorCreateRule());
digester.addRule("Server/Service/Connector", new SetAllPropertiesRule(new String[] { "executor" }));
digester.addSetNext("Server/Service/Connector", "addConnector", "org.apache.catalina.connector.Connector");
digester.addObjectCreate("Server/Service/Connector/Listener", null, "className");
digester.addSetProperties("Server/Service/Connector/Listener");
digester.addSetNext("Server/Service/Connector/Listener", "addLifecycleListener", "org.apache.catalina.LifecycleListener");
digester.addRuleSet(new NamingRuleSet("Server/GlobalNamingResources/"));
digester.addRuleSet(new EngineRuleSet("Server/Service/"));
digester.addRuleSet(new HostRuleSet("Server/Service/Engine/"));
digester.addRuleSet(new ContextRuleSet("Server/Service/Engine/Host/"));
addClusterRuleSet(digester, "Server/Service/Engine/Host/Cluster/");
digester.addRuleSet(new NamingRuleSet("Server/Service/Engine/Host/Context/"));
digester.addRule("Server/Service/Engine", new SetParentClassLoaderRule(this.parentClassLoader));
addClusterRuleSet(digester, "Server/Service/Engine/Cluster/");
long t2 = System.currentTimeMillis();
if (log.isDebugEnabled()) {
log.debug("Digester for server.xml created " + (t2 - t1));
}
return digester;
}
private void addClusterRuleSet(Digester digester, String prefix)
{
Class clazz = null;
Constructor constructor = null;
try {
clazz = Class.forName("org.apache.catalina.ha.ClusterRuleSet");
constructor = clazz.getConstructor(new Class[] { String.class });
RuleSet ruleSet = (RuleSet)constructor.newInstance(new Object[] { prefix });
digester.addRuleSet(ruleSet);
} catch (Exception e) {
if (log.isDebugEnabled()) {
log.debug(sm.getString("catalina.noCluster", new Object[] { e.getClass().getName() + ": " + e.getMessage() }), e);
}
else if (log.isInfoEnabled())
log.info(sm.getString("catalina.noCluster", new Object[] { e.getClass().getName() + ": " + e.getMessage() }));
}
}
protected Digester createStopDigester()
{
Digester digester = new Digester();
digester.setUseContextClassLoader(true);
digester.addObjectCreate("Server", "org.apache.catalina.core.StandardServer", "className");
digester.addSetProperties("Server");
digester.addSetNext("Server", "setServer", "org.apache.catalina.Server");
return digester;
}
public void stopServer()
{
stopServer(null);
}
public void stopServer(String[] arguments)
{
if (arguments != null) {
arguments(arguments);
}
Server s = getServer();
File file;
Throwable localThrowable4;
if (s == null)
{
Digester digester = createStopDigester();
file = configFile();
try { FileInputStream fis = new FileInputStream(file); localThrowable4 = null;
try { InputSource is = new InputSource(file.toURI().toURL().toString());
is.setByteStream(fis);
digester.push(this);
digester.parse(is);
}
catch (Throwable localThrowable1)
{
localThrowable4 = localThrowable1; throw localThrowable1;
}
finally
{
if (fis != null) if (localThrowable4 != null) try { fis.close(); } catch (Throwable x2) { localThrowable4.addSuppressed(x2); } else fis.close();
} } catch (Exception e) { log.error("Catalina.stop: ", e);
System.exit(1); }
}
else
{
try {
s.stop();
} catch (LifecycleException e) {
log.error("Catalina.stop: ", e);
}
return;
}
s = getServer();
if (s.getPort() > 0) {
try { Socket socket = new Socket(s.getAddress(), s.getPort()); file = null;
try { OutputStream stream = socket.getOutputStream();
localThrowable4 = null;
try {
String shutdown = s.getShutdown();
for (int i = 0; i < shutdown.length(); i++) {
stream.write(shutdown.charAt(i));
}
stream.flush();
}
catch (Throwable localThrowable2)
{
localThrowable4 = localThrowable2; throw localThrowable2; } finally { } } catch (Throwable localThrowable3) { file = localThrowable3; throw localThrowable3;
}
finally
{
if (socket != null) if (file != null) try { socket.close(); } catch (Throwable x2) { file.addSuppressed(x2); } else socket.close();
} } catch (ConnectException ce) { log.error(sm.getString("catalina.stopServer.connectException", new Object[] { s.getAddress(), String.valueOf(s.getPort()) }));
log.error("Catalina.stop: ", ce);
System.exit(1);
} catch (IOException e) {
log.error("Catalina.stop: ", e);
System.exit(1);
}
} else {
log.error(sm.getString("catalina.stopServer"));
System.exit(1);
}
}
public void load()
{
long t1 = System.nanoTime();
initDirs();
initNaming();
Digester digester = createStartDigester();
InputSource inputSource = null;
InputStream inputStream = null;
File file = null;
try {
file = configFile();
inputStream = new FileInputStream(file);
inputSource = new InputSource(file.toURI().toURL().toString());
} catch (Exception e) {
if (log.isDebugEnabled()) {
log.debug(sm.getString("catalina.configFail", new Object[] { file }), e);
}
}
if (inputStream == null) {
try {
inputStream = getClass().getClassLoader().getResourceAsStream(getConfigFile());
inputSource = new InputSource(getClass().getClassLoader().getResource(getConfigFile()).toString());
}
catch (Exception e)
{
if (log.isDebugEnabled()) {
log.debug(sm.getString("catalina.configFail", new Object[] { getConfigFile() }), e);
}
}
}
if (inputStream == null) {
try {
inputStream = getClass().getClassLoader().getResourceAsStream("server-embed.xml");
inputSource = new InputSource(getClass().getClassLoader().getResource("server-embed.xml").toString());
}
catch (Exception e)
{
if (log.isDebugEnabled()) {
log.debug(sm.getString("catalina.configFail", new Object[] { "server-embed.xml" }), e);
}
}
}
if ((inputStream == null) || (inputSource == null)) {
if (file == null) {
log.warn(sm.getString("catalina.configFail", new Object[] { getConfigFile() + "] or [server-embed.xml]" }));
}
else {
log.warn(sm.getString("catalina.configFail", new Object[] { file.getAbsolutePath() }));
if ((file.exists()) && (!file.canRead())) {
log.warn("Permissions incorrect, read permission is not allowed on the file.");
}
}
return;
}
try
{
inputSource.setByteStream(inputStream);
digester.push(this);
digester.parse(inputSource);
} catch (SAXParseException spe) { log.warn("Catalina.start using " + getConfigFile() + ": " + spe.getMessage());
return;
} catch (Exception e) { log.warn("Catalina.start using " + getConfigFile() + ": ", e);
return;
}
finally {
try {
inputStream.close();
}
catch (IOException local
IOException3)
{
}
}
getServer().setCatalina(this);
getServer().setCatalinaHome(Bootstrap.getCatalinaHomeFile());
getServer().setCatalinaBase(Bootstrap.getCatalinaBaseFile());
initStreams();
try
{
getServer().init();
评论