- 浏览: 2501500 次
- 性别:
- 来自: 成都
-
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
Grails(3)Guide Book Chapter 4 Configuration
4.3 The DataSource
If we use a database other than H2 we need a JDBC driver. It's the best to use Ivy to resolve the jar.
The dependency for the MySQL driver like this:
grails.project.dependency.resolution = {
inherits("global")
log "warn"
repositories{
grailsPlugins()
grailsHome()
grailsCentral()
mavenCentral()
}
dependencies {
runtime 'mysql:mysql-connector-java:5.1.16'
}
}
If we can't use Ivy then just put the JAR in our project's lib directory.
Database configuration file is her:
grails-app/conf/DataSource.groovy
Take these configuration as instance
dataSource{
pooled = true
dbCeate = "update"
url = "jdbc:mysql://localhost/yourDB"
driverClassName = "com.mysql.jdbc.Driver"
dialect = org.hibernate.dialect.MySQL5InnoDBDialect
username = "yourUser"
password = "yourPassword"
}
dbCreate ---- create, create-drop, update, validate
create - Drops the existing schema. Creates the schema on startup.
create-drop - Same as create, drops the tables when the application shuts down.
update - Creates missing tables and indexes.
validate - Make no changes to my database, just reports warning.
4.3.1 DataSources and Environments
Just note here, Grails' DataSource definition is 'environment aware'.
4.3.2 JNDI DataSources
Referring to a JNDI DataSource
dataSource {
jndiName = "java:comp/env/myDataSource"
}
4.3.3 Automatic Database Migration
4.3.4 Transaction-aware DataSource Proxy
4.3.5 Database Console
Config.groovy
environments {
production {
grails.serverURL = "http://www.sillycat.com"
grails.dbconsole.enabled = true
grails.dbconsole.urlRoot = '/admin/dbconsole'
}
development {
grails.serverURL = "http://localhost:8080/${appname}"
}
test {
grails.serverURL = "http://localhost:8080/${appname}"
}
}
4.3.6 Multiple Datasources
By default all domain classes share a single DataSource and a single database.
Configuring Additional DataSources
In grails-app/conf/DataSource.groovy
envionments {
development {
dataSource {
dbCreate = "create-drop"
url = "jdbc:h2:mem:devDb"
}
dataSource_lookup {
dialect = org.hibernate.dialect.MySQLInnoDBDialect
driverClassName = 'com.mysql.jdbc.Driver'
username = 'lookup'
password = '111111'
url = 'jdbc:mysql://localhost/lookup'
dbCreate = 'update'
}
}
…snip…
production {
dataSource {
dbCreate = "update"
url = "jdbc:h2:prodDb"
}
dataSource_lookup {
dialect = org.hibernate.dialect.Oracle10gDialect
driverClassName = 'oracle.jdbc.driver.OracleDrvier'
username = 'lookup'
password = '111111'
url = 'jdbc:oracle:thin:@localhost:1521:lookup'
dbCreate = 'update'
}
}
}
Configuring Domain Classes
If a domain class has no DataSource configuration, it defaults to the standard 'dataSource'. If we want to change that, we need to set the datasource property in the mapping block to configure a non-default DataSource.
class ZipCode {
String code
static mapping = {
datasource 'lookup'
}
}
A domain class can also use 2 or more DataSources.
static mapping = {
datasources(['lookup', 'auditing'])
}
DEFAULT is the special name for the default Datasource
static mapping = {
datasources(['lookup', 'DEFAULT'])
}
Haha, a domain can also configured to use all the DataSources
static mapping = {
datasource 'ALL'
}
Namespaces and GORM Methods
If a domain class uses more than one DataSource then we can use the namespace implied by each DataSource name to make GORM calls for a particular DataSource.
def zipCode = ZipCode.auditing.get(42)
…snip…
zipCode.auditing.save()
Hibernate Mapped Domain Classes
…
Services
class DataService {
static datasource = 'lookup'
…snip...
}
4.4 Externalized Configuration
In this example, we are losing configuration files (both Java Properties files and ConfigSlurper configurations) from different places on the class path and files located in USER_HOME.
Setting in Config.groovy
grails.config.locations = [
"classpath:${appName}-config.properties",
"classpath:${appName}-config.groovy",
"file:${userHome}/.grails/${appName}-config.properties",
"file:${userHome}/.grails/${appName}-config.groovy"
]
4.5 Versioning
Versioning Basics
When we create the project, the version is 0.1.
>grails set-version 0.2
Detecting Versions at Runtime
def version = grailsApplication.metadata['app.version']
Within controllers there is an implicit grailsApplication variable that can be used.
def grailsVersion = grailsApplication.metadata['app.grails.version']
alternatively
import grails.util.GrailsUtil
…
def grailsVersion = GrailsUtil.gailsVersion
4.6 Project Documentation
>grails doc
4.7 Dependency Resolution
grails-app/conf/BuildConfig.groovy
grails.project.dependency.resolution = {
…
dependencies {
runtime 'mysql:mysql-connector-java:5.1.16'
}
plugins {
compile ":hibernate:$grailsVersion"
compile ":jquery:1.6.1.1"
compile ":resources:1.0"
build ":tomcat:$grailsVersion"
}
}
4.7.1 Configurations and Dependencies
Grails features five dependency resolution configurations( or rather say scopes)
build Dependencies for the build system only
compile Dependencies for the compile step
runtime Dependencies needed at runtime but not for compilation
test Dependencies needed for testing but not at runtime
provided Dependencies needed at development time, but not during WAR deployment
Disabling transitive dependency resolution
By default, Grails will not only get the JARs and plugins that you declare, but it will also get their transitive dependencies. We can disable transitive dependency resolution.
runtime('com.mysql:mysql-connector-java:5.1.16',
'net.sf.ehcache:ehcache:1.6.1') {
transitive = false
}
Alternatively
runtime group: 'com.mysql',
name: 'mysql-connector-java',
version: '5.1.16',
transitive:false
Excluding specific transitive dependencies
runtime('com.mysql:mysql-connector-java:5.1.16',
'net.sf.ehcache:ehcache:1.6.1') {
excludes "xml-apis", "commons-logging"
}
alternatively
runtime(…snip..) {
excludes([ group: 'xml-apis', name: 'xml-apis'],
…
[name:'commons-logging' ] )
}
Where are the JARs
The default place is user.home/.grails/ivy-cache, we can change in settings.groovy
grails.dependency.cache.dir = "${userHome}/.my-dependency-cache"
References:
http://grails.org/doc/latest/guide/conf.html
http://grails.org/doc/latest/guide/index.html
4.3 The DataSource
If we use a database other than H2 we need a JDBC driver. It's the best to use Ivy to resolve the jar.
The dependency for the MySQL driver like this:
grails.project.dependency.resolution = {
inherits("global")
log "warn"
repositories{
grailsPlugins()
grailsHome()
grailsCentral()
mavenCentral()
}
dependencies {
runtime 'mysql:mysql-connector-java:5.1.16'
}
}
If we can't use Ivy then just put the JAR in our project's lib directory.
Database configuration file is her:
grails-app/conf/DataSource.groovy
Take these configuration as instance
dataSource{
pooled = true
dbCeate = "update"
url = "jdbc:mysql://localhost/yourDB"
driverClassName = "com.mysql.jdbc.Driver"
dialect = org.hibernate.dialect.MySQL5InnoDBDialect
username = "yourUser"
password = "yourPassword"
}
dbCreate ---- create, create-drop, update, validate
create - Drops the existing schema. Creates the schema on startup.
create-drop - Same as create, drops the tables when the application shuts down.
update - Creates missing tables and indexes.
validate - Make no changes to my database, just reports warning.
4.3.1 DataSources and Environments
Just note here, Grails' DataSource definition is 'environment aware'.
4.3.2 JNDI DataSources
Referring to a JNDI DataSource
dataSource {
jndiName = "java:comp/env/myDataSource"
}
4.3.3 Automatic Database Migration
4.3.4 Transaction-aware DataSource Proxy
4.3.5 Database Console
Config.groovy
environments {
production {
grails.serverURL = "http://www.sillycat.com"
grails.dbconsole.enabled = true
grails.dbconsole.urlRoot = '/admin/dbconsole'
}
development {
grails.serverURL = "http://localhost:8080/${appname}"
}
test {
grails.serverURL = "http://localhost:8080/${appname}"
}
}
4.3.6 Multiple Datasources
By default all domain classes share a single DataSource and a single database.
Configuring Additional DataSources
In grails-app/conf/DataSource.groovy
envionments {
development {
dataSource {
dbCreate = "create-drop"
url = "jdbc:h2:mem:devDb"
}
dataSource_lookup {
dialect = org.hibernate.dialect.MySQLInnoDBDialect
driverClassName = 'com.mysql.jdbc.Driver'
username = 'lookup'
password = '111111'
url = 'jdbc:mysql://localhost/lookup'
dbCreate = 'update'
}
}
…snip…
production {
dataSource {
dbCreate = "update"
url = "jdbc:h2:prodDb"
}
dataSource_lookup {
dialect = org.hibernate.dialect.Oracle10gDialect
driverClassName = 'oracle.jdbc.driver.OracleDrvier'
username = 'lookup'
password = '111111'
url = 'jdbc:oracle:thin:@localhost:1521:lookup'
dbCreate = 'update'
}
}
}
Configuring Domain Classes
If a domain class has no DataSource configuration, it defaults to the standard 'dataSource'. If we want to change that, we need to set the datasource property in the mapping block to configure a non-default DataSource.
class ZipCode {
String code
static mapping = {
datasource 'lookup'
}
}
A domain class can also use 2 or more DataSources.
static mapping = {
datasources(['lookup', 'auditing'])
}
DEFAULT is the special name for the default Datasource
static mapping = {
datasources(['lookup', 'DEFAULT'])
}
Haha, a domain can also configured to use all the DataSources
static mapping = {
datasource 'ALL'
}
Namespaces and GORM Methods
If a domain class uses more than one DataSource then we can use the namespace implied by each DataSource name to make GORM calls for a particular DataSource.
def zipCode = ZipCode.auditing.get(42)
…snip…
zipCode.auditing.save()
Hibernate Mapped Domain Classes
…
Services
class DataService {
static datasource = 'lookup'
…snip...
}
4.4 Externalized Configuration
In this example, we are losing configuration files (both Java Properties files and ConfigSlurper configurations) from different places on the class path and files located in USER_HOME.
Setting in Config.groovy
grails.config.locations = [
"classpath:${appName}-config.properties",
"classpath:${appName}-config.groovy",
"file:${userHome}/.grails/${appName}-config.properties",
"file:${userHome}/.grails/${appName}-config.groovy"
]
4.5 Versioning
Versioning Basics
When we create the project, the version is 0.1.
>grails set-version 0.2
Detecting Versions at Runtime
def version = grailsApplication.metadata['app.version']
Within controllers there is an implicit grailsApplication variable that can be used.
def grailsVersion = grailsApplication.metadata['app.grails.version']
alternatively
import grails.util.GrailsUtil
…
def grailsVersion = GrailsUtil.gailsVersion
4.6 Project Documentation
>grails doc
4.7 Dependency Resolution
grails-app/conf/BuildConfig.groovy
grails.project.dependency.resolution = {
…
dependencies {
runtime 'mysql:mysql-connector-java:5.1.16'
}
plugins {
compile ":hibernate:$grailsVersion"
compile ":jquery:1.6.1.1"
compile ":resources:1.0"
build ":tomcat:$grailsVersion"
}
}
4.7.1 Configurations and Dependencies
Grails features five dependency resolution configurations( or rather say scopes)
build Dependencies for the build system only
compile Dependencies for the compile step
runtime Dependencies needed at runtime but not for compilation
test Dependencies needed for testing but not at runtime
provided Dependencies needed at development time, but not during WAR deployment
Disabling transitive dependency resolution
By default, Grails will not only get the JARs and plugins that you declare, but it will also get their transitive dependencies. We can disable transitive dependency resolution.
runtime('com.mysql:mysql-connector-java:5.1.16',
'net.sf.ehcache:ehcache:1.6.1') {
transitive = false
}
Alternatively
runtime group: 'com.mysql',
name: 'mysql-connector-java',
version: '5.1.16',
transitive:false
Excluding specific transitive dependencies
runtime('com.mysql:mysql-connector-java:5.1.16',
'net.sf.ehcache:ehcache:1.6.1') {
excludes "xml-apis", "commons-logging"
}
alternatively
runtime(…snip..) {
excludes([ group: 'xml-apis', name: 'xml-apis'],
…
[name:'commons-logging' ] )
}
Where are the JARs
The default place is user.home/.grails/ivy-cache, we can change in settings.groovy
grails.dependency.cache.dir = "${userHome}/.my-dependency-cache"
References:
http://grails.org/doc/latest/guide/conf.html
http://grails.org/doc/latest/guide/index.html
发表评论
-
NodeJS12 and Zlib
2020-04-01 07:44 446NodeJS12 and Zlib It works as ... -
Traefik 2020(1)Introduction and Installation
2020-03-29 13:52 303Traefik 2020(1)Introduction and ... -
Private Registry 2020(1)No auth in registry Nginx AUTH for UI
2020-03-18 00:56 390Private Registry 2020(1)No auth ... -
Buffer in NodeJS 12 and NodeJS 8
2020-02-25 06:43 351Buffer in NodeJS 12 and NodeJS ... -
NodeJS ENV Similar to JENV and PyENV
2020-02-25 05:14 435NodeJS ENV Similar to JENV and ... -
Prometheus HA 2020(3)AlertManager Cluster
2020-02-24 01:47 382Prometheus HA 2020(3)AlertManag ... -
Serverless with NodeJS and TencentCloud 2020(5)CRON and Settings
2020-02-24 01:46 309Serverless with NodeJS and Tenc ... -
GraphQL 2019(3)Connect to MySQL
2020-02-24 01:48 221GraphQL 2019(3)Connect to MySQL ... -
GraphQL 2019(2)GraphQL and Deploy to Tencent Cloud
2020-02-24 01:48 409GraphQL 2019(2)GraphQL and Depl ... -
GraphQL 2019(1)Apollo Basic
2020-02-19 01:36 287GraphQL 2019(1)Apollo Basic Cl ... -
Serverless with NodeJS and TencentCloud 2020(4)Multiple Handlers and Running wit
2020-02-19 01:19 283Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(3)Build Tree and Traverse Tree
2020-02-19 01:19 279Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(2)Trigger SCF in SCF
2020-02-19 01:18 263Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(1)Running with Component
2020-02-19 01:17 252Serverless with NodeJS and Tenc ... -
NodeJS MySQL Library and npmjs
2020-02-07 06:21 250NodeJS MySQL Library and npmjs ... -
Python Library 2019(1)requests and aiohttp
2019-12-18 01:12 238Python Library 2019(1)requests ... -
NodeJS Installation 2019
2019-10-20 02:57 535NodeJS Installation 2019 Insta ... -
Monitor Tool 2019(2)Monit on Multiple Instances and Email Alerts
2019-10-18 10:57 238Monitor Tool 2019(2)Monit on Mu ... -
Sqlite Database 2019(1)Sqlite3 Installation and Docker phpsqliteadmin
2019-09-05 11:24 334Sqlite Database 2019(1)Sqlite3 ... -
Supervisor 2019(2)Ubuntu and Multiple Services
2019-08-19 10:53 335Supervisor 2019(2)Ubuntu and Mu ...
相关推荐
详细介绍grails框架的奥秘,英文版你值得拥有
The definitive guide to grails 2 英文版 书和随书代码, 学习grails必备
Grails Grails Grails Grails Grails
Grails3配置logback日志.pdf
grails1.0开发框架3 类似于ruby on rails的框架。
In Grails: A Quick-Start Guide, you’ll see how to use Grails by iteratively building an unique, working application. By the time we’re done, you’ll have built and deployed a real, functioning ...
grails1.0开发框架4 类似于ruby on rails的框架。
去年早些时候写的一个项目驱动的grails入门教程。绝对原创。
Definitive Guide to Grails
grails开发实践,欢迎下载使用 grails开发实践,欢迎下载使用 grails开发实践,欢迎下载使用
the best of the best. written by one of core author of grails team. Highly recommended.
Using the principle of convention-over-configuration and the dynamic Groovy programming language, Grails takes the pain out of web development and brings back the fun. This book will get you up and ...
Grails权威指南Grails权威指南Grails权威指南Grails权威指南Grails权威指南Grails权威指南
Grails 3开发邮件发送功能,完整教程!超级实用,手把手教你接入邮件发送
Grails3配置单独的数据库properties.pdf
Groovy / Grails F3 The Best Web Server
NULL 博文链接:https://youngbrick.iteye.com/blog/2352298
The Definitive Guide to Grails 2nd Edition.pdf
Grails设计者的作品,经典的Grails学习资料