diff --git a/pigx-visual/pigx-dynamic-datasource/README.md b/pigx-visual/pigx-dynamic-datasource/README.md new file mode 100644 index 000000000..79907b1b0 --- /dev/null +++ b/pigx-visual/pigx-dynamic-datasource/README.md @@ -0,0 +1,91 @@ +## 快速入门 +### 创建测试数据库 +``` +create database `test1` default character set utf8mb4 collate utf8mb4_general_ci; +create database `test2` default character set utf8mb4 collate utf8mb4_general_ci; +create database `test3` default character set utf8mb4 collate utf8mb4_general_ci; +``` +### 创建测试表 +``` + CREATE TABLE `user` ( + `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'id', + `username` varchar(255) DEFAULT NULL COMMENT '用户名', + `password` varchar(255) DEFAULT NULL COMMENT '密码', + PRIMARY KEY (`id`) USING BTREE + ) ENGINE=InnoDB AUTO_INCREMENT=40 DEFAULT CHARSET=utf8mb4 COMMENT='用户测试表'; +``` +### 插入测试数据 +``` +use test1; +BEGIN; +INSERT INTO `user` VALUES (1, 'test1测试一', '123'); +INSERT INTO `user` VALUES (2, 'test1测试二', '123'); +INSERT INTO `user` VALUES (3, 'test1测试三', '123'); +COMMIT; +use test2; +BEGIN; +INSERT INTO `user` VALUES (1, 'test2测试一', '123'); +INSERT INTO `user` VALUES (2, 'test2测试二', '123'); +INSERT INTO `user` VALUES (3, 'test2测试三', '123'); +COMMIT; +use test3; +BEGIN; +INSERT INTO `user` VALUES (1, 'test3测试一', '123'); +INSERT INTO `user` VALUES (2, 'test3测试二', '123'); +INSERT INTO `user` VALUES (3, 'test3测试三', '123'); +COMMIT; +``` +### Nocas新增配置 +``` +## pigx-dynamic-datasource-biz.yml +## spring security 配置 +security: + oauth2: + client: + client-id: ENC(ltJPpR50wT0oIY9kfOe1Iw==) + client-secret: ENC(ltJPpR50wT0oIY9kfOe1Iw==) + scope: server + +# 数据源 +spring: + datasource: + dynamic: + primary: test1 #设置默认的数据源 + druid: + filters: stat + datasource: + test1: + username: ${MYSQL-USER:root} + password: ${MYSQL-USER:root} + url: jdbc:mysql://${MYSQL-HOST:pigx-mysql}:${MYSQL-PORT:3306}/test1?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8&allowMultiQueries=true&allowPublicKeyRetrieval=true + driver-class-name: com.mysql.cj.jdbc.Driver + test2: + username: ${MYSQL-USER:root} + password: ${MYSQL-USER:root} + url: jdbc:mysql://${MYSQL-HOST:pigx-mysql}:${MYSQL-PORT:3306}/test2?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8&allowMultiQueries=true&allowPublicKeyRetrieval=true + driver-class-name: com.mysql.cj.jdbc.Driver + test3: + username: ${MYSQL-USER:root} + password: ${MYSQL-USER:root} + url: jdbc:mysql://${MYSQL-HOST:pigx-mysql}:${MYSQL-PORT:3306}/test3?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8&allowMultiQueries=true&allowPublicKeyRetrieval=true + driver-class-name: com.mysql.cj.jdbc.Driver +``` +### 启动以下应用 ++ PigxNacosApplication ++ PigxSentinelApplication ++ PigxAuthApplication ++ PigxAdminApplication ++ PigxGatewayApplication ++ PigxDynamicDatasourceApplication +### Postman看效果 +``` +http://localhost:6060/test/getTest1 +http://localhost:6060/test/getTest2?current=1&size=5 +http://localhost:6060/test/getTest3?current=1&size=5 +``` +![test1数据库](http://i1.fuimg.com/704023/83e168e45242e135.png "test1数据库") +![test2数据库](http://i1.fuimg.com/704023/cf8c42fbc3512c32.png "test2数据库") +![test3数据库](http://i1.fuimg.com/704023/534483515e3f7cd0.png "test3数据库") + +### 联系我 +QQ:55215085 \ No newline at end of file diff --git a/pigx-visual/pigx-dynamic-datasource/pigx-dynamic-datasource-api/pom.xml b/pigx-visual/pigx-dynamic-datasource/pigx-dynamic-datasource-api/pom.xml new file mode 100644 index 000000000..d44172a36 --- /dev/null +++ b/pigx-visual/pigx-dynamic-datasource/pigx-dynamic-datasource-api/pom.xml @@ -0,0 +1,27 @@ + + + 4.0.0 + + com.pig4cloud + pigx-dynamic-datasource + 3.5.0 + + + pigx-dynamic-datasource-api + + + + + com.pig4cloud + pigx-common-core + + + + com.baomidou + mybatis-plus-extension + + + \ No newline at end of file diff --git a/pigx-visual/pigx-dynamic-datasource/pigx-dynamic-datasource-api/src/main/java/com/pig4cloud/pigx/dynamic/feign/packge-info.java b/pigx-visual/pigx-dynamic-datasource/pigx-dynamic-datasource-api/src/main/java/com/pig4cloud/pigx/dynamic/feign/packge-info.java new file mode 100644 index 000000000..b497b9232 --- /dev/null +++ b/pigx-visual/pigx-dynamic-datasource/pigx-dynamic-datasource-api/src/main/java/com/pig4cloud/pigx/dynamic/feign/packge-info.java @@ -0,0 +1,6 @@ +/* + @author pigx archetype + *

+ * feign client 存放目录,注意 @EnablePigxFeignClients 的扫描范围 + */ +package com.pig4cloud.pigx.dynamic.feign; \ No newline at end of file diff --git a/pigx-visual/pigx-dynamic-datasource/pigx-dynamic-datasource-biz/pom.xml b/pigx-visual/pigx-dynamic-datasource/pigx-dynamic-datasource-biz/pom.xml new file mode 100644 index 000000000..481d203cd --- /dev/null +++ b/pigx-visual/pigx-dynamic-datasource/pigx-dynamic-datasource-biz/pom.xml @@ -0,0 +1,99 @@ + + + 4.0.0 + + com.pig4cloud + pigx-dynamic-datasource + 3.5.0 + + + pigx-dynamic-datasource-biz + + + + + org.springframework.boot + spring-boot-starter-undertow + + + + org.springframework.boot + spring-boot-starter-web + + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-discovery + + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-config + + + + com.pig4cloud + pigx-common-data + + + com.baomidou + mybatis-plus-boot-starter + + + druid-spring-boot-starter + com.alibaba + 1.1.20 + + + + com.baomidou + dynamic-datasource-spring-boot-starter + 2.5.6 + + + mysql + mysql-connector-java + + + com.pig4cloud + pigx-common-log + + + + com.pig4cloud + pigx-common-security + + + + com.pig4cloud + pigx-common-sentinel + + + + com.pig4cloud + pigx-dynamic-datasource-api + 3.5.0 + + + + com.pig4cloud + pigx-common-swagger + + + + org.springframework.boot + spring-boot-starter-test + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + \ No newline at end of file diff --git a/pigx-visual/pigx-dynamic-datasource/pigx-dynamic-datasource-biz/src/main/java/com/pig4cloud/pigx/dynamic/PigxDynamicDatasourceApplication.java b/pigx-visual/pigx-dynamic-datasource/pigx-dynamic-datasource-biz/src/main/java/com/pig4cloud/pigx/dynamic/PigxDynamicDatasourceApplication.java new file mode 100644 index 000000000..e572a2d4e --- /dev/null +++ b/pigx-visual/pigx-dynamic-datasource/pigx-dynamic-datasource-biz/src/main/java/com/pig4cloud/pigx/dynamic/PigxDynamicDatasourceApplication.java @@ -0,0 +1,25 @@ +package com.pig4cloud.pigx.dynamic; + +import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure; +import com.pig4cloud.pigx.common.security.annotation.EnablePigxFeignClients; +import com.pig4cloud.pigx.common.security.annotation.EnablePigxResourceServer; +import com.pig4cloud.pigx.common.swagger.annotation.EnablePigxSwagger2; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.cloud.client.SpringCloudApplication; + +/** + * @author pigx archetype + *

+ * 项目启动类 + */ +@EnablePigxSwagger2 +@SpringCloudApplication +@EnablePigxFeignClients +@EnablePigxResourceServer +@EnableAutoConfiguration(exclude={DruidDataSourceAutoConfigure.class}) +public class PigxDynamicDatasourceApplication { + public static void main(String[] args) { + SpringApplication.run(PigxDynamicDatasourceApplication.class, args); + } +} diff --git a/pigx-visual/pigx-dynamic-datasource/pigx-dynamic-datasource-biz/src/main/java/com/pig4cloud/pigx/dynamic/controller/TestController.java b/pigx-visual/pigx-dynamic-datasource/pigx-dynamic-datasource-biz/src/main/java/com/pig4cloud/pigx/dynamic/controller/TestController.java new file mode 100644 index 000000000..8f6208e7d --- /dev/null +++ b/pigx-visual/pigx-dynamic-datasource/pigx-dynamic-datasource-biz/src/main/java/com/pig4cloud/pigx/dynamic/controller/TestController.java @@ -0,0 +1,96 @@ +/* + * + * Copyright (c) 2018-2025, lengleng All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: lengleng (wangiegie@gmail.com) + * + */ + +package com.pig4cloud.pigx.dynamic.controller; + +import com.baomidou.mybatisplus.core.metadata.IPage; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import com.pig4cloud.pigx.common.core.util.R; +import com.pig4cloud.pigx.common.log.annotation.SysLog; +import com.pig4cloud.pigx.dynamic.mapper.TestMapper; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import lombok.AllArgsConstructor; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; +import java.util.Map; + +/** + * @author liuyes + * @date 2019/11/19 + */ +@RestController +@AllArgsConstructor +@RequestMapping("/test") +@Api(value = "test", tags = "动态多数据源示例模块") +public class TestController { + + private TestMapper testMapper; + + /** + * 获取test1数据库用户信息 + * + * @return 用户信息 + */ + @ApiOperation(value = "查询test1数据库用户信息", notes = "查询test1数据库用户信息") + @SysLog("查询test1数据库用户信息" ) + @PostMapping("/getTest1") + public R getTest1() { + List> list = testMapper.getTest1(); + if (list == null) { + return R.failed(null, "查询失败!"); + } + return R.ok(list,"操作成功!"); + } + + /** + * 获取test2数据库用户信息 + * + * @return 用户信息 + */ + @ApiOperation(value = "查询test2数据库用户信息", notes = "查询test2数据库用户信息") + @SysLog("查询test2数据库用户信息" ) + @PostMapping("/getTest2") + public R getTest2(Page page) { + IPage>> list = testMapper.getTest2(page); + if (list == null) { + return R.failed(null, "查询失败!"); + } + return R.ok(list,"操作成功!"); + } + /** + * 获取test3数据库用户信息 + * + * @return 用户信息 + */ + @ApiOperation(value = "查询test3数据库用户信息", notes = "查询test3数据库用户信息") + @SysLog("查询test3数据库用户信息" ) + @PostMapping("/getTest3") + public R getTest3(Page page) { + IPage>> list = testMapper.getTest3(page); + if (list == null) { + return R.failed(null, "查询失败!"); + } + return R.ok(list,"操作成功!"); + } + +} diff --git a/pigx-visual/pigx-dynamic-datasource/pigx-dynamic-datasource-biz/src/main/java/com/pig4cloud/pigx/dynamic/mapper/TestMapper.java b/pigx-visual/pigx-dynamic-datasource/pigx-dynamic-datasource-biz/src/main/java/com/pig4cloud/pigx/dynamic/mapper/TestMapper.java new file mode 100644 index 000000000..4a4bbfa3d --- /dev/null +++ b/pigx-visual/pigx-dynamic-datasource/pigx-dynamic-datasource-biz/src/main/java/com/pig4cloud/pigx/dynamic/mapper/TestMapper.java @@ -0,0 +1,50 @@ +/* + * + * Copyright (c) 2018-2025, lengleng All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: lengleng (wangiegie@gmail.com) + * + */ + +package com.pig4cloud.pigx.dynamic.mapper; + +import com.baomidou.dynamic.datasource.annotation.DS; +import com.baomidou.mybatisplus.core.metadata.IPage; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import org.apache.ibatis.annotations.Select; + +import java.util.List; +import java.util.Map; + +/** + *

+ * test Mapper 接口 + *

+ * + * @author liuyes + * @date 2019/11/19 + */ +public interface TestMapper { + + @Select("SELECT * FROM user") + @DS("test1") + List> getTest1(); + + @DS("test2") + IPage>> getTest2(Page page); + + @DS("test3") + IPage>> getTest3(Page page); + +} diff --git a/pigx-visual/pigx-dynamic-datasource/pigx-dynamic-datasource-biz/src/main/java/com/pig4cloud/pigx/dynamic/vo/UserVO.java b/pigx-visual/pigx-dynamic-datasource/pigx-dynamic-datasource-biz/src/main/java/com/pig4cloud/pigx/dynamic/vo/UserVO.java new file mode 100644 index 000000000..0950a4b28 --- /dev/null +++ b/pigx-visual/pigx-dynamic-datasource/pigx-dynamic-datasource-biz/src/main/java/com/pig4cloud/pigx/dynamic/vo/UserVO.java @@ -0,0 +1,10 @@ +package com.pig4cloud.pigx.dynamic.vo; + +import lombok.Data; + +@Data +public class UserVO { + private Integer id; + private String username; + private String password; +} diff --git a/pigx-visual/pigx-dynamic-datasource/pigx-dynamic-datasource-biz/src/main/resources/bootstrap.yml b/pigx-visual/pigx-dynamic-datasource/pigx-dynamic-datasource-biz/src/main/resources/bootstrap.yml new file mode 100644 index 000000000..05389d5f5 --- /dev/null +++ b/pigx-visual/pigx-dynamic-datasource/pigx-dynamic-datasource-biz/src/main/resources/bootstrap.yml @@ -0,0 +1,16 @@ +server: + port: 6060 + +spring: + application: + name: @artifactId@ + cloud: + nacos: + discovery: + server-addr: ${NACOS-HOST:pigx-register}:${NACOS-PORT:8848} + config: + server-addr: ${spring.cloud.nacos.discovery.server-addr} + file-extension: yml + shared-dataids: application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension} + profiles: + active: dev diff --git a/pigx-visual/pigx-dynamic-datasource/pigx-dynamic-datasource-biz/src/main/resources/logback-spring.xml b/pigx-visual/pigx-dynamic-datasource/pigx-dynamic-datasource-biz/src/main/resources/logback-spring.xml new file mode 100644 index 000000000..964ca8735 --- /dev/null +++ b/pigx-visual/pigx-dynamic-datasource/pigx-dynamic-datasource-biz/src/main/resources/logback-spring.xml @@ -0,0 +1,70 @@ + + + + + + + + + + + + + + ${CONSOLE_LOG_PATTERN} + + + + + + ${log.path}/debug.log + + ${log.path}/%d{yyyy-MM, aux}/debug.%d{yyyy-MM-dd}.%i.log.gz + 50MB + 30 + + + %date [%thread] %-5level [%logger{50}] %file:%line - %msg%n + + + + + + ${log.path}/error.log + + ${log.path}/%d{yyyy-MM}/error.%d{yyyy-MM-dd}.%i.log.gz + 50MB + 30 + + + %date [%thread] %-5level [%logger{50}] %file:%line - %msg%n + + + ERROR + + + + + + + + + + + + + + + + + diff --git a/pigx-visual/pigx-dynamic-datasource/pigx-dynamic-datasource-biz/src/main/resources/mapper/TestMapper.xml b/pigx-visual/pigx-dynamic-datasource/pigx-dynamic-datasource-biz/src/main/resources/mapper/TestMapper.xml new file mode 100644 index 000000000..fcbe041f9 --- /dev/null +++ b/pigx-visual/pigx-dynamic-datasource/pigx-dynamic-datasource-biz/src/main/resources/mapper/TestMapper.xml @@ -0,0 +1,13 @@ + + + + + + + + + diff --git a/pigx-visual/pigx-dynamic-datasource/pom.xml b/pigx-visual/pigx-dynamic-datasource/pom.xml new file mode 100644 index 000000000..bbb59f65c --- /dev/null +++ b/pigx-visual/pigx-dynamic-datasource/pom.xml @@ -0,0 +1,179 @@ + + + + pigx-visual + com.pig4cloud + 3.5.0 + + + 4.0.0 + + pigx-dynamic-datasource + pom + pigx-dynamic-datasource + + + 2.1.10.RELEASE + Greenwich.SR3 + 2.1.0.RELEASE + Cairo-SR8 + UTF-8 + 1.8 + 1.8 + 5.0.5 + 3.5.0 + 0.0.9 + 2.1.1 + 2.10.2 + 2.1.6 + 2.3.6.RELEASE + + + + + pigx-dynamic-datasource-api + pigx-dynamic-datasource-biz + + + + + + org.springframework.boot + spring-boot-configuration-processor + true + + + + com.github.ulisesbocchio + jasypt-spring-boot-starter + ${jasypt.version} + + + + org.springframework.boot + spring-boot-starter-actuator + + + + de.codecentric + spring-boot-admin-starter-client + ${spring-boot-admin.version} + + + + org.projectlombok + lombok + provided + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-dependencies + ${spring-boot.version} + pom + import + + + io.spring.platform + platform-bom + ${spring-platform.version} + pom + import + + + org.springframework.cloud + spring-cloud-dependencies + ${spring-cloud.version} + pom + import + + + com.pig4cloud + pigx-common-bom + ${pigx.bom.version} + pom + import + + + + com.alibaba.cloud + spring-cloud-alibaba-dependencies + ${spring-cloud-alibaba.version} + pom + import + + + + org.springframework.security.oauth + spring-security-oauth2 + ${security.oauth.version} + + + + org.springframework.boot + spring-boot-starter-web + ${spring-boot.version} + + + + spring-boot-starter-tomcat + org.springframework.boot + + + + + + + + ${project.name} + + + src/main/resources + true + + + + + + org.springframework.boot + spring-boot-maven-plugin + ${spring-boot.version} + + + + repackage + + + + + + + + + maven-compiler-plugin + 3.8.0 + + ${maven.compiler.target} + ${maven.compiler.source} + UTF-8 + true + + + + pl.project13.maven + git-commit-id-plugin + 2.2.5 + + + + diff --git a/pigx-visual/pom.xml b/pigx-visual/pom.xml index 20bcf2785..af477a99e 100644 --- a/pigx-visual/pom.xml +++ b/pigx-visual/pom.xml @@ -42,5 +42,6 @@ pigx-sso-client-demo pigx-sentinel-dashboard pigx-tx-manager + pigx-dynamic-datasource