在 Spring Boot 项目中使用 YAML 文件配置连接 Galera MySQL 集群的示例如下:
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql:replication://host1,host2,host3/dbname
username: user
password: pass
driver-class-name: com.mysql.jdbc.Driver
druid:
initial-size: 5
min-idle: 5
max-active: 20
test-on-borrow: true
test-on-return: true
test-while-idle: true
time-between-eviction-runs-millis: 60000
min-evictable-idle-time-millis: 300000
validation-query: SELECT 1
validation-query-timeout: 1
max-pool-prepared-statement-per-connection-size: 20
share-prepared-statements: true
use-global-data-source-stat: true
其中,url 的格式为 jdbc:mysql:replication://host1,host2,host3/dbname
,host1、host2、host3 分别为集群中的三个节点的地址,dbname 为数据库名称。username 和 password 分别为数据库的用户名和密码。
另外,由于 Galera 集群需要使用特定的 JDBC 驱动,需要在 pom.xml 中引入以下依赖:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.22</version>
</dependency>
这里我使用了阿里巴巴的Druid数据库连接池,需要引入:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.22</version>
</dependency>
以上是一个简单的示例,实际配置还需根据具体情况进行调整。