Search in sources :

Example 1 with URLImpl

use of com.networknt.registry.URLImpl in project light-4j by networknt.

the class ZooKeeperRegistryTest method setUp.

@BeforeClass
public static void setUp() throws Exception {
    int port = 9000;
    clientUrl = new URLImpl(Constants.PROTOCOL_LIGHT, "127.0.0.1", 0, service);
    clientUrl.addParameter("group", "aaa");
    serviceUrl = new URLImpl(Constants.PROTOCOL_LIGHT, "127.0.0.1", 8001, service);
    serviceUrl.addParameter("group", "aaa");
    InstanceSpec spec = new InstanceSpec(null, port, -1, -1, true, 1, -1, -1, new HashMap<>());
    zookeeper = new TestingServer(spec, true);
    client = (ZooKeeperClient) SingletonServiceFactory.getBean(ZooKeeperClient.class);
    registry = (ZooKeeperRegistry) SingletonServiceFactory.getBean(Registry.class);
    System.out.println("client = " + client + " registry = " + registry);
}
Also used : TestingServer(org.apache.curator.test.TestingServer) InstanceSpec(org.apache.curator.test.InstanceSpec) URLImpl(com.networknt.registry.URLImpl)

Example 2 with URLImpl

use of com.networknt.registry.URLImpl in project light-4j by networknt.

the class LocalFirstLoadBalanceTest method testSelect.

@Test
public void testSelect() throws Exception {
    List<URL> urls = new ArrayList<>();
    urls.add(new URLImpl("http", "127.0.0.10", 8081, "v1", new HashMap<String, String>()));
    urls.add(new URLImpl("http", "127.0.0.1", 8081, "v1", new HashMap<String, String>()));
    urls.add(new URLImpl("http", "127.0.0.11", 8082, "v1", new HashMap<String, String>()));
    urls.add(new URLImpl("http", "127.0.0.12", 8083, "v1", new HashMap<String, String>()));
    urls.add(new URLImpl("http", "127.0.0.115", 8084, "v1", new HashMap<String, String>()));
    URL url = loadBalance.select(urls, null);
    Assert.assertEquals(url, URLImpl.valueOf("http://127.0.0.1:8081/v1"));
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) URL(com.networknt.registry.URL) URLImpl(com.networknt.registry.URLImpl) Test(org.junit.Test)

Example 3 with URLImpl

use of com.networknt.registry.URLImpl in project light-4j by networknt.

the class LocalFirstLoadBalanceTest method testSelectFirstThenRoundRobin.

@Test
public void testSelectFirstThenRoundRobin() throws Exception {
    List<URL> urls = new ArrayList<>();
    urls.add(new URLImpl("http", "127.0.0.10", 8081, "v1", new HashMap<String, String>()));
    urls.add(new URLImpl("http", "127.0.0.10", 8082, "v1", new HashMap<String, String>()));
    urls.add(new URLImpl("http", "127.0.0.10", 8083, "v1", new HashMap<String, String>()));
    urls.add(new URLImpl("http", "127.0.0.10", 8084, "v1", new HashMap<String, String>()));
    // no local host URL available, go round-robin
    URL url = loadBalance.select(urls, null);
    Assert.assertEquals(url, URLImpl.valueOf("http://127.0.0.10:8082/v1"));
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) URL(com.networknt.registry.URL) URLImpl(com.networknt.registry.URLImpl) Test(org.junit.Test)

Example 4 with URLImpl

use of com.networknt.registry.URLImpl in project light-4j by networknt.

the class ConsulTest method getMockUrl.

public static URL getMockUrl(String protocol, String address, int port, String serviceName) {
    Map<String, String> params = new HashMap<>();
    params.put("environment", "test1");
    URL url = new URLImpl(protocol, address, port, serviceName, params);
    return url;
}
Also used : HashMap(java.util.HashMap) URL(com.networknt.registry.URL) URLImpl(com.networknt.registry.URLImpl)

Example 5 with URLImpl

use of com.networknt.registry.URLImpl 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)

Aggregations

URLImpl (com.networknt.registry.URLImpl)10 URL (com.networknt.registry.URL)7 HashMap (java.util.HashMap)6 ArrayList (java.util.ArrayList)4 Test (org.junit.Test)3 Registry (com.networknt.registry.Registry)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 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 InstanceSpec (org.apache.curator.test.InstanceSpec)1 TestingServer (org.apache.curator.test.TestingServer)1 Before (org.junit.Before)1 OptionMap (org.xnio.OptionMap)1