Assignment #4 Adding a new Microservice to host the MongoDB database
in our Weather Microservice we call an external Web Service to get a Weather record,
external Web Services are slow, and may be expensive to use, so we are going to cache
the weather object we get from the external Web Service.
the next time we ask for the weather we are going to check the cache (Microservice call)
and avoid the call to the external web service
if I do call the external Web Service, I will cache (add to DB) the results to avoid another call
our new Caching Microservice will provide a CRUD (Create/Read/Update/Delete) interface
via Http Verbs: POST/GET/PUT/DELETE
mapping a MongoDB records (JSON Document) to a Java class via JPA we will use the MapStruct tool
org.mapstruct
mapstruct
1.5.5.Final
org.mapstruct
mapstruct-processor
1.5.5.Final
Make sure your Spring Boot Project include the Spring-MongoDB dependency
org.springframework.boot
spring-boot-starter-data-mongodb
--------------------------------------------------------
the Steps I need to do:
1) Install MongoDB and start it
2) create a new Spring Boot Project, and add Spring Web and Spring MongoDB
this project needs a port number in the applicaiton.properties
and the logging to see the correlation id
3) in this new project I create a RestController w/ 2 Endpoints:
3.1) the query to see if the Weather records exists
3.2) the insert to add a new Weather record
@GetMapping(value="/weatherCache/{lat}/{lon}/{year}/{month}/{day}", produces="application/json")
Weather getWeatherCacheByLocationAndDate(@PathVariable double lat,
@PathVariable double lon,
@PathVariable int year,
@PathVariable int month,
@PathVariable int day) throws IOException;
@PostMapping(value="/weatherCache", produces="application/json", consumes="application/json")
void addWeatherCache(@RequestBody Weather weather) throws IOException;
4) in this new Project I need to connect to MongoDB, Spring makes this really easy
I need a Interface to define my MongoDB Repository
public interface CacheRepository extends MongoRepository {
@Query
WeatherRecord findWeatherByKey(WeatherKey key);
}
5) I need 2 Entity classes
5.1) I need the data to go into the Database (I called it WeatherRecord)
5.2) I need a compound primary key holding: Lat, Lon, Date (I called it WeatherKey)
@Document(collection="weather")
public class WeatherRecord implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private WeatherKey key;
@Version
private Integer version;
private Weather weather;
public class WeatherKey implements Serializable {
private static final long serialVersionUID = 1L;
private double lat;
private double lon;
private String date;
6) In the main class I need to enable MongoDB
@SpringBootApplication
@EnableMongoRepositories
public class CacheApplication {
@Autowired
CacheRepository cacheRepository;
7) in application.properties I need the MongoDB connection details:
spring.data.mongodb.auto-index-creation=true
spring.data.mongodb.uri=mongodb://localhost:27017
spring.data.mongodb.database=cache
8) the crictal command to read/insert to MongoDB are:
WeatherRecord record = repository.findWeatherByKey(key);
repository.save(record);