Skip to content

Load Local Configuration

Ryu Xin edited this page Oct 14, 2022 · 4 revisions

What you'll build

With this sample, you will learn:

  1. Storage and format of local business configuration
  2. Business configuration SPI mechanism, you can customize the loading method of business configuration based on SPI

What you’ll need

  1. An Integrated Developer Environment (IDE). Popular choices include IntelliJ IDEA, Spring Tools, Visual Studio Code, or Eclipse, and many more.
  2. A Java™ Development Kit (JDK). We recommend BellSoft Liberica JDK version 8 or version 11.
  3. Completed Register Business Configuration reading

Maven dependency

<dependency>
      <groupId>org.hiforce.lattice</groupId>
      <artifactId>lattice-model</artifactId>
      <version>1.0.11.2</version>
</dependency>
<dependency>
      <groupId>org.hiforce.lattice</groupId>
      <artifactId>lattice-runtime</artifactId>
      <version>1.0.11.2</version>
</dependency>
<dependency>
      <groupId>org.hiforce.lattice</groupId>
      <artifactId>lattice-load-config-res</artifactId>
      <version>1.0.11.2</version>
</dependency>

Step 1: Define the business configuration JSON file

In the Register Business Configuration chapter, we programmatically defined the business configuration process. Without a program, we can also use JSON files to define business configuration, such as the business configuration in the previous example, which is defined in JSON as follows:

{
  "bizCode": "business.b",
  "priority": 1000,
  "products": [
    {
      "code": "lattice.productGroupBuyProduct"
    }
  ],
  "extensions": [
    {
      "extCode": "OrderLinePriceExt.EXT_ORDER_LINE_CUSTOM_UNIT_PRICE",
      "priorities": [
        {
          "code": "lattice.productGroupBuyProduct",
          "type": "PRODUCT"
        },
        {
          "code": "business.b",
          "type": "BUSINESS"
        }
      ]
    }
  ]
}

This JSON text simply serializes the BusinessConfig object into JSON text format. Copy this content into a file and put it in the resource/lattice directory. The file name is named in lattice-{business-code}.json format.

Step 2: Sample Demonstration

This example is very simple. Start Lattice directly, without using code to declare the business configuration, and directly simulate a business invocation process. code show as below:

public class AutoLoadBusinessConfigSample {

    public static void main(String[] args) {
        Lattice.getInstance().start();

        System.out.println("---------------------------------------");
        LatticeOverlayProductSample.doBusiness("groupBy");
        System.out.println("---------------------------------------");
    }
}

The results are as follows:

---------------------------------------
GroupBuyProduct effect status:true
[Business B] overlay product unit price: 700
---------------------------------------

Business configuration loading extension SPI

In this example, we import the lattice-load-config-res toolkit. The local business configuration loader in this toolkit implements the BusinessConfigLoadSpi interface as follows:

@AutoService(BusinessConfigLoadSpi.class)
public class BizConfigResourceLoader implements BusinessConfigLoadSpi {
    ......
}

In the same way, we can also write a configuration reader to implement the BusinessConfigLoadSpi interface, so that the business configuration can be read and loaded from the remote configuration library when the container is started, such as using Alibaba Cloud OSS, ACM, etc. This can be left to you as a small homework.

The sample code can be obtained by visiting: https://github.com/hiforce/lattice-sample/tree/main/lattice-load-business-config


中文版:https://www.ryu.xin/2022/09/26/lattice-local-config/