2017-09-28 16 views
1

私はgrails 3.1.8アプリケーションを作成しています。私のデータソースはapplication.groovyファイルで書かれています。grails 3.1.8で外部ファイルからデータソース設定を読み込む方法は?

ユーザ名、パスワード、DBなどのデータソース設定を外部ファイルからロードしたいとします。 grails 3 +のバージョンでそれを行う方法はありますか?ここで

は私のデータソース設定はapplication.groovyである: -

hibernate { 
    cache { 
     queries = false 
     use_second_level_cache = true 
     use_query_cache = false 
     region.factory_class = 'org.hibernate.cache.ehcache.EhCacheRegionFactory' 
    } 
} 

dataSource { 
    pooled = true 
    jmxExport = true 
    dialect = 'org.hibernate.dialect.PostgreSQLDialect' 
    driverClassName = 'org.postgresql.Driver' 
    username = 'postgres' 
    password = 'postgres' 
    properties = { 
     jmxEnabled = true 
     initialSize = 5 
     maxActive = 50 
     minIdle = 5 
     maxIdle = 25 
     maxWait = 10000 
     maxAge = 10 * 60000 
     timeBetweenEvictionRunsMillis = 5000 
     minEvictableIdleTimeMillis = 60000 
     validationQuery = "SELECT 1" 
     validationQueryTimeout = 3 
     validationInterval = 15000 
     testOnBorrow = true 
     testWhileIdle = true 
     testOnReturn = false 
     ignoreExceptionOnPreLoad = true 
     jdbcInterceptors = "ConnectionState;StatementCache(max=200)" 
     defaultTransactionIsolation = java.sql.Connection.TRANSACTION_READ_COMMITTED // safe default 
     abandonWhenPercentageFull = 100 // settings are active only when pool is full 
     removeAbandonedTimeout = 120 
     removeAbandoned = true 
     logAbandoned = false // causes stacktrace recording overhead, use only for debugging 
    } 
} 

environments { 
    development { 
     dataSource { 
      dbCreate = 'update' 
      url = "jdbc:postgresql://localhost:5432/testdb" 
      logSql = true 
     } 
    } 
    test { 
     dataSource { 
      dbCreate = 'update' 
      url = "jdbc:postgresql://localhost:5432/testdb" 
      logSql = true 
     } 
    } 
    production { 
     dataSource { 
      dbCreate = 'update' 
      url = "jdbc:postgresql://localhost:5432/testdb" 
      logSql = true 
     } 
    } 
} 

答えて

3

は私のために働いたソリューションであり、あなたが試すことができます。

このソリューションは、Grailsのために動作します3.0+

まず第一に、以下の依存関係を追加する必要があります。

コンパイル 'org.grails.plugins:外部-config設定:1.1.2'

は、例えば外部のコンフィギュレーションGroovyのファイルを作成する必要があります。

DB-config.groovy

は、アプリケーションディレクトリまたはTomcatのライブラリーの外にその設定ファイルを配置する必要があります。例えば:

D:\ apacheの-tomcatに-8.0.47 \

libには、次に application.groovyから設定ファイルを読み込む必要があります。 application.groovyで は各環境のコードの次の行を配置する必要があります

grails.config.locations = [ 'ファイル:/// $ {catalina.home}/LIB/DB-コンフィグ。グルービー ']

又は

grails.config.locations = [' ファイル:/// D:/apache-tomcat-8.0.47/lib/db-config.groovy」 ]

あなたは、環境変数は、システム内のCATALINA_HOMEで設定した場合は、$ {} catalina.homeを使用することができます。あなたが示した直接の道を使わなければならない以外は。

だからあなたapplication.groovyには、次のことになります。

> environments { 
>  development { 
>   grails.config.locations = ['file:///${catalina.home}/lib/db-config.groovy'] 
>  } 
>  production { 
>   grails.config.locations = ['file:///${catalina.home}/lib/db-config.groovy'] 
>   ] 
>  } 
> } 

とあなたのDB-config.groovyファイルには、次の行が含まれています:

>  dataSource { 
>  username = <DB_USER_NAME> 
>  password = <DB_PASSWORD> 
>  dbCreate = 'update' 
>  url = <DB_URL> 
>  logSql = true 
>  } 

あなたは異なるDB-を使用することができます環境ごとのconfig.groovyファイル。

+0

良い答え。わたしにはできる。 – Rassel

0

あなたはexternal-config Grailsのプラグインを使用して、外部の設定ファイルに設定を定義することができます。

grails.config.locations = [ 
     "file:///etc/app/myconfig.groovy" 
] 

そして、あなたは次の実装を使用してファイルシステムから外部の設定ファイルを読み込むことができmyconfig.groovy

2

でのデータソースの構成を定義します。

この例では、各環境(開発/生産/テスト)に対して外部設定ファイルへの別のパスを定義しています。

environments { 
    development { 
      grails.config.locations = [ 
       "file:///home/<CONFIG_FILE_LOCATION_DIR>/myconfig_developement.groovy" //for Unix based systems 
      ] 
    } 
    production { 
      grails.config.locations = [ 
       "file:///home/<CONFIG_FILE_LOCATION_DIR>/myconfig_production.groovy" // for Unix based systems 
      ] 
    } 
} 

次のようにmyconfig_developement.groovyでデータベースの設定を入れて:ここで

dataSource { 
    dbCreate = 'update' 
    url = "jdbc:postgresql://localhost:5432/testdb" 
    logSql = true 
} 
1

あなたは

Grailsのアプリ/ initを/ Application.groovy(つまりのGrails 3.1.xをして、私のために働いていた)、このソリューションを使用することができます。

class Application extends GrailsAutoConfiguration implements EnvironmentAware { 
    static void main(String[] args) { 
     GrailsApp.run(Application, args) 
    } 

    @Override 
    void setEnvironment(Environment environment) { 
     def path = "/etc/grails-app-config.properties" 
     def file = new File(path) 

     if(file.exists()) { 
      def config = new ConfigSlurper().parse(file.text) 
      environment.propertySources.addFirst(new MapPropertySource(grails.util.Environment.getCurrent().name, config)) 
     } 
    } 
} 

あなたが設定のための環境変数を使用することができますパス:

System.getenv(ENV_CONF_FILE_VAR) 

grails-app-config.properties:

dataSource.dbCreate='update' 
dataSource.driverClassName='com.mysql.jdbc.Driver' 
dataSource.url='jdbc:mysql://localhost:5432/testdb' 
dataSource.username='user' 
dataSource.password='pass' 
com.test='test' 
com.numTest=4 
関連する問題