public void clean() { cleanDir(new File(config.getOutputDir()), false); }
@PostConstruct @Scheduled(fixedDelayString = "${service.registry.delay:1000}") public synchronized void schedule() { try { if (app.getApplication() != null && apiUrl != null) { app.getApplication() .forEach(moduleEntry -> sendPing(moduleEntry)); } } catch (Exception e) { logger.error("Failed to register services due to exception {}", e.getMessage(), e); } }
public static Optional<RegisterEntry> toRegisterEntry(UriInfo uriInfo) { if (uriInfo.getQueryParameters().isEmpty()) { return Optional.empty(); } else { MultivaluedMap<String, String> parameters = uriInfo.getQueryParameters(); RegisterEntry re = RegisterEntry.builder() .context(parameters.getFirst("context")) .hostname(parameters.getFirst("hostname")) .port(toInt(parameters.getFirst("port"))) .target(parameters.getFirst("target")) .externalPort(toInt(parameters.getFirst("externalPort"))) .module(parameters.getFirst("module")) .health(toHealth(parameters.getFirst("health"))) .build(); Map<String, String> manifest = ReactiveSeq.fromIterable(parameters.entrySet()) .filter(e -> e.getKey().startsWith("manifest.")) .toMap(e -> e.getKey().replace("manifest.", ""), e -> parameters.getFirst(e.getKey())); re.getManifest().clear(); re.getManifest().putAll(manifest); return Optional.of(re); } }
@Before public void setUp() throws Exception { entry = RegisterEntry.builder() .port(8080) .hostname("host") .module("module") .context("context") .time(new Date()) .uuid("1") .target("target") .externalPort(9090) .build(); Map<String, String> manifest = entry.getManifest(); manifest.put("Implementation-revision", "a2edfe4bc"); manifest.put("Implementation-Version", "version"); manifest.put("Implementation-Timestamp", "2017_1201"); }
private void sendPing(RegisterEntry moduleEntry) { final RegisterEntry entry = moduleEntry.withTime(new Date()) .withUuid(uuid) .withHealth(checker.isOk() ? Health.OK : Health.ERROR) .withStats(nonEmptyOrNull(statsChecker.stats())); try { logger.debug("Posting {} to " + apiUrl + resourcePath, JacksonUtil.serializeToJson(entry)); rest.post(apiUrl + resourcePath, JacksonUtil.serializeToJson(entry)) .join(); } catch (Exception e) { logger .warn("Failed posting {} to {}" + resourcePath, JacksonUtil.serializeToJson(entry), apiUrl); } }
public void register(final RegisterEntry entry) { File dir = new File(config.getOutputDir(), "" + entry.getModule()); dir.mkdirs(); File file = new File(dir, entry.getHostname() + "-" + entry.getModule() + "-" + entry.getUuid()); try { final RegisterEntry entryToUse = "use-ip".equals(entry.getHostname()) ? entry.withHostname(QueryIPRetriever.getIpAddress()) : entry; FileUtils.writeStringToFile(file, JacksonUtil.serializeToJson(entryToUse)); } catch (IOException e) { logger.error("Error registering service to disk {}", JacksonUtil.serializeToJson(entry)); } } }
@Before public void setUp() throws Exception { try { new File( System.getProperty("java.io.tmpdir"), "lana-service-reg-cleaner").delete(); } catch (Exception e) { } new File( System.getProperty("java.io.tmpdir"), "lana-service-reg-cleaner").mkdirs(); registerConfig = new RegisterConfig( new File( System.getProperty("java.io.tmpdir"), "lana-service-reg-cleaner").getAbsolutePath()); writer = new Register(registerConfig); finder = new Finder(registerConfig); cleaner = new Cleaner(registerConfig, 1); entry = new RegisterEntry( 8080, "host", "module", "context", new Date(), null, 8080); }
public List<RegisterEntry> find(final Optional<RegisterEntry> re) { List<RegisterEntry> entries = findDir(new File(config.getOutputDir())); if (re.isPresent()) { entries = entries.stream().filter(e -> e.matches(re.get())) .collect(Collectors.toList()); } return entries; }
@Test public void testRegister() { ServerData data1 = new ServerData(8080, new ArrayList<>(), null, "url", () -> ""); ServerData data2 = new ServerData(8080, new ArrayList<>(), null, "url", () -> ""); ServerData data3 = new ServerData(8080, new ArrayList<>(), null, "url", () -> ""); List<ServerData> datas = Arrays.asList(data1, data2, data3); applicationRegister = new ApplicationRegisterImpl(); ServerData[] dataArray = new ServerData[datas.size()]; applicationRegister.register(datas.toArray(dataArray)); applicationRegister.getApplication().forEach(it -> count++); assertThat(count, is(3)); } }
@Test public void testFind() { writer.register(entry); List<RegisterEntry> list = finder.find(Optional.empty()); assertThat(list.size(), greaterThan(0)); assertThat(list.get(0).getContext(), equalTo("context")); } }
@Before public void setUp() throws Exception { try { FileUtils.deleteDirectory(new File( System.getProperty("java.io.tmpdir"), "service-reg-writer")); } catch (Exception e) { } new File(System.getProperty("java.io.tmpdir"), "service-reg-writer").mkdirs(); writer = new Register( new RegisterConfig( new File( System.getProperty("java.io.tmpdir"), "service-reg-writer").getAbsolutePath())); entry = new RegisterEntry( 8080, "host", "module", "context", new Date(), null, 8080); }
public boolean matches(RegisterEntry re) { //Only the fields which make sense to query is added for now. return (re.port == -1 || re.port == port) && (isNull(re.hostname) || nonNull(hostname) && hostname.startsWith(re.hostname)) && (isNull(re.module) || nonNull(module) && module.startsWith(re.module)) && (isNull(re.context) || nonNull(context) && context.startsWith(re.context)) && (isNull(re.health) || re.health.equals(health)) && (re.externalPort == -1 || re.externalPort == externalPort) && (isNull(re.manifest) || re.manifest.isEmpty() || matchManifest(re.manifest)); }
private void cleanDir(File dir, boolean deleteWithoutCheck) { if (dir.listFiles() != null) { Stream.of(dir.listFiles()).forEach((next) -> { if (next.isDirectory()) { cleanDir(next, deleteWithoutCheck); } if (next.isFile()) { if (deleteWithoutCheck) { next.delete(); } else { checkFile(next); } } }); } }
@Test public void testConstructor() { applicationRegister = new ApplicationRegisterImpl(); assertThat(applicationRegister.getApplication(), is(nullValue())); }
private void checkFile(File f) { try { RegisterEntry entry = JacksonUtil .convertFromJson(FileUtils.readFileToString(f), RegisterEntry.class); if (new Date().getTime() - maxLive > entry.getTime().getTime()) { f.delete(); } } catch (Exception e) { f.delete(); } } }
@Before public void setUp() throws Exception { try { new File( System.getProperty("java.io.tmpdir"), "service-reg-finder").delete(); } catch (Exception e) { } new File(System.getProperty("java.io.tmpdir"), "service-reg-finder").mkdirs(); registerConfig = new RegisterConfig( new File(System.getProperty("java.io.tmpdir"), "service-reg-finder").getAbsolutePath()); writer = new Register(registerConfig); finder = new Finder(registerConfig); entry = new RegisterEntry( 8080, "host", "module", "context", new Date(), null, 8080); }
@PostConstruct public void deleteOldFilesAfterEachStartup() { cleanDir(new File(config.getOutputDir()), true); }