In this blog, we will know how we can create a SpringBoot application that uses MarkLogic as the database tier. In this application, we will be running MarkLogic in Docker containers.
SpringBoot
A spring boot is an open-source Java-based framework. Spring Boot is a project that is built on top of the Spring Framework. It makes it easy to create stand-alone, production-grade applications that we can just simply run.
MarkLogic
MarkLogic Server is an enterprise no sequel database. The unique structure of MarkLogic ensures that your applications are both scalable and have extreme performance. MarkLogic’s Server is a multi-model database. It has documents in place of a schema.
Docker
Docker is an open platform for developing and running applications. It’s a set of the platform as a service (PaaS) products that use OS-level virtualization to deliver software in packages called containers. Containers often deliver both an application and configuration.
Application
Pom.xml
A Project Object Model or POM is the fundamental unit of work in Maven. It is an XML file that contains information about the project and configuration details used by Maven to build the project.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>io.knoldus</groupId>
<artifactId>Spring-Boot-MarkLogic-Demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Spring-Boot-MarkLogic-Demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.marklogic</groupId>
<artifactId>marklogic-client-api</artifactId>
<version>5.5.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<repositories>
<repository>
<id>jcenter</id>
<url>http://jcenter.bintray.com</url>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
We need to add marklogic-client-api
dependency under the dependencies section and jcenter
repository under the repositories section to use MarkLogic’s Java Client API.
Application.yml
Application.yml is a configuration file that keeps our application configurations.
marklogic:
host: localhost
port: 8000
username: yourusername
password: yourpassword
Directory Structure
This is what the project structure will look like :
config
: Config package contains all the configuration files for the project.controller
: Controller package contains all the controller class files.entity
: The entity package contains all the entities’ class files.repository
: Repository package contains all the repository class files.service
: Service package contains all the service class files.
BasicConfig.java
package io.knoldus.config;
import com.marklogic.client.DatabaseClient;
import com.marklogic.client.DatabaseClientFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableAutoConfiguration
public class BasicConfig {
@Value("${marklogic.host}")
private String host;
@Value("${marklogic.port}")
private int port;
@Value("${marklogic.username}")
private String username;
@Value("${marklogic.password}")
private String password;
@Bean
public DatabaseClient databaseClient() {
return DatabaseClientFactory.newClient(host, port, new
DatabaseClientFactory.DigestAuthContext(username, password));
}
}
@Configuration
: Configuration annotation which indicates that the class has@Bean
definition methods.@EnableAutoConfiguration
: EnableAutoConfiguration annotation auto-configures the beans that are present in the classpath.databaseClient()
: The databaseClient method helps to create a database client.- Internally, the Java Client API maintains a
OkHttpClient
connection pool that is shared by all DatabaseClient objects.
Dockerfile
Docker is surrounding for growing and handling containers. Containers package up programs and their runtime dependencies. Strolling MarkLogic in docker bins keeps its variations isolated from every different. Spinning up a MarkLogic container is fast compared to the time needed for virtual machines to begin.
FROM centos:centos7
RUN yum -y update && yum clean all
RUN yum -y install glibc.i686 gdb.x86_64 redhat-lsb.x86_64 && yum clean all
RUN yum -y install initscripts && yum clean all
ENV PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/MarkLogic/mlcmd/bin
COPY MarkLogic-10.0-9.4.x86_64.rpm /tmp/MarkLogic.rpm
RUN yum -y install /tmp/MarkLogic.rpm && rm /tmp/MarkLogic.rpm
EXPOSE 7997 7998 7999 8000 8001 8002
CMD /etc/init.d/MarkLogic start && tail -f /dev/null
docker-compose.yml
version: '2'
services:
ml8node1:
build: .
image: ml8:build
expose:
- "7997"
- "7998"
- "7999"
ports:
- "8000:8000"
- "8001:8001"
- "8002:8002"
- "8010:8010"
hostname: "ml1.local"
container_name: "ml1.local"
Run docker build .
command to create a Docker image.
Output :
After creating the image run docker compose up -d
Output :
Now heads towards http://localhost:8001
Output :
then finish the rest of the MarkLogic post-installation steps.
References
- https://docs.marklogic.com/guide/java/intro
- https://www.marklogic.com/blog/building-a-marklogic-docker-container/
Thanks for Reading !!