com.netflix.karyon.server
Class KaryonServer
java.lang.Object
com.netflix.karyon.server.KaryonServer
- All Implemented Interfaces:
- java.io.Closeable
public class KaryonServer
- extends java.lang.Object
- implements java.io.Closeable
The core server for Karyon. This server provides all core functionality to startup a karyon server but is not useful
as is, since, it does not essentially open an endpoint for clients to connect.
In order to use karyon effectively, one should use the provided extensions in karyon-extensions
If one intends to provide a custom extension the following needs to be done:
- Call
initialize()
at startup to bootstrap Governator.
The bootstrapping is done via ServerBootstrap
. One can customize various aspects of governator bootstrapping
by providing an extension to ServerBootstrap
and overriding various methods therein. In such a case, fully
qualified, custom bootstrap class name, must be specified as a property
PropertyNames.SERVER_BOOTSTRAP_CLASS_OVERRIDE
available to archaius. Such a class must
also have a no-arg public constructor.
- Call
start()
to initialize the karyon environment and hence
initializing all classes annotated with Application
and Component
- Call
close()
at shutdown or to stop the server. This will cause
destruction of all governator initialized classes.
Eureka Integration
Karyon provides integration with eureka as a core component and does the necessary registration/update with eureka.
In order for the integration to work, the application writer, must provide the necessary configuration for eureka
client as specified in the "Configuring Eureka Client" section here
The easiest way of doing so is specifying a property file with name "eureka-client.properties" in the classpath.
This file must have the properties required by eureka with a prefix "eureka", eg: if you need to specify the VIP
address for eureka you must specify the property "eureka.vipAddress". If you need to use a different prefix, the
default prefix can be overridden by specifying the same in the property PropertyNames.EUREKA_PROPERTIES_NAME_PREFIX_PROP_NAME
as a property accessible to archaius
The integration with eureka can be disabled by specifying a property
PropertyNames.DISABLE_EUREKA_INTEGRATION
set to true
and accessible to
archaius
Archaius Integration
As such Archaius requires minimal integration and works out of the
box however, karyon does one additional step of loading the configuration from properties file located in the
classpath. The properties loading is done by calling ConfigurationManager.loadCascadedPropertiesFromResources(String)
with the name of the config as returned by DeploymentContext.getApplicationId()
from the
deployment context configured in archaius.
For the above to work correctly, one must provide a property with name "archaius.deployment.applicationId" to
archaius before karyon is initialized i.e. before calling initialize()
.
The value of the property must be the name of the application. By doing the above, archaius loads all properties
defined in properties file(s) having the names:
[application_name].properties
[application_name]-[environement].properties
The environment above is as retrieved from DeploymentContext.getApplicationId()
from the
deployment context configured in archaius. This can be set by a property "archaius.deployment.environment"
NOTE: The above property names are valid if the default deployment context is used for archaius.
If any customization is required in archaius, one should do the same before calling
initialize()
.
Example
One can create a very simple server using karyon as:
public class MyServer {
public static void main(String[] args) throws Exception {
final KaryonServer server = new KaryonServer();
server.initialize();
server.start();
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
public void run() {
try {
server.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}));
ServerSocket serverSocket = new ServerSocket(80);
while (true) {
final Socket s = serverSocket.accept();
// Process request
}
}
}
Field Summary |
protected static org.slf4j.Logger |
logger
|
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
logger
protected static final org.slf4j.Logger logger
KaryonServer
public KaryonServer()
initialize
public com.google.inject.Injector initialize()
- Bootstraps karyon by using
ServerBootstrap
by default. This can be customized by extending
ServerBootstrap
.
This method must be called during the server initialization.
- Returns:
- Guice's injector instance that will be used by Governator
start
public void start()
throws java.lang.Exception
- Starts the karyon server by starting Governator
and calling
ServerInitializer.initialize(com.google.inject.Injector)
for the applications, components to be
started inside karyon.
This typically should be called at startup.
- Throws:
java.lang.Exception
- If startup fails for any reason.
close
public void close()
throws java.io.IOException
- Stops the karyon server by stopping Governator and
ServerInitializer.close()
.
This typically should be called at termination of the server.
- Specified by:
close
in interface java.io.Closeable
- Throws:
java.io.IOException