<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Bhargav M</title>
    <description>The latest articles on DEV Community by Bhargav M (@bhargavm).</description>
    <link>https://dev.to/bhargavm</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F202957%2Fa2c0516f-0088-44d0-b29f-2da2314df908.png</url>
      <title>DEV Community: Bhargav M</title>
      <link>https://dev.to/bhargavm</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/bhargavm"/>
    <language>en</language>
    <item>
      <title>Deploy Spring Boot App to GCP</title>
      <dc:creator>Bhargav M</dc:creator>
      <pubDate>Thu, 29 Aug 2019 17:34:04 +0000</pubDate>
      <link>https://dev.to/bhargavm/deploy-spring-boot-app-to-gcp-4hb2</link>
      <guid>https://dev.to/bhargavm/deploy-spring-boot-app-to-gcp-4hb2</guid>
      <description>&lt;p&gt;This blog mainly concentrates how to deploy &lt;a href="https://github.com/BhargavModepalli/Spring-Boot-Google-Cloud"&gt;spring boot app&lt;/a&gt; to GCP&lt;/p&gt;

&lt;p&gt;1)Write the docker file&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
2) Docker image building and testing locally
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt; docker image build  --tag="spring_boot_hello_world:0.0.1" .
 docker run -p 8080:8080 -t spring_boot_hello_world:0.0.1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;3) For those of us don't know GCP runs everything through as projects, so in order to start doing anything on GCP we need to have project.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt; gcloud projects list
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;If the projects are empty and you want to create one&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt; gcloud projects create example-spring-boot-project --name="first project"
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;creates a new project with name &lt;code&gt;first project&lt;/code&gt; and &lt;code&gt;example-spring-boot-project&lt;/code&gt; as project id.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gcloud config set project example-spring-boot-project 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;check whether spring-boot-example-project is set as default project in the workspace&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;NOTE: Step 4 is needed only for docker images from docker hub registry, not needed for google registry &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;4) This is an extra step if you are pulling your image from docker hub registry. You need to create a secret first with the docker hub and then use that secret whenever we want to pull the image from docker hub during deployments.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kubectl create secret docker-registry --docker-server=DOCKER_REGISTRY_SERVER --docker-username=DOCKER_USER --docker-password=DOCKER_PASSWORD --docker-email=DOCKER_EMAIL
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;or&lt;br&gt;
   Create your own secret by following steps in the &lt;a href="https://kubernetes.io/docs/concepts/containers/images/#specifying-imagepullsecrets-on-a-pod"&gt;link&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;5) Now we have all the environment set up to do the real work&lt;br&gt;
&lt;strong&gt;Deployment Controller:&lt;/strong&gt; Deployment Controller is the abstraction of pods and replica controller. It always tries to maintain the same no of replicas as mentioned in the manifest file.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
now we have the manifest file ready, lets go and execute
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  kubectl apply -f spring-boot-deployment.yaml. 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In order to verify it created pods or not we can do &lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  kubectl get pods
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;it will display the total pods with their status as Running&lt;/p&gt;

&lt;p&gt;6) &lt;strong&gt;Services:&lt;/strong&gt; Now Deployment Controller deployed our app in to desirable number of pods running at internal ip which can not be accessed by outside cluster.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How to we make the our service available over the internet?&lt;/strong&gt;&lt;br&gt;
The answer is Services which selects running pods based on labels and exposes them to the internet. &lt;code&gt;NodePort, Loadbalancer&lt;/code&gt; follow different mechanisms to expose it.            &lt;/p&gt;

&lt;p&gt;7) &lt;strong&gt;Node port Service&lt;/strong&gt;&lt;br&gt;
Node Port service is basically where a vm listens to a port and sends traffic to the target port where pod is listening&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
8) &lt;strong&gt;LoadBalancer Service&lt;/strong&gt;&lt;br&gt;
Loadbalancer is one way where you create a loadbalancer which has an external ip and listens to a port at that particular ip and which ever requests come throught that port it will forward to the imploicit port of the node port and from there it will be forwarded to the pod instances&lt;br&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kubectl apply -f spring-boot-loadbalancer-service.yaml
kubectl get svc
take the external ip of spring-boot-loadbalancer-service and hit our endpoint /time
bhargavryl@cloudshell:~ (example-spring-boot-project)$ curl http://35.226.27.116:80/time
Wed Aug 28 02:50:53 GMT 2019
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;credits:&lt;a class="comment-mentioned-user" href="https://dev.to/dariusx"&gt;@dariusx&lt;/a&gt;
&lt;/p&gt;

</description>
      <category>microservices</category>
      <category>springboot</category>
      <category>googlecloud</category>
      <category>gke</category>
    </item>
    <item>
      <title>Microservices code coverage with Jacoco</title>
      <dc:creator>Bhargav M</dc:creator>
      <pubDate>Mon, 19 Aug 2019 21:10:22 +0000</pubDate>
      <link>https://dev.to/bhargavm/microservices-code-coverage-with-jacoco-4a36</link>
      <guid>https://dev.to/bhargavm/microservices-code-coverage-with-jacoco-4a36</guid>
      <description>&lt;p&gt;Frequent deployments is a huge step in &lt;strong&gt;Microservices&lt;/strong&gt; architecture. If you have a suite of tests that run before every deployment and gives a green check mark to deploy with confidence. Should I trust the automated tests or not? one way to decide is by checking the code coverage. &lt;strong&gt;Jacoco&lt;/strong&gt;  helps to get code coverage details.&lt;/p&gt;

&lt;p&gt;Jacoco is a java library that you can use it in your application to get the code coverage details. Jacoco agent gets installed on your jvm and that agent listen to jmx metrics and spits the coverage details. The output can be a file or socket. We can configure the path to the file and when jvm starts it locks the file and and when jvm stops it write the execution data to the file &lt;code&gt;jacoco.exec&lt;/code&gt;. We can feed that file to static code analysis like Sonar and it will display the results or use jacoco reports plugin to generate reports. &lt;a href="https://www.jacoco.org/jacoco/trunk/doc/agent.html" rel="noopener noreferrer"&gt;https://www.jacoco.org/jacoco/trunk/doc/agent.html&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  The above diagram shows the followings steps
&lt;/h4&gt;

&lt;p&gt;1) Jenkins is running on a virtual machine which has its own jvm &lt;code&gt;JVM-A&lt;/code&gt; which builds the application and deploys to the container on a remote host.&lt;/p&gt;

&lt;p&gt;2) Once the app is running in a container on a virtual machine with jvm &lt;code&gt;JVM-B&lt;/code&gt; where jacoco agent needs to be installed and configured. Jacoco agent on &lt;code&gt;JVM-B&lt;/code&gt; spits out the execution data to a file &lt;code&gt;(jacoco.exec)&lt;/code&gt; as configured. &lt;/p&gt;

&lt;p&gt;3) Literally step 3 can be anything that understands the file generated in step2 and generates the reports. Sonar does that if we provide the path to the file. Maven has a plugin &lt;code&gt;jacoco:report&lt;/code&gt; which understands jacoco.exec file and generates some nice ui reports.&lt;/p&gt;

&lt;h4&gt;
  
  
  How to do it
&lt;/h4&gt;

&lt;p&gt;I have an example &lt;/p&gt;
&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev.to%2Fassets%2Fgithub-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/BhargavModepalli" rel="noopener noreferrer"&gt;
        BhargavModepalli
      &lt;/a&gt; / &lt;a href="https://github.com/BhargavModepalli/Spring-Boot-jacoco-Example" rel="noopener noreferrer"&gt;
        Spring-Boot-jacoco-Example
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      
    &lt;/h3&gt;
  &lt;/div&gt;
  &lt;div class="ltag-github-body"&gt;
    
&lt;div id="readme" class="md"&gt;
&lt;div class="markdown-heading"&gt;
&lt;h1 class="heading-element"&gt;Spring-Boot-Jacoco-Example&lt;/h1&gt;

&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;GET &lt;a href="http://localhost:8080/today" rel="nofollow noopener noreferrer"&gt;http://localhost:8080/today&lt;/a&gt; gives today date&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Following &lt;a href="https://www.jacoco.org/jacoco/trunk/doc/agent.html" rel="nofollow noopener noreferrer"&gt;https://www.jacoco.org/jacoco/trunk/doc/agent.html&lt;/a&gt; to set up jacoco codecoverage to write to a file&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;build &lt;a href="https://github.com/jacoco/jacoco/tree/master/org.jacoco.agent" rel="noopener noreferrer"&gt;https://github.com/jacoco/jacoco/tree/master/org.jacoco.agent&lt;/a&gt; to get &lt;code&gt;jacocoagent.jar&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;java -javaagent:C:\Bhargav\jacocoagent.jar=append=true,includes=*,output=file,destfile=C:\Workspace\Github\Spring-Boot-Jacoco-Example\target\jacoco.exec -jar  Spring-Boot-Jacoco-Example-0.0.1-SNAPSHOT.jar&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;



&lt;/div&gt;
&lt;br&gt;
  &lt;div class="gh-btn-container"&gt;&lt;a class="gh-btn" href="https://github.com/BhargavModepalli/Spring-Boot-jacoco-Example" rel="noopener noreferrer"&gt;View on GitHub&lt;/a&gt;&lt;/div&gt;
&lt;br&gt;
&lt;/div&gt;
&lt;br&gt;
which is simple rest service returns today's date with endpoint &lt;a href="http://localhost:8080/today" rel="noopener noreferrer"&gt;&lt;/a&gt;&lt;a href="http://localhost:8080/today" rel="noopener noreferrer"&gt;http://localhost:8080/today&lt;/a&gt; and run the following command

&lt;h4&gt;
  
  
  Generate the execution data file
&lt;/h4&gt;



&lt;p&gt;&lt;code&gt;java -javaagent:C:\Bhargav\jacocoagent.jar=append=true,includes=*,output=file,destfile=C:\Workspace\Github\Spring-Boot-Jacoco-Example\target\jacoco.exec -jar Spring-Boot-Jacoco-Example-0.0.1-SNAPSHOT.jar&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;javaagent:&lt;/strong&gt;C:\Bhargav\jacocoagent.jar: pointing to the location where jacocoagent jar is present.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;append :&lt;/strong&gt; telling jacocoagent whether to append new metrics with old metrics if there is any old metrics&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;include :&lt;/strong&gt; * means includes all classes, we can limit to a particular package&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;output :&lt;/strong&gt; telling jacocoagent where you want to write the execution data, in this case we are telling jacocoagent to write it to a file.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;destfile:&lt;/strong&gt; location of the file where jacocoagent writes the execution data&lt;/p&gt;

&lt;p&gt;The following file will get generated in the given destfile path of the above command.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fi6surq5u8t89txa5refi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fi6surq5u8t89txa5refi.png" alt="Add descriptionexecution data generated by jacoco agent"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Generate reports from the execution data file
&lt;/h4&gt;

&lt;p&gt;Inorder make this file readable we can use a maven plugin called &lt;code&gt;jacoco-maven-plugin&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;br&gt;
&amp;lt;plugin&amp;gt;&lt;br&gt;
                    &amp;lt;groupId&amp;gt;org.jacoco&amp;lt;/groupId&amp;gt;&lt;br&gt;
                    &amp;lt;artifactId&amp;gt;jacoco-maven-plugin&amp;lt;/artifactId&amp;gt;&lt;br&gt;
                    &amp;lt;version&amp;gt;0.8.3&amp;lt;/version&amp;gt;&lt;br&gt;
                    &amp;lt;executions&amp;gt;&lt;br&gt;
                        &amp;lt;execution&amp;gt;&lt;br&gt;
                            &amp;lt;goals&amp;gt;&lt;br&gt;
                                &amp;lt;goal&amp;gt;report&amp;lt;/goal&amp;gt;&lt;br&gt;
                            &amp;lt;/goals&amp;gt;&lt;br&gt;
                            &amp;lt;configuration&amp;gt;&lt;br&gt;
                                &amp;lt;dataFile&amp;gt;${project.build.directory}/jacoco.exec&amp;lt;/dataFile&amp;gt;&lt;br&gt;
                                &amp;lt;outputDirectory&amp;gt;${basedir}/target/jacoco&amp;lt;/outputDirectory&amp;gt;&lt;br&gt;
                            &amp;lt;/configuration&amp;gt;&lt;br&gt;
                        &amp;lt;/execution&amp;gt;&lt;br&gt;
                    &amp;lt;/executions&amp;gt;&lt;br&gt;
&amp;lt;/plugin&amp;gt;&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;running it from the cmd&lt;/p&gt;

&lt;p&gt;&lt;code&gt;mvn org.jacoco:jacoco-maven-plugin:0.8.3:report&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fg08oz7hd0gkcqc3un4qq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fg08oz7hd0gkcqc3un4qq.png" alt="Add descriptionindex file&amp;lt;br&amp;gt;
"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fg08oz7hd0gkcqc3un4qq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fg08oz7hd0gkcqc3un4qq.png" alt="Add descriptionactual class with code coverage details"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>microservices</category>
      <category>java</category>
      <category>jacoco</category>
    </item>
  </channel>
</rss>
