首页 > 其他分享 >springboot 集成 Dataway

springboot 集成 Dataway

时间:2024-02-28 16:16:19浏览次数:19  
标签:集成 COMMENT hasor springboot spring Dataway api NULL

1. Dataway简介

Dataway是Hasor生态中的一员,它通过提供一个UI界面来帮助开发者完成接口配置、测试、冒烟和发布等一系列任务。用户可以在Dataway的界面中进行接口的配置,无需编写任何代码。Dataway底层基于DataQL服务聚合能力,通过DataQL语言实现接口的聚合和定义,从而消除了传统开发中的繁琐代码编写过程。

Dataway的优势

  • 无需编码:Dataway的主要优势在于无需编写任何代码即可完成接口配置。开发人员只需使用Dataway提供的UI界面进行操作,大大降低了开发的技术门槛,使得需求交付更加迅速。
  • 一站式解决方案:Dataway不仅仅是一个接口配置工具,它还提供了测试、冒烟和发布等功能,使得整个接口的生命周期管理都能在Dataway的UI界面中完成,简化了开发和测试的流程。
  • 内嵌集成:Dataway可以以Jar包的形式提供,并且可以与Spring Boot应用程序共享同一个HTTP端口,无需单独开辟管理端口。这种内嵌集成方式可以无缝地融合到现有的项目中,降低了项目迁移的难度。

2. 整合Dataway与Spring Boot

  1. 导入依赖
       
   		<!-- Hasor 相关依赖 -->
  		<!-- hasor-spring 负责 Spring 和 Hasor 框架之间的整合 -->
		<dependency>
            <groupId>net.hasor</groupId>
            <artifactId>hasor-spring</artifactId>
            <version>4.1.3</version>
        </dependency>
  		<!-- hasor-dataway 是工作在 Hasor 之上 -->
        <dependency>
            <groupId>net.hasor</groupId>
            <artifactId>hasor-dataway</artifactId>
            <version>4.1.3-fix20200414</version><!-- 4.1.3 包存在UI资源缺失问题 -->
        </dependency>

   		<!-- 数据源依赖 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.30</version>
        </dependency>
        <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>
            <version>1.1.10</version>
        </dependency>
  1. 配置 Dataway

初始化数据表

server.port=8080
# 是否启用 Dataway 功能(必选:默认false)
HASOR_DATAQL_DATAWAY=true

# 是否开启 Dataway 后台管理界面(必选:默认false)
HASOR_DATAQL_DATAWAY_ADMIN=true

# dataway  API工作路径(可选,默认:/api/)
HASOR_DATAQL_DATAWAY_API_URL=/api/

# dataway-ui 的工作路径(可选,默认:/interface-ui/)
HASOR_DATAQL_DATAWAY_UI_URL=/interface-ui/

# SQL执行器方言设置(可选,建议设置)
HASOR_DATAQL_FX_PAGE_DIALECT=mysql

# db配置
spring.datasource.url=jdbc:mysql://IP地址:端口/数据库名?useSSL=false&useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=账号
spring.datasource.password=密码
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=1

Dataway 一共涉及到 5个可以配置的配置项,但不是所有配置都是必须的。

其中 HASOR_DATAQL_DATAWAYHASOR_DATAQL_DATAWAY_ADMIN 两个配置是必须要打开的,默认情况下 Datawaty 是不启用的。

Dataway 需要两个数据表才能工作,下面是这两个数据表的简表语句。下面这个 SQL 可以在 dataway的依赖 jar 包中 META-INF/hasor-framework/mysql 目录下面找到,建表语句是用 mysql 语法写的。

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 '拦截路径',
    `api_status`      int(2)       NOT NULL                  COMMENT '状态:0草稿,1发布,2有变更,3禁用',
    `api_comment`     varchar(255)     NULL                  COMMENT '注释',
    `api_type`        varchar(24)  NOT NULL                  COMMENT '脚本类型:SQL、DataQL',
    `api_script`      mediumtext   NOT NULL                  COMMENT '查询脚本:xxxxxxx',
    `api_schema`      mediumtext       NULL                  COMMENT '接口的请求/响应数据结构',
    `api_sample`      mediumtext       NULL                  COMMENT '请求/响应/请求头样本数据',
    `api_create_time` datetime     DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `api_gmt_time`    datetime     DEFAULT CURRENT_TIMESTAMP COMMENT '修改时间',
    PRIMARY KEY (`api_id`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8mb4 COMMENT='Dataway 中的API';

CREATE TABLE `interface_release` (
    `pub_id`          int(11)      NOT NULL AUTO_INCREMENT   COMMENT 'Publish ID',
    `pub_api_id`      int(11)      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`      int(2)       NOT NULL                  COMMENT '状态:0有效,1无效(可能被下线)',
    `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       NULL                  COMMENT '接口的请求/响应数据结构',
    `pub_sample`      mediumtext       NULL                  COMMENT '请求/响应/请求头样本数据',
    `pub_release_time`datetime     DEFAULT CURRENT_TIMESTAMP COMMENT '发布时间(下线不更新)',
    PRIMARY KEY (`pub_id`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8mb4 COMMENT='Dataway API 发布历史。';

create index idx_interface_release on interface_release (pub_api_id);
  1. 配置Hasor和Dataway的集成

创建一个配置类,并使用Spring Boot的注解来实现Hasor和Spring Boot的整合。在该配置类上添加@Configuration注解,并使用@HasorComponentScan注解来扫描Hasor组件。

import net.hasor.spring.boot.EnableHasor;
import net.hasor.spring.boot.EnableHasorWeb;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * <p>
 *
 * <p>
 *
 * @Author: xufan_yang
 * @Date 2023/7/25/025 10:31
 */
@EnableHasor()    // 在Spring 中启用 Hasor
@EnableHasorWeb() // 将 hasor-web 配置到 Spring 环境中,Dataway 的 UI 是通过 hasor-web 提供服务。
@SpringBootApplication(scanBasePackages = { "com.xiaoyangtx.hasor" })
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}
  1. 创建Dataway的配置类

在项目中创建Dataway的配置类,并在该配置类上添加@EnableDataway注解来启用Dataway功能。

import net.hasor.core.ApiBinder;
import net.hasor.core.DimModule;
import net.hasor.db.JdbcModule;
import net.hasor.db.Level;
import net.hasor.spring.SpringModule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;

/**
 * <p>
 *
 * <p>
 *
 * @Author: xufan_yang
 * @Date 2023/7/25/025 10:26
 */
@DimModule
@Component
public class ExampleModule implements SpringModule {
    @Autowired
    private DataSource dataSource = null;

    public void loadModule(ApiBinder apiBinder) throws Throwable {
        // .DataSource form Spring boot into Hasor
        apiBinder.installModule(new JdbcModule(Level.Full, this.dataSource));
        // .custom DataQL
        //apiBinder.tryCast(QueryApiBinder.class).loadUdfSource(apiBinder.findClass(DimUdfSource.class));
        //apiBinder.tryCast(QueryApiBinder.class).bindFragment("sql", SqlFragment.class);
    }
}

整体目录结构

  1. 启动

现在,Hasor和Dataway已经成功整合到Spring Boot应用程序中。在应用程序启动时,Dataway的功能将会被启用,并可以通过Dataway的UI界面进行接口的配置和管理。

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

可以看到类似下面这些日志:

当看到 dataway api workAt /api/ dataway admin workAt /interface-ui/ 信息时,就可以确定 Dataway 的配置已经生效了。

3. 使用

  1. 访问页面

在浏览器中输入 http://127.0.0.1:8080/interface-ui/ 就可以看到界面了

  1. 新建接口

首先我们在 SQL 模式下尝试执行一条 select 查询,立刻就可以看到这条 SQL 的查询结果。

条件参数:

发布:

接口发布之后我们直接请求:http://127.0.0.1:8080/api/demos,就看到期待已久的接口返回值了。

Dataway是一个强大的接口配置工具,它通过提供UI界面来简化接口配置和管理流程,无需编写代码,降低了开发难度和研发成本。通过整合Dataway和Spring Boot,我们可以在现有项目中无缝地应用Dataway,并享受其带来的高效和便利。随着Dataway的不断发展和优化,相信它将在企业应用开发中发挥越来越重要的作用,帮助开发者更快、更轻松地交付高质量的接口服务。

官方连接:


原文链接:https://blog.csdn.net/severl/article/details/131915111

标签:集成,COMMENT,hasor,springboot,spring,Dataway,api,NULL
From: https://www.cnblogs.com/1399z3blog/p/18040719

相关文章

  • 关于druid与springboot版本问题
    datasource:druid:driver-class-name:${sky.datasource.driver-class-name}url:jdbc:mysql://${sky.datasource.host}:${sky.datasource.port}/${sky.datasource.database}?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=u......
  • 记一次WPF集成SemanticKernel+OneAPI+讯飞星火认知大模型实践
    开启OneAPI服务OneAPI介绍OpenAI接口管理&分发系统,支持Azure、AnthropicClaude、GooglePaLM2&Gemini、智谱ChatGLM、百度文心一言、讯飞星火认知、阿里通义千问、360智脑以及腾讯混元,可用于二次分发管理key,仅单可执行文件,已打包好Docker镜像,一键部署,开箱即用.Ope......
  • centos7环境用docker-compose部署mysql5.7集群,redis7.2.4,springboot项目
    文件目录结构关于每个配置项及docker-compose的安装,大家可以自己查mysql配置文件master---my.cnf[mysqld]server_id=1gtid-mode=ONenforce-gtid-consistency=1binlog-ignore-db=mysqllog-bin=mysql-binbinlog_cache_size=......
  • springboot两种配置文件的使用(举例:端口号、虚拟路径配置)
    1、properties配置文件使用   2、yml配置文件、yaml配置文件 在项目中更常用 在项目中一般yml配置文件更为常用,因为yml层级更加明确、清晰,更为关注数据。 ......
  • 如何在SpringBoot中优雅地重试调用第三方API?
    1引言在实际的应用中,我们经常需要调用第三方API来获取数据或执行某些操作。然而,由于网络不稳定、第三方服务异常等原因,API调用可能会失败。为了提高系统的稳定性和可靠性,我们通常会考虑实现重试机制。本文将深入探讨如何在SpringBoot项目中优雅地重试调用第三方API,并结合代码......
  • SpringBoot 2x 系列之(七)web场景
    web场景1.SpringMVC自动配置概览SpringBootprovidesauto-configurationforSpringMVCthatworkswellwithmostapplications.(大多场景我们都无需自定义配置)Theauto-configurationaddsthefollowingfeaturesontopofSpring’sdefaults:InclusionofCont......
  • SpringBoot 2x 系列之(五)开发技巧
    开发技巧1.Lombok1.应用场景简化JavaBean的开发帮我们在编译时生成get、set、toString方法2.安装及使用引入依赖【SpringBoot已经做了版本仲裁】<dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></depende......
  • SpringBoot 2x 系列之(四)自动配置
    自动配置1.引导加载自动配置类见SpringBoot1.md一、SpringBoot入门4.2主程序类,主入口类【自动配置原理】1.1@SpringBootConfiguration见SpringBoot1.md一、SpringBoot入门4.2主程序类,主入口类【自动配置原理】[email protected]@AutoConfi......
  • SpringBoot 2x 系列之(三)容器功能
    容器功能1.组件添加1.1@Configuration结合@Bean注册组件@Configuration见[email protected]@Component、@Controller、@Service、@Repository见Spring.md相关内容1.3@ComponentScan见[email protected]@Import见Spring.md8.......
  • SpringBoot 2x 系列之(二)SpringBoot特点
    SpringBoot特点1.依赖管理特性SpringBoot已经为我们做了版本仲裁,那么如果我们要修改已经做了版本仲裁的依赖,比如mysql驱动的依赖,该怎么做呢?查看spring-boot-dependencies里面规定的当前依赖版本用的properties属性在当前项目里面重新配置<properties><mysql.versi......