use of com.networknt.registry.Registry in project light-4j by networknt.
the class Server method bind.
private static boolean bind(HttpHandler handler, int port) {
try {
Undertow.Builder builder = Undertow.builder();
if (config.enableHttps) {
port = port < 0 ? config.httpsPort : port;
sslContext = createSSLContext();
builder.addHttpsListener(port, config.getIp(), sslContext);
} else if (config.enableHttp) {
port = port < 0 ? config.httpPort : port;
builder.addHttpListener(port, config.getIp());
} else {
throw new RuntimeException("Unable to start the server as both http and https are disabled in server.yml");
}
if (config.enableHttp2) {
builder.setServerOption(UndertowOptions.ENABLE_HTTP2, true);
}
server = builder.setBufferSize(1024 * 16).setIoThreads(// this seems slightly faster in some configurations
Runtime.getRuntime().availableProcessors() * 2).setSocketOption(Options.BACKLOG, 10000).setServerOption(UndertowOptions.ALWAYS_SET_KEEP_ALIVE, // don't send a keep-alive header for HTTP/1.1 requests, as it is not required
false).setServerOption(UndertowOptions.ALWAYS_SET_DATE, true).setServerOption(UndertowOptions.RECORD_REQUEST_START_TIME, false).setHandler(Handlers.header(handler, Headers.SERVER_STRING, "L")).setWorkerThreads(200).build();
server.start();
System.out.println("HOST IP " + System.getenv(STATUS_HOST_IP));
// application level service registry. only be used without docker container.
if (config.enableRegistry) {
// assuming that registry is defined in service.json, otherwise won't start server.
registry = SingletonServiceFactory.getBean(Registry.class);
if (registry == null)
throw new RuntimeException("Could not find registry instance in service map");
// in kubernetes pod, the hostIP is passed in as STATUS_HOST_IP environment variable. If this is null
// then get the current server IP as it is not running in Kubernetes.
String ipAddress = System.getenv(STATUS_HOST_IP);
if (ipAddress == null) {
InetAddress inetAddress = Util.getInetAddress();
ipAddress = inetAddress.getHostAddress();
}
Map parameters = new HashMap<>();
if (config.getEnvironment() != null)
parameters.put("environment", config.getEnvironment());
serviceUrl = new URLImpl("light", ipAddress, port, config.getServiceId(), parameters);
registry.register(serviceUrl);
if (logger.isInfoEnabled())
logger.info("register service: " + serviceUrl.toFullStr());
// start heart beat if registry is enabled
SwitcherUtil.setSwitcherValue(Constants.REGISTRY_HEARTBEAT_SWITCHER, true);
if (logger.isInfoEnabled())
logger.info("Registry heart beat switcher is on");
}
if (config.enableHttp) {
System.out.println("Http Server started on ip:" + config.getIp() + " Port:" + port);
if (logger.isInfoEnabled())
logger.info("Http Server started on ip:" + config.getIp() + " Port:" + port);
} else {
System.out.println("Http port disabled.");
if (logger.isInfoEnabled())
logger.info("Http port disabled.");
}
if (config.enableHttps) {
System.out.println("Https Server started on ip:" + config.getIp() + " Port:" + port);
if (logger.isInfoEnabled())
logger.info("Https Server started on ip:" + config.getIp() + " Port:" + port);
} else {
System.out.println("Https port disabled.");
if (logger.isInfoEnabled())
logger.info("Https port disabled.");
}
return true;
} catch (Exception e) {
System.out.println("Failed to bind to port " + port);
if (logger.isInfoEnabled())
logger.info("Failed to bind to port " + port);
return false;
}
}
use of com.networknt.registry.Registry in project light-4j by networknt.
the class DirectRegistryTest method testDirectRegistry.
@Test
public void testDirectRegistry() {
Registry registry = (Registry) SingletonServiceFactory.getBean(Registry.class);
URL subscribeUrl = URLImpl.valueOf("light://localhost:8080/token");
List<URL> urls = registry.discover(subscribeUrl);
Assert.assertEquals(1, urls.size());
subscribeUrl = URLImpl.valueOf("light://localhost:8080/code");
urls = registry.discover(subscribeUrl);
Assert.assertEquals(2, urls.size());
}
Aggregations