Search in sources :

Example 1 with Registry

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;
    }
}
Also used : Registry(com.networknt.registry.Registry) InetAddress(java.net.InetAddress) OptionMap(org.xnio.OptionMap) Undertow(io.undertow.Undertow) BindException(java.net.BindException) KeyStoreException(java.security.KeyStoreException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) URLImpl(com.networknt.registry.URLImpl)

Example 2 with Registry

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());
}
Also used : Registry(com.networknt.registry.Registry) URL(com.networknt.registry.URL) Test(org.junit.Test)

Aggregations

Registry (com.networknt.registry.Registry)2 URL (com.networknt.registry.URL)1 URLImpl (com.networknt.registry.URLImpl)1 Undertow (io.undertow.Undertow)1 IOException (java.io.IOException)1 BindException (java.net.BindException)1 InetAddress (java.net.InetAddress)1 KeyStoreException (java.security.KeyStoreException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 UnrecoverableKeyException (java.security.UnrecoverableKeyException)1 Test (org.junit.Test)1 OptionMap (org.xnio.OptionMap)1