The goal
What we want to do is developping a sample application, with a backend and a frontend.
- The backend will be in Java/Springboot, the database will be MongoDB
- The frontend will be Angular
A the end, the app will run in the cloud, and be accessible worldwide, thanks to the google infrastructure.
Ready to go ? Lest's start !
References
For this article, I combined some tutorials. Here are the links :
https://www.codementor.io/gtommee97/rest-api-java-spring-boot-and-mongodb-j7nluip8d
https://www.opencodez.com/java/use-mongodb-atlas-with-spring-boot.htm
https://spring.io/guides/gs/accessing-mongodb-data-rest/
https://cloud.google.com/community/tutorials/mongodb-atlas-appengineflex-nodejs-app
Step 1 : create the database
In order to be cloud ready, you need to create a mongoDB instance online.
To do this, we will use MongoDB Atlas. It's the Database As A Service for Mongo. The instance can be obtained for free. No need to give your credit card number ! Cooool.
- Either login or create a new account (click Register and fill the form). It's quick and free.
- Once logged, click the button "Build a Cluster"
- In "Cloud provider and region", choose GCP and Europe-West-1 (or another which is free).
- Click the "create cluster" button at the bottom. Cluster creation starts.
- While it is working, you can do the next steps. Click "Get Started"
- Click "Create your first database user". The system will guide you (showing where to click)
- As shown, click "Security" / "Add new user". Create an admin user. Use a strong password. Don't forget that your instance is on a public network.
- Now, configure the firewall. Click "Get Started" / "Whitelist your IP address"
- Follow the assistant. In the dialog, click "Allow access from anywhere" (we will improve security later).
- Click the "Confirm" button. Wait a couple of minutes that the rules are OK.
- Go back to the "Overview" tab, then click the "Connect" button, and "Connect with the Mongo Shell"
- Choose your platform and download the mongo shell. Unzip it and put the "mongo" executable in your path.
- Click "Short SRV connection String", copy the connection string (part "2") and run the command in a terminal
- When prompted, enter the password for the admin user you created in a previous step.
- Now, you are ready to create the database :
use rest_tutorial
- Create the collection
db.createCollection("pets");
- Insert some data
db.pets.insertMany([
{ "name" : "Spot", "species" : "dog", "breed" : "pitbull" }, { "name" : "Daisy", "species" : "cat", "breed" : "calico" }, { "name" : "Bella", "species" : "dog", "breed" : "australian shepard" }
]);
- Verify that your datas are accessible :
db.pets.find({});
- Congrats ! Your database is now online. Next step : create the SpringBoot application to consume this database.
Aucun commentaire:
Enregistrer un commentaire