后端启动
注意
本文档引用的链接或项目名称大多是lamp-datasource-max,若你使用的是lamp-cloumn-max项目,只需要自行联想替换一下即可。
修改配置并编译项目
创建数据库:根据你使用的数据库选择正确的创建数据库SQL语句
# mysql CREATE DATABASE IF NOT EXISTS `lamp_ds_c_defaults` CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci; CREATE DATABASE IF NOT EXISTS `lamp_base_1` CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci; # oracle create user lamp_ds_c_defaults identified by lamp_ds_c_defaults; create user lamp_base_1 identified by lamp_base_1; grant create session,create table,create sequence,create view,resource to lamp_ds_c_defaults; grant create session,create table,create sequence,create view,resource to lamp_base_1; # SQL Server CREATE DATABASE lamp_ds_c_defaults COLLATE Chinese_PRC_CI_AS; CREATE DATABASE lamp_base_1 COLLATE Chinese_PRC_CI_AS;
# mysql CREATE DATABASE IF NOT EXISTS `lamp_column` CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci; # oracle create user lamp_column identified by lamp_column; grant create session,create table,create sequence,create view,resource to lamp_column; # SQL Server CREATE DATABASE lamp_column COLLATE Chinese_PRC_CI_AS;
# mysql CREATE DATABASE IF NOT EXISTS `lamp_none` CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci; # oracle create user lamp_none identified by lamp_none; grant create session,create table,create sequence,create view,resource to lamp_none; # SQL Server CREATE DATABASE lamp_none COLLATE Chinese_PRC_CI_AS;
导入sql脚本
向 lamp_ds_c_defaults 库导入 lamp_ds_c_defaults.sql
向 lamp_base_1 库导入 lamp_base_1.sql
仅Oracle数据库需要执行:向 lamp_ds_c_defaults 库导入 defaults_data.sql
仅Oracle数据库需要执行:向 lamp_ds_c_defaults 库导入 defaults_seq.sql
仅Oracle数据库需要执行:向 lamp_base_1 库导入 base_data.sql
- 向 lamp_column 库导入 lamp_column.sql
- 向 lamp_none 库导入 lamp_none.sql
lamp-datasource-max项目,需要确保 ContextConstants.TENANT_BASE_POOL_NAME_HEADER 跟lamp_base库的前缀一致。
public final class ContextConstants { // "lamp_base" 一定要和 创建base库(CREATE DATABASE lamp_base_1)时 的前缀一致 public static final String TENANT_BASE_POOL_NAME_HEADER = "lamp_base"; }
参考 将配置文件导入Nacos , 将所有配置导入 nacos 对应的 命名空间 中,并在nacos的配置列表修改mysql、redis、rabbitmq等信息 (ip、端口、密码等)。
提示
密码是纯数字、含有特殊字符、没有密码的, 必须加上单引号。 如:
- 你的密码是:00000,必须写成:'00000'
- 没有密码必须写成:''
修改 nacos 中的 mysql.yml 配置文件
根据您使用的数据库类型,修改1-3处内容,没有贴出来的参数保持默认值
lamp-datasource-max
mysql.yml 配置文件内容:
lamp: druid-oracle: &druid-oracle validation-query: SELECT 'x' FROM DUAL druid-mysql: &druid-mysql validation-query: SELECT 'x' druid-sqlserver: &druid-sqlserver validation-query: SELECT 'x' # 修改1:数据库连接基本信息, 三选一!!!不需要同时需要三种数据库!!! oracle: &db-oracle driverClassName: oracle.jdbc.driver.OracleDriver username: 'lamp_ds_c_defaults' password: 'lamp_ds_c_defaults' url: jdbc:oracle:thin:@172.26.3.67:1521:helowin mysql: &db-mysql driverClassName: com.mysql.cj.jdbc.Driver username: 'root' password: 'root' url: jdbc:mysql://127.0.0.1:3306/lamp_ds_c_defaults?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useUnicode=true&useSSL=false&autoReconnect=true&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&nullCatalogMeansCurrent=true sqlserver: &db-sqlserver driverClassName: com.microsoft.sqlserver.jdbc.SQLServerDriver username: 'sa' password: '1234@abcd' url: jdbc:sqlserver://172.26.3.67:1433;DatabaseName=lamp_ds_c_defaults init: separator: GO spring: datasource: dynamic: druid: # 修改2:druid 信息 <<: *druid-mysql #<<: *db-sqlserver # sql server 使用我 #<<: *db-oracle # oracle使用我 datasource: # 修改3: lamp_defaults数据源 锚点链接 "0": <<: *db-mysql # mysql 使用我 #<<: *db-sqlserver # sql server 使用我 #<<: *db-oracle # oracle使用我
修改后的配置如下:
lamp: druid-mysql: &druid-mysql validation-query: SELECT 'x' mysql: &db-mysql driverClassName: com.mysql.cj.jdbc.Driver username: 'root' password: 'root' url: jdbc:mysql://127.0.0.1:3306/lamp_ds_c_defaults?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useUnicode=true&useSSL=false&autoReconnect=true&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&nullCatalogMeansCurrent=true database: dbType: MYSQL # 可选项: MYSQL SQL_SERVER ORACLE spring: datasource: dynamic: druid: <<: *druid-mysql datasource: "0": <<: *db-mysql # mysql 使用我
lamp: druid-oracle: &druid-oracle validation-query: SELECT 'x' FROM DUAL oracle: &db-oracle driverClassName: oracle.jdbc.driver.OracleDriver username: 'lamp_ds_c_defaults' password: 'lamp_ds_c_defaults' url: jdbc:oracle:thin:@172.26.3.67:1521:helowin spring: datasource: dynamic: druid: <<: *druid-oracle datasource: "0": <<: *db-oracle
lamp: druid-sqlserver: &druid-sqlserver validation-query: SELECT 'x' sqlserver: &db-sqlserver driverClassName: com.microsoft.sqlserver.jdbc.SQLServerDriver username: 'sa' password: '1234@abcd' url: jdbc:sqlserver://172.26.3.67:1433;DatabaseName=lamp_ds_c_defaults init: separator: GO database: dbType: SQL_SERVER # 可选项: MYSQL SQL_SERVER ORACLE spring: datasource: dynamic: druid: <<: *druid-sqlserver datasource: "0": <<: *db-sqlserver
lamp-column-max
mysql.yml 配置文件内容:
lamp: # 修改1:数据库连接基本信息, 三选一!!!三选一!!!不是同时需要三种数据库!!! oracle: &db-oracle db-type: oracle validation-query: SELECT 'x' FROM DUAL filters: stat,wall,slf4j # druid不支持使用p6spy打印日志,所以采用druid 的 slf4j 过滤器来打印可执行日志 username: 'lamp_column' password: 'lamp_column' driverClassName: oracle.jdbc.driver.OracleDriver url: jdbc:oracle:thin:@172.26.3.67:1521:helowin mysql: &db-mysql db-type: mysql validation-query: SELECT 'x' filters: stat,wall username: 'root' password: 'root' driverClassName: com.p6spy.engine.spy.P6SpyDriver url: jdbc:p6spy:mysql://127.0.0.1:3306/lamp_column?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useUnicode=true&useSSL=false&autoReconnect=true&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&nullCatalogMeansCurrent=true sqlserver: &db-sqlserver username: 'sa' password: '1234@abcd' driverClassName: com.microsoft.sqlserver.jdbc.SQLServerDriver url: jdbc:sqlserver://172.26.3.67:1433;DatabaseName=lamp_column db-type: sqlserver validation-query: SELECT 'x' filters: stat,wall database: multiTenantType: COLUMN tenantIdColumn: 'tenant_id' spring: datasource: # 修改2:druid 信息 druid: <<: *db-mysql
修改后的配置如下:
lamp: mysql: &db-mysql db-type: mysql validation-query: SELECT 'x' filters: stat,wall username: 'root' password: 'root' driverClassName: com.p6spy.engine.spy.P6SpyDriver url: jdbc:p6spy:mysql://127.0.0.1:3306/lamp_column?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useUnicode=true&useSSL=false&autoReconnect=true&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&nullCatalogMeansCurrent=true spring: datasource: druid: <<: *db-mysql
lamp: oracle: &db-oracle db-type: oracle validation-query: SELECT 'x' FROM DUAL filters: stat,wall,slf4j # 使用oracle时,开发环境使用druid 的slf4j的filter来打印执行SQL username: 'lamp_column' password: 'lamp_column' driverClassName: oracle.jdbc.driver.OracleDriver url: jdbc:oracle:thin:@172.26.3.67:1521:helowin spring: datasource: # 修改2:druid 信息 druid: <<: *db-oracle filter: slf4j: enabled: true # 使用oracle时,开发环境使用druid 的slf4j的filter来打印执行SQL statement-executable-sql-log-enable: true
lamp: sqlserver: &db-sqlserver username: 'sa' password: '1234@abcd' driverClassName: com.p6spy.engine.spy.P6SpyDriver url: jdbc:p6spy:sqlserver://172.26.3.67:1433;DatabaseName=lamp_column db-type: sqlserver validation-query: SELECT 'x' filters: stat,wall database: multiTenantType: COLUMN tenantIdColumn: 'tenant_id' spring: datasource: # 修改2:druid 信息 druid: <<: *db-sqlserver
lamp-cloud
mysql.yml 配置文件内容:
lamp: oracle: &db-oracle db-type: oracle validation-query: SELECT 'x' FROM DUAL filters: stat,wall # druid不支持使用p6spy打印日志,所以采用druid 的 slf4j 过滤器来打印可执行日志 username: 'lamp_none' password: 'lamp_none' driverClassName: oracle.jdbc.driver.OracleDriver url: jdbc:oracle:thin:@172.26.3.67:1521:helowin mysql: &db-mysql filters: stat,wall db-type: mysql validation-query: SELECT 'x' username: 'root' password: 'root' # 开发使用p6spy驱动打印日志 driverClassName: com.p6spy.engine.spy.P6SpyDriver url: jdbc:p6spy:mysql://127.0.0.1:3306/lamp_none?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useUnicode=true&useSSL=false&autoReconnect=true&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&nullCatalogMeansCurrent=true sqlserver: &db-sqlserver username: 'sa' password: '1234@abcd' driverClassName: com.p6spy.engine.spy.P6SpyDriver url: jdbc:p6spy:sqlserver://172.26.3.67:1433;DatabaseName=lamp_none db-type: sqlserver validation-query: SELECT 'x' filters: stat,wall database: multiTenantType: NONE spring: datasource: druid: enable: true <<: *db-mysql # <<: *db-sqlserver # <<: *db-oracle
修改后的配置如下:
lamp: mysql: &db-mysql filters: stat,wall db-type: mysql validation-query: SELECT 'x' username: 'root' password: 'root' driverClassName: com.p6spy.engine.spy.P6SpyDriver url: jdbc:p6spy:mysql://127.0.0.1:3306/lamp_none?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useUnicode=true&useSSL=false&autoReconnect=true&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&nullCatalogMeansCurrent=true spring: datasource: druid: enable: true <<: *db-mysql
lamp: oracle: &db-oracle db-type: oracle validation-query: SELECT 'x' FROM DUAL filters: stat,wall,slf4j # 使用oracle时,开发环境使用druid 的slf4j的filter来打印执行SQL username: 'lamp_none' password: 'lamp_none' driverClassName: oracle.jdbc.driver.OracleDriver url: jdbc:oracle:thin:@172.26.3.67:1521:helowin database: multiTenantType: NONE spring: datasource: druid: <<: *db-oracle filter: slf4j: enabled: true # 使用oracle时,开发环境使用druid 的slf4j的filter来打印执行SQL statement-executable-sql-log-enable: true
lamp: sqlserver: &db-sqlserver username: 'sa' password: '1234@abcd' driverClassName: com.p6spy.engine.spy.P6SpyDriver url: jdbc:p6spy:sqlserver://172.26.3.67:1433;DatabaseName=lamp_none db-type: sqlserver validation-query: SELECT 'x' filters: stat,wall database: multiTenantType: NONE spring: datasource: druid: enable: true <<: *db-sqlserver
修改数据库驱动
根据使用的数据库,修改lamp-public/lamp-database-mode/pom.xml中的驱动依赖
<!-- mysql 驱动 --> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> </dependency>
<!-- Oracle 驱动 --> <dependency> <groupId>com.oracle.database.jdbc</groupId> <artifactId>ojdbc8</artifactId> </dependency>
<!-- sqlserver 驱动 --> <dependency> <groupId>com.microsoft.sqlserver</groupId> <artifactId>mssql-jdbc</artifactId> </dependency>
修改常量类 Condition
public class Condition { /** MySQL、SQLServer 数据库的 模糊查询 */ public static final String LIKE = SqlCondition.LIKE; // /** ORACLE 数据库的 模糊查询 */ // public static final String LIKE = SqlCondition.ORACLE_LIKE; }
修改 src/main/filters/config-dev.properties 文件的的配置
nacos.xx : nacos相关参数
强烈建议namespace跟我的保持一致,减少出错概率!
seata.xx: seata 相关参数
logging.file.path: 项目的日志文件存放路径
database.type: 数据库类型
# ip 可以配置成自定义域名,并在hosts文件配置映射,解决切换网络时nacos不可用的bug nacos.ip=127.0.0.1 nacos.port=8848 nacos.namespace=b16f7baf-56e7-4f4e-a26c-425ee0668016 nacos.username=nacos nacos.password=nacos nacos.seata.namespace=3cca7d98-3b1c-44d3-90e5-86abaaf0048a # seata 的 grouplist ip 端口 seata.ip=127.0.0.1 seata.port=8091 # 日志存放位置 # 生成环境,建议修改为绝对路径 logging.file.path=./logs sentinel.dashboard=127.0.0.1:8088 # 数据库类型 mysql.yml oracle.yml sqlserver.yml database.type=mysql.yml
编译项目, 参考 项目导入 将 lamp-datasource-max 和 lamp-util-max 同时导入到一个IDEA工作空间, 然后使用 maven 编译项目
编译成功后,验证所有服务的
lamp-datasource-max/lamp-*/lamp-*-server/target/classes/bootstrap.yml
文件中,@nacos.*@ 是否成功替换为 src/main/filters/config-dev.properties 中的内容.
启动项目
先确保
nacos
、MySQL
、Redis
、Seata
、RabbitMQ
(可选)、等基础服务是否启动成功逐一启动 OauthServerApplication、SystemServerApplication、BaseServerApplication、GatewayServerApplication、MonitorApplication
验证是否启动成功
打开 nacos控制台 若看到下图,则说明服务注册成功
或者打开项目控制台,看到打印的如图信息说明启动成功。
通过以下地址访问Swagger文档
- 认证服务文档:http://127.0.0.1:18773/doc.html
- 系统服务文档:http://127.0.0.1:18771/doc.html
- 基础服务文档:http://127.0.0.1:18764/doc.html
- SpringBootAdmin监控:http://127.0.0.1:18762/lamp-monitor
- gateway网关聚合文档:http://127.0.0.1:18760/api/doc.html
常见问题
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
答: 了解详情
yml配置中
${NACOS_IP:@nacos.ip@}
是什么意思答: 了解详情
怎么将lamp-cloud和lamp-util等任意两个java maven管理的项目同时导入一个IDEA的工作空间?
答:了解详情
org.yaml.snakeyaml.error.YAMLException: java.nio.charset.MalformedInputException: Input length = 1
答:了解详情
❗️❗️❗️若评论区无法显示,请使用"手机热点"或"科学上网"。