Appearance
微服务应用
- 检查指定接口是否有访问权限
java
/**
* 检查指定接口是否有访问权限
*
* @param path 请求路径
* @param method 请求方法
* @return 是否有权限
*/
public Boolean checkUri(String path, String method) {
// 中间代码省略...
return apiList.parallelStream().distinct().anyMatch(item -> {
String uri = item.getUri();
/*
* 若您确定只使用acuity-boot,而非acuity-cloud,请将def_resource_api表中uri的代理的前缀(/base、/system、/oauth)去除,即可 删除删除删除 if里面的代码!
* 因为脚本数据是基于acuity-cloud配置的,所以uri地址会多一段gateway代理前缀。如
* acuity-cloud 中地址为:/base/baseEmployee/page
* 对应acuity-boot的地址为:/baseEmployee/page
* 其中/base是因为使用了gateway增加的!
*/
if (!StrUtil.startWithAny(uri, "/gateway")) {
uri = StrUtil.subSuf(uri, StrUtil.indexOf(uri, '/', 1));
}
boolean matchUri = PATH_MATCHER.match(StrUtil.trim(uri), StrUtil.trim(path));
log.info("path={}, uri={}, matchUri={}, method={} apiId={}", path, uri, matchUri, item.getRequestMethod(), item.getId());
if (HttpMethod.ALL.name().equalsIgnoreCase(item.getRequestMethod())) {
return matchUri;
}
return matchUri && StrUtil.equalsIgnoreCase(method, item.getRequestMethod());
});
}
/**
* 检查指定接口是否有访问权限
*
* @param path 请求路径
* @param method 请求方法
* @return 是否有权限
*/
public Boolean checkUri(String path, String method) {
// 中间代码省略...
return apiList.parallelStream().distinct().anyMatch(item -> {
String uri = item.getUri();
/*
* 若您确定只使用acuity-boot,而非acuity-cloud,请将def_resource_api表中uri的代理的前缀(/base、/system、/oauth)去除,即可 删除删除删除 if里面的代码!
* 因为脚本数据是基于acuity-cloud配置的,所以uri地址会多一段gateway代理前缀。如
* acuity-cloud 中地址为:/base/baseEmployee/page
* 对应acuity-boot的地址为:/baseEmployee/page
* 其中/base是因为使用了gateway增加的!
*/
if (!StrUtil.startWithAny(uri, "/gateway")) {
uri = StrUtil.subSuf(uri, StrUtil.indexOf(uri, '/', 1));
}
boolean matchUri = PATH_MATCHER.match(StrUtil.trim(uri), StrUtil.trim(path));
log.info("path={}, uri={}, matchUri={}, method={} apiId={}", path, uri, matchUri, item.getRequestMethod(), item.getId());
if (HttpMethod.ALL.name().equalsIgnoreCase(item.getRequestMethod())) {
return matchUri;
}
return matchUri && StrUtil.equalsIgnoreCase(method, item.getRequestMethod());
});
}
java
/**
* 检查指定接口是否有访问权限
*
* @param path 请求路径
* @param method 请求方法
* @return 是否有权限
*/
public Boolean checkUri(String path, String method) {
// 中间代码省略...
return apiList.parallelStream().distinct().anyMatch(item -> {
String uri = item.getUri();
boolean matchUri = PATH_MATCHER.match(StrUtil.trim(uri), StrUtil.trim(path));
log.info("path={}, uri={}, matchUri={}, method={} apiId={}", path, uri, matchUri, item.getRequestMethod(), item.getId());
if (HttpMethod.ALL.name().equalsIgnoreCase(item.getRequestMethod())) {
return matchUri;
}
return matchUri && StrUtil.equalsIgnoreCase(method, item.getRequestMethod());
});
}
/**
* 检查指定接口是否有访问权限
*
* @param path 请求路径
* @param method 请求方法
* @return 是否有权限
*/
public Boolean checkUri(String path, String method) {
// 中间代码省略...
return apiList.parallelStream().distinct().anyMatch(item -> {
String uri = item.getUri();
boolean matchUri = PATH_MATCHER.match(StrUtil.trim(uri), StrUtil.trim(path));
log.info("path={}, uri={}, matchUri={}, method={} apiId={}", path, uri, matchUri, item.getRequestMethod(), item.getId());
if (HttpMethod.ALL.name().equalsIgnoreCase(item.getRequestMethod())) {
return matchUri;
}
return matchUri && StrUtil.equalsIgnoreCase(method, item.getRequestMethod());
});
}
- 初始化数据源的实现也有所区别
java
// acuity-boot 项目GeneratorController位于acuity-boot-server
@ResponseBody
@ApiOperation(value = "查询在线服务的前缀")
@GetMapping("/gateway/findOnlineServicePrefix")
public R<Map<String, String>> findOnlineServicePrefix() {
Map<String, String> map = MapUtil.newHashMap();
map.put(application, "base");
return R.success(map);
}
// acuity-boot 项目GeneratorController位于acuity-boot-server
@ResponseBody
@ApiOperation(value = "查询在线服务的前缀")
@GetMapping("/gateway/findOnlineServicePrefix")
public R<Map<String, String>> findOnlineServicePrefix() {
Map<String, String> map = MapUtil.newHashMap();
map.put(application, "base");
return R.success(map);
}
java
// acuity-cloud 项目GeneratorController位于acuity-gateway-server
@ResponseBody
@ApiOperation(value = "查询在线服务的前缀")
@GetMapping("/gateway/findOnlineServicePrefix")
public R<Map<String, String>> findOnlineServicePrefix() {
List<String> services = discoveryClient.getServices();
Map<String, String> map = MapUtil.newHashMap();
services.forEach(service ->
gatewayProperties.getRoutes().forEach(route -> {
if (StrUtil.equalsIgnoreCase(service, route.getUri().getHost())) {
if (CollUtil.isEmpty(route.getPredicates())) {
return;
}
PredicateDefinition predicateDefinition = route.getPredicates().get(0);
predicateDefinition.getArgs().forEach((k, v) -> {
map.put(service, StrUtil.subBetween(v, "/", "/**"));
});
}
})
);
return R.success(map);
}
// acuity-cloud 项目GeneratorController位于acuity-gateway-server
@ResponseBody
@ApiOperation(value = "查询在线服务的前缀")
@GetMapping("/gateway/findOnlineServicePrefix")
public R<Map<String, String>> findOnlineServicePrefix() {
List<String> services = discoveryClient.getServices();
Map<String, String> map = MapUtil.newHashMap();
services.forEach(service ->
gatewayProperties.getRoutes().forEach(route -> {
if (StrUtil.equalsIgnoreCase(service, route.getUri().getHost())) {
if (CollUtil.isEmpty(route.getPredicates())) {
return;
}
PredicateDefinition predicateDefinition = route.getPredicates().get(0);
predicateDefinition.getArgs().forEach((k, v) -> {
map.put(service, StrUtil.subBetween(v, "/", "/**"));
});
}
})
);
return R.success(map);
}
acuity-cloud 项目亮点功能介绍:
服务注册&发现与调用:
基于Nacos来实现的服务注册与发现,使用使用Feign来实现服务互调, 可以做到使用HTTP请求远程调用时能与调用本地方法一样的编码体验,开发者完全感知不到这是远程方法,更感知不到这是个HTTP请求。
负载均衡:
将服务保留的rest进行代理和网关控制,除了平常经常使用的node.js、nginx外,Spring Cloud系列的gateway 和 loadbalancer 可以帮我们进行正常的网关管控和负载均衡。
RBAC:
通过JWT生成token,存储用户基本信息,并自研权限配置、鉴权方案。
熔断机制:
因为采取了服务的分布,为了避免服务之间的调用“雪崩”,采用了Sentinel的作为熔断器,避免了服务之间的“雪崩”。
监控:
利用Spring Boot Admin 来监控各个独立服务的运行状态、健康情况。
链路调用监控:
同时实现了SkyWalking作为本项目的全链路性能监控, 从整体维度到局部维度展示各项指标,将跨应用的所有调用链性能信息集中展现,可方便度量整体和局部性能,并且方便找到故障产生的源头,生产上可极大缩短故障排除时间。
数据权限
利用基于Mybatis的DataScopeInnerInterceptor拦截器实现了数据权限功能
SaaS(多租户)的无感解决方案
本项目支持3种常见的租户解决方案和无租户方案,同一套代码,修改一个配置即可实现租户模式只有切换。
缓存抽象
采用CacheOps操作缓存,内置2种实现:Caffeine、 Redis,可以让项目应急时在无Redis环境正常运行
优雅的Bean转换
采用Dozer、BeanUtil等组件来对 DTO、DO、PO等对象的优化转换
前后端统一表单验证
严谨的表单验证通常需要 前端+后端同时验证, 但传统的项目,均只能前后端各做一次检验, 后期规则变更,又得前后端同时修改。 故在
hibernate-validator
的基础上封装了acuity-validator-starter
起步依赖,提供一个通用接口,可以获取需要校验表单的规则,然后前端使用后端返回的规则, 以后若规则改变,只需要后端修改即可。防跨站脚本攻击(XSS)
- 通过过滤器对所有请求中的 表单参数 进行过滤
- 通过Json反序列化器实现对所有 application/json 类型的参数 进行过滤
当前登录用户信息注入器
- 通过注解实现用户身份注入
在线API
由于原生swagger-ui某些功能支持不够友好,故采用了国内开源的
knife4j
,并制作了stater,方便springboot用户使用。代码生成器
基于Mybatis-plus-generator自定义了一套代码生成器, 通过配置数据库字段的注释,自动生成枚举类、数据字典注解、SaveDTO、UpdateDTO、表单验证规则注解、Swagger注解等。
定时任务调度器:
基于xxl-jobs进行了功能增强。(如:指定时间发送任务、执行器和调度器合并项目、多数据源)
分布式事务
集成了阿里的分布式事务中间件:seata,以 高效 并且对业务 0侵入 的方式,解决 微服务 场景下面临的分布式事务问题。
跨表、跨库、跨服务的关联数据自动回显
用于解决跨表、跨库、跨服务分页数据的属性或单个对象的属性 回显关联数据之痛, 支持对静态数据属性(数据字典)、动态主键数据进行自动回显。
灰度发布
为了解决频繁的服务更新上线,版本回退,快速迭代,公司内部协同开发,本项目采用修改 loadbalancer 的负载均衡策略来实现来灰度发布。
acuity-cloud 技术栈:
- 开发方面:
- JSON序列化:Jackson
- 消息队列:RabbitMQ
- 缓存:Redis
- 数据库: MySQL 5.7.9 或者 MySQL 8.0.19
- 定时器:采用xxl-job项目进行二次改造
- 前端1(后台管理):vue2 + element-ui
- 前端2(后台管理):vue3 + ant-design-vue + vite + TypeScript
- 持久层框架: Mybatis-plus
- 代码生成器:基于Mybatis-plus-generator自定义
- API网关:Gateway
- 服务注册&发现和配置中心: Nacos
- 服务消费:OpenFeign
- 负载均衡:Ribbon
- 服务熔断:Sentinel
- 项目构建:Maven
- 分布式事务: seata
- 文件服务器:FastDFS 5.0.5/阿里云OSS/本地存储/MinIO/华为云/七牛云
- 监控方面:
- 监控: spring-boot-admin
- 链路调用跟踪: SkyWalking
- 分布式系统的流量防卫兵: Sentinel
- 部署方面:
- 服务器:CentOS
- Nginx
- Jenkins
- Docker
- Kubernetes