Skip to content

Guides and tutorials for getting started with Broadleaf Microservices

Notifications You must be signed in to change notification settings

evrimulgen/MicroservicesStarter

Repository files navigation

Getting Started with Broadleaf Microservices

The Broadleaf Microservices Framework is a collection of enterprise components and libraries to help accelerate your application development. The framework includes commerce service components built on Java and Spring as well as a centralized metadata-driven admin built in React.

This project is intended to give you a hands on look at core concepts and extension patterns as well as providing an overview of the key commerce and PIM features provided with the framework.



1. Project Goals

The guides and tutorials in this project will walk you through:

  • getting the core commerce framework services running locally

  • running a reference implementation of Broadleaf’s unified microservices admin which can manage data across the various microservices

  • running a reference implementation of a consumer-facing storefront application

  • concepts around extending and customizing the framework

1.1. Example Admin

Microservices Admin Example

1.2. Example Storefront

Microservices Admin Example

2. What Do I Need?

In order to get this reference project running locally, you will want to obtain and install the following:

2.1. Credentials

Access to Broadleaf’s Docker Registry and Maven Nexus is needed in order to pull down the appropriate resources.

Once you have obtained these credentials, you will need to configure them for the dependencies below.

ℹ️

Contact [email protected] for information on how to receive these credentials

2.2. Java

You will need Java 8 or 11 installed on your machine.

2.3. Docker

You will need to have Docker Engine & Docker Compose installed locally

💡

Docker Desktop for both Mac and Windows already includes compose along with other docker apps.

Once you have docker installed, you will want to authenticate with Broadleaf’s docker registry.

Type the following into your CLI:

docker login repository.broadleafcommerce.com:5001

When prompted, type in the username and password you received above.

You’ll also want to configure Docker settings to use appropriate resources. A good rule of thumb is to allow Docker to consume around 3/4 of your system resources as necessary.

At a minimum, you will want to allocate at least 2 CPUs and 4-6 GB Memory

Ideally for a developer machine, you will want to allocate something like: 6 CPUs, 8 GB Memory, 4 GB Swap.

2.4. Maven

You’ll need to have Maven installed locally as well. Maven 3.5 or later is recommended.

Once you have maven installed, another step you need to do before you can build the project is to configure the authentication. Maven requires it to be specified in a file called settings.xml that has to be in the .m2 subdirectory in a user’s home directory.

  1. Create a folder called .m2 in your home directory

  2. Create a file called settings.xml in the .m2 folder

  3. Copy the following contents to your ~/.m2/settings.xml making sure to replace the credentials with the ones you received above:

<settings xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.1.0 http://maven.apache.org/xsd/settings-1.1.0.xsd"
    xmlns="http://maven.apache.org/SETTINGS/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <servers>
    <server>
      <id>broadleaf-microservices</id>
      <username>username_here</username>
      <password>password_here</password>
    </server>
  </servers>
</settings>
💡

More info about authenticating with private repositories can be found here and here

3. Building & Running the Project

3.1. Building

Now that all of the pre-requisites are installed, let’s build and run the project!

  1. First, we’ll need to download or clone this project so that we can run everything locally on your own machine. You can utilize the Clone or Download button at the top right of this page.

  2. Once you have the project downloaded, let’s go ahead and build it using Maven. You’re going to navigate to the root folder (i.e. the folder that contains the main pom.xml file) of where you downloaded/un-zipped or cloned the project. In the root of this project run the following to build:

    mvn clean install
    ℹ️

    It may take some time during the initial installation as the process needs to download all required dependencies. All subsequent builds should go much faster.

    💡

    You can enable running the example tests in this project by running mvn clean install -DskipTests=false

  3. Before you start up all the APIs, you’ll need to first start up a few supporting services that are required for this example. These services are provided via docker images and can be run and started from the command line by navigating to the root of this project and running:

    docker-compose up -d

    and you should see the following output in your console:

    Creating network "microservicesstarter_postgresnet" with the default driver
    Creating network "microservicesstarter_kafkanet" with the default driver
    Creating network "microservicesstarter_solrnet" with the default driver
    Creating network "microservicesstarter_backend" with the default driver
    Creating microservicesstarter_zk_1       ... done
    Creating microservicesstarter_postgres_1 ... done
    Creating microservicesstarter_localkafka_1 ... done
    Creating microservicesstarter_localsolr_1       ... done
    Creating microservicesstarter_commercegateway_1 ... done
    Creating microservicesstarter_admingateway_1    ... done
    Creating microservicesstarter_auth_1            ... done
    Creating microservicesstarter_adminweb_1        ... done
    Creating microservicesstarter_commerceweb_1     ... done

    Docker Dashboard Example

    💡

    If you are using one of the latest versions of Docker Dashboard, you can verify that all the supporting services are running and have started up completely as the image shown above.

    You can also run docker ps from the command line to view running processes. It’s also useful to tail the logs e.g. docker-compose logs -f auth to verify that the startup process has completed for the relevant service

3.2. Running Backend APIs

3.2.1. spring-boot:run

Next, you will want to start up the core backend commerce APIs by running the following command:

You’ll want to make sure that all your docker images have started up successfully before running this command, otherwise you may get startup errors trying to connect to a service that may not have finished completing.

mvn spring-boot:run
ℹ️

Depending on your configured system resources, this may take some time to fully start up.

In general, the sequence of events that you will encounter include:

  1. Tomcat initialization

  2. SQL changelog execution

  3. SOLR and Zookeeper connection initialization

  4. Spring bean initialization

  5. Kafka Pub/Sub messaging channel and consumer initialization

  6. Tomcat started on port(s): 8447 (https) with context path ''

  7. Started TutorialGettingStartedApplication

That’s it! Once you’ve verified that the supporting services and backend APIs are running, you can visit the administration console and the consumer storefronts from a browser:

If you are having issues visiting the storefront links, you will either need to set up DNSMasq in order to map URLs to localhost or set up /etc/hosts with the heatclinic and aaahotsauces subdomains.

We recommend using Chrome since the above should be handled automatically. You’ll also want to enable insecure localhost by visiting: chrome://flags/#allow-insecure-localhost

ℹ️

Remember to shutdown your docker services when finished in order to free up resources i.e. docker-compose down

3.2.2. Viewing OpenAPI Specs & Testing Endpoints [Alternative]

During local development or evaluation, you may find it beneficial to view API documentation around exposed services within the project. To facilitate this, we’ve provided a supporting docker image that will run in the background that includes our Open API specs for some of the core framework services along with a Swagger UI that allows users to "Try Out" various endpoints.

To access this, you’ll want to start up the backend services using the following command (instead of the simple mvn spring-boot:run command outlined above) so that you don’t have to worry about getting a valid JWT authentication token every time:

mvn spring-boot:run -Pnosec

Next, you can visit the Open API UI by visiting:

ℹ️

make sure you have the supporting docker container openapi-ui running

You should be able to see an interface that allows you to navigate and try out various APIs provided by some core Broadleaf services.

Open API UI

💡

Be sure to "Try Out" a few endpoints as the specs do define example payloads that should return valid data given the test seed data loaded for this project

3.2.3. Testing Endpoints Directly using cURL [Alternative]

Another alternative evaluation flow or local development flow may include being able to easily hit endpoints directly either via cURL or utilizing a tool like POSTMAN. In this case, you can disable security when starting up the backend services using the following command (instead of the simple mvn spring-boot:run command outlined above) so that you don’t have to worry about getting a valid JWT authentication token every time:

mvn spring-boot:run -Pnosec

Once you have the services running, you should be able to issue a cURL request like:

curl --insecure -X GET "https://localhost:8447/products?offset=0&forward=true&pageSize=50" -H "accept: application/json" -H "X-Context-Request: {\"tenantId\":\"5DF1363059675161A85F576D\",\"catalogId\":\"3\"}"

3.2.4. Containerize Backend Services [Alternative]

Instead of running the backend APIs using mvn spring-boot:run you may wish to manage and run everything via containers. In this case, we’ve provided several config files to help facilitate this setup.

  1. You’ll first want to build the project and then create the docker image

    mvn clean install
    mvn clean package -am -pl . -Pdocker -Ddockerfile.tag=latest-snapshot
  2. The above commands will produce a docker image with the name repository.broadleafcommerce.com:5001/broadleaf-evaluation/tutorial-getting-started:latest-snapshot

  3. You can now start up the whole project using the following docker-compose files

    docker-compose -f docker-compose.yml -f docker-compose.tgs.yml up -d

4. Project Structure Overview

This project is structured in a manner that is meant to easily demonstrate various features and aspects of the framework and does not necessarily reflect how you may want to structure your real implementation project.

Specifically, the structure intends to demonstrate the framework’s flexible composition model - you have the ability to selectively compose multiple services into a single deployment or deploy them independently.

MS Custom Deployment Options
ℹ️

This particular project demonstrates the ability to compose a majority of the core backend commerce services together into a single deployable spring boot application.

4.1. Important Pieces

The following are the main components of this project and should be noted:

  1. docker-compose.yml - this is the main entrypoint for utilizing the project. This defines some supporting services that are required to run the full scope of these examples. See this FAQ: "What is the purpose for all of these supporting docker services?" to get a description about each of these images

  2. pom.xml - this defines the core commerce library dependencies that will be utilized for this guide (e.g. catalog, pricing, offers, etc…​)

  3. src/main/resources/application-default.yml - this file configures a few default local property configurations for the backend commerce services including database configuration and initialization parameters

5. Things to Try

Once you’ve started up both the admin and the storefront. Here are a couple things you may want to try:

  • As the "Master" admin user, try switching between different applications using the site selector in the left hand navigation. Once you are working in a particular context (e.g. Heat Clinic), you can start modifying and editing items which you can then deploy to the corresponding storefront.

  • Modifing an existing product in the admin, saving those changes, and then promoting and deploying that change into production.

  • Creating a new offer that targets a specific SKU or Variant

  • Try logging in as a different admin user to view the experience for someone with limited roles and permission:

  • Try logging into the Heat Clinic storefront as the below user. Notice that when adding items to your cart, this customer automatically qualifies for "10% Off Order" based on a promotion that has been set up.

6. What’s Next?

Now that you have the project running and have a high level understanding of how to build and use it, the next step would be to walk through some typical customizations.

You can reference the following tutorial project and guide as you apply the same customization steps to this project.

💡

When you finish, you can check your results against the code in MicroservicesTutorial/ms-tutorial-extension-complete.

Once you’re done with the extensions tutorial, you can move on to learning how to create new domains, services, and endpoints on top of the existing core Catalog Microservice.

💡

When you finish, you can check your results against the code in MicroservicesTutorial/ms-tutorial-new-entity-complete.

7. FAQ

7.1. Where is the data for these services stored?

  • One of the supporting services defined in this project is a Postgres DB. For this project, each individual service is configured to point to the same Postgres instance with a unique schema. The default schemas that correspond to each service can be found in application-default.yml

7.2. Why did my data get wiped after a restart of the services?

  • By default, the project is configured to drop all tables and re-run import scripts on startup in the default runtime environment. You can find the following type of property config in application-default.yml

broadleaf:
  service_abc:
    jpa:
      hibernate:
        ddl-auto: create

7.3. Where did all this initial data come from?

  • All of the Broadleaf services come with a set of default demo data. The seed data scripts are contained within each of the individual services dependency .jar and are loaded via a property configured in application-default.yml

broadleaf:
  service_abc:
    datasource:
      data:
        - 'classpath:/service-abc-data-jpa.sql'

7.4. What core commerce services are utilized in this reference project?

See this page for a listing of the services used in this project, along with a link to their JavaDoc: Service Directory

7.5. What is the purpose for all of these supporting docker services?

  • the docker-compose file defines various services that aid in facilitating a full commerce experience needed for this overview project and tutorial, such as an auth service and a couple API gateways. A majority of these images should be considered for example purposes only and NOT FOR PRODUCTION USE. The following is a list of all the supporting services with a brief description of their intent.

Table 1. Non-Production Supporting Services Directory
Service Description

admingateway

this is a lightweight API Gateway built on top of Spring Cloud Gateway to facilitate routing for the admin console

adminweb

this is a reference image of our React Microservices Admin SPA

auth

this is a lightweight OAuth2 authorization server used to generate the necessary JWT tokens for authentication

commercegateway

this is a lightweight API Gateway built on top of Spring Cloud Gateway to facilitate routing for the admin console

commerceweb

this is a reference image of our React Commerce Storefront SPA

localkafka

Official Confluent Docker Image for Kafka (Community Version)

localsolr

Broadleaf-flavored image based on Solr’s official 8.2 distro

openapi-ui

Broadleaf Open API Specs and Swagger UI

postgres

Official Postgres 11.2 Docker Image

zk

Official Confluent Docker Image for Zookeeper

7.6. What open ports do I need for this example project?

This project will attempt to utilize the following ports:

Table 2. Reference Project Default Ports
Service Port(s)

zk

2181

adminweb

3000

open api ui

3003

commerceweb

4000

postgres

5432

localkafka

9092, 7777 [JMX]

auth

8443, 8000 [Debug]

admingateway

8446, 8001 [Debug]

commercegateway

8456, 8014 [Debug]

localsolr

8983

backend commerce api

8447, 8004 [Debug]

7.7. How do I create a new Application in the Admin?

See the steps outlined here for Creating an Application

7.8. Why are images broken on the commerce storefront?

Images are being served on a different port, so you may need to accept the self-signed localhost SSL cert first. Try visiting an image directly here

Once you’ve informed your browser it is safe to proceed, try refreshing the page and the browser should proceed to load the images.

7.9. I’m running in a linux environment and getting "Unable to connect to host.docker.internal" errors?

"host.docker.internal" doesn’t currently resolve properly using Docker for linux as it does for Docker Desktop for Mac or Windows. As a workaround, you can change the references in the .env file in this project to use the docker0 interface ip instead (typically 172.17.0.1) which you can verify by running the following: ip route | grep docker0 | awk '{print $9}'

About

Guides and tutorials for getting started with Broadleaf Microservices

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published