快轉到主要內容

來架個網站吧-13.網站開發-3: 設定環境參數-2

·856 字·2 分鐘·
PolloChang
作者
PolloChang
我是一隻雞

來架個網站吧-13.網站開發-3: 設定環境參數-2
#

tags: 來架個網站吧 Grails
#

我是目錄


昨天本來想要一口氣講完,但是發現篇幅好像有點長,所以決定切兩部份。

設定專案環境參數
#

grails-app/conf/application.yml
#

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
---
grails:
    profile: web
    codegen:
        defaultPackage: ironman.dict
    gorm:
        reactor:
            # Whether to translate GORM events into Reactor events
            # Disabled by default for performance reasons
            events: false
info:
    app:
        name: '@info.app.name@'
        version: '@info.app.version@'
        grailsVersion: '@info.app.grailsVersion@'
spring:
    jmx:
        unique-names: true
    main:
        banner-mode: "off"
    groovy:
        template:
            check-template-location: false
    devtools:
        restart:
            additional-exclude:
                - '*.gsp'
                - '**/*.gsp'
                - '*.gson'
                - '**/*.gson'
                - 'logback.groovy'
                - '*.properties'
environments:
    development:
        management:
            endpoints:
                enabled-by-default: true
                web:
                    base-path: '/actuator'
                    exposure:
                        include: '*'
    production:
        management:
            endpoints:
                enabled-by-default: false

---
grails:
    mime:
        disable:
            accept:
                header:
                    userAgents:
                        - Gecko
                        - WebKit
                        - Presto
                        - Trident
        types:
            all: '*/*'
            atom: application/atom+xml
            css: text/css
            csv: text/csv
            form: application/x-www-form-urlencoded
            html:
              - text/html
              - application/xhtml+xml
            js: text/javascript
            json:
              - application/json
              - text/json
            multipartForm: multipart/form-data
            pdf: application/pdf
            rss: application/rss+xml
            text: text/plain
            hal:
              - application/hal+json
              - application/hal+xml
            xml:
              - text/xml
              - application/xml
    urlmapping:
        cache:
            maxsize: 1000
    controllers:
        defaultScope: singleton
    converters:
        encoding: UTF-8
    views:
        default:
            codec: html
        gsp:
            encoding: UTF-8
            htmlcodec: xml
            codecs:
                expression: html
                scriptlet: html
                taglib: none
                staticparts: none

---
hibernate:
    cache:
        queries: false
        use_second_level_cache: false
        use_query_cache: false
dataSource:
    pooled: true
    jmxExport: true
    driverClassName: org.postgresql.Driver
    dialect: org.hibernate.dialect.PostgreSQLDialect
    type: com.zaxxer.hikari.HikariDataSource
    username: dict_ap
    password: 'LF2.net'
    logSql: false
    formatSql: true

environments:
    development:
        dataSource:
            dbCreate: none
            url: jdbc:postgresql://127.0.0.1:5432/ironman_dict_db
    test:
        dataSource:
            dbCreate: none
            url: jdbc:postgresql://127.0.0.1:5432/ironman_dict_db
    production:
        dataSource:
            dbCreate: none
            url: jdbc:postgresql://127.0.0.1:5432/ironman_dict_db
            properties:
                jmxEnabled: true
                initialSize: 5
                maxActive: 50
                minIdle: 5
                maxIdle: 25
                maxWait: 10000
                maxAge: 600000
                timeBetweenEvictionRunsMillis: 5000
                minEvictableIdleTimeMillis: 60000
                validationQuery: SELECT 1
                validationQueryTimeout: 3
                validationInterval: 15000
                testOnBorrow: true
                testWhileIdle: true
                testOnReturn: false
                jdbcInterceptors: ConnectionState
                defaultTransactionIsolation: 2 # TRANSACTION_READ_COMMITTED

grails-app/conf/application.yml 主要是專案的參數文件配置。

在 grails 框架中有三個預設環境,方便在不同環境中切換:

  • development: 開發環境
  • test: 測試環境
  • production: 生產環境

development 因為有配置 spring-boot-devtools ,所以在更動程式原始碼時,會啟用熱部署。

另外如果有第四種以上的環境需要特別部署,可以自行命名,範例如下:

  • grails-app/conf/application.yml
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
...
environments:
    test:
        dataSource:
            dbCreate: none
            url: jdbc:postgresql://127.0.0.1:5432/ironman_dict_db
    example:
        dataSource:
            dbCreate: none
            url: jdbc:postgresql://127.0.0.1:5432/ironman_dict_db
...

在執行,時需要加參數指定環境: -Dgrails.env

1
-Dgrails.env=example

不同環境的log配置
#

grails log 預設是 grails-app/conf/logback.xml,有需求可以另外新增。以下是針對不同環境處理的文件:

  • 預設: grails-app/conf/logback.xml
  • 測試環境: grails-app/conf/logback-test.xml
  • 開發環境: grails-app/conf/logback-development.xml

相對應的文件描述如下

  • grails-app/conf/application.yml
1
2
3
4
5
6
7
8
9
...
environments:
    development:
        logging:
            config: grails-app/conf/logback-development.xml
    test:
        logging:
            config: "${catalina.base}/webapps/ironman-dict/WEB-INF/classes/logback-test.xml"
...
  • grails-app/conf/logback.xml
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
<?xml version="1.0" encoding="UTF-8"?>
<configuration>

    <conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter" />
    <conversionRule conversionWord="wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter" />

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <charset>UTF-8</charset>
            <pattern>%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(%5p) %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n%wex</pattern>
        </encoder>
    </appender>

    <root level="error">
        <appender-ref ref="STDOUT" />
    </root>
</configuration>
  • grails-app/conf/logback-test.xml, grails-app/conf/logback-development.xml
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
<?xml version="1.0" encoding="UTF-8"?>
<configuration>

    <conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter" />
    <conversionRule conversionWord="wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter" />

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <charset>UTF-8</charset>
            <pattern>%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(%5p) %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n%wex</pattern>
        </encoder>
    </appender>

    <root level="info">
        <appender-ref ref="STDOUT" />
    </root>
</configuration>