<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Springboot &#8211; Best DevOps</title>
	<atom:link href="https://www.bestdevops.com/tag/springboot/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.bestdevops.com</link>
	<description>Lets Learn, Do it &#38; Share! Thats a Best DevOps!!!</description>
	<lastBuildDate>Fri, 09 May 2025 11:58:57 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.1.3</generator>
	<item>
		<title>Spring Framework: Architecture, Use Cases, and Getting Started Guide</title>
		<link>https://www.bestdevops.com/spring-framework-architecture-use-cases-and-getting-started-guide/</link>
					<comments>https://www.bestdevops.com/spring-framework-architecture-use-cases-and-getting-started-guide/#respond</comments>
		
		<dc:creator><![CDATA[vijay1 vijay1]]></dc:creator>
		<pubDate>Fri, 09 May 2025 11:58:53 +0000</pubDate>
				<category><![CDATA[DevOps]]></category>
		<category><![CDATA[Getting]]></category>
		<category><![CDATA[JavaDevelopment]]></category>
		<category><![CDATA[SpringArchitecture]]></category>
		<category><![CDATA[Springboot]]></category>
		<category><![CDATA[SpringFramework]]></category>
		<category><![CDATA[StartedWithSpring]]></category>
		<guid isPermaLink="false">https://www.bestdevops.com/?p=31078</guid>

					<description><![CDATA[What is Spring? Spring is an open-source, comprehensive framework for building Java-based applications, primarily focused on simplifying the development of [&#8230;]]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-image size-full is-resized"><img fetchpriority="high" decoding="async" width="520" height="277" src="https://www.bestdevops.com/wp-content/uploads/2025/05/image-24.png" alt="" class="wp-image-31085" style="width:837px;height:auto" srcset="https://www.bestdevops.com/wp-content/uploads/2025/05/image-24.png 520w, https://www.bestdevops.com/wp-content/uploads/2025/05/image-24-300x160.png 300w" sizes="(max-width: 520px) 100vw, 520px" /></figure>



<h3 class="wp-block-heading">What is Spring?</h3>



<p class="wp-block-paragraph"><strong>Spring</strong> is an open-source, comprehensive framework for building Java-based applications, primarily focused on simplifying the development of enterprise-level applications. Originally created by <strong>Rod Johnson</strong> in 2002, Spring was developed to address the complexity of enterprise applications, especially in the context of Java&#8217;s Enterprise Edition (JEE). Over time, Spring evolved into a popular framework with a wide range of projects that facilitate different aspects of application development.</p>



<p class="wp-block-paragraph">The core principles of Spring include <strong>dependency injection (DI)</strong>, <strong>aspect-oriented programming (AOP)</strong>, and <strong>modularity</strong>, which together promote clean, maintainable, and loosely-coupled code. <strong>Spring Framework</strong> serves as the foundation for many projects like <strong>Spring Boot</strong>, <strong>Spring Cloud</strong>, <strong>Spring Security</strong>, and <strong>Spring Data</strong>.</p>



<h3 class="wp-block-heading">Core Features of Spring:</h3>



<ul class="wp-block-list">
<li><strong>Inversion of Control (IoC)</strong>: Through dependency injection, Spring promotes loose coupling between components.</li>



<li><strong>Aspect-Oriented Programming (AOP)</strong>: Helps separate cross-cutting concerns like logging, security, and transactions from business logic.</li>



<li><strong>Comprehensive Data Access</strong>: Spring integrates with various data access technologies, including JDBC, JPA, Hibernate, and more.</li>



<li><strong>Transaction Management</strong>: Spring provides a consistent and declarative approach to transaction management.</li>



<li><strong>Security</strong>: Spring Security offers a robust authentication and authorization framework for applications.</li>
</ul>



<h3 class="wp-block-heading">What are the Major Use Cases of Spring?</h3>



<p class="wp-block-paragraph">Spring&#8217;s versatility makes it suitable for a wide range of applications, from simple web apps to complex enterprise solutions. Below are some of the key use cases of Spring:</p>



<h4 class="wp-block-heading">1. <strong>Enterprise-Level Applications</strong></h4>



<p class="wp-block-paragraph">Spring is widely used to build large-scale, mission-critical enterprise applications, thanks to its modularity, scalability, and support for integrating with different enterprise services.</p>



<p class="wp-block-paragraph"><strong>Example</strong>: Large financial systems, customer relationship management (CRM) platforms, and enterprise resource planning (ERP) systems.</p>



<h4 class="wp-block-heading">2. <strong>Web Development</strong></h4>



<p class="wp-block-paragraph">Spring&#8217;s web module, particularly <strong>Spring MVC</strong> (Model-View-Controller), is widely used to build web applications. It provides powerful capabilities for request handling, response management, and page rendering.</p>



<p class="wp-block-paragraph"><strong>Example</strong>: E-commerce websites, content management systems, and social media platforms.</p>



<h4 class="wp-block-heading">3. <strong>Microservices Architecture</strong></h4>



<p class="wp-block-paragraph"><strong>Spring Boot</strong> and <strong>Spring Cloud</strong> have become the go-to tools for building and managing microservices. Spring simplifies the creation of stand-alone, production-grade microservices with minimal configuration.</p>



<p class="wp-block-paragraph"><strong>Example</strong>: Building microservices-based applications for large systems that require independent deployment, scalability, and fault tolerance.</p>



<h4 class="wp-block-heading">4. <strong>RESTful Web Services</strong></h4>



<p class="wp-block-paragraph">Spring is widely used to create <strong>RESTful web services</strong>. It supports building APIs that are stateless, lightweight, and maintainable.</p>



<p class="wp-block-paragraph"><strong>Example</strong>: Building backend services for mobile applications, third-party integrations, and data processing.</p>



<h4 class="wp-block-heading">5. <strong>Data Access Layer</strong></h4>



<p class="wp-block-paragraph">Spring provides extensive support for integrating with <strong>databases</strong> and <strong>NoSQL</strong> systems, including <strong>JPA</strong>, <strong>Hibernate</strong>, and <strong>MongoDB</strong>. It abstracts the complexity of working with data and provides a consistent interface for database operations.</p>



<p class="wp-block-paragraph"><strong>Example</strong>: Database-backed applications, data-heavy enterprise systems, and applications requiring complex data processing.</p>



<h4 class="wp-block-heading">6. <strong>Cloud-Based Applications</strong></h4>



<p class="wp-block-paragraph">With <strong>Spring Cloud</strong>, developers can build robust cloud-based applications with microservices architectures that can handle things like distributed tracing, configuration management, service discovery, and more.</p>



<p class="wp-block-paragraph"><strong>Example</strong>: Building cloud-native applications using platforms like <strong>AWS</strong>, <strong>Azure</strong>, or <strong>Google Cloud</strong>.</p>



<h3 class="wp-block-heading">How Spring Works: Architecture Overview</h3>



<figure class="wp-block-image size-full is-resized"><img decoding="async" width="600" height="400" src="https://www.bestdevops.com/wp-content/uploads/2025/05/image-25.png" alt="" class="wp-image-31086" style="width:830px;height:auto" srcset="https://www.bestdevops.com/wp-content/uploads/2025/05/image-25.png 600w, https://www.bestdevops.com/wp-content/uploads/2025/05/image-25-300x200.png 300w" sizes="(max-width: 600px) 100vw, 600px" /></figure>



<p class="wp-block-paragraph">Spring follows a <strong>modular architecture</strong> that allows developers to use only the parts of the framework they need. The core of Spring is centered around <strong>Inversion of Control (IoC)</strong> and <strong>Dependency Injection (DI)</strong>, which help create loosely coupled, testable, and maintainable code.</p>



<h4 class="wp-block-heading">1. <strong>Core Container</strong></h4>



<p class="wp-block-paragraph">The <strong>Core Container</strong> is the central part of Spring and is composed of several modules that handle different responsibilities:</p>



<ul class="wp-block-list">
<li><strong>Core</strong>: The fundamental building blocks of the Spring Framework, including the IoC container.</li>



<li><strong>Beans</strong>: Manages the lifecycle and configuration of beans (objects).</li>



<li><strong>Context</strong>: Provides a framework for accessing application configuration and context.</li>



<li><strong>Expression Language (SpEL)</strong>: Allows for dynamic evaluation of expressions in Spring configuration.</li>
</ul>



<h4 class="wp-block-heading">2. <strong>Inversion of Control (IoC) and Dependency Injection (DI)</strong></h4>



<ul class="wp-block-list">
<li><strong>IoC</strong> is a core principle of Spring, allowing objects to be created and managed by the Spring container. Dependency Injection (DI) is one of the most commonly used techniques in IoC, where dependencies (other objects) are injected into a class rather than the class creating them itself.</li>



<li>This promotes loose coupling, making the application more modular and easier to test.</li>
</ul>



<p class="wp-block-paragraph"><strong>Example</strong>:</p>


<pre class="wp-block-code"><span><code class="hljs language-php">@Component
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ServiceA</span> </span>{
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">final</span> ServiceB serviceB;

    @Autowired
    <span class="hljs-keyword">public</span> ServiceA(ServiceB serviceB) {
        this.serviceB = serviceB;
    }
}
</code></span></pre>


<p class="wp-block-paragraph">Here, <code>ServiceA</code> depends on <code>ServiceB</code>, and Spring automatically injects <code>ServiceB</code> into <code>ServiceA</code> via the constructor.</p>



<h4 class="wp-block-heading">3. <strong>Spring AOP (Aspect-Oriented Programming)</strong></h4>



<ul class="wp-block-list">
<li>Spring supports <strong>Aspect-Oriented Programming (AOP)</strong>, which allows developers to define cross-cutting concerns like logging, security, and transaction management separately from the business logic.</li>



<li>AOP enables the application of aspects (like logging or security) to specific methods or classes, without altering the actual code.</li>
</ul>



<p class="wp-block-paragraph"><strong>Example</strong>:</p>


<pre class="wp-block-code"><span><code class="hljs language-php">@Aspect
@Component
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">LoggingAspect</span> </span>{
    @Before(<span class="hljs-string">"execution(* com.example.service.*.*(..))"</span>)
    <span class="hljs-keyword">public</span> void logMethodCall(JoinPoint joinPoint) {
        System.out.println(<span class="hljs-string">"Method called: "</span> + joinPoint.getSignature().getName());
    }
}
</code></span></pre>


<h4 class="wp-block-heading">4. <strong>Spring MVC (Model-View-Controller)</strong></h4>



<ul class="wp-block-list">
<li><strong>Spring MVC</strong> is a web framework built on the principles of the <strong>Model-View-Controller</strong> design pattern. It separates the business logic (Model), the presentation (View), and the request handling (Controller).</li>



<li>It is highly configurable and works well with RESTful web services and other HTTP-based applications.</li>
</ul>



<p class="wp-block-paragraph"><strong>Example</strong>: A controller method that handles HTTP GET requests and returns a view:</p>


<pre class="wp-block-code"><span><code class="hljs language-php">@Controller
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">HomeController</span> </span>{
    @GetMapping(<span class="hljs-string">"/home"</span>)
    <span class="hljs-keyword">public</span> String homePage() {
        <span class="hljs-keyword">return</span> <span class="hljs-string">"home"</span>; <span class="hljs-comment">// Return view name</span>
    }
}
</code></span></pre>


<h4 class="wp-block-heading">5. <strong>Spring Boot</strong></h4>



<ul class="wp-block-list">
<li><strong>Spring Boot</strong> simplifies the setup and configuration of Spring applications. It helps developers create stand-alone applications that are production-ready with minimal configuration.</li>



<li>It comes with an embedded <strong>Tomcat</strong> server, which allows applications to be run directly without requiring an external servlet container.</li>
</ul>



<p class="wp-block-paragraph"><strong>Example</strong>: A basic Spring Boot application:</p>


<pre class="wp-block-code"><span><code class="hljs language-php">@SpringBootApplication
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyApplication</span> </span>{
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> void main(String&#91;] args) {
        SpringApplication.run(MyApplication<span class="hljs-class">.<span class="hljs-keyword">class</span>, <span class="hljs-title">args</span>);
    }
}
</span></code></span></pre>


<h4 class="wp-block-heading">6. <strong>Spring Security</strong></h4>



<ul class="wp-block-list">
<li><strong>Spring Security</strong> provides comprehensive authentication and authorization support for Spring applications. It can secure applications with various mechanisms, including form-based login, OAuth, LDAP, and more.</li>
</ul>



<p class="wp-block-paragraph"><strong>Example</strong>: A simple configuration for securing a web application:</p>


<pre class="wp-block-code"><span><code class="hljs language-php">@Configuration
@EnableWebSecurity
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">SecurityConfig</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">WebSecurityConfigurerAdapter</span> </span>{
    @Override
    <span class="hljs-keyword">protected</span> void configure(HttpSecurity http) throws <span class="hljs-keyword">Exception</span> {
        http.authorizeRequests()
            .antMatchers(<span class="hljs-string">"/admin/**"</span>).hasRole(<span class="hljs-string">"ADMIN"</span>)
            .anyRequest().authenticated()
            .<span class="hljs-keyword">and</span>().formLogin();
    }
}
</code></span></pre>


<h3 class="wp-block-heading">Basic Workflow of Spring</h3>



<ol class="wp-block-list">
<li><strong>Application Context Setup</strong>: When a Spring application starts, the Spring <strong>ApplicationContext</strong> is created. It acts as a container for managing beans (objects) and their dependencies.</li>



<li><strong>Bean Creation</strong>: Spring uses the IoC container to create objects (beans) and inject their dependencies. Beans are managed by Spring and are instantiated when needed.</li>



<li><strong>Request Handling (for Web Apps)</strong>: In the case of a web application, Spring&#8217;s <strong>DispatcherServlet</strong> routes incoming HTTP requests to the appropriate controller based on the URL and HTTP method.</li>



<li><strong>Controller Execution</strong>: The controller processes the request, interacts with the model (database or business logic), and returns a view or data to the user.</li>



<li><strong>View Rendering</strong>: In MVC-based applications, the view is rendered based on the model data returned by the controller, often using a templating engine like <strong>Thymeleaf</strong> or <strong>JSP</strong>.</li>
</ol>



<h3 class="wp-block-heading">Step-by-Step Guide to Getting Started with Spring</h3>



<p class="wp-block-paragraph">To get started with Spring, follow these steps:</p>



<h4 class="wp-block-heading">Step 1: <strong>Install Prerequisites</strong></h4>



<ul class="wp-block-list">
<li><strong>Java</strong>: Make sure you have JDK 8 or higher installed.</li>



<li><strong>Maven</strong> or <strong>Gradle</strong>: These are the build tools that manage dependencies in Spring projects.</li>
</ul>



<h4 class="wp-block-heading">Step 2: <strong>Create a Spring Project</strong></h4>



<ul class="wp-block-list">
<li>You can use the <strong>Spring Initializr</strong> (<a href="https://start.spring.io/">https://start.spring.io/</a>) to generate a Spring Boot project. This tool allows you to choose dependencies like <strong>Spring Web</strong>, <strong>Spring Data JPA</strong>, and <strong>Spring Security</strong> and creates the project for you.</li>
</ul>



<h4 class="wp-block-heading">Step 3: <strong>Set Up Your IDE</strong></h4>



<ul class="wp-block-list">
<li>Import the generated project into your Integrated Development Environment (IDE) such as <strong>IntelliJ IDEA</strong> or <strong>Eclipse</strong>.</li>
</ul>



<h4 class="wp-block-heading">Step 4: <strong>Build Your First Spring Bean</strong></h4>



<ul class="wp-block-list">
<li>Create a simple Java class and annotate it with <code>@Component</code> or another suitable annotation like <code>@Service</code>, <code>@Repository</code>, or <code>@Controller</code>.</li>
</ul>



<h4 class="wp-block-heading">Step 5: <strong>Create a Controller and View</strong></h4>



<ul class="wp-block-list">
<li>Define a controller to handle HTTP requests and return a view or data. Use the <code>@GetMapping</code> or <code>@PostMapping</code> annotations for request mappings.</li>
</ul>



<h4 class="wp-block-heading">Step 6: <strong>Run the Application</strong></h4>



<ul class="wp-block-list">
<li>Run the application using <strong>Spring Boot&#8217;s</strong> embedded server. You can use the command line or run it directly from your IDE.</li>
</ul>



<p class="wp-block-paragraph"></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.bestdevops.com/spring-framework-architecture-use-cases-and-getting-started-guide/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Java in the Cloud: Continuous Integration Setup Tutorial</title>
		<link>https://www.bestdevops.com/java-in-the-cloud-continuous-integration-setup-tutorial/</link>
					<comments>https://www.bestdevops.com/java-in-the-cloud-continuous-integration-setup-tutorial/#respond</comments>
		
		<dc:creator><![CDATA[anil]]></dc:creator>
		<pubDate>Thu, 09 Feb 2017 06:57:21 +0000</pubDate>
				<category><![CDATA[Cloud Computing]]></category>
		<category><![CDATA[Continuous Integration]]></category>
		<category><![CDATA[DevOps Tools]]></category>
		<category><![CDATA[AWS]]></category>
		<category><![CDATA[CI]]></category>
		<category><![CDATA[Cloud]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Jenkins]]></category>
		<category><![CDATA[Springboot]]></category>
		<category><![CDATA[tutorial]]></category>
		<guid isPermaLink="false">http://www.bestdevops.com/?p=557</guid>

					<description><![CDATA[Source:- whatmatrix.com Year after year, we bear witness to the increasingly rapid evolution of the IT industry. It has now [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>Source:- whatmatrix.com</p>
<p>Year after year, we bear witness to the increasingly rapid evolution of the IT industry. It has now been more than two decades since the groundbreaking slogan ”Write once, run anywhere“ set a whole new level of expectation for the software development community. And here we are today, with a resulting, ever-expanding set of tools that have collectively taken Java development in particular, and software development in general, to a whole new universe of possibility.</p>
<p>Methodologies such as Agile, <a href="http://www.scmgalaxy.com/courses/agenda/devops/devops-training-course-online.html">DevOps</a>, and Continuous Integration and Deployment – along with the evolution of microservices – have collectively boosted software development process productivity to a point where it is a pleasure to be developing software more than ever before.</p>
<p>Utilizing automation and setting up the correct set of tools can make the development and delivery of software products surprisingly painless.</p>
<p>This article will take a look at this new universe from the perspective of a Java developer who crosses into DevOps and searches to optimize product development and delivery to its maximum.</p>
<p>Today, terms such as Spring Boot, <a href="http://www.scmgalaxy.com/courses/agenda/virtulization-and-container-tools/docker-training.html">Docker</a>, <a href="http://www.scmgalaxy.com/courses/agenda/virtulization-and-container-tools/cloud-computing-essentials.html">Cloud</a>, <a href="http://www.scmgalaxy.com/courses/agenda/virtulization-and-container-tools/amazon-aws-training.html">Amazon Web Services</a>, Continuous Delivery are widely used but less widely understood. This article will take the easiest route possible to present all these technologies and explain these terms, and wrap it up in the form of a tutorial where we will develop a small piece of software and prepare it for production delivery using all the mentioned tools.</p>
<p><img decoding="async" class="alignnone  wp-image-558" src="http://www.bestdevops.com/wp-content/uploads/2017/02/devops-tools-1-300x300.jpg" alt="" width="350" height="350" srcset="https://www.bestdevops.com/wp-content/uploads/2017/02/devops-tools-1-300x300.jpg 300w, https://www.bestdevops.com/wp-content/uploads/2017/02/devops-tools-1-150x150.jpg 150w, https://www.bestdevops.com/wp-content/uploads/2017/02/devops-tools-1.jpg 640w" sizes="(max-width: 350px) 100vw, 350px" /></p>
<h5><strong>Why These Tools?</strong></h5>
<h5>
<strong>Simplify Deployments with Docker</strong></h5>
<p>”Write once, run anywhere“ was the conceptual breakthrough that yielded technologies like the Java Virtual Machine (JVM) which enabled your code to run anywhere. And now here we are, a couple of decades later, with something called Docker being presented to the IT community. Docker is a containment tool in which you can place your software and run it painlessly, almost anywhere you wish.</p>
<p>However, a Java developer may look at Docker and say “Why would we need that, we already have the JVM, which is well recognized as the master portable solution.” But is it?</p>
<p>”Write once, run anywhere“ sounds nice, and plays out well… at least most of the time. Until you encounter multiple JVM vendors, multiple Java versions, multiple operating systems, and various permutations and combinations of all the above. You then find yourself switching from the elegant ”Write once, run anywhere“ paradigm to the counter-productive ”Write once, debug everywhere“ pitfall.</p>
<p>And that’s where Docker comes in to help save the day.</p>
<p>Docker simplifies development, testing, and shipping of software. If you have the software you want to test, put it in the Docker container, and it will run and be painless to install for all the parties involved.</p>
<h5 id="speed-up-development-with-spring-boot">Speed Up Development with Spring Boot</h5>
<p>Less than a decade after the “run anywhere” slogan was introduced, the Spring framework appeared on the scene. Today, the Spring ecosystem continues to flourish, and has produced lots of valuable Spring-based projects, perhaps most notably Spring Boot. As stated on the Spring Boot website:</p>
<blockquote><p>Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can “just run”.</p></blockquote>
<p>Spring Boot enables you to bring the application up and running in a matter of minutes. Software developers can focus on software development and then can benefit from a tool that does all the configuration for them.</p>
<p>In this tutorial, we will use Spring Boot to develop our microservice.</p>
<h5><strong>Continuous Integration (CI) with <a href="http://www.scmgalaxy.com/courses/agenda/continous-integration-tools/jenkins-training.html">Jenkins</a></strong></h5>
<p>DevOps is a rapidly growing movement that is tightly integrating software development and systems administration teams, with the goal of making a software development and delivery lifecycle as painless, seamless, and productive as possible for all parties involved: developers, sysadmins, testers, and ultimately, end users.</p>
<p>Continuous integration (CI), is one of the cornerstones of the DevOps revolution. The idea is that whenever a developer commits code to the code repository, it is automatically tested and packaged for delivery (deployment) to production.</p>
<p>CI goes hand-in-hand with:</p>
<ul>
<li>Continuous Delivery – Automatic delivery of the package prepared for end user business testing with manual trigger to production deployment.</li>
<li>Continuous Deployment – Automatic deployment of the packaged product directly to production.</li>
</ul>
<p>More than a few tools exist which can be utilized for implementing the CI process. One of the most popular is Jenkins, an open source CI tool. With more than a thousand plugins and a huge community behind it, Jenkins is an easy choice when starting to think about implementing continuous integration, delivery, or deployment.</p>
<p>In our tutorial, Jenkins will be used to deliver our product to the cloud, more specifically, the Amazon (AWS) cloud.</p>
<h5><strong>Cloud Computing with AWS</strong></h5>
<p>If you have some sysadmin experience, imagine removing some of the worries of system administration from your shoulders. You have a few applications; you have an idea of how much resource they will require, but you don’t know exactly the hardware sizing you will need. You make the estimation, the resources are bought, and the system goes to production. If you are lucky, you will find that you overestimated and have more resources than you need. But given Murphy’s Law, you will more likely find that you underestimated resource requirements and end up scrambling to get a bit more memory or processing power under tremendous time pressure. In contrast, if you’re deploying to the cloud, you simply put your system out there and size it as needed, with the flexibility offered by the cloud providers. With the cloud, you neither need to worry about running out of system resources, nor do you need to worry about having 90 percent of your memory or CPU sitting idle.</p>
<p>Of course, there is the challenge of deciding which provider to choose. Cloud wars are still in progress. Clash of Microsoft, Amazon and Google for the future of computing is an example title you can find lately in tech-world news. For this blog, I have chosen Amazon Web Services (AWS), largely based on its current popularity and market share.</p>
<p>One of the advantages of AWS is that Amazon offers lots of services after you sign up</p>
<p><img loading="lazy" decoding="async" class="alignnone  wp-image-560" src="http://www.bestdevops.com/wp-content/uploads/2017/02/amazon-web-services-bestdevops-1-300x212.jpg" alt="" width="383" height="271" srcset="https://www.bestdevops.com/wp-content/uploads/2017/02/amazon-web-services-bestdevops-1-300x212.jpg 300w, https://www.bestdevops.com/wp-content/uploads/2017/02/amazon-web-services-bestdevops-1-100x70.jpg 100w, https://www.bestdevops.com/wp-content/uploads/2017/02/amazon-web-services-bestdevops-1.jpg 618w" sizes="auto, (max-width: 383px) 100vw, 383px" /></p>
<p>In this tutorial we will use the following two AWS services: Elastic Compute Cloud EC2 (more specifically, Amazon EC2 Container Registry, or Amazon ECR), and Amazon S3 (Simple Storage Services).</p>
<h5 id="amazon-ecr"><strong>Amazon ECR</strong></h5>
<p>We will need to store our Docker images somewhere. Amazon ECR is a managed AWS Docker registry service. As stated on the Amazon ECR web site:</p>
<blockquote><p>…makes it easy for developers to store, manage, and deploy Docker container images. Amazon ECR is integrated with Amazon EC2 Container Service (ECS), simplifying your development to production workflow. Amazon ECR eliminates the need to operate your own container repositories or worry about scaling the underlying infrastructure.</p></blockquote>
<h5 id="amazon-s3">Amazon S3</h5>
<p>As mentioned, the application we develop will be a Spring Boot microservice that will be uploading files to Amazon S3. As stated on the Amazon S3 web site:</p>
<blockquote><p>…provides developers and IT teams with secure, durable, highly-scalable cloud storage. Amazon S3 is easy to use object storage, with a simple web service interface to store and retrieve any amount of data from anywhere on the web.</p></blockquote>
<h5 id="a-practical-how-to-tutorial">A Practical “How To” Tutorial</h5>
<p>The goal is to prepare for deployment a Spring Boot microservice which will upload files to Amazon S3. Steps are the following:</p>
<ul>
<li>Develop the microservice</li>
<li>Define the build process in which the service will be dockerized</li>
<li>Use <a href="http://www.scmgalaxy.com/courses/agenda/source-code-management-tools/bitbucket-administrator-training.html">Bitbucket </a>for hosting the Git code repository</li>
<li>Integrate Bitbucket with Jenkins to package the application using Gradle</li>
<li>Push it to a remote Amazon ECR</li>
</ul>
<p>What follows is a tutorial for setting up all the needed components:</p>
<ul>
<li>Spring Boot example application – microservice packaged and dockerized using Gradle</li>
<li>Jenkins installation on a fresh Ubuntu server</li>
<li>Bitbucket integration with Jenkins via webhook</li>
<li>Jenkins job configuration</li>
<li>Amazon ECR to store the Docker images containing our application</li>
</ul>
<h5 id="prerequisites">Prerequisites</h5>
<p>To be able to use AWS cloud resources, we need to register at Amazon first. By registering, we will get an account with immediate Free Tier usage benefits, for purposes of enabling hands-on experience during the 12 months following registration.</p>
<p>As mentioned, in this tutorial we will use Amazon S3 and Amazon ECR. For both, we will need access keys to connect to the services.</p>
<p>After signing up with AWS, we go to our account <em>Security credentials</em>, where we choose <em>Access keys</em> and click on “Create New Access Key”. After clicking, a key is generated along with its ID. You need to store this somewhere safe, as we will use it later when configuring AWS Jenkins integration and developing our S3 file upload.</p>
<p><img loading="lazy" decoding="async" class="alignnone  wp-image-561" src="http://www.bestdevops.com/wp-content/uploads/2017/02/aws-security-credentials-300x261.jpg" alt="" width="408" height="355" srcset="https://www.bestdevops.com/wp-content/uploads/2017/02/aws-security-credentials-300x261.jpg 300w, https://www.bestdevops.com/wp-content/uploads/2017/02/aws-security-credentials.jpg 467w" sizes="auto, (max-width: 408px) 100vw, 408px" /></p>
<p>The next prerequisite is that we need an Amazon S3 bucket (storage container). Our Spring Boot Service will be uploading and downloading files to and from Amazon S3 storage. Bucket creation is simple enough and just requires few clicks. A full description of how to do it is provided in the Create a Bucket documentation.</p>
<p>We will also be using Bitbucket for hosting our code and triggering requests to Jenkins, so a Bitbucket account is needed as well. Bitbucket is a great option for developers, with one of its main benefits being the unlimited amount of private repositories you can create.</p>
<h5 id="application-development">Application Development</h5>
<p>Rather than getting into all the details of Spring annotations and how they work, I will instead focus, from a pure developer perspective, on the more challenging part of the whole setup; namely, installing and configuring Linux, Jenkins, and other tools needed for CI. All the code examples used in this tutorial, including the Spring Boot microservice application, are available on the Bickbucket repository for the project.</p>
<p>Our application composition is simple. We have a Spring Boot application entry point in our <code>StorageWebserviceApplication.java</code> file. The logic for uploading and downloading files is in <code>StorageService.java</code>. <code>StorageController.java</code> is a Rest controller, containing API endpoints used for file upload and download. Here is the project hierarchy:</p>
<p><img loading="lazy" decoding="async" class="alignnone  wp-image-562" src="http://www.bestdevops.com/wp-content/uploads/2017/02/storagewebservice-236x300.png" alt="" width="247" height="314" srcset="https://www.bestdevops.com/wp-content/uploads/2017/02/storagewebservice-236x300.png 236w, https://www.bestdevops.com/wp-content/uploads/2017/02/storagewebservice.png 300w" sizes="auto, (max-width: 247px) 100vw, 247px" /></p>
<p>We have chosen Gradle as a build tool, and it will package our application and compose the Docker image. So next, we will discuss the Gradle build file, service component, and the Dockerfile.</p>
<p>To be able to use AWS API, we need to include dependencies in our build file, as defined in the AWS documentation for using Gradle.</p>
<p>Summed up, the AWS dependency configuration part of our Gradle script will look like the following:</p>
<pre><code>buildscript {
   	   	...
   	repositories {
         	mavenCentral()
   	}
   	dependencies {
...
         	classpath("io.spring.gradle:dependency-management-plugin:0.5.4.RELEASE")
   	}
}
..
apply plugin: "io.spring.dependency-management"
 
dependencyManagement {
	imports {
    	mavenBom ('com.amazonaws:aws-java-sdk-bom:1.10.47')
	}
}
 
dependencies {
   	..
   	compile ('com.amazonaws:aws-java-sdk-s3')
}
</code></pre>
<p>As stated previously, when uploading files to Amazon S3, we do so by uploading files to an S3 <em>bucket</em>.</p>
<p>To connect to the bucket, our Amazon S3 client needs to have credentials provided. Credentials are the access keys we created earlier. We define the access key ID and value in the <code>application.properties</code> file; we have named our bucket <code>toptal-s3-example</code>.</p>
<p><img loading="lazy" decoding="async" class="alignnone  wp-image-563" src="http://www.bestdevops.com/wp-content/uploads/2017/02/amazon-s3-client-300x73.png" alt="" width="383" height="93" srcset="https://www.bestdevops.com/wp-content/uploads/2017/02/amazon-s3-client-300x73.png 300w, https://www.bestdevops.com/wp-content/uploads/2017/02/amazon-s3-client.png 425w" sizes="auto, (max-width: 383px) 100vw, 383px" /></p>
<p>Our main service component is now as follows:</p>
<pre><code class="language-java hljs"><span class="hljs-annotation">@Service</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">StorageService</span> {</span>
 
  	<span class="hljs-annotation">@Value</span>(<span class="hljs-string">"${aws.accesKeyId}"</span>)
  	<span class="hljs-keyword">private</span> String awsAccessKeyId;
 
  	<span class="hljs-annotation">@Value</span>(<span class="hljs-string">"${aws.secretKey}"</span>)
  	<span class="hljs-keyword">private</span> String awsSecretKey;
 
  	<span class="hljs-annotation">@Value</span>(<span class="hljs-string">"${aws.bucketName}"</span>)
  	<span class="hljs-keyword">private</span> String awsBucketName;
  	
  	 <span class="hljs-keyword">private</span> AWSCredentials credentials;
  	 <span class="hljs-keyword">private</span> AmazonS3 s3client;;
 
  	<span class="hljs-annotation">@PostConstruct</span>
	<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">init</span>(){
        	 credentials = <span class="hljs-keyword">new</span> BasicAWSCredentials(awsAccessKeyId, awsSecretKey);
        	 s3client = <span class="hljs-keyword">new</span> AmazonS3Client(credentials);
	}
 
 
  	<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">uploadFile</span>(MultipartFile file) <span class="hljs-keyword">throws</span> IOException {
        	File fileForUpload = transformMultipartToFile(file);
        	s3client.putObject(<span class="hljs-keyword">new</span> PutObjectRequest(awsBucketName, file.getOriginalFilename(), fileForUpload));
  	}
  	
  	<span class="hljs-keyword">public</span> InputStream <span class="hljs-title">downloadFile</span>(String amazonFileKey) <span class="hljs-keyword">throws</span> IOException {
        	S3Object fetchFile = s3client.getObject(<span class="hljs-keyword">new</span> GetObjectRequest(awsBucketName, amazonFileKey));
        	InputStream objectData = fetchFile.getObjectContent();
        	<span class="hljs-keyword">return</span> objectData;
  	}
…
</code></pre>
<p><code>StorageService</code> reads the credentials from the <code>application.properties</code> file and uses them to instantiate <code>BasicAWSCredentials</code> object, and subsequently the <code>AmazonS3Client</code> object. What follows is a simple matter of invoking <code>putObject</code> for file upload and <code>getObject</code> for file download, on the Amazon S3 client object.</p>
<p>We will run the service inside a Docker container and, during the Gradle build process, we will build the Docker image. We will do this by additionally configuring the <code>build.gradle</code> file, as follows:</p>
<pre><code>buildscript {
   	...
   	dependencies {
         	...
         	classpath('se.transmode.gradle:gradle-docker:1.2')
   	}
}
 
.....
apply plugin: 'docker'
...
task buildDocker(type: Docker, dependsOn: build) {
 push = false
 applicationName = "storageservice"
 dockerfile = file('src/main/docker/Dockerfile')
 doFirst {
   	copy {
   	from jar
   	into stageDir
   	}
 }
}
</code></pre>
<p>The <code>Buildscript</code> part and <code>apply plugin</code> are pretty standard. We have also defined a <code>buildDocker</code> task which reads the Docker configuration stored in the <code>src/main/docker/Dockerfile</code>, and copies the JAR file to the Docker build.</p>
<p>Dockerfile contains a list of pure Docker commands with which we will prepare our image:</p>
<pre><code>FROM frolvlad/alpine-oraclejdk8
ADD storageWebService-0.0.1-SNAPSHOT.jar storageService.jar
EXPOSE 8080
CMD ["java", "-Djava.security.egd=file:/dev/./urandom", "-jar", "/storageService.jar"]
</code></pre>
<p>A prerequisite for running our application is to have installed a Java Virtual Machine (JVM). Docker provides a list of images with Java installed, and we will choose one of the smallest, based on a minimal 5MB Alpine Linux. <code>frolvlad/alpine-oraclejdk8</code> image has all that we need and is quite small (just 170 MB).</p>
<p>The <code>FROM</code> command sets the mentioned image as the base on which our own will be built. We <code>ADD</code> the built JAR file to the container filesystem under the name <code>storageService.jar</code>. Next, we define for Docker container to listen on port <code>8080</code> at runtime with the <code>EXPOSE</code> command. This, however, will not enable communication to <code>8080</code> from the host. When the image is done, and we want to run it, we will also need to publish the port on the container with the following command <code>docker run -p 8080:8080 amazonRepository/storageservice</code>, where <code>amazonRepository</code> is a repository we will configure later in this tutorial. With <code>CMD</code>, we define which commands will be executed when we run the container. Values in the brackets of the <code>CMD</code> command simply mean the following will be executed when we run the container:</p>
<pre><code class="language-bash hljs">java -Djava.security.egd=file:/dev/./urandom -jar /storageService.jar
</code></pre>
<p>The option <code>-Djava.security.egd=file:/dev/./urandom</code> is needed to help mitigate JVM delays during startup. If omitted, it will make the application boot up extremely slow due to a random number generation process needed during the boot process.</p>
<p>This sums up the “Application development” part. With this done, the service we have created here will be automatically started when we run a Docker container later on. So, let’s start installation and configuration of the other tools needed to set up the continuous integration process.</p>
<h5 id="application-and-system-operations">Application and System Operations</h5>
<p>First off, we need a clean Linux server on which to set up the Jenkins CI tool. Note that the following instructions are specifically for Ubuntu 14.04. Keep in mind instructions may slightly differ for other Linux distributions. The Jenkins version used is 2.7.1 and screens and instructions may differ slightly depending on the version of Jenkins being used.</p>
<p>So, we go to our Linux server console and start installing the prerequisites.</p>
<h5 id="jdk-prerequisite">JDK Prerequisite</h5>
<p>We need to have a JDK installed. Following are instructions for installing JDK8.</p>
<pre><code class="language-bash hljs"><span class="hljs-built_in">sudo</span> add-apt-repository ppa:webupd8team/java
<span class="hljs-built_in">sudo</span> apt-get install python-software-properties
<span class="hljs-built_in">sudo</span> apt-get update
<span class="hljs-built_in">sudo</span> apt-get install oracle-java8-installer
java -version 
</code></pre>
<h5 id="install-docker">Install Docker</h5>
<p>For Jenkins to be able to trigger Docker builds, we need to install <code>docker-engine</code> as follows:</p>
<pre><code class="language-bash hljs"><span class="hljs-built_in">sudo</span> apt-get install apt-transport-https ca-certificates

<span class="hljs-built_in">sudo</span> apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:<span class="hljs-number">80</span> --recv-keys <span class="hljs-number">58118</span>E89F3A912897C070ADBF76221572C52609D

<span class="hljs-comment">#create a docker list file</span>
<span class="hljs-built_in">sudo</span> vi /etc/apt/sources.list.d/docker.list

<span class="hljs-comment">#add the following entry in the docker.list file (change trusty to #the release you are running on if you are running on different, ie. #xenial, precise...):</span>
deb https://apt.dockerproject.org/repo ubuntu-trusty main
<span class="hljs-comment">#save and exit the file</span>
 
<span class="hljs-built_in">sudo</span> apt-get update
apt-cache policy docker-engine
<span class="hljs-built_in">sudo</span> apt-get install docker-engine
</code></pre>
<p>As we have now installed the Docker engine, with the following command we will start a <code>hello-world</code> Docker image to confirm Docker works correctly.</p>
<pre><code class="language-bash hljs"><span class="hljs-built_in">sudo</span> docker run hello-world
</code></pre>
<p><code>Hello-world</code> image output will look as follows, and with this, we can confirm that the engine is working ok.</p>
<p><img loading="lazy" decoding="async" class="alignnone  wp-image-564" src="http://www.bestdevops.com/wp-content/uploads/2017/02/sudo-docker-300x186.png" alt="" width="389" height="241" srcset="https://www.bestdevops.com/wp-content/uploads/2017/02/sudo-docker-300x186.png 300w, https://www.bestdevops.com/wp-content/uploads/2017/02/sudo-docker.png 654w" sizes="auto, (max-width: 389px) 100vw, 389px" /></p>
<h5 id="install-aws-command-line-interface-cli">Install AWS Command Line Interface (CLI)</h5>
<p>Next, we will install the AWS CLI. Later on, in Jenkins job configuration, we will use the CLI to execute commands for AWS authentication and Docker image push to Amazon EC2 container registry.</p>
<p>To install the AWS CLI, we follow the guidelines described in details on the Amazon CLI documentation.</p>
<p>Of the two installation options, we will choose installation using Pip, a package management system used for installing and managing Python programs. We will install Pip and AWS CLI simply by running the following three commands:</p>
<pre><code class="language-bash hljs"><span class="hljs-comment">#install Python version 2.7 if it was not already installed during the JDK #prerequisite installation</span>
<span class="hljs-built_in">sudo</span> apt-get install python2.<span class="hljs-number">7</span>
<span class="hljs-comment">#install Pip package management for python</span>
<span class="hljs-built_in">sudo</span> apt-get install python-pip
<span class="hljs-comment">#install AWS CLI</span>
<span class="hljs-built_in">sudo</span> pip install awscli
</code></pre>
<h5 id="aws-ecr">AWS ECR</h5>
<p>As the last step of the build process, we will be pushing our Docker image to the Amazon container registry. In the Amazon web services console, we find the AWS EC2 Container Service.</p>
<p><img loading="lazy" decoding="async" class="alignnone  wp-image-565" src="http://www.bestdevops.com/wp-content/uploads/2017/02/aws-ecr-300x97.png" alt="" width="390" height="126" srcset="https://www.bestdevops.com/wp-content/uploads/2017/02/aws-ecr-300x97.png 300w, https://www.bestdevops.com/wp-content/uploads/2017/02/aws-ecr-768x250.png 768w, https://www.bestdevops.com/wp-content/uploads/2017/02/aws-ecr-1024x333.png 1024w, https://www.bestdevops.com/wp-content/uploads/2017/02/aws-ecr.png 1034w" sizes="auto, (max-width: 390px) 100vw, 390px" /></p>
<p>We select <em>Repositories</em> submenu on the left and click on <em>Get started</em>.</p>
<p><img loading="lazy" decoding="async" class="alignnone  wp-image-566" src="http://www.bestdevops.com/wp-content/uploads/2017/02/amazon-ec2-container-300x139.png" alt="" width="395" height="183" srcset="https://www.bestdevops.com/wp-content/uploads/2017/02/amazon-ec2-container-300x139.png 300w, https://www.bestdevops.com/wp-content/uploads/2017/02/amazon-ec2-container.png 720w" sizes="auto, (max-width: 395px) 100vw, 395px" /></p>
<p>We are then presented with the first screen for configuring the repository where we enter the repository name and click on the <em>Next Step</em> button.</p>
<p><img loading="lazy" decoding="async" class="alignnone  wp-image-567" src="http://www.bestdevops.com/wp-content/uploads/2017/02/ec2-container-registry-300x128.png" alt="" width="401" height="171" srcset="https://www.bestdevops.com/wp-content/uploads/2017/02/ec2-container-registry-300x128.png 300w, https://www.bestdevops.com/wp-content/uploads/2017/02/ec2-container-registry-768x328.png 768w, https://www.bestdevops.com/wp-content/uploads/2017/02/ec2-container-registry.png 963w" sizes="auto, (max-width: 401px) 100vw, 401px" /></p>
<p>Clicking on <em>Next Step</em> then shows us a screen with instructions on how to push images to the repository.</p>
<p><img loading="lazy" decoding="async" class="alignnone  wp-image-569" src="http://www.bestdevops.com/wp-content/uploads/2017/02/ec2-container-registry-2-300x198.png" alt="" width="400" height="264" srcset="https://www.bestdevops.com/wp-content/uploads/2017/02/ec2-container-registry-2-300x198.png 300w, https://www.bestdevops.com/wp-content/uploads/2017/02/ec2-container-registry-2-768x507.png 768w, https://www.bestdevops.com/wp-content/uploads/2017/02/ec2-container-registry-2.png 940w" sizes="auto, (max-width: 400px) 100vw, 400px" /></p>
<p>&nbsp;</p>
<p>We are presented with an example of how to build and push a Docker image to the registry, but we don’t need to concern ourselves with this now. With this, we have created a repository.</p>
<h5 id="install-and-configure-jenkins">Install and Configure Jenkins</h5>
<p>To install Jenkins, we enter the following commands in the shell:</p>
<pre><code class="language-bash hljs"><span class="hljs-comment">#Download Jenkins key and pipe it to apt-key tool, apt-key command #add will read from input stream, as defined by „–„. When added</span>
<span class="hljs-comment">#apt will be able to authenticate package to be installed.</span>
wget -q -O - https://jenkins-ci.org/debian/jenkins-ci.org.key | <span class="hljs-built_in">sudo</span> apt-key add -
<span class="hljs-comment">#create a sources list for jenkins</span>
<span class="hljs-built_in">sudo</span> sh -c <span class="hljs-string">'echo deb http://pkg.jenkins-ci.org/debian-stable binary/ &gt; /etc/apt/sources.list.d/jenkins.list'</span>

<span class="hljs-comment">#update your local package list</span>
<span class="hljs-built_in">sudo</span> apt-get update

<span class="hljs-comment">#install jenkins</span>
<span class="hljs-built_in">sudo</span> apt-get install jenkins
</code></pre>
<p>When the installation finishes, Jenkins starts automatically. Check the service status with the following command:</p>
<pre><code class="language-bash hljs"><span class="hljs-built_in">sudo</span> service jenkins status
</code></pre>
<p>Jenkins will be connecting to Bitbucket Git repository, and to do so, we need to install Git.</p>
<pre><code class="language-bash hljs"><span class="hljs-comment">#install Git</span>
<span class="hljs-built_in">sudo</span> apt-get install git
</code></pre>
<p>Jenkins will be triggering Gradle build process, during which a Docker image will be created. To be able to do this, the Jenkins user needs to be added to the <code>docker</code> user group:</p>
<pre><code class="language-bash hljs"><span class="hljs-comment">#add Jenkins user to docker user group</span>
<span class="hljs-built_in">sudo</span> usermod <span class="hljs-operator">-a</span>G docker jenkins
</code></pre>
<p>During the build process, Jenkins will be pushing Docker images to Amazon ECR. To enable this, we need to configure AWS for the Jenkins user.</p>
<p>First, we need to switch to the <code>jenkins</code> user. To do so, we need to set a password.</p>
<pre><code class="language-bash hljs"><span class="hljs-comment">#change Jenkins password</span>
<span class="hljs-built_in">sudo</span> passwd jenkins

<span class="hljs-comment">#switch to Jenkins user</span>
su – jenkins

<span class="hljs-comment">#configure AWS</span>
aws configure
</code></pre>
<p>After entering the <code>aws configure</code> command, we start inputting the generated secret access key and key ID (these are the credentials we generated earlier in the process). In my case, the account’s region is <code>us-west-2</code>, and so I input that. We also set the default output format for AWS commands to be JSON.</p>
<p><img loading="lazy" decoding="async" class="alignnone  wp-image-570" src="http://www.bestdevops.com/wp-content/uploads/2017/02/aws-commands-json-300x41.png" alt="" width="454" height="62" srcset="https://www.bestdevops.com/wp-content/uploads/2017/02/aws-commands-json-300x41.png 300w, https://www.bestdevops.com/wp-content/uploads/2017/02/aws-commands-json.png 570w" sizes="auto, (max-width: 454px) 100vw, 454px" /></p>
<p>We can now move on to configuring Jenkins through the web console accessible on port 8080.</p>
<p>When we access the URL, we are presented with the following <em>Getting started</em> screen.</p>
<p><img loading="lazy" decoding="async" class="alignnone  wp-image-571" src="http://www.bestdevops.com/wp-content/uploads/2017/02/unlock-jenkins-300x131.png" alt="" width="440" height="192" srcset="https://www.bestdevops.com/wp-content/uploads/2017/02/unlock-jenkins-300x131.png 300w, https://www.bestdevops.com/wp-content/uploads/2017/02/unlock-jenkins.png 662w" sizes="auto, (max-width: 440px) 100vw, 440px" /></p>
<p>As stated on the screen, we need to input the password. Having done this, the setup wizard prompts us to do the following:</p>
<ul>
<li>Choose which plugins to install – we will choose <em>Install suggested plugins</em>.</li>
<li>Create first admin user by inputting user credentials</li>
</ul>
<p>When done, click <em>Save and Finish</em>. With this, we have finished Jenkins setup configuration.</p>
<p>Before we start defining the build job, we need to add a few additional plugins. We will go to <em>Manage Jenkins</em> and click on <em>Manage plugins</em>. In the <em>Available</em> tab, we first find the <em>Bitbucket plugin</em>, check the box, and click on <em>Download and install after restart</em>.</p>
<p><img loading="lazy" decoding="async" class="alignnone  wp-image-572" src="http://www.bestdevops.com/wp-content/uploads/2017/02/jenkins-300x166.png" alt="" width="451" height="249" srcset="https://www.bestdevops.com/wp-content/uploads/2017/02/jenkins-300x166.png 300w, https://www.bestdevops.com/wp-content/uploads/2017/02/jenkins.png 605w" sizes="auto, (max-width: 451px) 100vw, 451px" /></p>
<p>You will then be presented with something like the following screen.</p>
<p><img loading="lazy" decoding="async" class="alignnone  wp-image-573" src="http://www.bestdevops.com/wp-content/uploads/2017/02/installing-plugins-296x300.png" alt="" width="337" height="342" srcset="https://www.bestdevops.com/wp-content/uploads/2017/02/installing-plugins-296x300.png 296w, https://www.bestdevops.com/wp-content/uploads/2017/02/installing-plugins.png 605w" sizes="auto, (max-width: 337px) 100vw, 337px" /></p>
<p>After plugin installs, we repeat the process for the following additional plugins that will be needed to set up the job:</p>
<ul>
<li>Gradle plugin</li>
<li>Docker build step plugin</li>
<li>Cloudbees Docker custom build environment plugin</li>
<li>Amazon ECR plugin</li>
</ul>
<p>The Docker build step plugin we use will send requests to the Docker daemon. For this purpose, we need to enable the TCP socket on port 2375. To do to so, we enter the Docker configuration file located at <code>etc/default/docker</code>.</p>
<pre><code class="language-bash hljs"><span class="hljs-built_in">sudo</span> vi /etc/default/docker
</code></pre>
<p>Here we add the following line in the configuration:</p>
<pre><code class="language-bash hljs">DOCKER_OPTS=<span class="hljs-string">'-H tcp://127.0.0.1:2375 -H unix:///var/run/docker.sock'</span>
</code></pre>
<p>We save and exit the file and restart both Docker and Jenkins service.</p>
<pre><code class="language-bash hljs"><span class="hljs-built_in">sudo</span> service docker restart
<span class="hljs-built_in">sudo</span> service jenkins restart
</code></pre>
<p>After Jenkins reboots, we go to the Jenkins console and, from <em>Manage Jenkins</em>, we choose <em>Configure System</em>.</p>
<p><img loading="lazy" decoding="async" class="alignnone  wp-image-574" src="http://www.bestdevops.com/wp-content/uploads/2017/02/manage-jenkins-300x120.png" alt="" width="373" height="149" srcset="https://www.bestdevops.com/wp-content/uploads/2017/02/manage-jenkins-300x120.png 300w, https://www.bestdevops.com/wp-content/uploads/2017/02/manage-jenkins-768x308.png 768w, https://www.bestdevops.com/wp-content/uploads/2017/02/manage-jenkins.png 882w" sizes="auto, (max-width: 373px) 100vw, 373px" /></p>
<p>We find the <em>Docker builder</em> section and enter <code>http://localhost:2375</code> for the REST API URL, click on <em>Apply</em> to confirm the change. We then click the <em>Test Connection</em> to hopefully confirm that everything is ok.</p>
<p><img loading="lazy" decoding="async" class="alignnone  wp-image-575" src="http://www.bestdevops.com/wp-content/uploads/2017/02/docker-builder-300x73.png" alt="" width="395" height="96" srcset="https://www.bestdevops.com/wp-content/uploads/2017/02/docker-builder-300x73.png 300w, https://www.bestdevops.com/wp-content/uploads/2017/02/docker-builder-768x188.png 768w, https://www.bestdevops.com/wp-content/uploads/2017/02/docker-builder.png 921w" sizes="auto, (max-width: 395px) 100vw, 395px" /></p>
<p>We save the configuration and proceed to Jenkins job configuration.</p>
<h6 id="job-configuration">Job Configuration</h6>
<p>We go to the Jenkins home page and create a <em>New Item</em>.</p>
<p><img loading="lazy" decoding="async" class="alignnone  wp-image-576" src="http://www.bestdevops.com/wp-content/uploads/2017/02/jenkins-homepage.png" alt="" width="144" height="174" /></p>
<p>We choose a <em>Freestyle project</em> and enter the project name as shown on the following screen:</p>
<p><img loading="lazy" decoding="async" class="alignnone  wp-image-577" src="http://www.bestdevops.com/wp-content/uploads/2017/02/freestyle-project--300x168.png" alt="" width="316" height="177" srcset="https://www.bestdevops.com/wp-content/uploads/2017/02/freestyle-project--300x168.png 300w, https://www.bestdevops.com/wp-content/uploads/2017/02/freestyle-project--768x429.png 768w, https://www.bestdevops.com/wp-content/uploads/2017/02/freestyle-project--1024x572.png 1024w, https://www.bestdevops.com/wp-content/uploads/2017/02/freestyle-project--375x210.png 375w, https://www.bestdevops.com/wp-content/uploads/2017/02/freestyle-project-.png 1485w" sizes="auto, (max-width: 316px) 100vw, 316px" /></p>
<p>Clicking on <em>OK</em>, we are presented with the job configuration page. We want the project to be built on each push to our Bitbucket Git repository. To achieve this, we first need to define the repository we are connecting to.</p>
<h5 id="step-1-source-code-management">Step 1: Source Code Management</h5>
<p>Under source code management, we choose Git and enter the URL of our Bitbucket repository. The URL has the form of <code>https://bitbucket.org/bitbucketUsername/repositoryName</code>.</p>
<p>After we enter the URL, Jenkins will automatically try to test the connection. Since we didn’t yet input credentials, it will show an error indicating it cannot connect.</p>
<p><img loading="lazy" decoding="async" class="alignnone  wp-image-578" src="http://www.bestdevops.com/wp-content/uploads/2017/02/source-code-management-300x55.png" alt="" width="424" height="78" srcset="https://www.bestdevops.com/wp-content/uploads/2017/02/source-code-management-300x55.png 300w, https://www.bestdevops.com/wp-content/uploads/2017/02/source-code-management.png 605w" sizes="auto, (max-width: 424px) 100vw, 424px" /></p>
<p>Open the drop down list <em>Add</em>, and click on <em>Credentials Jenkins</em> provider.</p>
<p><img loading="lazy" decoding="async" class="alignnone size-full wp-image-579" src="http://www.bestdevops.com/wp-content/uploads/2017/02/credentials.png" alt="" width="279" height="65" /></p>
<p>We are presented with the following screen, where we enter the username and password for our Bitbucket account</p>
<p><img loading="lazy" decoding="async" class="alignnone  wp-image-580" src="http://www.bestdevops.com/wp-content/uploads/2017/02/jenkins-credentials-provider-290x300.png" alt="" width="300" height="310" srcset="https://www.bestdevops.com/wp-content/uploads/2017/02/jenkins-credentials-provider-290x300.png 290w, https://www.bestdevops.com/wp-content/uploads/2017/02/jenkins-credentials-provider.png 302w" sizes="auto, (max-width: 300px) 100vw, 300px" /></p>
<p>After adding the new credentials record, we make sure to select it in the credentials drop down, and this finishes the <em>Source code management</em> setup.</p>
<h5 id="step-2-build-triggers">Step 2: Build Triggers</h5>
<p>Check <em>Trigger builds remotely</em> and define an authentication token. Make sure to define a random and secure token.</p>
<p><img loading="lazy" decoding="async" class="alignnone  wp-image-581" src="http://www.bestdevops.com/wp-content/uploads/2017/02/build-triggers-300x92.png" alt="" width="349" height="107" srcset="https://www.bestdevops.com/wp-content/uploads/2017/02/build-triggers-300x92.png 300w, https://www.bestdevops.com/wp-content/uploads/2017/02/build-triggers.png 605w" sizes="auto, (max-width: 349px) 100vw, 349px" /></p>
<h5 id="step-3-bitbucket-webhook">Step 3: Bitbucket Webhook</h5>
<p>Jenkins already provided the URL for us which we will use on Bitbucket. We go to our Bitbucket repository page and, in the settings menu, click on <em>Web hooks</em>. Subsequently clicking on <em>Add webhook</em> presents us with the following screen, which we fill in as follows:</p>
<p><img loading="lazy" decoding="async" class="alignnone  wp-image-582" src="http://www.bestdevops.com/wp-content/uploads/2017/02/bitbucket-interfase-300x209.png" alt="" width="349" height="243" srcset="https://www.bestdevops.com/wp-content/uploads/2017/02/bitbucket-interfase-300x209.png 300w, https://www.bestdevops.com/wp-content/uploads/2017/02/bitbucket-interfase-100x70.png 100w, https://www.bestdevops.com/wp-content/uploads/2017/02/bitbucket-interfase.png 739w" sizes="auto, (max-width: 349px) 100vw, 349px" /></p>
<p>URL has the following structure: <code>http://JENKINS_URL _HOST:PORT/job/JOB_NAME/build?token=TOKEN</code>.</p>
<p>Enter the values above respectively with the Jenkins URL, port it is running on, the name of the job you have created, and token you have previously defined.</p>
<p>After saving the Webhook, you will be provided with the following screen, which you can edit if needed, or view requests generated each time we push new code.</p>
<p><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-583" src="http://www.bestdevops.com/wp-content/uploads/2017/02/webhooks-300x94.png" alt="" width="300" height="94" srcset="https://www.bestdevops.com/wp-content/uploads/2017/02/webhooks-300x94.png 300w, https://www.bestdevops.com/wp-content/uploads/2017/02/webhooks.png 605w" sizes="auto, (max-width: 300px) 100vw, 300px" /></p>
<p>With this configuration, the webhook is triggered on each repository push, regardless of the branch. On the Jenkins side, we can define which branch push will trigger the build.</p>
<p>For Bitbucket to be able to push code to Jenkins, we need to reconfigure Jenkins global security to allow anonymous read access. Additionally, for our setup, we have to disable the default Jenkins option which prevents cross-site request forgery. To do this, go to <em>Manage Jenkins</em> and choose <em>Configure global security</em>. Check <em>Allow anonymous read access</em> and check <em>Prevent cross-site forgery exploits</em>. Then save the configuration.</p>
<p><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-584" src="http://www.bestdevops.com/wp-content/uploads/2017/02/configure-global-security-231x300.png" alt="" width="231" height="300" srcset="https://www.bestdevops.com/wp-content/uploads/2017/02/configure-global-security-231x300.png 231w, https://www.bestdevops.com/wp-content/uploads/2017/02/configure-global-security.png 484w" sizes="auto, (max-width: 231px) 100vw, 231px" /></p>
<p>Please note that this is only done for simplicity reasons. Full setup surpasses the coverage of this tutorial, and it would include further securing Jenkins behind a reverse proxy, on TLS connection, and enabling CSRF prevention.</p>
<h5 id="step-4-gradle-build">Step 4: <a href="http://www.scmgalaxy.com/courses/agenda/build-management-tools/gradle-training.html">Gradle Build</a></h5>
<p>We can now return to the Jenkins job and continue configuring it. In the build section, we add a build step: <em>Invoke gradle script</em>.</p>
<p><img loading="lazy" decoding="async" class="alignnone size-full wp-image-585" src="http://www.bestdevops.com/wp-content/uploads/2017/02/add-build-setup.png" alt="" width="239" height="184" /></p>
<p>On this form, we enter the following:</p>
<p><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-586" src="http://www.bestdevops.com/wp-content/uploads/2017/02/ivolve-gradle-script-300x179.png" alt="" width="300" height="179" srcset="https://www.bestdevops.com/wp-content/uploads/2017/02/ivolve-gradle-script-300x179.png 300w, https://www.bestdevops.com/wp-content/uploads/2017/02/ivolve-gradle-script-768x457.png 768w, https://www.bestdevops.com/wp-content/uploads/2017/02/ivolve-gradle-script.png 915w" sizes="auto, (max-width: 300px) 100vw, 300px" /></p>
<p>As shown on the screen, we will use the Gradle wrapper, a convenient Gradle feature that doesn’t require you to have Gradle installed on the host. Make sure to check the <em>Make gradlew executable</em> box.</p>
<p>In the tasks, we specify <code>build</code> and <code>buildDocker</code>.</p>
<h5 id="step-5-docker-tag-image">Step 5: Docker Tag Image</h5>
<p>This part of the build tags a Docker image previously prepared by Gradle’s <code>dockerBuild</code> task. For this, we add a new build step to the job: <em>Execute Docker command</em>. We choose the <em>Tag image</em> command and set the image name, target repository where we will push the image, and tag:</p>
<p><img loading="lazy" decoding="async" class="alignnone size-medium wp-image-587" src="http://www.bestdevops.com/wp-content/uploads/2017/02/execute-docker-command-300x87.png" alt="" width="300" height="87" srcset="https://www.bestdevops.com/wp-content/uploads/2017/02/execute-docker-command-300x87.png 300w, https://www.bestdevops.com/wp-content/uploads/2017/02/execute-docker-command-768x223.png 768w, https://www.bestdevops.com/wp-content/uploads/2017/02/execute-docker-command.png 819w" sizes="auto, (max-width: 300px) 100vw, 300px" /></p>
<h4 id="step-6-docker-push-to-amazon-ecr"></h4>
<p>&nbsp;</p>
<h5 id="step-6-docker-push-to-amazon-ecr">Step 6: Docker Push to Amazon ECR</h5>
<p>Lastly, we need to define how to push our image to the Amazon ECR. For this, we add a new <em>Execute shell</em> build step and set the commands to authenticate to AWS and to push the image to Amazon ECR:</p>
<pre><code>#region for our account is us-west-2
aws ecr get-login --region us-west-2 | bash
#push the previously tagged image
docker push 058432294874.dkr.ecr.us-west-2.amazonaws.com/springbootdocker:${BUILD_NUMBER}
</code></pre>
<p><img decoding="async" src="https://uploads.toptal.io/blog/image/121104/toptal-blog-image-1472043675964-d4d406113219c18faca1e4f74bdc17aa.png" alt="Docker Push to Amazon ECR" /></p>
<p>With this, we have finished our build process. After pushing new code to the repo, this job will activate, and we will have a new Docker image uploaded to the Docker registry “auto-magically”.</p>
<p>The image can be then pulled to wherever we have <code>docker-engine</code> installed, and can be run with the following command:</p>
<pre><code>docker run -p 8080:8080 amazonRepository/springbootdocker
</code></pre>
<p>This command will start our Spring Boot microservice, with the following endpoints for uploading and downloading our files to the S3 bucket:</p>
<ul>
<li><code>http://hostnameURL:8080/api/storage/upload</code></li>
<li><code>http://hostnameURL:8080/api/storage/download?fileName=xyz</code></li>
</ul>
<h5 id="further-steps-with-java-and-continuous-integration">Further Steps with Java and Continuous Integration</h5>
<p>There are always more things to do. A lot of ground has been covered in this tutorial, but I would consider this only a starting point from which to learn further. Putting Jenkins behind a web proxy server, like Nginx, and establishing a TLS connection, are only two examples of what more could, and arguably should, be done.</p>
<p>Our Docker image is available on Amazon ECR and ready for deploying. We can now take it and deploy it manually. However, a finer solution would be to automate it further. CI is only the first step, and next step is Continuous Delivery. What about a high availability? Amazon AWS EC2 provides features for registering containers in the cloud on a clustered environment which is mandatory for production based service. A good working example of developing a continuous delivery process can be found on the following <a href="https://blogs.aws.amazon.com/application-management/post/Tx32RHFZHXY6ME1/Set-up-a-build-pipeline-with-Jenkins-and-Amazon-ECS" target="_blank" rel="noopener noreferrer">AWS blog post</a>.</p>
<h5 id="conclusion">Conclusion</h5>
<p>All in all, we have put in place a smooth and clean software development process. Utilizing the tools available, we have created an infrastructure that helps maximize our productivity. Now, we do not have to worry about the configuration of our Java service which is a simple web service with a REST endpoint. We let the Spring Boot convention take care of everything and focus only on the service logic. We utilize Jenkins to build a new Docker image each time we push our code to our Bitbucket Git repository, and in the end, we’ve set the cloud to be responsible for storing our Docker images and files. When we deploy our service contained within a Docker image, we will be care free of any restrictions of the operating system (as long as the operating system has a <code>docker-engine</code> installed).</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.bestdevops.com/java-in-the-cloud-continuous-integration-setup-tutorial/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
