How to Specify the Connection Address of TiDB in Spring Boot

Note:
This topic has been translated from a Chinese forum by GPT and might contain errors.

Original topic: springboot如何指定TiDB的连接地址

| username: TiDBer_RjFH58Ar

[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?

| username: 啦啦啦啦啦 | Original post link

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:

| username: tidb菜鸟一只 | Original post link

8.1 comes with tiproxy, just connect to the tiproxy address.

| username: zhanggame1 | Original post link

You need a load balancer.

| username: 呢莫不爱吃鱼 | Original post link

Configure the proxy to ensure high availability and load balancing.

| username: vincentLi | Original post link

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.

| username: 成为一名优秀的DBA | Original post link

Install Keepalived and assign a virtual VIP.

| username: TiDBer_LM | Original post link

If you have hardware F5, just use it. If not, then install a software load balancer.

| username: 我是人间不清醒 | Original post link

The version you are using supports the tiproxy feature and automatic load balancing.

| username: zhaokede | Original post link

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.