Note:
This topic has been translated from a Chinese forum by GPT and might contain errors.
Original topic: springboot如何指定TiDB的连接地址
[TiDB Usage Environment] Production Environment / Testing / PoC
[TiDB Version] 8.1.0
[Encountered Problem: Problem Phenomenon and Impact]
spring:
datasource:
url: ${TIDB_JDBC_URL:jdbc:mysql://localhost:4000/test}
username: ${TIDB_USER:root}
password: ${TIDB_PASSWORD:}
driver-class-name: com.mysql.cj.jdbc.Driver
jpa:
show-sql: true
database-platform: org.hibernate.dialect.TiDBDialect
hibernate:
ddl-auto: create-drop
Currently, there are 3 physical servers all deployed with TiDB. How should the IP address of TIDB_JDBC_URL be specified? If it is specified as one of them, will a server crash cause data to be inaccessible? Does TiDB have a load balancing handling mode?
An additional component is needed on top of TiDB to ensure high availability and load balancing, such as F5, HAProxy, TiProxy, etc. Here are the best practices for HAProxy:
8.1 comes with tiproxy, just connect to the tiproxy address.
You need a load balancer.
Configure the proxy to ensure high availability and load balancing.
Deploy tiproxy, then fill in the address of tiproxy. Tiproxy is a new feature added in version 8.1. In previous versions, you needed to install load balancing tools like haproxy or nginx yourself. However, there is an issue with these load balancing tools as they cannot solve the session sharing problem. In other words, if one of the backend TiDB servers goes down and unfortunately Spring Boot is connected to this server, the ongoing transactions would likely be “lost.” I haven’t tried it myself, but you can experiment to see what happens.
Install Keepalived and assign a virtual VIP.
If you have hardware F5, just use it. If not, then install a software load balancer.
The version you are using supports the tiproxy feature and automatic load balancing.
Deploy tiproxy and configure the address for tiproxy. You can also use a hardware Layer 4 switch or software like nginx to configure the proxy. For Spring Boot, just configure the proxy IP address and port.