一、pig4cloud介绍
Pig4Cloud 是一个基于 Spring Cloud 的企业级微服务架构平台,专注于快速构建数字化业务系统,支持开源与商业双版本。
Github地址:https://github.com/pig-mesh/pig
架构
pig
├── pig-boot -- 单体模式启动器[9999]
├── pig-auth -- 授权服务提供[3000]
└── pig-common -- 系统公共模块
├── pig-common-bom -- 全局依赖管理控制
├── pig-common-core -- 公共工具类核心包
├── pig-common-datasource -- 动态数据源包
├── pig-common-log -- 日志服务
├── pig-common-oss -- 文件上传工具类
├── pig-common-mybatis -- mybatis 扩展封装
├── pig-common-seata -- 分布式事务
├── pig-common-websocket -- websocket 封装
├── pig-common-security -- 安全工具类
├── pig-common-swagger -- 接口文档
├── pig-common-feign -- feign 扩展封装
└── pig-common-xss -- xss 安全封装
├── pig-register -- Nacos Server[8848]
├── pig-gateway -- Spring Cloud Gateway网关[9999]
└── pig-upms -- 通用用户权限管理模块
└── pig-upms-api -- 通用用户权限管理系统公共api模块
└── pig-upms-biz -- 通用用户权限管理系统业务处理模块[4000]
└── pig-visual
└── pig-monitor -- 服务监控 [5001]
├── pig-codegen -- 图形化代码生成 [5002]
└── pig-quartz -- 定时任务管理台 [5007]
依赖
依赖 | 版本 |
---|---|
Spring Boot | 3.5.5 |
Spring Cloud | 2025 |
Spring Cloud Alibaba | 2023 |
Spring Authorization Server | 1.5.2 |
Mybatis Plus | 3.5.12 |
二、运行后端
环境准备
- maven3.9
- jdk17
- idea社区版/商业版
- redis7.4
- MySQL8.0
1.初始化数据库
执行初始化脚本,生成数据库。
2.配置数据库链接
3.运行pig-register
注意:pig内置nacos3.5
4.访问nacos
地址:http://127.0.0.1:8080
账号:nacos/nacos
5.运行pig-auth
注意:需要先启动redis
6.运行pig-upms-biz
7.运行pig-gateway
全部正常运行后如下:
8.访问统一身份平台界面
地址:http://localhost:3000/token/login
三、运行前端
代码地址:https://github.com/pig-mesh/pig-ui
前置条件
必须运行Pig后端服务:
环境准备
- Node.js: 版本 18.0.0
- npm: 版本 8.0.0 或更高
克隆并运行前端程序
下载代码后安装并运行前端程序:
npm i
npm run dev
访问前端:http://localhost:8888/
四、接口与调试
前置条件
需启动以下服务:
swagger
- 微服务swagger: http://127.0.0.1:9999/swagger-ui.html
- 单体服务swagger:http://127.0.0.1:9999/admin/swagger-ui.html
模拟权限:
username: admin
password: YehdBPev (如果不正确改为123456)
client_id: test
client_secret : test
knife4j
- 微服务地址:http://127.0.0.1:9999/doc.html
- 单体地址:http://127.0.0.1:9999/admin/doc.html
username: admin
password: YehdBPev
client_id: test
client_secret : test
五、定制化开发
1.生成模块
在线生成:https://pig4cloud.com/data/other/archetype.html?v=3.9.0
- 自动生成目录结构
- 自动写入pom依赖
2.自动生成代码
不推荐:一般只用作制式数据库表的增删改查生成,现在已有代码逻辑,不需要使用该功能
如果要用:一般启用PigCodeGenApplication服务,在前端登录后使用。
3.配置nacos的yml
本步骤主要配置对应微服务的数据库配置,可按个人需要修改或新增配置。
4.服务鉴权
- 对外服务一律使用网关+鉴权模式,即通过url通过路由网关进入,再由鉴权服务完成令牌发放,最后进入需要调用接口。
- 对内服务一律使用OpenFeign调用,并特别注意:被调用的服务要标注@Inner注解 。否则也将走鉴权机制。
例如:
Controller:
/**
* 获取指定用户全部信息
*
* @return 用户信息
*/
@Inner
@GetMapping("/info/{username}")
public R info(@PathVariable String username) {
SysUser user = userService.getOne(Wrappers.<SysUser>query()
.lambda().eq(SysUser::getUsername, username));
if (user == null) {
return R.failed(null, String.format("用户信息为空 %s", username));
}
return R.ok(userService.findUserInfo(user));
}
Feign接口:
@FeignClient(contextId = "remoteUserService", value = ServiceNameConstants.UMPS_SERVICE)
public interface RemoteUserService {
/**
* 通过用户名查询用户、角色信息
*
* @param username 用户名
* @param from 调用标志
* @return R
*/
@GetMapping("/user/info/{username}")
R<UserInfo> info(@PathVariable("username") String username
, @RequestHeader(SecurityConstants.FROM) String from);
}
Feign-client:
/**
* 用户密码登录
*
* @param username 用户名
* @return
* @throws UsernameNotFoundException
*/
@Override
@SneakyThrows
public UserDetails loadUserByUsername(String username) {
Cache cache = cacheManager.getCache(CacheConstants.USER_DETAILS);
if (cache != null && cache.get(username) != null) {
return (PigxUser) cache.get(username).get();
}
R<UserInfo> result = remoteUserService.info(username, SecurityConstants.FROM_IN);
UserDetails userDetails = getUserDetails(result);
cache.put(username, userDetails);
return userDetails;
}
5.对外暴露接口(绕过鉴权)
方法1:
// 如果配置在controller类上 是整个类的接口对外暴露
@Inner(value = false)
@GetMapping("/")
public R api() {
}
方法2:
security:
oauth2:
ignore:
urls:
- 目标接口的Ant表达式即可
六、部署
1.使用docker compose部署(微服务)
cd /doc #包含docker-compose.yaml的目录
docker compose up -d
docker-compose.yaml:
version: '3'
services:
pig-mysql:
image: registry.cn-hangzhou.aliyuncs.com/pigx/pig-mysql:latest
environment:
MYSQL_ROOT_HOST: "%"
MYSQL_ROOT_PASSWORD: root
restart: always
container_name: pig-mysql
ports:
- 33306:3306
pig-redis:
image: redis:7
ports:
- 36379:6379
restart: always
container_name: pig-redis
pig-register:
image: registry.cn-hangzhou.aliyuncs.com/pigx/pig-register:latest
restart: always
ports:
- 8848:8848
- 9848:9848
container_name: pig-register
pig-gateway:
image: registry.cn-hangzhou.aliyuncs.com/pigx/pig-gateway:latest
restart: always
ports:
- 9999:9999
container_name: pig-gateway
pig-auth:
image: registry.cn-hangzhou.aliyuncs.com/pigx/pig-auth:latest
restart: always
container_name: pig-auth
pig-upms:
image: registry.cn-hangzhou.aliyuncs.com/pigx/pig-upms:latest
restart: always
container_name: pig-upms
pig-monitor:
image: registry.cn-hangzhou.aliyuncs.com/pigx/pig-monitor:latest
restart: always
ports:
- 5001:5001
container_name: pig-monitor
pig-codegen:
image: registry.cn-hangzhou.aliyuncs.com/pigx/pig-codegen:latest
restart: always
container_name: pig-codegen
pig-quartz:
image: registry.cn-hangzhou.aliyuncs.com/pigx/pig-quartz:latest
restart: always
container_name: pig-quartz
pig-ui:
image: registry.cn-hangzhou.aliyuncs.com/pigx/pig-ui:latest
restart: always
ports:
- 80:80
2.单体服务部署
如果部署单体服务,需要在打包时使用boot方式做单体jar包。
修改pig-boot下的配置并选择boot打包:
七、其他优秀架构推荐
1.SpringBlade
- (https://github.com/chillzhuang/SpringBlade) 6.7k star
- [√]多租户(SaaS)、鉴权、代码生成、资源管理、文件服务、分布式服务
- [×]文档收费
- Apache-2.0 license
2.pig4cloud
- (https://github.com/pig-mesh/pig) 6.3k star
- [√]鉴权、文件上传工具、定时任务、代码生成、监控中心、分布式服务、文档免费且丰富
- [×]不支持SaaS需定制
- Apache-2.0 license
3.ruoyi-cloud
- (https://github.com/yangzongzhuan/RuoYi-Cloud) 1.5k star
- [√]鉴权、缓存、定时任务、代码生成、文件服务、监控中心、数据脱敏、分布式服务、多数据源、文档免费且丰富、前后端
- [×]不支持SaaS需定制
- MIT license
- 补充:ruoyi-plus(https://github.com/dromara/RuoYi-Vue-Plus)项目是其他人基于ruoyi二次开发,功能更强,可做参考
4.Lamp-cloud
- (https://github.com/dromara/lamp-cloud) 5.6k star
- [√]多租户(SaaS)、鉴权、文件存储、分布式事务、定时任务
- [×]文档收费
- Apache-2.0 license