我正在尋找一種實現故障轉移的方法,並在我的Rails應用程序中對三個數據庫進行了另外的負載平衡。如何在Rails中設置分佈式數據庫
下面的代碼當前沒有問題,但是如果db_1
關閉,那麼我的應用程序就會死亡。
我想用db_1
作爲主,另外兩個db_2
和db_3
作爲故障切換。
此外,我想負載平衡他們,但這是一個不太重要的要求。
的config/database.yml的
db_1:
adapter: mysql2
reconnect: false
pool: 5
username: <username>
password: <password>
database: database_test
host: 101.101.101.1
db_2:
adapter: mysql2
reconnect: false
pool: 5
username: <username>
password: <password>
database: database_test
host: 101.101.101.2
db_3:
adapter: mysql2
reconnect: false
pool: 5
username: <username>
password: <password>
database: database_test
host: 101.101.101.3
應用程序/模型/ ext_databases.rb
class ExtDatabases < ActiveRecord::Base
self.abstract_class = true
establish_connection :db_1
end
應用程序/模型/ users.rb的
class Users < ExtDatabases
Users.table_name = "ext_users"
end