Skip to content

Commit 373c3a8

Browse files
committed
feat: SpringBoot 整合 Dataway(DataQL/Hasor)服务聚合框架使用示例
1 parent bfeca87 commit 373c3a8

File tree

7 files changed

+478
-0
lines changed

7 files changed

+478
-0
lines changed

springboot-dataway/.gitignore

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
HELP.md
2+
target/
3+
!.mvn/wrapper/maven-wrapper.jar
4+
!**/src/main/**
5+
!**/src/test/**
6+
7+
### STS ###
8+
.apt_generated
9+
.classpath
10+
.factorypath
11+
.project
12+
.settings
13+
.springBeans
14+
.sts4-cache
15+
16+
### IntelliJ IDEA ###
17+
.idea
18+
*.iws
19+
*.iml
20+
*.ipr
21+
22+
### NetBeans ###
23+
/nbproject/private/
24+
/nbbuild/
25+
/dist/
26+
/nbdist/
27+
/.nb-gradle/
28+
build/
29+
30+
### VS Code ###
31+
.vscode/

springboot-dataway/README.md

Lines changed: 240 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,240 @@
1+
# 工程简介
2+
> Spring Boot 整合 Dataway(DataQL/Hasor)的示例工程。
3+
4+
5+
# 搭建步骤
6+
## 前言:
7+
> 让 SpringBoot 不在需要 Controller, service, dao、mapper 层的书写。(言外之意,这些个性化的接口编写的权限交给了hasor 前端控制面的 DataQL 去自定义和修改 接口的服务逻辑)
8+
9+
## 一、认识 Dataway
10+
1. Dataway 是基于 DataQL 服务聚合能力,为应用提供的一个接口配置工具。
11+
2. 使得使用者无需开发任何代码就配置一个满足需求的接口。整个接口配置、测试、冒烟、发布。
12+
3. 一站式都通过 Dataway 提供的 UI 界面完成。
13+
4. UI 会以 Jar 包方式提供并集成到应用中并和应用共享同一个 http 端口,应用无需单独为 Dataway 开辟新的管理端口。
14+
这种内嵌集成方式模式的优点是,可以使得大部分老项目都可以在无侵入的情况下直接应用 Dataway。进而改进老项目的迭代效率,大大减少企业项目研发成本。
15+
5. Dataway 工具化的提供 DataQL 配置能力。这种研发模式的变革使得,相当多的需求开发场景只需要配置即可完成交付。从而避免了从数据存取到前端接口之间的一系列开发任务,例如:Mapper、BO、VO、DO、DAO、Service、Controller 统统不在需要。
16+
6. Dataway 是 Hasor 生态中的一员,因此在  Spring 中使用 Dataway 首先要做的就是打通两个生态。根据官方文档中推荐的方式我们将 Hasor 和 Spring Boot 整合起来。
17+
> 文章地址: https://www.hasor.net/web/extends/spring/for_boot.html
18+
19+
20+
## 二、初步体验
21+
###(1)引入核心依赖
22+
```xml
23+
<!-- hasor-spring 负责 Spring 和 Hasor 框架之间的整合。 -->
24+
<dependency>
25+
<groupId>net.hasor</groupId>
26+
<artifactId>hasor-spring</artifactId>
27+
<version>4.1.6</version>
28+
</dependency>
29+
30+
<!-- hasor-dataway 是工作在 Hasor 之上,利用 hasor-spring 我们就可以使用 dataway了。 -->
31+
<dependency>
32+
<groupId>net.hasor</groupId>
33+
<artifactId>hasor-dataway</artifactId>
34+
<version>4.1.6</version>
35+
</dependency>
36+
```
37+
38+
39+
###(2)配置 Dataway, 初始化数据表
40+
41+
#### ① 配置 application.properties
42+
> 说明:dataway 会提供一个界面让我们配置接口,这一点类似 Swagger 只要jar包集成就可以实现接口配置。找到我们 springboot 项目的配置文件 application.properties
43+
```properties
44+
# 是否启用 Dataway 功能(必选:默认false)
45+
HASOR_DATAQL_DATAWAY=true
46+
47+
# 是否开启 Dataway 后台管理界面(必选:默认false)
48+
HASOR_DATAQL_DATAWAY_ADMIN=true
49+
50+
# dataway API工作路径(可选,默认:/api/)
51+
HASOR_DATAQL_DATAWAY_API_URL=/api/
52+
53+
# dataway-ui 的工作路径(可选,默认:/interface-ui/)
54+
HASOR_DATAQL_DATAWAY_UI_URL=/interface-ui/
55+
56+
# SQL执行器方言设置(可选,建议设置)
57+
HASOR_DATAQL_FX_PAGE_DIALECT=mysql
58+
```
59+
60+
61+
#### ② 创建数据表
62+
> 说明:Dataway 需要两个数据表才能工作,下面是这两个数据表的简表语句。下面这个 SQL 可以在 dataway的依赖 jar 包中 “META-INF/hasor-framework/mysql” 目录下面找到,建表语句是用 mysql 语法写的。
63+
```sql
64+
CREATE TABLE `interface_info` (
65+
`api_id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'ID',
66+
`api_method` varchar(12) NOT NULL COMMENT 'HttpMethod:GET、PUT、POST',
67+
`api_path` varchar(512) NOT NULL COMMENT '拦截路径',
68+
`api_status` int(2) NOT NULL COMMENT '状态:0草稿,1发布,2有变更,3禁用',
69+
`api_comment` varchar(255) NULL COMMENT '注释',
70+
`api_type` varchar(24) NOT NULL COMMENT '脚本类型:SQL、DataQL',
71+
`api_script` mediumtext NOT NULL COMMENT '查询脚本:xxxxxxx',
72+
`api_schema` mediumtext NULL COMMENT '接口的请求/响应数据结构',
73+
`api_sample` mediumtext NULL COMMENT '请求/响应/请求头样本数据',
74+
`api_option` mediumtext NULL COMMENT '扩展配置信息',
75+
`api_create_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
76+
`api_gmt_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '修改时间',
77+
PRIMARY KEY (`api_id`)
78+
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8mb4 COMMENT='Dataway 中的API';
79+
80+
CREATE TABLE `interface_release` (
81+
`pub_id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'Publish ID',
82+
`pub_api_id` int(11) NOT NULL COMMENT '所属API ID',
83+
`pub_method` varchar(12) NOT NULL COMMENT 'HttpMethod:GET、PUT、POST',
84+
`pub_path` varchar(512) NOT NULL COMMENT '拦截路径',
85+
`pub_status` int(2) NOT NULL COMMENT '状态:0有效,1无效(可能被下线)',
86+
`pub_type` varchar(24) NOT NULL COMMENT '脚本类型:SQL、DataQL',
87+
`pub_script` mediumtext NOT NULL COMMENT '查询脚本:xxxxxxx',
88+
`pub_script_ori` mediumtext NOT NULL COMMENT '原始查询脚本,仅当类型为SQL时不同',
89+
`pub_schema` mediumtext NULL COMMENT '接口的请求/响应数据结构',
90+
`pub_sample` mediumtext NULL COMMENT '请求/响应/请求头样本数据',
91+
`pub_option` mediumtext NULL COMMENT '扩展配置信息',
92+
`pub_release_time`datetime DEFAULT CURRENT_TIMESTAMP COMMENT '发布时间(下线不更新)',
93+
PRIMARY KEY (`pub_id`)
94+
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8mb4 COMMENT='Dataway API 发布历史。';
95+
96+
create index idx_interface_release on interface_release (pub_api_id);
97+
98+
```
99+
100+
###(3)配置数据源
101+
> 说明:
102+
如果项目已经集成了自己的数据源,那么可以忽略这一步骤。
103+
作为 Spring Boot 项目有着自己完善的数据库方面工具支持。我们这次采用 druid + mysql + spring-boot-starter-jdbc 的方式
104+
#### ① 引入其他相关依赖
105+
```xml
106+
<dependency>
107+
<groupId>mysql</groupId>
108+
<artifactId>mysql-connector-java</artifactId>
109+
<version>5.1.30</version>
110+
</dependency>
111+
<dependency>
112+
<groupId>com.alibaba</groupId>
113+
<artifactId>druid</artifactId>
114+
<version>1.1.21</version>
115+
</dependency>
116+
<dependency>
117+
<groupId>org.springframework.boot</groupId>
118+
<artifactId>spring-boot-starter-jdbc</artifactId>
119+
</dependency>
120+
<dependency>
121+
<groupId>com.alibaba</groupId>
122+
<artifactId>druid-spring-boot-starter</artifactId>
123+
<version>1.1.10</version>
124+
</dependency>
125+
<dependency>
126+
<groupId>org.springframework.boot</groupId>
127+
<artifactId>spring-boot-starter-web</artifactId>
128+
</dependency>
129+
```
130+
131+
#### ② 增加数据源配置
132+
```properties
133+
# db
134+
spring.datasource.url=jdbc:mysql://localhost:3306/example
135+
spring.datasource.username=root
136+
spring.datasource.password=root
137+
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
138+
spring.datasource.type:com.alibaba.druid.pool.DruidDataSource
139+
# druid
140+
spring.datasource.druid.initial-size=3
141+
spring.datasource.druid.min-idle=3
142+
spring.datasource.druid.max-active=10
143+
spring.datasource.druid.max-wait=60000
144+
spring.datasource.druid.stat-view-servlet.login-username=admin
145+
spring.datasource.druid.stat-view-servlet.login-password=admin
146+
spring.datasource.druid.filter.stat.log-slow-sql=true
147+
spring.datasource.druid.filter.stat.slow-sql-millis=1
148+
```
149+
150+
151+
###(4)把数据源设置到 Hasor 中
152+
> 说明:
153+
Spring Boot 和 Hasor 本是两个独立的容器框架,我们做整合之后为了使用 Dataway 的能力需要把 Spring 中的数据源设置到 Hasor 中。
154+
首先新建一个 Hasor 的 模块,并且将其交给 Spring 管理。然后把数据源通过 Spring 注入进来。
155+
Hasor 启动的时候会调用 loadModule 方法,在这里再把 DataSource 设置到 Hasor 中。
156+
157+
```java
158+
@DimModule
159+
@Component
160+
public class ExampleModule implements SpringModule {
161+
@Autowired
162+
private DataSource dataSource = null;
163+
164+
@Override
165+
public void loadModule(ApiBinder apiBinder) throws Throwable {
166+
// .DataSource form Spring boot into Hasor
167+
apiBinder.installModule(new JdbcModule(Level.Full, this.dataSource));
168+
}
169+
}
170+
```
171+
172+
###(5)在 SpringBoot 中启用 Hasor
173+
> 说明:这一步非常简单,只需要在 Spring 启动类上增加 两个注解 即可。
174+
```java
175+
@EnableHasor()
176+
@EnableHasorWeb()
177+
@SpringBootApplication(scanBasePackages = {"net.example.hasor"})
178+
public class ExampleApplication {
179+
public static void main(String[] args) {
180+
SpringApplication.run(ExampleApplication.class, args);
181+
}
182+
}
183+
```
184+
185+
186+
###(6)启动应用
187+
> 说明:应用在启动过程中会看到 Hasor Boot 的欢迎信息。
188+
```text
189+
_ _ ____ _
190+
| | | | | _ | |
191+
| |__| | __ _ ___ ___ _ __ | |_) | ___ ___ | |_
192+
| __ |/ _` / __|/ _ | '__| | _ < / _ / _ | __|
193+
| | | | (_| __ (_) | | | |_) | (_) | (_) | |_
194+
|_| |_|__,_|___/___/|_| |____/ ___/ ___/ __|
195+
```
196+
当后面的日志中可以看到:dataway api workAt /api/ 、 dataway admin workAt /interface-ui/ 信息时,就可以确定 Dataway 的配置已经生效了。
197+
```text
198+
_ _ ____ _
199+
| | | | | _ \ | |
200+
| |__| | __ _ ___ ___ _ __ | |_) | ___ ___ | |_
201+
| __ |/ _` / __|/ _ \| '__| | _ < / _ \ / _ \| __|
202+
| | | | (_| \__ \ (_) | | | |_) | (_) | (_) | |_
203+
|_| |_|\__,_|___/\___/|_| |____/ \___/ \___/ \__|
204+
205+
2021-10-11 12:28:35.702 INFO 1552 --- [ main] net.hasor.core.Hasor : runMode at Full ,runPath at D:\Workspace\GitProjects\SpringBootDemo\springboot-dataway
206+
2021-10-11 12:28:35.705 INFO 1552 --- [ main] n.h.c.setting.StandardContextSettings : addConfig '/META-INF/hasor-framework/core-hconfig.xml' in 'jar:file:/D:/maven_repository/net/hasor/hasor-core/4.1.6/hasor-core-4.1.6.jar!/META-INF/hasor.schemas'
207+
2021-10-11 12:28:35.706 INFO 1552 --- [ main] n.h.c.setting.StandardContextSettings : addConfig '/META-INF/hasor-framework/dataway-hconfig.xml' in 'jar:file:/D:/maven_repository/net/hasor/hasor-dataway/4.1.6/hasor-dataway-4.1.6.jar!/META-INF/hasor.schemas'
208+
2021-10-11 12:28:35.707 INFO 1552 --- [ main] n.h.c.setting.StandardContextSettings : addConfig '/META-INF/hasor-framework/web-hconfig.xml' in 'jar:file:/D:/maven_repository/net/hasor/hasor-web/4.1.6/hasor-web-4.1.6.jar!/META-INF/hasor.schemas'
209+
2021-10-11 12:28:35.707 INFO 1552 --- [ main] n.h.c.setting.StandardContextSettings : addConfig '/META-INF/hasor-framework/db-hconfig.xml' in 'jar:file:/D:/maven_repository/net/hasor/hasor-db/4.1.6/hasor-db-4.1.6.jar!/META-INF/hasor.schemas'
210+
2021-10-11 12:28:35.708 INFO 1552 --- [ main] n.h.c.setting.StandardContextSettings : addConfig '/META-INF/hasor-framework/dataql-hconfig.xml' in 'jar:file:/D:/maven_repository/net/hasor/hasor-dataql/4.1.6/hasor-dataql-4.1.6.jar!/META-INF/hasor.schemas'
211+
2021-10-11 12:28:35.708 INFO 1552 --- [ main] n.h.c.setting.StandardContextSettings : addConfig '/META-INF/hasor-framework/dataql-fx-hconfig.xml' in 'jar:file:/D:/maven_repository/net/hasor/hasor-dataql-fx/4.1.6/hasor-dataql-fx-4.1.6.jar!/META-INF/hasor.schemas'
212+
2021-10-11 12:28:35.737 INFO 1552 --- [ main] n.h.c.environment.AbstractEnvironment : loadPackages = com.*, net.*, net.hasor.*, net.hasor.dataql.*, net.hasor.dataql.fx.*, net.hasor.dataway.*, net.hasor.db.*, net.hasor.web.*, org.*
213+
2021-10-11 12:28:35.780 INFO 1552 --- [ main] n.hasor.core.context.TemplateAppContext : loadModule class net.hasor.dataway.config.DatawayModule
214+
2021-10-11 12:28:35.780 INFO 1552 --- [ main] net.hasor.dataway.config.DatawayModule : dataway api workAt /api/
215+
2021-10-11 12:28:35.780 INFO 1552 --- [ main] n.h.c.environment.AbstractEnvironment : var -> HASOR_DATAQL_DATAWAY_API_URL = /api/.
216+
2021-10-11 12:28:35.785 INFO 1552 --- [ main] net.hasor.dataway.config.DatawayModule : dataway self isolation ->net.hasor.dataway.config.DatawayModule
217+
2021-10-11 12:28:35.787 INFO 1552 --- [ main] net.hasor.dataway.config.DatawayModule : dataway admin workAt /interface-ui/
218+
2021-10-11 12:28:35.794 INFO 1552 --- [ main] net.hasor.core.binder.ApiBinderWrap : mapingTo[92d9cd1c966e451080db3f66045edc88] -> bindType 'class net.hasor.dataway.web.ApiDetailController' mappingTo: '[/interface-ui/api/api-detail]'.
219+
2021-10-11 12:28:35.795 INFO 1552 --- [ main] net.hasor.core.binder.ApiBinderWrap : mapingTo[aa5555875e2044c99581d8f6386af769] -> bindType 'class net.hasor.dataway.web.ApiHistoryListController' mappingTo: '[/interface-ui/api/api-history]'.
220+
2021-10-11 12:28:35.795 INFO 1552 --- [ main] net.hasor.core.binder.ApiBinderWrap : mapingTo[f6e085739ca94f608138ce08508c9628] -> bindType 'class net.hasor.dataway.web.ApiInfoController' mappingTo: '[/interface-ui/api/api-info]'.
221+
2021-10-11 12:28:35.795 INFO 1552 --- [ main] net.hasor.core.binder.ApiBinderWrap : mapingTo[499cef05bd7f4ec592133d176d919089] -> bindType 'class net.hasor.dataway.web.ApiListController' mappingTo: '[/interface-ui/api/api-list]'.
222+
2021-10-11 12:28:35.796 INFO 1552 --- [ main] net.hasor.core.binder.ApiBinderWrap : mapingTo[94d2a6c388074cabb44c3fd5dddfae59] -> bindType 'class net.hasor.dataway.web.ApiHistoryGetController' mappingTo: '[/interface-ui/api/get-history]'.
223+
2021-10-11 12:28:35.798 INFO 1552 --- [ main] net.hasor.core.binder.ApiBinderWrap : mapingTo[6b701bc97888477da0492e1aa6889777] -> bindType 'class net.hasor.dataway.web.DisableController' mappingTo: '[/interface-ui/api/disable]'.
224+
2021-10-11 12:28:35.798 INFO 1552 --- [ main] net.hasor.core.binder.ApiBinderWrap : mapingTo[53285ad24f6f4648b2f2e573fe3a0adc] -> bindType 'class net.hasor.dataway.web.SmokeController' mappingTo: '[/interface-ui/api/smoke]'.
225+
2021-10-11 12:28:35.799 INFO 1552 --- [ main] net.hasor.core.binder.ApiBinderWrap : mapingTo[628678b132784efc8160c46a8665b455] -> bindType 'class net.hasor.dataway.web.SaveApiController' mappingTo: '[/interface-ui/api/save-api]'.
226+
2021-10-11 12:28:35.799 INFO 1552 --- [ main] net.hasor.core.binder.ApiBinderWrap : mapingTo[5938a7dfc07f48f288ae607fc8a66074] -> bindType 'class net.hasor.dataway.web.PublishController' mappingTo: '[/interface-ui/api/publish]'.
227+
2021-10-11 12:28:35.800 INFO 1552 --- [ main] net.hasor.core.binder.ApiBinderWrap : mapingTo[7c277730d1d24d1e9ef28f41cf50d00b] -> bindType 'class net.hasor.dataway.web.PerformController' mappingTo: '[/interface-ui/api/perform]'.
228+
2021-10-11 12:28:35.800 INFO 1552 --- [ main] net.hasor.core.binder.ApiBinderWrap : mapingTo[8c35e7df02cc45cd91b80f77f069d853] -> bindType 'class net.hasor.dataway.web.DeleteController' mappingTo: '[/interface-ui/api/delete]'.
229+
```
230+
### (7)访问接口管理页面进行接口配置(♥)
231+
> 说明:在浏览器中输入 http://127.0.0.1:8080/interface-ui/ 就可以看到期待已久的界面了。
232+
233+
234+
# 延伸阅读
235+
- 知识借鉴:[让SpringBoot不再需要Controller、Service、Mapper,这款开源工具绝了!!!](https://mp.weixin.qq.com/s/UCbhmivLK2LtQY1z88KKPA)
236+
- 官方手册:[Hasor 介绍 - Hasor - ClouGence@Hasor](https://www.hasor.net/doc/)
237+
- Dataway 官方手册:[https://www.hasor.net/web/dataway/about.html](https://www.hasor.net/web/dataway/about.html)
238+
- Dataway 在 OSC 上的项目地址,欢迎收藏:[https://www.oschina.net/p/dataway](https://www.oschina.net/p/dataway)
239+
- DataQL 手册地址:[https://www.hasor.net/web/dataql/what_is_dataql.html](https://www.hasor.net/web/dataql/what_is_dataql.html)
240+
- Hasor 项目的首页:[https://www.hasor.net/web/index.html](https://www.hasor.net/web/index.html)

0 commit comments

Comments
 (0)