martes, 9 de julio de 2019

MicroServicio con SpringBoot Parte 2, lab IDE servicio REST


            curl  http://localhost:8080/api/miinfo

    Browser  -------------------------------->  servicioREST
                    <------------------------------
Información de microserver springboot_lab, hostname: 127.0.1.1
propiedad de clase 'asignadaExternamente' en application.properties tiene el valor: asignada en application.properties


https://github.com/jalbertomr/springboot_lab/commit/ff7844365eb01a6190ee23fbdc521ae8269f0c84

El microservicio con SprinbBoot que se creo desde linea de comandos CLI, nos dejo una estructura de proyecto, sobre la cual es importada como proyecto existente en Maven desde Eclipse.

Este Eclipse (photon) se el agregan desde marketplace JBoss tools, Git, y m2Eclipse desde help->install new software... (http://download.eclipse.org/technology/m2e/releases), tambien el Spring Tool Suite  STS.

Con el m2Eclipse podemos integrar comandos maven al Ide.

Al proyecto le agregaremos un controlador REST con ruta /api/info para que nos de simple información de nuestro microservicio como el host.

Esto con las anotaciones en la clase controller

@RestConroller
@RequestMapping("/ruta")

y en el método de la clase

@RequestMapping(method=RequestMethod.GET, value="/info", produ...)

Configuración Externa

Los atributos de la clase se pueden asignar desde fuera del programa en este caso en el archivo de recursos application.properties, con la anotación en la clase
@ConfigurationProperties(prefix="asignaciones")

src/main/resources/application.properties

asignaciones.asignadaExternamente=asignada en application.properties


Al ejecutarlo vemos

Exponiendo información de la aplicación y metricas.

Al agregar la dependencia al proyecto podemos obtener una serie de informacion sobre la aplicación.

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>


La ejecutamos

bext@bext-VPCF13WFX:~/eclipse-workspace/springboot_lab$ mvn clean package spring-boot:run
[INFO] Scanning for projects...
[INFO] 
[INFO] ----------------------< com.bext:springboot_lab >-----------------------
[INFO] Building springboot_lab 1.0
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ springboot_lab ---
[INFO] Deleting /home/bext/eclipse-workspace/springboot_lab/target
[INFO] 
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ springboot_lab ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ springboot_lab ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 2 source files to /home/bext/eclipse-workspace/springboot_lab/target/classes
[INFO] 
[INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ springboot_lab ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/bext/eclipse-workspace/springboot_lab/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ springboot_lab ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /home/bext/eclipse-workspace/springboot_lab/target/test-classes
[INFO] 
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ springboot_lab ---
[INFO] 
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.bext.ejemplo.holaspringboot.HolaSpringbootApplicationTests
14:08:34.551 [main] DEBUG org.springframework.test.context.junit4.SpringJUnit4ClassRunner - SpringJUnit4ClassRunner constructor called with [class com.bext.ejemplo.holaspringboot.HolaSpringbootApplicationTests]
14:08:34.559 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating CacheAwareContextLoaderDelegate from class [org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate]
14:08:34.570 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating BootstrapContext using constructor [public org.springframework.test.context.support.DefaultBootstrapContext(java.lang.Class,org.springframework.test.context.CacheAwareContextLoaderDelegate)]
14:08:34.600 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating TestContextBootstrapper for test class [com.bext.ejemplo.holaspringboot.HolaSpringbootApplicationTests] from class [org.springframework.boot.test.context.SpringBootTestContextBootstrapper]
14:08:34.618 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Neither @ContextConfiguration nor @ContextHierarchy found for test class [com.bext.ejemplo.holaspringboot.HolaSpringbootApplicationTests], using SpringBootContextLoader
14:08:34.622 [main] DEBUG org.springframework.test.context.support.AbstractContextLoader - Did not detect default resource location for test class [com.bext.ejemplo.holaspringboot.HolaSpringbootApplicationTests]: class path resource [com/bext/ejemplo/holaspringboot/HolaSpringbootApplicationTests-context.xml] does not exist
14:08:34.622 [main] DEBUG org.springframework.test.context.support.AbstractContextLoader - Did not detect default resource location for test class [com.bext.ejemplo.holaspringboot.HolaSpringbootApplicationTests]: class path resource [com/bext/ejemplo/holaspringboot/HolaSpringbootApplicationTestsContext.groovy] does not exist
14:08:34.623 [main] INFO org.springframework.test.context.support.AbstractContextLoader - Could not detect default resource locations for test class [com.bext.ejemplo.holaspringboot.HolaSpringbootApplicationTests]: no resource found for suffixes {-context.xml, Context.groovy}.
14:08:34.624 [main] INFO org.springframework.test.context.support.AnnotationConfigContextLoaderUtils - Could not detect default configuration classes for test class [com.bext.ejemplo.holaspringboot.HolaSpringbootApplicationTests]: HolaSpringbootApplicationTests does not declare any static, non-private, non-final, nested classes annotated with @Configuration.
14:08:34.689 [main] DEBUG org.springframework.test.context.support.ActiveProfilesUtils - Could not find an 'annotation declaring class' for annotation type [org.springframework.test.context.ActiveProfiles] and class [com.bext.ejemplo.holaspringboot.HolaSpringbootApplicationTests]
14:08:34.822 [main] DEBUG org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider - Identified candidate component class: file [/home/bext/eclipse-workspace/springboot_lab/target/classes/com/bext/SpringbootApplication.class]
14:08:34.824 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Found @SpringBootConfiguration com.bext.SpringbootApplication for test class com.bext.ejemplo.holaspringboot.HolaSpringbootApplicationTests
14:08:34.950 [main] DEBUG org.springframework.boot.test.context.SpringBootTestContextBootstrapper - @TestExecutionListeners is not present for class [com.bext.ejemplo.holaspringboot.HolaSpringbootApplicationTests]: using defaults.
14:08:34.950 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener, org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]
14:08:34.963 [main] DEBUG org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Skipping candidate TestExecutionListener [org.springframework.test.context.transaction.TransactionalTestExecutionListener] due to a missing dependency. Specify custom listener classes or make the default listener classes and their required dependencies available. Offending class: [org/springframework/transaction/TransactionDefinition]
14:08:34.964 [main] DEBUG org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Skipping candidate TestExecutionListener [org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener] due to a missing dependency. Specify custom listener classes or make the default listener classes and their required dependencies available. Offending class: [org/springframework/transaction/interceptor/TransactionAttribute]
14:08:34.964 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@31368b99, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@1725dc0f, org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener@3911c2a7, org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener@4ac3c60d, org.springframework.test.context.support.DirtiesContextTestExecutionListener@4facf68f, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener@76508ed1, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener@41e36e46, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener@15c43bd9, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener@3d74bf60, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener@4f209819]
14:08:34.966 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved @ProfileValueSourceConfiguration [null] for test class [com.bext.ejemplo.holaspringboot.HolaSpringbootApplicationTests]
14:08:34.967 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [com.bext.ejemplo.holaspringboot.HolaSpringbootApplicationTests]
14:08:34.968 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved @ProfileValueSourceConfiguration [null] for test class [com.bext.ejemplo.holaspringboot.HolaSpringbootApplicationTests]
14:08:34.969 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [com.bext.ejemplo.holaspringboot.HolaSpringbootApplicationTests]
14:08:34.969 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved @ProfileValueSourceConfiguration [null] for test class [com.bext.ejemplo.holaspringboot.HolaSpringbootApplicationTests]
14:08:34.969 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [com.bext.ejemplo.holaspringboot.HolaSpringbootApplicationTests]
14:08:34.974 [main] DEBUG org.springframework.test.context.support.AbstractDirtiesContextTestExecutionListener - Before test class: context [DefaultTestContext@3835c46 testClass = HolaSpringbootApplicationTests, testInstance = [null], testMethod = [null], testException = [null], mergedContextConfiguration = [WebMergedContextConfiguration@1dde4cb2 testClass = HolaSpringbootApplicationTests, locations = '{}', classes = '{class com.bext.SpringbootApplication}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@4e7dc304, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@48503868, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@1f57539, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@bd8db5a], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes = map['org.springframework.test.context.web.ServletTestExecutionListener.activateListener' -> true]], class annotated with @DirtiesContext [false] with mode [null].
14:08:34.975 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved @ProfileValueSourceConfiguration [null] for test class [com.bext.ejemplo.holaspringboot.HolaSpringbootApplicationTests]
14:08:34.976 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [com.bext.ejemplo.holaspringboot.HolaSpringbootApplicationTests]
14:08:35.002 [main] DEBUG org.springframework.test.context.support.TestPropertySourceUtils - Adding inlined properties to environment: {spring.jmx.enabled=false, org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true, server.port=-1}

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.6.RELEASE)

2019-07-09 14:08:35.331  INFO 11398 --- [           main] c.b.e.h.HolaSpringbootApplicationTests   : Starting HolaSpringbootApplicationTests on bext-VPCF13WFX with PID 11398 (started by bext in /home/bext/eclipse-workspace/springboot_lab)
2019-07-09 14:08:35.334  INFO 11398 --- [           main] c.b.e.h.HolaSpringbootApplicationTests   : No active profile set, falling back to default profiles: default
2019-07-09 14:08:38.462  INFO 11398 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-07-09 14:08:39.696  INFO 11398 --- [           main] o.s.b.a.e.web.EndpointLinksResolver      : Exposing 2 endpoint(s) beneath base path '/actuator'
2019-07-09 14:08:39.822  INFO 11398 --- [           main] c.b.e.h.HolaSpringbootApplicationTests   : Started HolaSpringbootApplicationTests in 4.808 seconds (JVM running for 5.885)
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 5.706 s - in com.bext.ejemplo.holaspringboot.HolaSpringbootApplicationTests
2019-07-09 14:08:40.127  INFO 11398 --- [       Thread-2] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] 
[INFO] --- maven-jar-plugin:3.1.2:jar (default-jar) @ springboot_lab ---
[INFO] Building jar: /home/bext/eclipse-workspace/springboot_lab/target/springboot_lab-1.0.jar
[INFO] 
[INFO] --- spring-boot-maven-plugin:2.1.6.RELEASE:repackage (repackage) @ springboot_lab ---
[INFO] Replacing main artifact with repackaged archive
[INFO] 
[INFO] >>> spring-boot-maven-plugin:2.1.6.RELEASE:run (default-cli) > test-compile @ springboot_lab >>>
[INFO] 
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ springboot_lab ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ springboot_lab ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 2 source files to /home/bext/eclipse-workspace/springboot_lab/target/classes
[INFO] 
[INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ springboot_lab ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/bext/eclipse-workspace/springboot_lab/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ springboot_lab ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /home/bext/eclipse-workspace/springboot_lab/target/test-classes
[INFO] 
[INFO] <<< spring-boot-maven-plugin:2.1.6.RELEASE:run (default-cli) < test-compile @ springboot_lab <<<
[INFO] 
[INFO] 
[INFO] --- spring-boot-maven-plugin:2.1.6.RELEASE:run (default-cli) @ springboot_lab ---

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.6.RELEASE)

2019-07-09 14:08:43.111  INFO 11351 --- [           main] com.bext.SpringbootApplication           : Starting SpringbootApplication on bext-VPCF13WFX with PID 11351 (/home/bext/eclipse-workspace/springboot_lab/target/classes started by bext in /home/bext/eclipse-workspace/springboot_lab)
2019-07-09 14:08:43.117  INFO 11351 --- [           main] com.bext.SpringbootApplication           : No active profile set, falling back to default profiles: default
2019-07-09 14:08:45.201  INFO 11351 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2019-07-09 14:08:45.259  INFO 11351 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-07-09 14:08:45.260  INFO 11351 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.21]
2019-07-09 14:08:45.402  INFO 11351 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-07-09 14:08:45.403  INFO 11351 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2221 ms
2019-07-09 14:08:46.844  INFO 11351 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-07-09 14:08:47.293  INFO 11351 --- [           main] o.s.b.a.e.web.EndpointLinksResolver      : Exposing 2 endpoint(s) beneath base path '/actuator'
2019-07-09 14:08:47.417  INFO 11351 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2019-07-09 14:08:47.422  INFO 11351 --- [           main] com.bext.SpringbootApplication           : Started SpringbootApplication in 4.943 seconds (JVM running for 20.73)
2019-07-09 14:08:59.941  INFO 11351 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2019-07-09 14:08:59.942  INFO 11351 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2019-07-09 14:08:59.953  INFO 11351 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 11 ms

Al consultar la página del servicio obtenemos


Para ejecutarlo desde linea de comando

bext@bext-VPCF13WFX:~/eclipse-workspace/springboot_lab$ mvn clean package

bext@bext-VPCF13WFX:~/eclipse-workspace/springboot_lab$ java -jar target/springboot_lab-1.0.jar

eot


No hay comentarios:

Publicar un comentario