Monday, November 23, 2015

CloudFoundry – Getting started


This article helps you getting started on cloudfoundry. This does not explain on the cloudfoundry concepts.

Step 1: Create an account in pivotal cloudfoundry. 
                You can create a 2 months free subscription account from pivotal cloudfoundry services web site https://run.pivotal.io. An organization as the name specifies – can be your company name, project group or so.
The space is nothing but the environment you wish to create, you may want to have a development, qa and production spaces. In short applications are hosted in spaces (environemnts) and spaces belongs to an organization.



Step 2: Create a simple spring application, you can also create in Grails, Ruby etc., You can use maven, gradle as the build tool.
                You can use spring boot to create a simple application. Please refer http://ndeiveehan.blogspot.com/2015/11/spring-boot-getting-started.html to create a new spring application.

You can also create in other languages as Cloudfoundry offers other buildpacks.
Once created, you can generate the war or jar using the maven command.

Step 3: Login to cloudfoundry and create required organization and spaces.
From you PC, login to cloudfoundry using the CLI tools.
cf login -a https://api.run.pivotal.io

Enter your credentials to login.



Step 4: Deploy your application.
You can push the apps in the following ways:
- Use the Cloudfoundry CLI tool.
- Use cloufdoundry eclipse plugin.
- Use a build plugin to integrate deployment as part of your build tool.
 but this article shows you how to push application using the CLI tool.

Now issue the following cf command.
cf push springbootfirst -p target/springbootfirst-0.0.1-SNAPSHOT.jar
               
 This command uploads the spring jar file to the cloudfoundry site and starts the server. 

Step 5: You can now access the web application in the following web url:




Now there is a lot more you can do using CF tool such as:
-          Configuring your own domain instead of cfapps.io.
  • Scale up an application.
  • Deleting an app, renaming it, start or stop an application.
  • View logs for an application
  • Enable ssh for an application.
  • View services from the market place.
  •  Create, update organizations, spaces, buildpacks etc.,
  •  Perform user administration on organizations / projects.



See you in the next article on cloudfoundry. 

Spring boot - Getting started

Spring boot - Getting started


This article explains how to get started on Spring boot. Assuming you already have JDK, maven and eclipse installed in your PC, lets get started. 


Step 1: Create a simple maven project in eclipse.
Step 2: Create the following parent definition in pom
       <parent>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-starter-parent</artifactId>
              <version>1.3.0.RELEASE</version>
       </parent>

Add the following dependencies
              <dependency>
                     <groupId>org.springframework.boot</groupId>
                     <artifactId>spring-boot-starter-web</artifactId>
              </dependency>
Spring boot starter web dependency takes care of loading required jars for the web such as spring web etc., No more hassles on the need to remember the spring jars and version names.

Step 3: Create a simple web controller
@RestController
@EnableAutoConfiguration
public class SampleController {

       @RequestMapping("/")
       String home() {
              return "Hi Buddy";
       }
      
       public static void main(String args[]) {
              SpringApplication.run(SampleController.class, args);
       }
}
The EnableAutoConfiguration annotation informs boot to automatically configure required annotations based on the jars that you have added in your application. Cool isn’t it.
Run the Above Sample Controller as a java application
What just happened. When did I start the web server ??
Actually Spring boot takes care of that. When you actually run SpringApplication.run, it internally runs a web server and then loads the controller into that server.




Step 4: Invoke the controller in the web browser: 


Simple and easy to develop without having to worry about dependencies or the server. 

Now if you don’t want to add the parent pom for whatever reasons like you want to add your own project parent pom or so, you can add the following dependencies.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.3.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>

You may also want to add the following to package your project as an executable jar.
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>