# Overview
Service orchestration is a powerful function provided by the FizzGate integration platform. It can quickly generate an aggregate interface based on existing business microservices through online configuration, reducing middle-layer glue code and coding investment. Functions are supported in service orchestration. In this advanced tutorial, we will introduce data in three articles (Part 1: List expansion & merging, Part 2: List extraction & association, Part 2: List field renaming & field removal). Use of list functions.
This article is the middle part of the service orchestration processing list data series.
# Function
Functions are a powerful and convenient function provided by service orchestration. By encapsulating some commonly used logic into functions, and using functions in service orchestration to process data in the service orchestration process, we can simplify our configuration and realize complex functions.
# Function classification
Version requirement: v2.3 or above
Functions start with fn., such as fn.date.timestamp(), date is the category, and the gateway has built-in functions of the following categories:
- Public class fn.common
- Date class fn.date
- String class fn.string
- Codec class fn.codec
- Math fn.math
- Data list class fn.list
# Reference value
Reference values are supported in functions, such as:
fn.string.concat({step1.result.channelCode},{step1.result.nonce})
{step1.result.channelCode} represents the reference value, which refers to the result of step1
fn.string.concat({step1.result.channelCode},{g.account.name})
{g.account.name} represents a reference value, which refers to the public resource "account.name". Starting with g represents a reference to a public resource.
# Nested calls
Supports multiple nested calls, such as:
fn.codec.md5(fn.date.add(fn.date.add("2021-07-09 22:44:55", "yyyy-MM-dd HH:mm:ss", 1, fn.math. addExact(999,1)), "yyyy-MM-dd HH:mm:ss", fn.math.addExact(0,1), 1000))
fn.string.toUpperCase(fn.codec.sha256(fn.string.concat({step1.result.channelCode},{step1.result.nonce},{step1.result.timestamp},{step1.result.appSecret}) ))
# Data list function
Function | Return value type | Purpose | Parameter description |
---|---|---|---|
fn.list.expand( List<List<Object>> data) | List<Object> | Expand a two-dimensional array (list) into a one-dimensional array (list), For example: data=[[{a:1}],[{a:2}],[{a:3}]] fn.list.expand(data)<br/ >Return: [{a:1},{a:2},{a:3}] | data: two-dimensional array (list) |
fn.list.merge( List<Object>... data) | List<Object> | Merge multiple lists into one, such as: data1=[{a: 1}] data2=[{a:2}] data3=[{a:3}] fn.list.merge(data1,data2,data3) Return :[{a:1},{a:2},{a:3}] | data: list, can be multiple |
fn.list.extract( List<Map<String, Object>> data, String... fields) | List<Map<String, Object>> | Extract only some fields in the list, such as: data=[ {a:1,b:4,c:7}, {a:2,b:5,c :8}, {a:3,b:6,c:9} ] fn.list.extract(data, "a", "c") Return: [ {a:1,c:7}, {a:2,c:8}, {a:3,c:9} ] | data: list fields: [optional] fields, there can be multiple |
fn.list.join( List<Map<String, Object>> dest, List<Map<String, Object>> src, String joinField, String... fields) | List<Map<String, Object>> | Merge 2 list fields, you can only merge specified fields, such as: dest=[ {a:1,b:4,c:7}, {a:2,b:5,c:8}, {a:3,b:6,c:9 } ] src=[ {a:1,d:444,e:777}, {a:2,d:555,e:888}<br/ />] fn.list.join(dest,src, "a", "d") Return: [ {a:1,b:4,c:7,d :444}, {a:2,b:5,c:8,d:555}, {a:3,b:6,c:9} ] | dest : Target list src: The merged list joinField: The field associated with the two lists Format: The associated field of the dest list: The associated field of the src list , such as: userName:uname, If the associated fields of the two lists are the same, you can fill in only one, such as: userName fields: [Optional] The fields that need to be merged in the merged list, Multiple fields are allowed. If left blank, all fields will be merged |
fn.list.rename( List<Map<String, Object>> data, String... fieldPairs) | List<Map<String, Object>> | Rename some fields in the list, such as: data=[ {a:1,b:4,c:7}, {a:2,b:5,c :8}, {a:3,b:6,c:9} ] fn.list.rename(data, "a:apple", "c:cat") Return: [ {apple:1,b:4,cat:7}, {apple:2,b:5,cat:8}, {apple: 3,b:6,cat:9} ] | data: list fieldPairs: [Optional] There can be multiple field pairs to be renamed . Format: Original field name: New field name, such as: c:cat |
fn.list.removeFields( List<Map<String, Object>> data, String... fields) | List<Map<String, Object>> | Delete some fields in the list, such as: data=[ {a:1,b:4,c:7}, {a:2,b:5,c: 8}, {a:3,b:6,c:9} ] fn.list.removeFields(data, "b") Return: [ {a:1,c:7}, {a:2,c:8}, {a:3,c:9} ] | data: list<br/ />fields: [Optional] fields to be deleted, there can be multiple |
# Data preparation
# FizzGate integrated platform installation
Please refer to: https://www.fizzgate.com/fizz/guide/installation (opens new window)
# Underlying service interface
The source code of the underlying service interface called by the service orchestration in this article can be obtained from github (https://github.com/wehotel/fizz-examples). Clone the latest fizz-examples source code from github and start the fizz-examples-rest-api service.
In the management background (menu location: RPC Management->Service Statement) configure the service statement, as shown in the figure.
# Added new orchestration interface
Management background, menu location: Service Edit->Interface List, click Add.
# List extraction fn.list.extract
This example calls the /fn-list/user-basic-info-list-by-role/{role} interface of the underlying fizz-examples-rest-api service in the orchestration interface (interface source code: [FnListController](https:// github.com/wehotel/fizz-examples/blob/master/fizz-examples-rest-api/src/main/java/we/controller/FnListController.java)) to obtain the user’s basic information list and respond to this interface The data is list extracted to fit our desired data format.
# Basic information
# Configuration input
In this example we do not need to enter parameters, leave them all blank.
# Configuration steps
Add a new step step1, and then add a request request1 in step step1, select fizz-examples-rest-api as the service, and request the /fn-list/user-basic-info-list-by-role/admin interface.
# Configure output
Configure the response message to be returned to the front end. Here, the response body is configured with the following fields:
code: reference type, the value is the code field of the response to request1 in step 1;
message: reference type, the value is the message field of the response to request1 in step 1;
data: reference type, the value is the data field of the response to request1 in step 1 and is extracted using the fn.list.extract function, leaving only the id and name fields.
# Test
The response obtained by directly calling the /fn-list/user-basic-info-list-by-role/admin interface is as shown in the figure.
The test interface gets the response as shown in the figure.
From the response of the test interface, we can see that the service orchestration interface has completed the request to the /fn-list/user-basic-info-list-by-role/admin interface and correctly extracted the field list of the response data field.
# List association fn.list.join
This example calls the /fn-list/user-basic-info-list-by-role/{role} interface of the underlying fizz-examples-rest-api service in the orchestration interface (interface source code: [FnListController](https:// github.com/wehotel/fizz-examples/blob/master/fizz-examples-rest-api/src/main/java/we/controller/FnListController.java)) to obtain the user’s basic information list, and then call the underlying fizz-examples -The /fn-list/user-private-list-by-user-ids/{{userIds} interface of the rest-api service (interface source code: [FnListController](https://github.com/wehotel/fizz-examples/ blob/master/fizz-examples-rest-api/src/main/java/we/controller/FnListController.java)) to obtain the user’s private information list and perform list association on the call response data of the two interfaces to satisfy us Desired data format.
# Basic information
# Configuration input
In this example we do not need to enter parameters, leave them all blank.
# Configuration steps
Add a new step step1, add a request request1 in step step1, select fizz-examples-rest-api as the service, and request the /fn-list/user-basic-info-list-by-role/admin interface. Add a new step step2, add a request request1 in step step2, select fizz-examples-rest-api as the service, and request /fn-list/user-private-list-by-user-ids/{step1.request1 .response.body.ids} interface.
In order to obtain userIds in step2, the request1 response of step1 is configured, the ids field is added, and the configuration is fn.string.replace(fn.string.replace(fn.string.toString({step1.request1.response .body.data[*].id}), "[", ""), "]", "") .
# Configure output
Configure the response message to be returned to the front end. Here, the response body is configured with the following fields:
code: reference type, the value is the code field of the response to request1 in step 1;
message: reference type, the value is the message field of the response to request1 in step 1;
data: reference type, the value is the data field of the response to request1 in step 1 and the data field of the response to request1 in step 2, and is associated with id:userId using the fn.list.join function.
# Test
The response obtained by directly calling the /fn-list/user-basic-info-list-by-role/admin interface is as shown in the figure.
The response obtained by directly calling the /fn-list/user-private-list-by-user-ids/1,2 interface is as shown in the figure.
The test interface gets the response as shown in the figure.
From the response of the test interface, we can see that the service orchestration interface has completed the request to the /fn-list/user-basic-info-list-by-role/admin interface and the /fn-list/user-private-list-by -user-ids/1,2 interface request and correctly associate the list of data fields in the responses of the two calls.
# Introduction to FizzGate Integration Platform
FizzGate is a microservice aggregation gateway developed based on Java. It can achieve hot service orchestration and aggregation, automatic authorization selection, online service script coding, online testing, high-performance routing, API audit management, callback management, etc., and has powerful customization The plug-in system can be expanded by itself and provides a friendly graphical configuration interface, which can quickly help enterprises manage API services, reduce middle-layer glue codes, reduce coding investment, and improve the stability and security of API services.
Official website: https://www.fizzgate.com (opens new window)
GitHub: https://github.com/fizzgate/fizz-gateway-node (opens new window)
Code cloud: https://gitee.com/fizzgate/fizz-gateway (opens new window)
Getting Started Tutorial: https://www.fizzgate.com/fizz/guide/GettingStarted/ (opens new window)
Advanced tutorial: https://www.fizzgate.com/fizz/guide/advanced/ (opens new window)
Author: ZHONG.J