@Override public void handle(final NetSocket socket) { NetClient client = null; List<String> paths = serviceMap.getPaths(); TcpClientRequestFacade requestFacade = new TcpClientRequestFacade(socket); String path = pathLoadBalancer.choose(paths, requestFacade); if (path != null) { List<ServiceDetails> services = serviceMap.getServices(path); if (!services.isEmpty()) { ServiceDetails serviceDetails = serviceLoadBalancer.choose(services, requestFacade); if (serviceDetails != null) { List<String> urlStrings = serviceDetails.getServices(); for (String urlString : urlStrings) { if (Strings.notEmpty(urlString)) { // lets create a client for this request... try { URI uri = new URI(urlString); // URL url = new URL(urlString); String urlProtocol = uri.getScheme(); if (Objects.equal(protocol, urlProtocol)) { Handler<AsyncResult<NetSocket>> handler = new Handler<AsyncResult<NetSocket>>() { public void handle(final AsyncResult<NetSocket> asyncSocket) { NetSocket clientSocket = asyncSocket.result(); Pump.createPump(clientSocket, socket).start(); Pump.createPump(socket, clientSocket).start(); } }; client = createClient(socket, uri, handler); break; } } catch (MalformedURLException e) { LOG.warn("Failed to parse URL: " + urlString + ". " + e, e); } catch (URISyntaxException e) { LOG.warn("Failed to parse URI: " + urlString + ". " + e, e); } } } } } } if (client == null) { // fail to route LOG.info("No service available for protocol " + protocol + " for paths " + paths); socket.close(); } }
protected void treeCacheEvent(PathChildrenCacheEvent event) { ChildData childData = event.getData(); if (childData == null) { return; } String path = childData.getPath(); PathChildrenCacheEvent.Type type = event.getType(); byte[] data = childData.getData(); if (data == null || data.length == 0 || path == null) { return; } if (path.startsWith(zkPath)) { path = path.substring(zkPath.length()); } // Lets just use the group name as the service path. path = Strings.splitAndTrimAsList(path, "/").get(0); boolean remove = false; switch (type) { case CHILD_ADDED: case CHILD_UPDATED: break; case CHILD_REMOVED: remove = true; break; default: return; } ServiceDTO dto = null; try { dto = mapper.readValue(data, ServiceDTO.class); expandPropertyResolvers(dto); if (remove) { LOG.info("Removed gateway service: " + path + ": " + new String(data, "UTF-8")); serviceMap.serviceRemoved(path, dto); } else { LOG.info("Updated gateway service: " + path + ": " + new String(data, "UTF-8")); serviceMap.serviceUpdated(path, dto); } } catch (IOException e) { LOG.warn("Failed to parse the JSON: " + new String(data) + ". Reason: " + e, e); } catch (URISyntaxException e) { LOG.warn("Failed to update URI for dto: " + dto + ", .Reason: " + e, e); } }