{"id":821,"date":"2020-03-30T07:00:00","date_gmt":"2020-03-30T07:00:00","guid":{"rendered":"http:\/\/bullyrooks.com\/index.php\/2020\/03\/30\/simple-spring-boot-service-to-kubernetes-application-step-7-2a141b03db3f\/"},"modified":"2021-02-04T01:51:08","modified_gmt":"2021-02-04T01:51:08","slug":"simple-spring-boot-service-to-kubernetes-application-step-7-2a141b03db3f","status":"publish","type":"post","link":"https:\/\/bullyrooks.com\/index.php\/2020\/03\/30\/simple-spring-boot-service-to-kubernetes-application-step-7-2a141b03db3f\/","title":{"rendered":"Database as a Service"},"content":{"rendered":"\n<p class=\"graf graf--p graf-after--h3 graf--trailing\" id=\"bc5a\">Now that we\u2019ve got our service pretty might tightened up lets get started moving towards deploying it. The first step is using a real database instead of the in memory one.<\/p>\n\n\n\n<h3 class=\"graf graf--h3 graf--leading wp-block-heading\" id=\"82f9\">Cloud Based Database&nbsp;Setup<\/h3>\n\n\n\n<p class=\"graf graf--p graf-after--h3\" id=\"b23e\">We\u2019re going to use postgreSQL as our database. The most straightforward option is to run postgreSQL locally. However, this is not going to reflect how we would run in the cloud. Running a DB locally also uses up resources that we would rather have for our application. Although this doesn\u2019t really matter much when we\u2019re running locally, it\u2019s going to have a large impact when we deploy into a production environment where every instance and CPU cycle costs us money.<\/p>\n\n\n\n<p class=\"graf graf--p graf-after--p\" id=\"a044\">So lets get started with a DB SaaS solution right off the bat. A very good, easy to setup and inexpensive solution is <a class=\"markup--anchor markup--p-anchor\" href=\"https:\/\/www.heroku.com\/home\" target=\"_blank\" rel=\"noopener\" data-href=\"https:\/\/www.heroku.com\/home\">heroku<\/a>. Get started by creating an account. Then we\u2019re going to follow the instructions <a class=\"markup--anchor markup--p-anchor\" href=\"https:\/\/docs.appery.io\/docs\/apiexpress-databaseconnection-heroku-postgres\" target=\"_blank\" rel=\"noopener\" data-href=\"https:\/\/docs.appery.io\/docs\/apiexpress-databaseconnection-heroku-postgres\">here <\/a>to setup our database instance and get our datasource configuration. Keep track of these items:<\/p>\n\n\n\n<ul class=\"postList wp-block-list\"><li id=\"7759\" class=\"graf graf--li graf-after--p\">datasource connection url<\/li><li id=\"1097\" class=\"graf graf--li graf-after--li\">login<\/li><li id=\"e35f\" class=\"graf graf--li graf-after--li\">password<\/li><\/ul>\n\n\n\n<h3 class=\"graf graf--h3 graf-after--li wp-block-heading\" id=\"b427\">Setup our Application Configuration<\/h3>\n\n\n\n<p class=\"graf graf--p graf-after--h3\" id=\"3588\">Open up our <code class=\"markup--code markup--p-code\">application.yaml<\/code> file and add this configuration, making sure to put your datasource url into the correct location. The format is <code class=\"markup--code markup--p-code\">jdbc:postgresql:\/\/${host}\/${database}<\/code><\/p>\n\n\n\n<pre id=\"e317\" class=\"wp-block-preformatted graf graf--pre graf-after--p\">---<\/pre>\n\n\n\n<pre id=\"3e8f\" class=\"wp-block-code graf graf--pre graf-after--pre\"><code>spring:\n  profiles: herokudb\n  datasource:\n    hikari:\n      connectionTimeout: 20000\n      maximumPoolSize: 2\n    url: jdbc:postgresql:\/\/ec2***.amazonaws.com:5432\/df***e<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote graf graf--blockquote graf-after--pre is-layout-flow wp-block-quote-is-layout-flow\" id=\"4587\"><p>We\u2019re defining a spring profile here because we don\u2019t want to use this datasource by default. If we put it into the default profile, it will become visible to our tests and they will prefer postgreSQL to H2, which is not preferred. By putting the postgreSQL configuration into a separate profile, we can control when we connect to it.<\/p><\/blockquote>\n\n\n\n<p class=\"graf graf--p graf-after--blockquote\" id=\"9f0a\">We could put our username and password here, but that would be extremely unsafe and also problematic if we have several environments (dev, test, prod, etc\u2026). So for the meantime we\u2019re going to put that into our runtime configuration.<\/p>\n\n\n\n<p class=\"graf graf--p graf-after--p\" id=\"1c16\">Go into the <code class=\"markup--code markup--p-code\">Edit Configurations\u2026<\/code> menu item on the run dropdown in intellij. Go into the <code class=\"markup--code markup--p-code\">Configuration<\/code> tab of the <code class=\"markup--code markup--p-code\">MediumCustomerApplication <\/code>application. Expand the VM options section and add this configuration (replacing with your username and password)<\/p>\n\n\n\n<pre id=\"5fa7\" class=\"wp-block-code graf graf--pre graf-after--p\"><code>-Dspring.profiles.active=herokudb\n-Dspring.datasource.username=amk***d\n-Dspring.datasource.password=2a***97c<\/code><\/pre>\n\n\n\n<p class=\"graf graf--p graf-after--pre\" id=\"f3bf\">I also had to update my <code class=\"markup--code markup--p-code\">pom.xml<\/code> in order to get it working:<\/p>\n\n\n\n<pre id=\"27d5\" class=\"wp-block-code graf graf--pre graf-after--p\"><code>&lt;dependency&gt;\n   &lt;groupId&gt;org.postgresql&lt;\/groupId&gt;\n   &lt;artifactId&gt;postgresql&lt;\/artifactId&gt;\n&lt;\/dependency&gt;\n&lt;dependency&gt;\n   &lt;groupId&gt;org.liquibase&lt;\/groupId&gt;\n   &lt;artifactId&gt;liquibase-core&lt;\/artifactId&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n\n\n\n<p class=\"graf graf--p graf-after--pre\" id=\"2f98\">Click OK and then startup our application. We should see that it connected to our postgres database.<\/p>\n\n\n\n<pre id=\"c30f\" class=\"wp-block-code graf graf--pre graf-after--p\"><code>2020-03-30 13:54:01.157  INFO &#91;customer,,,] 7732 --- &#91;  restartedMain] o.s.b.a.h2.H2ConsoleAutoConfiguration    : H2 console available at '\/h2-console'. Database available at 'jdbc:postgresql:\/\/ec2****.amazonaws.com:5432\/df***e'<\/code><\/pre>\n\n\n\n<p class=\"graf graf--p graf-after--pre\" id=\"50dd\">Lets confirm with the DB Browser. Click on the DB Browser tab under project on intellij<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/i0.wp.com\/bullyrooks.com\/wp-content\/uploads\/2021\/02\/10HVv9pX_2zTPRVL61TJhJg.png?w=960\" alt=\"\" data-recalc-dims=\"1\"\/><\/figure>\n\n\n\n<p class=\"graf graf--p graf-after--figure\" id=\"8a9a\">Create a new connection to postgreSQL. Add your host information, username and password. Change the Database to the one supplied on the heroku datasource configuration page.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/i0.wp.com\/bullyrooks.com\/wp-content\/uploads\/2021\/02\/10HVv9pX_2zTPRVL61TJhJg.png?w=960\" alt=\"\" data-recalc-dims=\"1\"\/><\/figure>\n\n\n\n<p class=\"graf graf--p graf-after--figure\" id=\"080a\">When we test connection we should see a success. Now lets put some data in and make a request to confirm we can see the state of the database.<\/p>\n\n\n\n<p class=\"graf graf--p graf-after--p\" id=\"3e75\">Go ahead and use the postman POST statement we used to create a customer. Open up a DB browser window, change the database to <code class=\"markup--code markup--p-code\">public<\/code> and execute this query:<\/p>\n\n\n\n<pre id=\"b18e\" class=\"wp-block-code graf graf--pre graf-after--p\"><code>select * from customer;<\/code><\/pre>\n\n\n\n<p class=\"graf graf--p graf-after--pre\" id=\"838d\">we should see this data returned:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/i0.wp.com\/bullyrooks.com\/wp-content\/uploads\/2021\/02\/10HVv9pX_2zTPRVL61TJhJg.png?w=960\" alt=\"\" data-recalc-dims=\"1\"\/><\/figure>\n\n\n\n<p class=\"graf graf--p graf-after--figure\" id=\"d283\">One thing to make note of here is that if we send the POST request in multiple times, we get different behavior. It appears that new records are allowed to be created in postgres. We will address this as we add validation to this service.<\/p>\n\n\n\n<h3 class=\"graf graf--h3 graf-after--p wp-block-heading\" id=\"b792\">Build and&nbsp;Commit<\/h3>\n\n\n\n<pre id=\"2185\" class=\"wp-block-code graf graf--pre graf-after--h3\"><code>git checkout -b postgres\nmvn clean install\ngit add .\ngit commit -m \"Added postgreSQL configuration\"\ngit push --set-upstream origin postgres\ngit checkout master\ngit merge postgres\ngit push<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<div class=\"entry-summary\">\nNow that we\u2019ve got our service pretty might tightened up lets get&hellip;\n<\/div>\n<div class=\"link-more\"><a href=\"https:\/\/bullyrooks.com\/index.php\/2020\/03\/30\/simple-spring-boot-service-to-kubernetes-application-step-7-2a141b03db3f\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &ldquo;Database as a Service&rdquo;<\/span>&hellip;<\/a><\/div>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[41],"tags":[52,29,72,50,73,42,43],"course":[40],"class_list":["post-821","post","type-post","status-publish","format-standard","hentry","category-software-development","tag-database","tag-git","tag-heroku","tag-maven","tag-postgresql","tag-spring","tag-spring-boot","course-spring-with-kubernetes","entry"],"jetpack_featured_media_url":"","jetpack-related-posts":[{"id":1230,"url":"https:\/\/bullyrooks.com\/index.php\/2022\/01\/23\/kube-cloud-pt2-database-configuration-in-kubernetes\/","url_meta":{"origin":821,"position":0},"title":"Kube Cloud Pt2 | Database Configuration in Kubernetes","author":"Bullyrook","date":"January 23, 2022","format":false,"excerpt":"We now have an application that will connect to our MongoDB locally, but we'll need to tell it how to connect when its deployed to Okteto. Create Kubernetes Configuration First, create a secret in okteto with the database details. Run this command on your terminal. The uri should look like\u2026","rel":"","context":"In &quot;Software Development&quot;","block_context":{"text":"Software Development","link":"https:\/\/bullyrooks.com\/index.php\/category\/software-development\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/bullyrooks.com\/wp-content\/uploads\/2022\/01\/image-43.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/bullyrooks.com\/wp-content\/uploads\/2022\/01\/image-43.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/bullyrooks.com\/wp-content\/uploads\/2022\/01\/image-43.png?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":816,"url":"https:\/\/bullyrooks.com\/index.php\/2020\/03\/30\/simple-spring-boot-service-to-kubernetes-application-step-12-c6423261a93a\/","url_meta":{"origin":821,"position":1},"title":"Setting up a Kubernetes Cluster","author":"Bullyrook","date":"March 30, 2020","format":false,"excerpt":"Finally, we\u2019re going to be able to deploy our application. We need to get access to a cluster first. Install Tooling We\u2019re going to need more tools in order to get started. Use your OS package management tool to install these tools: Kubectl (Interact with a k8s instance)Minikube (Run a\u2026","rel":"","context":"In &quot;Software Development&quot;","block_context":{"text":"Software Development","link":"https:\/\/bullyrooks.com\/index.php\/category\/software-development\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1141,"url":"https:\/\/bullyrooks.com\/index.php\/2022\/01\/02\/kubernetes-application-hosted-in-the-cloud\/","url_meta":{"origin":821,"position":2},"title":"Kubernetes Application Hosted in the Cloud","author":"Bullyrook","date":"January 2, 2022","format":false,"excerpt":"It's been a few years since my last spring boot based kubernetes application. That course ended with a microservice deployed into kubernetes via minikube locally. I'm using this course to expand on that effort. In this course I'm going to show a few ways to do the same things in\u2026","rel":"","context":"In &quot;Software Development&quot;","block_context":{"text":"Software Development","link":"https:\/\/bullyrooks.com\/index.php\/category\/software-development\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1229,"url":"https:\/\/bullyrooks.com\/index.php\/2022\/01\/23\/kube-cloud-pt2-automated-testing-with-testcontainers\/","url_meta":{"origin":821,"position":3},"title":"Kube Cloud Pt2 | Automated Testing with TestContainers","author":"Bullyrook","date":"January 23, 2022","format":false,"excerpt":"In the last section we implemented service endpoint that stored data in a mongodb backend. In this session we're going to build a component test to automatically verify that functionality. TestContainer Overview TestContainers allow us to mock external resources with a docker based implementation. This is similar to \"regular\" mocking\u2026","rel":"","context":"In &quot;Software Development&quot;","block_context":{"text":"Software Development","link":"https:\/\/bullyrooks.com\/index.php\/category\/software-development\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":830,"url":"https:\/\/bullyrooks.com\/index.php\/2020\/03\/30\/simple-spring-boot-service-to-kubernetes-application-step-2-2ffb7e2550d5\/","url_meta":{"origin":821,"position":4},"title":"Create the Data Repository","author":"Bullyrook","date":"March 30, 2020","format":false,"excerpt":"In the previous step we initialized a spring boot project so that we can start development. In this step we\u2019re going to setup the persistence tier of the customer service. Create the Data Repository I like to start with the data model at the persistence level and work up. Since\u2026","rel":"","context":"In &quot;Software Development&quot;","block_context":{"text":"Software Development","link":"https:\/\/bullyrooks.com\/index.php\/category\/software-development\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1220,"url":"https:\/\/bullyrooks.com\/index.php\/2022\/01\/23\/cloud-kube-pt2-setting-up-a-datastore\/","url_meta":{"origin":821,"position":5},"title":"Cloud Kube Pt2 | Setting Up a Datastore","author":"Bullyrook","date":"January 23, 2022","format":false,"excerpt":"The first part of these courses was all about setting up an build and deploy pipeline so that we could automate building a helm chart and deploying to our cloud hosted environment at Okteto. In this course we're going to start adding more functionality so that we can demonstrate best\u2026","rel":"","context":"In &quot;Software Development&quot;","block_context":{"text":"Software Development","link":"https:\/\/bullyrooks.com\/index.php\/category\/software-development\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/bullyrooks.com\/wp-content\/uploads\/2022\/01\/image-38.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/bullyrooks.com\/wp-content\/uploads\/2022\/01\/image-38.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/bullyrooks.com\/wp-content\/uploads\/2022\/01\/image-38.png?resize=700%2C400&ssl=1 2x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/bullyrooks.com\/index.php\/wp-json\/wp\/v2\/posts\/821","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bullyrooks.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bullyrooks.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bullyrooks.com\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/bullyrooks.com\/index.php\/wp-json\/wp\/v2\/comments?post=821"}],"version-history":[{"count":3,"href":"https:\/\/bullyrooks.com\/index.php\/wp-json\/wp\/v2\/posts\/821\/revisions"}],"predecessor-version":[{"id":889,"href":"https:\/\/bullyrooks.com\/index.php\/wp-json\/wp\/v2\/posts\/821\/revisions\/889"}],"wp:attachment":[{"href":"https:\/\/bullyrooks.com\/index.php\/wp-json\/wp\/v2\/media?parent=821"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bullyrooks.com\/index.php\/wp-json\/wp\/v2\/categories?post=821"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bullyrooks.com\/index.php\/wp-json\/wp\/v2\/tags?post=821"},{"taxonomy":"course","embeddable":true,"href":"https:\/\/bullyrooks.com\/index.php\/wp-json\/wp\/v2\/course?post=821"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}