Search This Blog

Saturday 10 October 2015

IBM Bluemix - Spring Boot Elasticsearch Repositories demo

I decided to take the Elasticsearch repositories as part of the Spring Data project for a test drive. The Spring boot Elasticsearch repositories are described in the link below.

http://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#elasticsearch.repositories

In this example I create a basic Spring Boot application using Elasticsearch, Web, Rest Repositories along with Thymeleaf / Bootstrap as the view pages. The code for this is on GitHub which also provides the "Deploy to Bluemix" button to deploy to your own instance of this application into your own Bluemix accout.

https://github.com/papicella/SpringBootElasticSearch



Like all Spring Data Repositories you can create an interface and be given basic CRUD operations to the Elastisearch DOCUMENT as shown below.

EmployeeRepository.java
 
package pas.au.ibm.bluemix.elastic;

import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

import java.util.List;

public interface EmployeeRepository extends ElasticsearchRepository<Employee, String>
{
    public List<Employee> findByFirstNameContaining(String firstName);
} 

Employee.java
  
package pas.au.ibm.bluemix.elastic;

import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;

@Document(indexName = "employee", type = "employee", shards = 1, replicas = 0, refreshInterval = "-1")
public class Employee
{
    @Id
    private String id;
    private String firstName;
    private String lastName;
    private String job;
    private int deptNo;

    public Employee()
    {
    }

....

If you don't have a IBM DevOps Jazzhub account the manifest.yml can be used if you clone the project, and compile it using maven with "mvn package". Be sure to alter the application name / host to be a unique name within Bluemix.

applications:
 - name: pas-sb-elastic
   memory: 450M
   path: ./target/SpringBootElasticSearch-0.0.1-SNAPSHOT.jar
   instances: 1
   host: pas-sb-elastic
   buildpack: java_buildpack
   domain: mybluemix.net


Example Deployment

pasapicella@pas-macbook-pro:~/ibm/DemoProjects/spring-starter/jazzhub/SpringBootElasticSearch$ cf push
Using manifest file /Users/pasapicella/ibm/DemoProjects/spring-starter/jazzhub/SpringBootElasticSearch/manifest.yml

Creating app pas-sb-elastic in org pasapi@au1.ibm.com / space dev as pasapi@au1.ibm.com...
OK

Using route pas-sb-elastic.mybluemix.net
Binding pas-sb-elastic.mybluemix.net to pas-sb-elastic...
OK

Uploading pas-sb-elastic...
Uploading app files from: /Users/pasapicella/ibm/DemoProjects/spring-starter/jazzhub/SpringBootElasticSearch/target/SpringBootElasticSearch-0.0.1-SNAPSHOT.jar
Uploading 1M, 133 files
Done uploading
OK

Starting app pas-sb-elastic in org pasapi@au1.ibm.com / space dev as pasapi@au1.ibm.com...
-----> Downloaded app package (35M)
-----> Java Buildpack Version: v3.0 | https://github.com/cloudfoundry/java-buildpack.git#3bd15e1
-----> Downloading Open Jdk JRE 1.8.0_60 from https://download.run.pivotal.io/openjdk/trusty/x86_64/openjdk-1.8.0_60.tar.gz (2.7s)
       Expanding Open Jdk JRE to .java-buildpack/open_jdk_jre (1.7s)
-----> Downloading Spring Auto Reconfiguration 1.10.0_RELEASE from https://download.run.pivotal.io/auto-reconfiguration/auto-reconfiguration-1.10.0_RELEASE.jar (0.9s)

-----> Uploading droplet (79M)

0 of 1 instances running, 1 starting
0 of 1 instances running, 1 starting
0 of 1 instances running, 1 starting
0 of 1 instances running, 1 starting
1 of 1 instances running

App started


OK

App pas-sb-elastic was started using this command `SERVER_PORT=$PORT $PWD/.java-buildpack/open_jdk_jre/bin/java -cp $PWD/.:$PWD/.java-buildpack/spring_auto_reconfiguration/spring_auto_reconfiguration-1.10.0_RELEASE.jar -Djava.io.tmpdir=$TMPDIR -XX:OnOutOfMemoryError=$PWD/.java-buildpack/open_jdk_jre/bin/killjava.sh -Xmx329386K -Xms329386K -XX:MaxMetaspaceSize=64M -XX:MetaspaceSize=64M -Xss975K org.springframework.boot.loader.JarLauncher`

Showing health and status for app pas-sb-elastic in org pasapi@au1.ibm.com / space dev as pasapi@au1.ibm.com...
OK

requested state: started
instances: 1/1
usage: 450M x 1 instances
urls: pas-sb-elastic.mybluemix.net
last uploaded: Sat Oct 10 10:20:52 UTC 2015
stack: cflinuxfs2
buildpack: java_buildpack

     state     since                    cpu    memory           disk           details
#0   running   2015-10-10 09:22:42 PM   0.3%   425.1M of 450M   158.1M of 1G


The application is currently deployed and is accessible at the URL below.

http://pas-sb-elastic.mybluemix.net/





More Information

http://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#elasticsearch.repositories

https://www.elastic.co/

Tuesday 6 October 2015

IBM Bluemix - Specify only Liberty buildpack features you require

I am more often then not using spring boot applications on IBM Bluemix and most of what I need is packaged with the application from JPA or JDBC, drivers, Rest etc. Of course with IBM Bluemix we can specify which build pack we wish to use but by default for java applications LIberty is used.

When a stand-alone application is deployed, a default Liberty configuration is provided for the application. The default configuration enables the following Liberty features:
  • beanValidation-1.1
  • cdi-1.2
  • ejbLite-3.2
  • el-3.0
  • jaxrs-2.0
  • jdbc-4.1
  • jndi-1.0
  • jpa-2.1
  • jsf-2.2
  • jsonp-1.0
  • jsp-2.3
  • managedBeans-1.0
  • servlet-3.1
  • websocket-1.1
  • icap:managementConnector-1.0
  • appstate-1.0
Here is how I strip out some of what isn't required in my Liberty runtime container to a bare minimal of what I need.

manifest.yml

applications:
 - name: pas-speedtest
   memory: 512M
   instances: 1
   path: ./demo-0.0.1-SNAPSHOT.jar
   host: pas-speedtest
   domain: mybluemix.net
   env:
     JBP_CONFIG_LIBERTY: "app_archive: {features: [jsp-2.3, websocket-1.1, servlet-3.1]}"


 More Information

https://www.ng.bluemix.net/docs/starters/liberty/index.html#optionsforpushinglibertyapplications


Thursday 1 October 2015

IBM Bluemix - Triggerring backing service creation from "Deploy to Bluemix" button

I recently posted about the "Deploy to Bluemix" button which will automatically deploy an application into IBM Bluemix from a single click.

http://theblasfrompas.blogspot.com.au/2015/09/adding-deploy-to-bluemix-button-to-my.html

If the application requires backing services they can automatically be created using the"declared-services" tag as shown below. Declared services are a manifest extension, which creates or looks for the required or optional services that are expected to be set up before the app is deployed, such as a data cache service. You can find a list of the eligible Bluemix services, labels, and plans by using the CF Command Line Interface and running cf marketplace

manifest.yml

declared-services:
  redis-session:
    label: rediscloud
    plan: 30mb
applications:
 - name: pas-sbsessions
   memory: 512M
   instances: 2
   path: ./target/SpringBootHTTPSession-0.0.1-SNAPSHOT.jar
   host: pas-sbsessions
   domain: mybluemix.net
   buildpack: java_buildpack
   services:
    - redis-session


Note: Declared services is an IBM extension of the standard Cloud Foundry manifest format. This extension might be revised in a future release as the feature evolves and improves.