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);
}
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"));
}
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"));
}
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;
}
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;
}
}
Aggregations