The cities-service subproject is an example of a very simple but fully functional microservice. The microservice is built using:
-
Spring Data JPA to access a database
-
Spring Data REST to expose the database contents via a REST API
-
Spring Cloud Connectors is used to create the database connection from information exposed by the cloud environment
Key concepts:
-
Simple application configuration using Spring Boot
-
Data access using Spring Data JPA and exposing a REST API using Spring Data REST
The cities-client subproject provides a client library for use by Java apps consuming the microservice. The main goal of this library is to show an example of a Spring Cloud Connectors extension for consuming a microservice in a cloud environment.
The client library uses Feign to expose the microservice REST API using a Repository pattern. This provides a nice analog to the Repository abstraction used by Spring Data.
The same Spring Cloud Connectors extension technique could be used to create lower-level REST API connection objects like Spring RestTemplate or Apache HttpClient.
Key concepts:
-
A
ServiceInfo
class to model the connection information needed to access the REST API (just a URL in this example) -
A
ServiceInfoCreator
class to populate theServiceInfo
class from information exposed by Cloud Foundry -
A
ServiceConnectionCreator
class to create the Feign repository from the information contained in theServiceInfo
-
Registration of the
ServiceInfoCreator
andServiceConnectionCreator
to the Spring Cloud Connectors framework
The cities-ui subproject is a web UI application that uses the client library to consume the microservice REST API. It is built using Spring Boot and AngularJS.
Key concepts:
-
Simple application configuration using Spring Boot
-
Proxying calls from the AngularJS front-end to the repository backend with Spring MVC
-
Consuming the client library using Spring Cloud Connectors
The microservice loads a very large dataset at startup to show the power of the paging, sorting, and search capabilities in Spring Data. The default import.sql
file contains just under 43,000 small rows (representing all postal codes in the United States) that get loaded when the application starts.
Free database service tiers on public Cloud Foundry services often limit the size of the database you can use and the number of records you can load at startup. You will likely need to reduce the size of the dataset when deploying to a public Cloud Foundry service with a free database tier.
The default import.sql
file works with the in-memory HyperSQL database (HSQLDB) and MySQL.