Dataway is introduced

Dataway is an interface configuration tool for applications based on DataQL service aggregation capabilities. Enables users to configure an interface that meets their requirements without developing any code. The entire interface is configured, tested, smoked, and published. One-stop is done through the UI provided by Dataway. The UI is provided as a Jar package and integrated into the application and shares the same HTTP port with the application; the application does not need to create a separate management port for the Dataway.





The advantage of this inline integration approach pattern is that most older projects can apply Dataway directly without incursion. Thus, the iterative efficiency of old projects is improved and the r&d cost of enterprise projects is greatly reduced.





Dataway provides DataQL configuration capabilities instrumentally. As a result of this change in the r&d model, a significant number of requirements development scenarios require only configuration to complete delivery. This avoids a series of development tasks from data access to the front-end interface, such as Mapper, BO, VO, DO, DAO, Service, Controller are not needed.

Dataway is a member of the Hasor ecosystem, so the first thing to do with using Dataway in Spring is to get through the two ecosystems. We integrate Hasor and Spring Boot in the way recommended in the official documentation.

Step 1: Introduce related dependencies

<dependency> <groupId>net.hasor</groupId> <artifactId>hasor-spring</artifactId> <version>4.1.3</version> </dependency> < the dependency > < groupId > net. Hasor < / groupId > < artifactId > hasor - dataway < / artifactId > < version > 4.1.3 - fix20200414 < / version > <! </dependency> </dependency>Copy the code

Hasor-spring is responsible for the integration between the Spring and Hasor frameworks. Hasor-dataway works on top of Hasor. With Hasor-spring we can use dataway.

Step 2: Configure the Dataway and initialize the data table

Dataway will provide an interface to configure the interface, similar to Swagger that can be configured as long as the JAR package is integrated. Find the configuration file application.properties for our Springboot project

# Enable Dataway (Mandatory: default false)
HASOR_DATAQL_DATAWAY=true

# Enable Dataway (Mandatory: default false)
HASOR_DATAQL_DATAWAY_ADMIN=true

# dataway API working path (optional, default: / API /)
HASOR_DATAQL_DATAWAY_API_URL=/api/

# dataway- UI working path (optional, default: /interface-ui/)
HASOR_DATAQL_DATAWAY_UI_URL=/interface-ui/

# SQL executor dialect setting (optional, recommended)
HASOR_DATAQL_FX_PAGE_DIALECT=mysqlCopy the code

Dataway involves five configuration items that can be configured, but not all of them are required.

HASOR_DATAQL_DATAWAY and HASOR_DATAQL_DATAWAY_ADMIN must be enabled. By default, Datawaty is disabled.

The Dataway requires two tables to work, and here are the short table statements for both tables. The following SQL can be found in the META-INF/hasor-framework/mysql directory of the dataway dependent JAR package. The statement is written in mysql syntax.

CREATE TABLE `interface_info` (
    `api_id`          int(11)      NOT NULL AUTO_INCREMENT   COMMENT 'ID',
    `api_method`      varchar(12)  NOT NULL                  COMMENT 'HttpMethod: GET, PUT, POST',
    `api_path`        varchar(512) NOT NULL                  COMMENT 'Intercept path',
    `api_status`      int(2)       NOT NULL                  COMMENT 'Status: 0 draft, 1 published, 2 Changed, 3 disabled',
    `api_comment`     varchar(255)     NULL                  COMMENT 'comments',
    `api_type`        varchar(24)  NOT NULL                  COMMENT 'Script type: SQL, DataQL',
    `api_script`      mediumtext   NOT NULL                  COMMENT 'Query script: XXXXXXX',
    `api_schema`      mediumtext       NULL                  COMMENT 'Request/Response data structure for interface',
    `api_sample`      mediumtext       NULL                  COMMENT 'Request/Response/Request Header sample data',
    `api_create_time` datetime     DEFAULT CURRENT_TIMESTAMP COMMENT 'Creation time',
    `api_gmt_time`    datetime     DEFAULT CURRENT_TIMESTAMP COMMENT 'Modification time',
    PRIMARY KEY (`api_id`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8mb4 COMMENT='the API Dataway';

CREATE TABLE `interface_release` (
    `pub_id`          int(11)      NOT NULL AUTO_INCREMENT   COMMENT 'Publish ID',
    `pub_api_id`      int(11)      NOT NULL                  COMMENT 'Owning API ID',
    `pub_method`      varchar(12)  NOT NULL                  COMMENT 'HttpMethod: GET, PUT, POST',
    `pub_path`        varchar(512) NOT NULL                  COMMENT 'Intercept path',
    `pub_status`      int(2)       NOT NULL                  COMMENT 'Status: 0 valid, 1 invalid (may be taken offline)',
    `pub_type`        varchar(24)  NOT NULL                  COMMENT 'Script type: SQL, DataQL',
    `pub_script`      mediumtext   NOT NULL                  COMMENT 'Query script: XXXXXXX',
    `pub_script_ori`  mediumtext   NOT NULL                  COMMENT 'Raw query script, different only if type is SQL',
    `pub_schema`      mediumtext       NULL                  COMMENT 'Request/Response data structure for interface',
    `pub_sample`      mediumtext       NULL                  COMMENT 'Request/Response/Request Header sample data',
    `pub_release_time`datetime     DEFAULT CURRENT_TIMESTAMP COMMENT 'Release time (offline not updated)',
    PRIMARY KEY (`pub_id`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8mb4 COMMENT='Dataway API release history. ';

create index idx_interface_release on interface_release (pub_api_id);Copy the code

Step 3: Configure the data source

As a Spring Boot project, it has its own perfect database tool support. Druid + mysql + spring-boot-starter-JDBC

First introduce dependencies

< the dependency > < groupId > mysql < / groupId > < artifactId > mysql connector - Java < / artifactId > < version > 5.1.30 < / version > </dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.21</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId>  </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> The < version > 1.1.10 < / version > < / dependency >Copy the code

Then add the configuration of the data source

# db
spring.datasource.url=jdbc:mysql://xxxxxxx:3306/example
spring.datasource.username=xxxxx
spring.datasource.password=xxxxx
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.type:com.alibaba.druid.pool.DruidDataSource
# druid
spring.datasource.druid.initial-size=3
spring.datasource.druid.min-idle=3
spring.datasource.druid.max-active=10
spring.datasource.druid.max-wait=60000
spring.datasource.druid.stat-view-servlet.login-username=admin
spring.datasource.druid.stat-view-servlet.login-password=admin
spring.datasource.druid.filter.stat.log-slow-sql=true
spring.datasource.druid.filter.stat.slow-sql-millis=1Copy the code

If the project has already integrated its own data sources, you can skip step 3.

Step 4: Set up the data source into the Hasor container

Spring Boot and Hasor are two separate container frameworks. After integration, we need to set the data source in Spring to Hasor in order to use Dataway capabilities.

Start by creating a Hasor module and handing it over to Spring to manage. The data source is then injected through Spring.

@DimModule @Component public class ExampleModule implements SpringModule { @Autowired private DataSource dataSource = null; @Override public void loadModule(ApiBinder apiBinder) throws Throwable { // .DataSource form Spring boot into Hasor apiBinder.installModule(new JdbcModule(Level.Full, this.dataSource)); }}Copy the code

When Hasor starts, it calls the loadModule method, where it assigns the DataSource to Hasor.

Step 5: Enable Hasor in SprintBoot

@EnableHasor()
@EnableHasorWeb()
@SpringBootApplication(scanBasePackages = { "net.example.hasor"}) public class ExampleApplication { public static void main(String[] args) { SpringApplication.run(ExampleApplication.class, args); }}Copy the code

This step is as simple as adding two annotations to the Spring startup class.

Step 6: Launch the application

The welcome message of Hasor Boot is displayed during application startup

_ _ ____ _ | | | | | _ \ | | | | __ | | __ __ ___ ___ __ __ | | | ___ ___ _) | | _ | | / _ ` / (/ _ \ | |'__ | | _ < / / _ \ _ \ | __ | | | | | (_ | \ __ \ (_) | | | | _) | | (_) (_) | | _ _ - | | | _ | \ __, _ | ___ / \ ___ / | _ | | ____ / \ ___ \ ___ / \ | 4Copy the code

You can also see logs similar to the following in later logs.

The 2020-04-14 13:52:59. 696. [the main] INFO N.H.C ore. Context. TemplateAppContext - loadModule class Net. Hasor. Dataway. Config. DatawayModule 13:52:59. 2020-04-14, 697 [main] INFO n.h asor. Dataway. Config. DatawayModule - Dataway API 13:52:59 workAt/API / 2020-04-14. 697. [the main] INFO N.H.C.E.A bstractEnvironment - var - > 13:52:59 HASOR_DATAQL_DATAWAY_API_URL = / API /. 2020-04-14. 704. [the main] INFO n.h asor. Dataway. Config. DatawayModule - dataway Admin workAt/interface - UI / 13:52:59. 2020-04-14, 716 [main] INFO net. Hasor. Core. Binder. ApiBinderWrap - mapingTo[901d38f22faa419a8593bb349905ed0e] ->bindType 'class net. Hasor. Dataway. Web. ApiDetailController mappingTo' : '[/ interface - the UI/API/API - the detail].' the 2020-04-14 13:52:59. 716 net [main] INFO. Hasor. Core. Binder. ApiBinderWrap - mapingTo[c6eb9f3b3d4c4c8d8a4f807435538172] ->bindType 'class net. Hasor. Dataway. Web. ApiHistoryListController mappingTo' : '[/ interface - the UI/API/API - history].' the 2020-04-14 13:52:59. 717 net [main] INFO. Hasor. Core. Binder. ApiBinderWrap - mapingTo[eb841dc72ad54023957233ef602c4327] ->bindType 'class net. Hasor. Dataway. Web. ApiInfoController mappingTo' : '[/ interface - the UI/API/API - info].' the 2020-04-14 13:52:59. 717 net [main] info. Hasor. Core. Binder. ApiBinderWrap - mapingTo[96aebb46265245459ae21d558e530921] ->bindType 'class net. Hasor. Dataway. Web. ApiListController mappingTo' : '[/ interface - the UI/API/API - the list].' the 2020-04-14 13:52:59. 718 net [main] INFO. Hasor. Core. Binder. ApiBinderWrap - mapingTo[7467c07f160244df8f228321f6262d3d] ->bindType 'class net. Hasor. Dataway. Web. ApiHistoryGetController mappingTo' : '[/ interface - the UI/API/get - history].' the 2020-04-14 13:52:59. 719 net [main] INFO. Hasor. Core. Binder. ApiBinderWrap - mapingTo[97d8da5363c741ba99d87c073a344412] ->bindThe Type 'class net. Hasor. Dataway. Web. DisableController mappingTo' : '[/ interface - UI/API /disable720 [main]] '. The 2020-04-14 13:52:59. INFO net. Hasor. Core. Binder. ApiBinderWrap - mapingTo ddc3316ef2642dfa4395ca8ac0fff04 [8]  ->bindType 'class net. Hasor. Dataway. Web. SmokeController mappingTo' : '[/ interface - the UI/API/smoke]. "the 2020-04-14 13:52:59. 720 net [main] INFO. Hasor. Core. Binder. ApiBinderWrap - mapingTo[cc06c5fb343b471aacedc58fb2fe7bf8] ->bindType 'class net. Hasor. Dataway. Web. SaveApiController mappingTo' : '[/ interface - the UI/API/save - API].' the 2020-04-14 13:52:59. 720 net [main] INFO. Hasor. Core. Binder. ApiBinderWrap - mapingTo[7915b2b1f89a4e73891edab0264c9bd4] ->bindType 'class net. Hasor. Dataway. Web. PublishController mappingTo' : '[/ interface - the UI/API/publish]'. The 2020-04-14 13:52:59. 721 net [main] INFO. Hasor. Core. Binder. ApiBinderWrap - mapingTo[0cfa34586455414591bdc389bff23ccb] ->bindType 'class net. Hasor. Dataway. Web. PerformController mappingTo' : '[/ interface - the UI/API/perform]'. The 2020-04-14 13:52:59. [the main] 721 INFO net. Hasor. Core. Binder. ApiBinderWrap - mapingTo[37fe4af3e2994acb8deb72d21f02217c] ->bindType 'class net. Hasor. Dataway. Web. DeleteController mappingTo' : '[/ interface - the UI/API/delete]'.Copy the code

When you see the “Dataway API workAt/API/” and the” dataway admin workAt /interface-ui/ “message, you can confirm that the dataway configuration has taken effect.

Step 7: Access the interface management page to configure interfaces

Enter “http://127.0.0.1:8080/interface-ui/” in your browser to see the long-awaited interface.

Step 8: Create an interface

Dataway provides two language patterns, which can be used either in the powerful DataQL query language or directly in SQL (the SQL language is also converted to DataQL for execution within Dataway).

We first try to execute a SELECT query in SQL mode, and immediately see the result of this SQL query.

In the same way that we use DataQL we need to write:

var query = @@sql()<%
    select * from interface_info
%>
return query()Copy the code

Var query = @@sql()<%… %> is used to define the SQL external code block and store this definition in the Query variable name. <% %> in the middle is the SQL statement.

Finally, this code block is called in DataQL and the query result is returned.

When the interface is written, it can be saved and published. For the convenience of testing, I choose GET mode.

Interface release after we request directly: http://127.0.0.1:8080/api/demos, to see the joints of the long-awaited return values.

The final summary

After the previous steps we showed you how to use Dataway to simply configure the interface based on the Spring Boot project. The Dataway approach is really refreshing because an interface can be configured so easily without developing a single line of code or doing any Mapping entity Mapping binding.

Follow the official account to learn more