Example of a microservice that works on Spring Boot.
To make setting up microservices in micro-time we needed to extract the common building blocks to separate libraries. In this template we are using the following 4finance custom libraries (available in jCenter)
- "Micro Deps Spring Config" library (micro-deps-spring-config module)
- "Micro Infra Spring".
- "Micro Deps Spring Config" library (micro-deps-spring-test-config module)
##How it works?
###Production code
Below you can find description of the most crucial parts of the application's production code.
contains Spring Boot autoconfiguration and contains main method
imports configuration from 4finance's "Micro Deps Spring Config" library (micro-deps-spring-config module) that contains Service Discovery configuration. For Service Discovery we are using Zookeeper from 4finance's "Micro Deps" library. Note: This configuration should not be imported in the same profiles as tests since the application will try to connect to a running Zookeeper instance.
imports configuration from 4finance's "Micro Infra Spring". That module contains all common web configuration like Swagger, CorrelationId filters, custom RestTemplate, custom exception handling, health check controllers etc.
Below you can find description of the most crucial parts of the application's test code.
configuration that is a point of entry for all the integration tests. It imports the com.ofg.infrastructure.discovery.ServiceDiscoveryStubbingConfiguration configuration that stubs the Zookeeper server with a map of dependency names vs dependency address.
extends com.ofg.infrastructure.base.IntegrationSpec Spock Specification class that initializes Spring web-context.
extends com.ofg.infrastructure.base.MvcIntegrationSpec Spock Specification class that initializes Spring web-context and provides some autowired fields including the ServiceProvider interface that allows you to stub Zookeeper entries.
extends com.ofg.infrastructure.base.MvcWiremockIntegrationSpec Spock Specification class that extends the MvcIntegrationSpec spec. Additionally it provides WireMock related fields and methods.
##Sample business requirement Twitter places analyzer, searches through tweets for places. Then analyzers send those to Collerators.
Hit PUT at:
/api/{pairId}
with list of tweets:
[
{
"created_at": "Sat Jul 26 09:15:10 +0000 2014",
"id": 492961315070439424,
"id_str": "492961315070439424",
"geo": null,
"coordinates":
{
"coordinates":
[
-75.14310264,
40.05701649
],
"type":"Point"
},
},
{
"created_at": "Sat Jul 26 09:15:10 +0000 2014",
"id": 492961315070439424,
"id_str": "492961315070439424",
"geo": null,
"coordinates":
{
"coordinates":
[
-75.14310264,
40.05701649
],
"type":"Point"
},
}
]
And it will hit collectors at /{pairId} with tweets taken from twitter
[
{
"pair_id" : 1,
"tweet_id" : "492967299297845248",
"place" :
{
"name":"Washington",
"country_code": "US"
},
"probability" : "2",
"origin" : "twitter_place_section"
},
{
"pair_id" : 2,
"tweet_id" : "123187246819263981"
},
]