背景:前端开发需要用到后台提供的接口,可以考虑一些第三方接口平台,同时可以自己开发一个公共API接口平台。
目的:完成一个开放API接口平台
API接口平台应该具备的功能,参照现有的平台功能,再添加自己需要的内容:
- 防止攻击(安全性)
- 不能随便调用(限制、开通)
- 统计调用次数
- 计费
- 流量保护
- API接入
做一个提供API接口调用的平台。
- 用户可以注册登录,开通接口调用权限。
- 用户可以使用接口,并且每次调用会进行统计。
- 管理员可以发布接口、下线接口、接入接口,以及可视化接口的调用情况、数据。
- Ant Design Pro
- Ant Design Procompoments
- React
- Umi
- Umi Request (Axios的封装)
-
spring boot
-
Spring Boot Starter (SDk开发)
-
网关、限流、日志实现
-
MySQL 数据库
-
Redis 内存数据库
-
Elasticsearch 搜索引擎
-
腾讯云 COS 对象存储
- Easy Excel 表格处理
- Hutool 工具库
- Gson 解析库
- Apache Commons Lang3 工具类
- Lombok 注解
- Spring Session Redis 分布式登录
- 全局请求响应拦截器(记录日志)
- 全局异常处理器
- 自定义错误码
- 封装通用响应类
- Swagger + Knife4j 接口文档
- 自定义权限注解 + 全局校验
- 全局跨域处理
- 长整数丢失精度解决
- 多环境配置
- JUnit5 单元测试
- 示例单元测试类
- 合理分层
所有需要修改的地方都标记了
todo
,便于大家找到修改的位置~
1)修改 application.yml
的数据库配置为你自己的:
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/my_db
username: root
password: root
2)执行 sql/create_table.sql
中的数据库语句,自动创建库表
3)启动项目,访问 http://localhost:8101/api/doc.html
即可打开接口文档,不需要写前端就能在线调试接口了~
1)修改 application.yml
的 Redis 配置为你自己的:
spring:
redis:
database: 1
host: localhost
port: 6379
timeout: 5000
password: 123456
2)修改 application.yml
中的 session 存储方式:
spring:
session:
store-type: redis
3)移除 MainApplication
类开头 @SpringBootApplication
注解内的 exclude 参数:
修改前:
@SpringBootApplication(exclude = {RedisAutoConfiguration.class})
修改后:
@SpringBootApplication
1)修改 application.yml
的 Elasticsearch 配置为你自己的:
spring:
elasticsearch:
uris: http://localhost:9200
username: root
password: 123456
2)复制 sql/post_es_mapping.json
文件中的内容,通过调用 Elasticsearch 的接口或者 Kibana Dev Tools 来创建索引(相当于数据库建表)
PUT post_v1
{
参数见 sql/post_es_mapping.json 文件
}
这步不会操作的话需要补充下 Elasticsearch 的知识,或者自行百度一下~
3)开启同步任务,将数据库的帖子同步到 Elasticsearch
找到 job 目录下的 FullSyncPostToEs
和 IncSyncPostToEs
文件,取消掉 @Component
注解的注释,再次执行程序即可触发同步:
// todo 取消注释开启任务
//@Component