@@ -259,7 +259,7 @@ go get gofr.dev/pkg/gofr/datasource/dbresolver@latest
259259
260260** 1. Environment Variables**
261261
262- Add replica connection details to your .env file:
262+ Configure the primary database in your .env file:
263263
264264``` editorconfig
265265# Primary database
@@ -269,12 +269,6 @@ DB_USER=root
269269DB_PASSWORD=root123
270270DB_NAME=test_db
271271DB_DIALECT=mysql
272-
273- # Replica configuration
274- DB_REPLICA_HOSTS=localhost,replica1,replica2
275- DB_REPLICA_PORTS=3307,3308,3309
276- DB_REPLICA_USERS=readonly1,readonly2,readonly3
277- DB_REPLICA_PASSWORDS=pass1,pass2,pass3
278272```
279273
280274** 2. Initialize DBResolver**
@@ -299,12 +293,30 @@ func main() {
299293 a := gofr.New ()
300294
301295 // Initialize DB resolver with default settings
302- err := dbresolver.InitDBResolver (a, dbresolver.Config {
296+ err := dbresolver.InitDBResolver (a, & dbresolver.Config {
303297 Strategy: dbresolver.StrategyRoundRobin , // use round-robin strategy or random strategy
304298 ReadFallback: true , // allow reads on primary if all replicas are down
305299 MaxFailures: 3 , // number of allowed failures before marking a replica as down
306300 TimeoutSec: 30 , // timeout for marking a replica as down
307- PrimaryRoutes: []string {" /admin" , " /api/payments/*" }, // routes that should go to primary
301+ PrimaryRoutes: []string {" /admin" , " /api/payments/*" },
302+
303+ Replicas: []dbresolver.ReplicaCredential {
304+ {
305+ Host: " localhost:3307" ,
306+ User: " replica_user1" ,
307+ Password: " pass1" ,
308+ },
309+ {
310+ Host: " replica2.example.com:3308" ,
311+ User: " replica_user2" ,
312+ Password: " pass2" ,
313+ },
314+ {
315+ Host: " replica3.example.com:3309" ,
316+ User: " replica_user3" ,
317+ Password: " pass3" ,
318+ },
319+ },// routes that should go to primary
308320 })
309321 if err != nil {
310322 a.Logger ().Errorf (" failed to initialize db resolver: %v " , err)
0 commit comments