使用环境:
spring-boot:2.7.10
shardingsphere-jdbc-core:5.3.2
snakeyaml:1.3.3
druid:1.1.21
mybatis-plus:3.5.3.1
场景、问题:
以yml配置的方式,在application.yml中配置了
spring:
datasource:
driver-class-name: org.apache.shardingsphere.driver.ShardingSphereDriver
url: jdbc:shardingsphere:classpath:sharding-config.yml
后,希望能够在sharding-config.yml中以环境变量匹配占位符${}的方式配置主从数据库。
现状:
服务正常启动,application.yml中的占位符能够正常匹配环境变量。
在不使用占位符配置sharding-config.yml时能够完成数据库操作读写分离;
在使用占位符${MYSQL_HOST}等配置sharding-config.yml中的数据库信息后,执行任意数据库操作报错Caused by: java.net.UnknownHostException: ${MYSQL_HOST}。sharding-config.yml占位符未能成功匹配环境变量。
报错信息:
2023-11-16 15:12:54.906 ERROR 14920 --- [eate-1840721324] com.alibaba.druid.pool.DruidDataSource : create connection SQLException, url: jdbc:mysql://${MYSQL_HOST}:3308/jnpf_tenant?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true&useSSL=false, errorCode 0, state 08S01
com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:172)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:862)
at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:444)
at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:230)
at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:226)
at com.alibaba.druid.pool.DruidAbstractDataSource.createPhysicalConnection(DruidAbstractDataSource.java:1644)
at com.alibaba.druid.pool.DruidAbstractDataSource.createPhysicalConnection(DruidAbstractDataSource.java:1710)
at com.alibaba.druid.pool.DruidDataSource$CreateConnectionThread.run(DruidDataSource.java:2753)
Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:59)
at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:103)
at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:149)
at com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:165)
at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:92)
at com.mysql.cj.NativeSession.connect(NativeSession.java:152)
at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:982)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:852)
... 6 common frames omitted
Caused by: java.net.UnknownHostException: ${MYSQL_HOST}
at java.net.InetAddress.getAllByName0(InetAddress.java:1292)
at java.net.InetAddress.getAllByName(InetAddress.java:1203)
at java.net.InetAddress.getAllByName(InetAddress.java:1127)
at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:150)
at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:66)
... 9 common frames omitted