[Hasor framework] SpringBoot integrates Dataway codeless interface tool configuration and problem solving (including GreenPlum table building statement, demo source code, test description)

This article has participated in the "Newcomer Creation Ceremony" event to start the road of gold creation together.

What

Description from the official website:

  1. Dataway is an interface configuration tool for applications based on DataQL service aggregation capabilities. It enables users to configure an interface that meets their needs without developing any code. The entire interface is configured, tested, smoked, released. One-stop is done through the UI interface provided by Dataway. The UI will be provided in the form of Jar package and integrated into the application and share the same http port with the application. The application does not need to open a new management port for Dataway.
  2. The advantage of this inline integration mode is that most old projects can directly apply Dataway without intrusion. This improves the iterative efficiency of old projects and greatly reduces the R&D cost of enterprise projects.
  3. Dataway tool provides DataQL configuration capabilities. This change in the R&D model makes it possible to deliver a considerable number of demand development scenarios with only configuration. This avoids a series of development tasks from data access to front-end interfaces, such as Mapper, BO, VO, DO, DAO, Service, and Controller are not needed.
  4. Dataway is a member of the Hasor ecosystem, so the first thing to do when using Dataway in Spring is to connect the two ecosystems. We integrated Hasor and Spring Boot according to the method recommended in the official documentation .

use

1. Dependence

SpringBoot version: 2.5.0 [only the main dependencies are posted here]

		 <!--hasor核心依赖-->
        <dependency>
            <groupId>net.hasor</groupId>
            <artifactId>hasor-spring</artifactId>
            <version>4.2.5</version>
        </dependency>
        <dependency>
            <groupId>net.hasor</groupId>
            <artifactId>hasor-dataway</artifactId>
            <version>4.2.5</version>
        </dependency>
        <!--p6spy 打印执行的SQL语句-->
        <dependency>
            <groupId>com.github.gavlyukovskiy</groupId>
            <artifactId>p6spy-spring-boot-starter</artifactId>
            <version>1.7.1</version>
        </dependency>
        <!--数据库相关-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>com.jdbc</groupId>
            <artifactId>greenplum</artifactId>
            <version>5.1.4</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>
复制代码

2. Configuration

[Question 1]: Chinese garbled characters displayed on the interface. [Solution 1]: Configure [servlet.encoding.charset] and [servlet.encoding.force]

server:
  port: 8088
  servlet:
    encoding:
      charset: utf-8
      force: true
spring:
  application:
    name: dataWay-demo
 # 数据源配置
  datasource:
    url: jdbc:pivotal:greenplum://xxx.xx.xxx.xxx:2345;DatabaseName=gpdb
    username: gpadmin
    password: gpadmin
    driver-class-name: com.pivotal.jdbc.GreenplumDriver
    type: com.alibaba.druid.pool.DruidDataSource
    druid:
      initial-size: 3
      min-idle: 3
      max-active: 10
      max-wait: 60000
      stat-view-servlet:
        login-username: admin
        login-password: admin
# p6spy配置
decorator:
  datasource:
    p6spy:
      logging: slf4j
      log-file: syp.log
      log-format: executionTime:%(executionTime) | sql:%(sqlSingleLine)
# dataWay核心配置
# 启用 Dataway 功能(默认不启用)
HASOR_DATAQL_DATAWAY: true
# 开启 ui 管理功能(注意生产环境必须要设置为 false,否则会造成严重的生产安全事故)
HASOR_DATAQL_DATAWAY_ADMIN: true
# (可选)API工作路径
HASOR_DATAQL_DATAWAY_API_URL: /api/
# (可选)ui 的工作路径,只有开启 ui 管理功能后才有效
HASOR_DATAQL_DATAWAY_UI_URL: /interface-ui/
复制代码

3. Data initialization

Following is the initialization SQL for MySQL database:

CREATE TABLE interface_info (
  api_id          varchar(64)  NOT NULL COMMENT 'ID',
  api_method      varchar(12)  NOT NULL COMMENT 'HttpMethod:GET、PUT、POST',
  api_path        varchar(512) NOT NULL COMMENT '拦截路径',
  api_status      varchar(4)   NOT NULL COMMENT '状态:-1-删除, 0-草稿,1-发布,2-有变更,3-禁用',
  api_comment     varchar(255) NOT NULL COMMENT '注释',
  api_type        varchar(24)  NOT NULL COMMENT '脚本类型:SQL、DataQL',
  api_script      mediumtext   NOT NULL COMMENT '查询脚本:xxxxxxx',
  api_schema      mediumtext   NOT NULL COMMENT '接口的请求/响应数据结构',
  api_sample      mediumtext   NOT NULL COMMENT '请求/响应/请求头样本数据',
  api_option      mediumtext   NOT NULL COMMENT '扩展配置信息',
  api_create_time varchar(32)  NOT NULL COMMENT '创建时间',
  api_gmt_time    varchar(32)  NOT NULL COMMENT '修改时间',
  PRIMARY KEY (api_id),
  UNIQUE KEY uk_interface_info (api_path)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='Dataway 中的API';

CREATE TABLE interface_release (
  pub_id           varchar(64)  NOT NULL COMMENT 'Publish ID',
  pub_api_id       varchar(64)  NOT NULL COMMENT '所属API ID',
  pub_method       varchar(12)  NOT NULL COMMENT 'HttpMethod:GET、PUT、POST',
  pub_path         varchar(512) NOT NULL COMMENT '拦截路径',
  pub_status       varchar(4)   NOT NULL COMMENT '状态:-1-删除, 0-草稿,1-发布,2-有变更,3-禁用',
  pub_comment      varchar(255) NOT NULL COMMENT '注释',
  pub_type         varchar(24)  NOT NULL COMMENT '脚本类型:SQL、DataQL',
  pub_script       mediumtext   NOT NULL COMMENT '查询脚本:xxxxxxx',
  pub_script_ori   mediumtext   NOT NULL COMMENT '原始查询脚本,仅当类型为SQL时不同',
  pub_schema       mediumtext   NOT NULL COMMENT '接口的请求/响应数据结构',
  pub_sample       mediumtext   NOT NULL COMMENT '请求/响应/请求头样本数据',
  pub_option       mediumtext   NOT NULL COMMENT '扩展配置信息',
  pub_release_time varchar(32)  NOT NULL COMMENT '发布时间(下线不更新)',
  PRIMARY KEY (pub_id),
  KEY idx_interface_release_api  (pub_api_id),
  KEY idx_interface_release_path (pub_path)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='Dataway API 发布历史。';
create index idx_interface_release on interface_release (pub_api_id);
复制代码

【问题二】:> 1071 - Specified key was too long; max key length is 767 bytes 【解决二】:解决:调整相应字段的长度。也可以参考这篇博文

这里再提供以下GreenPlum数据库的初始化SQL:

CREATE TABLE interface_info (
	api_id VARCHAR ( 64 ),
	api_method VARCHAR ( 12 ),
	api_path VARCHAR ( 512 ),
	api_status VARCHAR ( 4 ),
	api_comment VARCHAR ( 255 ),
	api_type VARCHAR ( 24 ),
	api_script TEXT,
	api_schema TEXT,
	api_sample TEXT,
	api_option TEXT,
	api_create_time VARCHAR ( 32 ),
	api_gmt_time VARCHAR ( 32 ) 
);
CREATE TABLE interface_release (
	pub_id VARCHAR ( 64 ),
	pub_api_id VARCHAR ( 64 ),
	pub_method VARCHAR ( 12 ),
	pub_path VARCHAR ( 512 ),
	pub_status VARCHAR ( 4 ),
	pub_comment VARCHAR ( 255 ),
	pub_type VARCHAR ( 24 ),
	pub_script TEXT,
	pub_script_ori TEXT,
	pub_schema TEXT,
	pub_sample TEXT,
	pub_option TEXT,
	pub_release_time VARCHAR ( 32 ) 
);
CREATE INDEX idx_interface_release ON interface_release ( pub_api_id );
复制代码

4.数据源初始化

Spring Boot 和 Hasor 本是两个独立的容器框架,做整合之后为了使用 Dataway 的能力需要把 Spring 中的数据源设置到 Hasor 中。首先新建一个 Hasor 的 模块,并且将其交给 Spring 管理。然后把数据源通过 Spring 注入进来。

@DimModule
@Component
public class ExampleModule implements SpringModule {
    @Autowired
    private DataSource dataSource = null;
    @Override
    public void loadModule(ApiBinder apiBinder) throws Throwable {
        apiBinder.installModule(new JdbcModule(Level.Full, this.dataSource));
    }
}
复制代码

5.启动类添加注解

两个注解 @EnableHasor() 和 @EnableHasorWeb()

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

应用在启动过程中会看到 Hasor Boot 的欢迎信息

6.测试

Access the front-end according to the configured ui working path http://localhost:8088/interface-ui/ , the account and password are both admin: Please add image descriptionthe page is briefly explained [you can use it]: Please add image descriptionDataway provides 2 language modes, you can use powerful You can also use the SQL language directly (in Dataway, the SQL language will also be converted into DataQL for execution.) Add a DataQL and test: Please add image descriptionThis is just a simple introduction. The interface development needs to be customized according to the business, and the application of Dataway Scenarios should also be considered. For details on DataQL and SQL-related development, please refer to the official document [DataQL Aggregate Query Engine SQL Executor] and finally put a few useful connections: Dataway official manual: www.hasor.net/web/dataway… Dataway is on OSC The project address on the website, welcome to collect: www.oschina.net/p/dataway DataQL manual address: www.hasor.net/web/dataql/… Hasor project home page: www.hasor.net/web/index.h…

Guess you like

Origin juejin.im/post/7085355199836979208