2017-09-28 11 views
1

Ich schreibe eine Grals 3.1.8 Anwendung. Meine Datenquelle wurde in der Datei application.groovy geschrieben.So laden Sie die Datenquellenkonfiguration von externen Dateien in Grails 3.1.8?

Ich möchte Datenquelle Konfiguration wie Benutzername, Passwort, DB aus einer externen Datei laden. Gibt es eine Möglichkeit, es in Grails 3+ Versionen zu tun.

Hier ist meine Datenquelle Konfiguration in 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 
     } 
    } 
} 

Antwort

3

Hier ist die Lösung, die für mich gearbeitet haben, können Sie versuchen.

Diese Lösung wird für Grails arbeiten 3.0+

Zunächst müssen folgende Abhängigkeit hinzuzufügen:

Kompilierung 'org.grails.plugins: external-config: 1.1.2'

müssen dann externe Konfiguration groovy Datei zum Beispiel erstellen:

db-config.groovy

dann müssen diese Konfigurationsdatei in außerhalb des Anwendungsverzeichnisses oder tomcat Bibliothek platzieren. zum Beispiel:

D: \ Apache-tomcat-8.0.47 \ lib

Dann Konfigurationsdatei aus dem application.groovy lesen müssen. In application.groovy Notwendigkeit für jede Umgebung folgende Zeile Code zu platzieren:

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

oder

grails.config.locations = [' file: /// D: /apache-tomcat-8.0.47/lib/db-config.groovy‘]

können Sie $ verwenden {catalina.home}, wenn Sie die Umgebungsvariable ist CATALINA_HOME in Ihrem System. Anders als Sie müssen den direkten Weg verwenden, den ich zeigte.

So Ihre application.groovy wird folgende sein:

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

und Ihre db-config.groovy Datei folgende Zeilen enthalten:

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

können Sie verschiedene db- verwenden config.groovy-Datei für jede Umgebung.

+0

Schöne Antwort. Für mich geht das. – Rassel

0

können Sie verwenden external-config Grails Plugin und definieren Sie die Konfiguration in Ihrer externen Konfigurationsdatei.

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

Und dann Datenquelle Konfiguration in myconfig.groovy

2

definieren Sie können Ihre externe Konfigurationsdatei aus dem Dateisystem unter Verwendung der folgenden Implementierung laden.

Dieses Beispiel definiert für jede Umgebung (Entwicklung/Produktion/Test) einen separaten Pfad zu einer externen Konfigurationsdatei.

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 
      ] 
    } 
} 

Legen Sie Ihre Datenbankkonfiguration in myconfig_developement.groovy wie folgt:

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

können Sie mit dieser Lösung (die für mich gearbeitet, mit Grails 3.1.x)

Grails-app/init/Application.groovy:

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)) 
     } 
    } 
} 

Sie Umgebungsvariable für den Config verwenden können Pfad:

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