diff --git a/micro-deps/micro-deps/src/main/java/com/ofg/infrastructure/discovery/ServiceConfigurationResolver.java b/micro-deps/micro-deps/src/main/java/com/ofg/infrastructure/discovery/ServiceConfigurationResolver.java index f3870e2c..034f66b4 100755 --- a/micro-deps/micro-deps/src/main/java/com/ofg/infrastructure/discovery/ServiceConfigurationResolver.java +++ b/micro-deps/micro-deps/src/main/java/com/ofg/infrastructure/discovery/ServiceConfigurationResolver.java @@ -1,25 +1,15 @@ package com.ofg.infrastructure.discovery; -import com.google.common.base.Function; import com.google.common.base.Predicate; -import com.google.common.collect.Collections2; -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.Lists; -import com.google.common.collect.Maps; import com.ofg.infrastructure.discovery.util.CollectionUtils; import com.ofg.infrastructure.discovery.util.LoadBalancerType; import net.sf.json.JSONObject; import net.sf.json.JSONSerializer; import org.apache.commons.lang.StringUtils; -import java.util.ArrayList; -import java.util.HashMap; import java.util.List; -import java.util.Map; import java.util.Set; -import static com.ofg.infrastructure.discovery.ServiceConfigurationProperties.PATH; - public class ServiceConfigurationResolver { private final String basePath; @@ -61,9 +51,9 @@ private String extractBasePath(JSONObject parsedJson) { private void validateConfiguration(JSONObject metaData) { serviceConfigurationValidator.checkThatServiceMetadataContainsValidElements(metaData); - jsonToMicroserviceConfigurationConverter.convertFlatDependenciesToMapFormat(metaData); + JsonToMicroserviceConfigurationConverter.convertFlatDependenciesToMapFormat(metaData); serviceConfigurationValidator.validateDependencyEntries(metaData); - jsonToMicroserviceConfigurationConverter.setDefaultsForMissingOptionalElements(metaData); + JsonToMicroserviceConfigurationConverter.setDefaultsForMissingOptionalElements(metaData); } private static JSONObject getDependenciesAsJsonObject(JSONObject serviceMetadata) { diff --git a/micro-infra-spring-base/src/main/groovy/com/ofg/infrastructure/correlationid/CorrelationIdHolder.groovy b/micro-infra-spring-base/src/main/groovy/com/ofg/infrastructure/correlationid/CorrelationIdHolder.groovy deleted file mode 100644 index 0656dcdb..00000000 --- a/micro-infra-spring-base/src/main/groovy/com/ofg/infrastructure/correlationid/CorrelationIdHolder.groovy +++ /dev/null @@ -1,25 +0,0 @@ -package com.ofg.infrastructure.correlationid - -import groovy.transform.CompileStatic - -/** - * Component that stores correlation id using {@link ThreadLocal} - */ -@CompileStatic -//from: https://github.com/daniel-bryant-uk/correlation-id-sync/ -class CorrelationIdHolder { - public static final String CORRELATION_ID_HEADER = "correlationId" - private static final ThreadLocal id = new ThreadLocal() - - static void set(String correlationId) { - id.set(correlationId) - } - - static String get() { - return id.get() - } - - static void remove() { - id.remove() - } -} diff --git a/micro-infra-spring-base/src/main/groovy/com/ofg/infrastructure/correlationid/CorrelationIdHolder.java b/micro-infra-spring-base/src/main/groovy/com/ofg/infrastructure/correlationid/CorrelationIdHolder.java new file mode 100644 index 00000000..c0b1d4e4 --- /dev/null +++ b/micro-infra-spring-base/src/main/groovy/com/ofg/infrastructure/correlationid/CorrelationIdHolder.java @@ -0,0 +1,21 @@ +package com.ofg.infrastructure.correlationid; + +/** + * Component that stores correlation id using {@link ThreadLocal} + */ +public class CorrelationIdHolder { + public static final String CORRELATION_ID_HEADER = "correlationId"; + private static final ThreadLocal id = new ThreadLocal(); + + public static void set(String correlationId) { + id.set(correlationId); + } + + public static String get() { + return id.get(); + } + + public static void remove() { + id.remove(); + } +} diff --git a/micro-infra-spring-base/src/main/groovy/com/ofg/infrastructure/correlationid/UuidGenerator.groovy b/micro-infra-spring-base/src/main/groovy/com/ofg/infrastructure/correlationid/UuidGenerator.groovy deleted file mode 100644 index 767cfb9e..00000000 --- a/micro-infra-spring-base/src/main/groovy/com/ofg/infrastructure/correlationid/UuidGenerator.groovy +++ /dev/null @@ -1,12 +0,0 @@ -package com.ofg.infrastructure.correlationid - -import groovy.transform.CompileStatic - -@CompileStatic -class UuidGenerator { - - String create() { - return UUID.randomUUID().toString() - } - -} diff --git a/micro-infra-spring-base/src/main/groovy/com/ofg/infrastructure/correlationid/UuidGenerator.java b/micro-infra-spring-base/src/main/groovy/com/ofg/infrastructure/correlationid/UuidGenerator.java new file mode 100644 index 00000000..f17cb38d --- /dev/null +++ b/micro-infra-spring-base/src/main/groovy/com/ofg/infrastructure/correlationid/UuidGenerator.java @@ -0,0 +1,10 @@ +package com.ofg.infrastructure.correlationid; + +import java.util.UUID; + +public class UuidGenerator { + public String create() { + return UUID.randomUUID().toString(); + } + +} diff --git a/micro-infra-spring-base/src/main/groovy/com/ofg/infrastructure/healthcheck/CollaboratorStatus.groovy b/micro-infra-spring-base/src/main/groovy/com/ofg/infrastructure/healthcheck/CollaboratorStatus.groovy deleted file mode 100644 index a872458c..00000000 --- a/micro-infra-spring-base/src/main/groovy/com/ofg/infrastructure/healthcheck/CollaboratorStatus.groovy +++ /dev/null @@ -1,12 +0,0 @@ -package com.ofg.infrastructure.healthcheck - - -enum CollaboratorStatus { - - UP, DOWN - - static CollaboratorStatus of(boolean isUp) { - return isUp? UP : DOWN - } - -} \ No newline at end of file diff --git a/micro-infra-spring-base/src/main/groovy/com/ofg/infrastructure/healthcheck/CollaboratorStatus.java b/micro-infra-spring-base/src/main/groovy/com/ofg/infrastructure/healthcheck/CollaboratorStatus.java new file mode 100644 index 00000000..585ffb88 --- /dev/null +++ b/micro-infra-spring-base/src/main/groovy/com/ofg/infrastructure/healthcheck/CollaboratorStatus.java @@ -0,0 +1,10 @@ +package com.ofg.infrastructure.healthcheck; + +public enum CollaboratorStatus { + UP, DOWN; + + public static CollaboratorStatus of(boolean isUp) { + return isUp ? UP : DOWN; + } + +} diff --git a/micro-infra-spring-base/src/main/groovy/com/ofg/infrastructure/healthcheck/CollaboratorsConfiguration.groovy b/micro-infra-spring-base/src/main/groovy/com/ofg/infrastructure/healthcheck/CollaboratorsConfiguration.groovy deleted file mode 100644 index 0422a685..00000000 --- a/micro-infra-spring-base/src/main/groovy/com/ofg/infrastructure/healthcheck/CollaboratorsConfiguration.groovy +++ /dev/null @@ -1,31 +0,0 @@ -package com.ofg.infrastructure.healthcheck - -import com.ofg.infrastructure.discovery.ServiceResolver -import com.ofg.infrastructure.web.resttemplate.fluent.ServiceRestClient -import groovy.transform.CompileStatic -import org.springframework.beans.factory.annotation.Value -import org.springframework.context.annotation.Bean -import org.springframework.context.annotation.Configuration -import org.springframework.core.io.Resource - -/** - * Registers {@link PingController} (the microservice health check controller) and {@link CollaboratorsConnectivityController} (provider of a state of microservice connection with dependent services). - * - * @see PingController - * @see CollaboratorsConnectivityController - */ -@CompileStatic -@Configuration -class CollaboratorsConfiguration { - - @Bean - CollaboratorsStatusResolver collaboratorsStatusResolver(ServiceResolver serviceResolver, PingClient pingClient) { - return new CollaboratorsStatusResolver(serviceResolver, pingClient) - } - - @Bean - PingClient pingClient(ServiceRestClient serviceRestClient) { - return new PingClient(serviceRestClient) - } - -} diff --git a/micro-infra-spring-base/src/main/groovy/com/ofg/infrastructure/healthcheck/CollaboratorsConfiguration.java b/micro-infra-spring-base/src/main/groovy/com/ofg/infrastructure/healthcheck/CollaboratorsConfiguration.java new file mode 100644 index 00000000..8cba6c6b --- /dev/null +++ b/micro-infra-spring-base/src/main/groovy/com/ofg/infrastructure/healthcheck/CollaboratorsConfiguration.java @@ -0,0 +1,26 @@ +package com.ofg.infrastructure.healthcheck; + +import com.ofg.infrastructure.discovery.ServiceResolver; +import com.ofg.infrastructure.web.resttemplate.fluent.ServiceRestClient; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * Registers {@link PingController} (the microservice health check controller) and {@link CollaboratorsConnectivityController} (provider of a state of microservice connection with dependent services). + * + * @see PingController + * @see CollaboratorsConnectivityController + */ +@Configuration +public class CollaboratorsConfiguration { + @Bean + public CollaboratorsStatusResolver collaboratorsStatusResolver(ServiceResolver serviceResolver, PingClient pingClient) { + return new CollaboratorsStatusResolver(serviceResolver, pingClient); + } + + @Bean + public PingClient pingClient(ServiceRestClient serviceRestClient) { + return new PingClient(serviceRestClient); + } + +} diff --git a/micro-infra-spring-base/src/main/groovy/com/ofg/infrastructure/healthcheck/CollaboratorsConnectivityController.groovy b/micro-infra-spring-base/src/main/groovy/com/ofg/infrastructure/healthcheck/CollaboratorsConnectivityController.groovy deleted file mode 100644 index 43458472..00000000 --- a/micro-infra-spring-base/src/main/groovy/com/ofg/infrastructure/healthcheck/CollaboratorsConnectivityController.groovy +++ /dev/null @@ -1,46 +0,0 @@ -package com.ofg.infrastructure.healthcheck - -import com.google.common.base.Optional -import com.google.common.base.Optional as GuavaOptional -import com.ofg.infrastructure.discovery.ServiceAlias -import com.ofg.infrastructure.discovery.ServicePath -import com.ofg.infrastructure.discovery.ServiceResolver -import groovy.transform.CompileStatic -import groovy.transform.PackageScope -import groovy.util.logging.Slf4j -import org.springframework.http.MediaType -import org.springframework.web.bind.annotation.RequestMapping -import org.springframework.web.bind.annotation.RequestMethod -import org.springframework.web.bind.annotation.RestController - -/** - * {@link RestController} providing connection state with services the microservice depends upon. - */ -@RestController -@CompileStatic -@PackageScope -@RequestMapping(value = '/collaborators', method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) -class CollaboratorsConnectivityController { - - private final CollaboratorsStatusResolver collaboratorsStatusResolver - - CollaboratorsConnectivityController(CollaboratorsStatusResolver collaboratorsStatusResolver) { - this.collaboratorsStatusResolver = collaboratorsStatusResolver - } - - /** - * Returns information about connection status of microservice with other microservices. - * For properly connected service UP state is provided and DOWN otherwise. - * - * @return connection status - */ - @RequestMapping - Map getCollaboratorsConnectivityInfo() { - return collaboratorsStatusResolver.statusOfMyCollaborators() - } - - @RequestMapping('/all') - Map getAllCollaboratorsConnectivityInfo() { - return collaboratorsStatusResolver.statusOfAllDependencies() - } -} diff --git a/micro-infra-spring-base/src/main/groovy/com/ofg/infrastructure/healthcheck/CollaboratorsConnectivityController.java b/micro-infra-spring-base/src/main/groovy/com/ofg/infrastructure/healthcheck/CollaboratorsConnectivityController.java new file mode 100644 index 00000000..dc01b2be --- /dev/null +++ b/micro-infra-spring-base/src/main/groovy/com/ofg/infrastructure/healthcheck/CollaboratorsConnectivityController.java @@ -0,0 +1,37 @@ +package com.ofg.infrastructure.healthcheck; + +import org.springframework.http.MediaType; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + +import java.util.Map; + +/** + * {@link RestController} providing connection state with services the microservice depends upon. + */ +@RestController +@RequestMapping(value = "/collaborators", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) +class CollaboratorsConnectivityController { + public CollaboratorsConnectivityController(CollaboratorsStatusResolver collaboratorsStatusResolver) { + this.collaboratorsStatusResolver = collaboratorsStatusResolver; + } + + /** + * Returns information about connection status of microservice with other microservices. + * For properly connected service UP state is provided and DOWN otherwise. + * + * @return connection status + */ + @RequestMapping + public Map getCollaboratorsConnectivityInfo() { + return collaboratorsStatusResolver.statusOfMyCollaborators(); + } + + @RequestMapping("/all") + public Map getAllCollaboratorsConnectivityInfo() { + return collaboratorsStatusResolver.statusOfAllDependencies(); + } + + private final CollaboratorsStatusResolver collaboratorsStatusResolver; +} diff --git a/micro-infra-spring-base/src/main/groovy/com/ofg/infrastructure/healthcheck/HealthCheckConfiguration.groovy b/micro-infra-spring-base/src/main/groovy/com/ofg/infrastructure/healthcheck/HealthCheckConfiguration.groovy deleted file mode 100644 index a842f2f5..00000000 --- a/micro-infra-spring-base/src/main/groovy/com/ofg/infrastructure/healthcheck/HealthCheckConfiguration.groovy +++ /dev/null @@ -1,38 +0,0 @@ -package com.ofg.infrastructure.healthcheck - -import com.ofg.infrastructure.discovery.ServiceResolver -import com.ofg.infrastructure.web.resttemplate.fluent.ServiceRestClient -import groovy.transform.CompileStatic -import org.springframework.beans.factory.annotation.Value -import org.springframework.context.annotation.Bean -import org.springframework.context.annotation.Configuration -import org.springframework.context.annotation.Import -import org.springframework.core.io.Resource - -/** - * Registers {@link PingController} (the microservice health check controller) and {@link CollaboratorsConnectivityController} (provider of a state of microservice connection with dependent services). - * - * @see PingController - * @see CollaboratorsConnectivityController - */ -@CompileStatic -@Configuration -@Import(CollaboratorsConfiguration) -class HealthCheckConfiguration { - - @Bean - PingController pingController() { - return new PingController() - } - - @Bean - CollaboratorsConnectivityController collaboratorsConnectivityController(CollaboratorsStatusResolver collaboratorsStatusResolver) { - return new CollaboratorsConnectivityController(collaboratorsStatusResolver) - } - - @Bean - MicroserviceConfigurationController microserviceConfigurationController(@Value('${microservice.config.file:classpath:microservice.json}') Resource microserviceConfig) { - return new MicroserviceConfigurationController(microserviceConfig) - } - -} diff --git a/micro-infra-spring-base/src/main/groovy/com/ofg/infrastructure/healthcheck/HealthCheckConfiguration.java b/micro-infra-spring-base/src/main/groovy/com/ofg/infrastructure/healthcheck/HealthCheckConfiguration.java new file mode 100644 index 00000000..0eac148a --- /dev/null +++ b/micro-infra-spring-base/src/main/groovy/com/ofg/infrastructure/healthcheck/HealthCheckConfiguration.java @@ -0,0 +1,33 @@ +package com.ofg.infrastructure.healthcheck; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; +import org.springframework.core.io.Resource; + +/** + * Registers {@link PingController} (the microservice health check controller) and {@link CollaboratorsConnectivityController} (provider of a state of microservice connection with dependent services). + * + * @see PingController + * @see CollaboratorsConnectivityController + */ +@Configuration +@Import(CollaboratorsConfiguration.class) +public class HealthCheckConfiguration { + @Bean + public PingController pingController() { + return new PingController(); + } + + @Bean + public CollaboratorsConnectivityController collaboratorsConnectivityController(CollaboratorsStatusResolver collaboratorsStatusResolver) { + return new CollaboratorsConnectivityController(collaboratorsStatusResolver); + } + + @Bean + public MicroserviceConfigurationController microserviceConfigurationController(@Value("${microservice.config.file:classpath:microservice.json}") Resource microserviceConfig) { + return new MicroserviceConfigurationController(microserviceConfig); + } + +} diff --git a/micro-infra-spring-base/src/main/groovy/com/ofg/infrastructure/healthcheck/PingClient.groovy b/micro-infra-spring-base/src/main/groovy/com/ofg/infrastructure/healthcheck/PingClient.groovy deleted file mode 100644 index c74c4e72..00000000 --- a/micro-infra-spring-base/src/main/groovy/com/ofg/infrastructure/healthcheck/PingClient.groovy +++ /dev/null @@ -1,48 +0,0 @@ -package com.ofg.infrastructure.healthcheck - -import com.google.common.base.Function -import com.google.common.base.Optional as GuavaOptional -import com.ofg.infrastructure.web.resttemplate.fluent.ServiceRestClient -import groovy.transform.CompileStatic -import groovy.transform.PackageScope -import groovy.util.logging.Slf4j - -@Slf4j -@PackageScope -@CompileStatic -class PingClient { - - private final ServiceRestClient serviceRestClient - - PingClient(ServiceRestClient serviceRestClient) { - this.serviceRestClient = serviceRestClient - } - - GuavaOptional ping(URI uri) { - return restCall(uri.toURL(), 'ping', String) - .transform({ String s -> s.trim() } as Function) - } - - GuavaOptional checkCollaborators(URI uri) { - return restCall(uri.toURL(), 'collaborators', Map) - } - - private GuavaOptional restCall(URL url, String path, Class type) { - try { - String fullUrl = "$url/$path" - GuavaOptional result = GuavaOptional.fromNullable( - serviceRestClient.forExternalService() - .get() - .onUrl(fullUrl) - .andExecuteFor() - .anObject() - .ofType(type)) - log.debug("$fullUrl returned $result") - return result - } catch (Exception e) { - log.warn("Unable to ping service '$url'!", e) - return GuavaOptional.absent() - } - } - -} diff --git a/micro-infra-spring-base/src/main/groovy/com/ofg/infrastructure/healthcheck/PingClient.java b/micro-infra-spring-base/src/main/groovy/com/ofg/infrastructure/healthcheck/PingClient.java new file mode 100644 index 00000000..2911d005 --- /dev/null +++ b/micro-infra-spring-base/src/main/groovy/com/ofg/infrastructure/healthcheck/PingClient.java @@ -0,0 +1,62 @@ +package com.ofg.infrastructure.healthcheck; + +import com.google.common.base.Function; +import com.google.common.base.Optional; +import com.ofg.infrastructure.web.resttemplate.fluent.ServiceRestClient; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.lang.invoke.MethodHandles; +import java.net.MalformedURLException; +import java.net.URI; +import java.net.URL; +import java.util.Map; + +class PingClient { + private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); + + private final ServiceRestClient serviceRestClient; + + public PingClient(ServiceRestClient serviceRestClient) { + this.serviceRestClient = serviceRestClient; + } + + public Optional ping(URI uri) { + try { + return restCall(uri.toURL(), "ping", String.class).transform(new Function() { + public String apply(String s) { + return s.trim(); + } + }); + } catch (MalformedURLException e) { + log.error("Exception occurred while trying to perform a rest call", e); + return Optional.absent(); + } + } + + public Optional checkCollaborators(URI uri) { + try { + return restCall(uri.toURL(), "collaborators", Map.class); + } catch (MalformedURLException e) { + log.error("Exception occurred while trying to perform a rest call", e); + return Optional.absent(); + } + } + + private Optional restCall(URL url, String path, Class type) { + try { + String fullUrl = String.valueOf(url) + "/" + path; + Optional result = Optional.fromNullable(serviceRestClient.forExternalService() + .get() + .onUrl(fullUrl) + .andExecuteFor() + .anObject() + .ofType(type)); + log.debug(fullUrl + " returned " + String.valueOf(result)); + return result; + } catch (Exception e) { + log.warn("Unable to ping service [" + String.valueOf(url) + "]!", e); + return Optional.absent(); + } + } +} diff --git a/micro-infra-spring-base/src/main/groovy/com/ofg/infrastructure/healthcheck/PingController.groovy b/micro-infra-spring-base/src/main/groovy/com/ofg/infrastructure/healthcheck/PingController.groovy deleted file mode 100644 index 48658363..00000000 --- a/micro-infra-spring-base/src/main/groovy/com/ofg/infrastructure/healthcheck/PingController.groovy +++ /dev/null @@ -1,22 +0,0 @@ -package com.ofg.infrastructure.healthcheck - -import groovy.transform.CompileStatic -import groovy.transform.PackageScope -import org.springframework.http.MediaType -import org.springframework.web.bind.annotation.RequestMapping -import org.springframework.web.bind.annotation.RequestMethod -import org.springframework.web.bind.annotation.RestController - -/** - * {@link RestController} that responds with OK when server is alive - */ -@RestController -@CompileStatic -@PackageScope -class PingController { - - @RequestMapping(value = "/ping", method = [RequestMethod.GET, RequestMethod.HEAD], produces = MediaType.TEXT_PLAIN_VALUE) - String ping() { - return "OK" - } -} diff --git a/micro-infra-spring-base/src/main/groovy/com/ofg/infrastructure/healthcheck/PingController.java b/micro-infra-spring-base/src/main/groovy/com/ofg/infrastructure/healthcheck/PingController.java new file mode 100644 index 00000000..b8de0756 --- /dev/null +++ b/micro-infra-spring-base/src/main/groovy/com/ofg/infrastructure/healthcheck/PingController.java @@ -0,0 +1,19 @@ +package com.ofg.infrastructure.healthcheck; + +import org.springframework.http.MediaType; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + +/** + * {@link RestController} that responds with OK when server is alive + */ +@RestController +public class PingController { + + @RequestMapping(value = "/ping", method = {RequestMethod.GET, RequestMethod.HEAD}, produces = MediaType.TEXT_PLAIN_VALUE) + public String ping() { + return "OK"; + } + +}