iT邦幫忙

2023 iThome 鐵人賽

DAY 13
0
Modern Web

來架個網站吧系列 第 13

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

  • 分享至 

  • xImage
  •  
tags: 來架個網站吧 Grails

我是目錄


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

設定專案環境參數

grails-app/conf/application.yml

---
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
...
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

-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
...
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
<?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
<?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>

上一篇
來架個網站吧-12.網站開發-2: 設定環境參數-1
下一篇
來架個網站吧-14.網站開發-4-MVC-1-view
系列文
來架個網站吧30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言