Development Environment Setup for Spring Boot-based Microservices With Maven and GitLab

Ganesh
TS Tech

--

This tutorial focuses on the steps in setting up a local development environment for developers undertaking Spring Boot-based microservices development with Maven and GitLab. Our objective is to expedite the setup process and ensure accuracy from get go.

Prerequisites:

Maven Configuration:

  • Download and install Maven following the instruction from the official documentation of Maven — https://maven.apache.org/download.cgi
  • Configure Maven to access the GitLab package repository to store/read the artifacts
  • Login to GitLab repo, go to Profile -> Settings -> Access Tokens -> Generate New Access Tokens. Assign appropriate permissions needed for the project
  • Copy the access token and keep it securely. We need to configure the same on the local Maven directory. The token needs to be encrypted before configuring the same in settings.xml
  • Generate a master password using the following command if not done earlier
mvn — encrypt-master-password

Enter the prompted password to generate the encrypted version of the master password and store the same in

${user.home}/.m2/settings-security.xml

 <settingsSecurity>
<master>{encrytedmasterpwd}</master>
</settingsSecurity>
  • Generate an encrypted password for the git access token copied in the earlier steps using the following command
mvn — encrypt-password

Enter the access token when prompted and it will generate the encrypted password for the same

  • Configure the <server> block in the ${MAVEN_HOME}\conf\settings.xml
<server>
<id>gitlab-maven</id>
<configuration>
<httpHeaders>
<property>
<name>Private-Token</name>
<value>{encrypted password}</value>
</property>
</httpHeaders>
</configuration>
</server>

Note: Use this id in the <repositories> section for getting access to it

  • We shall also optionally configure the Maven profile to have different configuration like dependencies for diverse environments. Profile name shall be passed with the following command
mvn clean install -Pdevlocal
  • If we need only h2 dependency configured as part of devlocal environment, then the Maven profile configuration will look like:
<profiles>
<profile>
<id>devlocal</id>
<dependencies>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
</profile>
</profiles>

Spring Boot Microservices:

  • Create a Maven-based project, configure parent as Spring Boot, and include the required Spring Boot dependencies
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.1</version>
<relativePath />
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
  • Alternatively, if the project follows a Design-First approach, then the spring project shall be directly downloaded from the swagger hub as base which contains the autogenerated bean classes as well.
  • Follow the GitLab branching strategy defined for the project and push the code to the specified branch of git repo
  • Project needs different configuration for different environment like Dev, Pre-Production, and Production. In Spring Project, we shall configure the same by appending the profile name in the application.properties file
  • Profile names shall be “devlocal” for developer local environment, “dev” for dev environment on the cloud or on premise server, “preprod” for pre-production environment, and “Prod” for production environment.
  • For devlocal profile application.properties file shall be “application-devlocal.properties”
  • As an option, we can also use the AWS Parameter store to configure and store the properties centrally and manage it in code using org.springframework.cloud:spring-cloud-starter-aws-parameter-store-config dependency. In AWS Parameter store, new keys shall be defined as:
/config/{{application_name}}_{{profilename}}/{{propertykeyname}}

Using Postman for Verification

  • Postman shall be used for testing the Spring Boot-based REST API
  • If the deployed REST APIs are protected over Keycloak kindly follow the below steps to authenticate and access the APIs
  • Go to Postman ->Select the Request and import collection -> Select Authorization -> Select Type -> OAuth2 -> Get New Access Token -> Enter User name, password and Login -> Use Token and execute the operation. Postman manages this session from here on till expiry and while making the request select the Authorization type as NoAuth

We would like to hear your experience from using this short tutorial. Feel free to connect with us, and follow for more updates on technology.

--

--

TS Tech
TS Tech

Published in TS Tech

L&T Technology Services Limited (LTTS) is a listed subsidiary of Larsen & Toubro Limited focused on Engineering and R&D (ER&D) services. We offer consultancy, design, development & testing services across the product and process development life cycle for our clients.