Google Code Prettify

顯示具有 spring boot 標籤的文章。 顯示所有文章
顯示具有 spring boot 標籤的文章。 顯示所有文章

2018年3月18日 星期日

在 embedded tomcat 中使用 Tomcat 資料庫連線

在 spring boot 中,如果在 build.gradle 中包含了 web 套件,就會自動包裝了 embedded tomcat,那麼,如果程式要連線資料庫,要怎麼利用這個 embedded tomcat 中的 connection pool ? 很簡單,如下。
  • build.gradle
buildscript {
    ext {
        springBootVersion = '2.0.0.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse-wtp'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'war'

group = 'idv.steven.jndi'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

configurations {
    providedRuntime
}

dependencies {
    compile('org.springframework.boot:spring-boot-starter-data-jpa')
    compile('org.springframework.boot:spring-boot-starter-web')
    compile('org.apache.tomcat:tomcat-jdbc')
    compileOnly('org.projectlombok:lombok')
    testCompile('org.springframework.boot:spring-boot-starter-test')
 
    compile group: 'org.mariadb.jdbc', name: 'mariadb-java-client', version: '2.2.1'
}
如上所示,我引入了 spring boot 2.x 版,因為我的資料庫是 maria db,所以也要把相關的 JDBC 驅動程式引入,同時,因為這裡要使用 tomcat connection pool,要引入 tomcat jdbc。spring boot 在後續程式中,會根據資料庫的 url 判斷是什麼資料庫,而載入相關驅動程式。
  • application.properties
spring.datasource.type=org.apache.tomcat.jdbc.pool.DataSource
#spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
spring.datasource.url=jdbc:mariadb://192.168.0.103:3306/demo
spring.datasource.username=steven
spring.datasource.password=P@ssw0rd

spring.datasource.tomcat.max-active=23
spring.datasource.tomcat.max-idle=7
spring.datasource.tomcat.min-idle=2
spring.datasource.tomcat.max-wait=9997
spring.datasource.type 指定要使用的 connection pool,spring.datasource.tomcat.*,是設定 connection pool 的一些參數,我故意設定一些比較奇怪的值,是為了觀察是否真的有使用到 tomcat connection pool。spring.datasource.driver-class-name 不需設定,spring boot 從 url 中可以自行判斷。在 application.properties 設定完,不需要寫任何程式,spring boot 就幫我們建立好 tomcat connection pool。
  • 觀察
我在 eclipse 使用 debug mode 執行,觀察 EntityManager 的值,於「em > h > target Factory > h > entityManagerFactoryBean > persistenceUnitInfo > nonJtaDataSource > poolProperties」裡看到如下內容,證明確實有用到 tomcat connection pool。

2018年2月28日 星期三

Spring Boot Admin

Spring Boot 廣泛用於微服務,在這種分散式系統,監控是一件很重要的事,Spring Boot 提供了一個名為 Spring Boot Admin 的監控工具,只需要進行一些設定就可以監控,非常方便,說明如下:
  • 建立一個 spring boot admin 的 server 端程式
下面是這個程式的 build.gradle,和一般的 spring boot 沒什麼不同,主要就是加入紅色那幾行,讓 gradle 協助我們載入相關的 jar 檔。spring boot 預設使用的 log 是 logback,但是我習慣用 log4j2,所以加入綠色的那幾行,將 logback 移除並加入 log4j2。
buildscript {
 ext {
  springBootVersion = '1.5.10.RELEASE'
 }
 repositories {
  mavenCentral()
 }
 dependencies {
  classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
 }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'

group = 'idv.steven'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
 mavenCentral()
}

configurations.all {
      exclude group: 'log4j', module: 'log4j'
      exclude group: 'org.slf4j', module: 'slf4j-log4j12'
      exclude group: 'org.slf4j', module: 'log4j-over-slf4j'
      exclude group: 'ch.qos.logback', module: 'logback-core'
      exclude group: 'ch.qos.logback', module: 'logback-classic'
}

ext {
      springBootAdminVersion = '1.5.7'
      log4j2Version = '2.10.0'
}

dependencies {
      compile('de.codecentric:spring-boot-admin-starter-server')
      compile('de.codecentric:spring-boot-admin-server-ui')
 
      compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: "${log4j2Version}"
      compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: "${log4j2Version}"
      compile group: 'org.apache.logging.log4j', name: 'log4j-slf4j-impl', version: "${log4j2Version}"
 
      testCompile('org.springframework.boot:spring-boot-starter-test')
}

dependencyManagement {
      imports {
            mavenBom "de.codecentric:spring-boot-admin-dependencies:${springBootAdminVersion}"
      }
}
  • 設定 port
server.port=8081
embedded tomcat 預設的 port 是 8080,因為我打算用來測試的 client 程式「第一個 spring boot web 程式 (使用 embedded tomcat)」也有用到 embedded tomcat 已經佔去了 8080 port,所以在 application.properties 中加入如上內容設定使用 8081 port。
  • 執行 server
執行上述 server 程式後,打開瀏覽器,打入 http://localhost:8081,可以看到如下畫面,這樣就確定已經正常啟動了!
接下來要來看一下 client 程式怎麼修改,可以讓這個 server 進行監控。
  • 修改 client 程式的 build.gradle
buildscript {
 ext {
  springBootVersion = '1.5.10.RELEASE'
 }
 repositories {
  mavenCentral()
 }
 dependencies {
  classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
 }
}

apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'

group = 'idv.steven'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
      mavenCentral()
}

configurations.all {
      exclude group: 'log4j', module: 'log4j'
      exclude group: 'org.slf4j', module: 'slf4j-log4j12'
      exclude group: 'org.slf4j', module: 'log4j-over-slf4j'
      exclude group: 'ch.qos.logback', module: 'logback-core'
      exclude group: 'ch.qos.logback', module: 'logback-classic'
}

ext {
      springBootAdminVersion = '1.5.7'
      log4j2Version = '2.10.0'
}

dependencies {
      compile('org.springframework.boot:spring-boot-starter-data-jpa') 
      compile('org.springframework.boot:spring-boot-starter-web')
      compile('org.springframework.boot:spring-boot-starter-thymeleaf')
      compile('de.codecentric:spring-boot-admin-starter-client')
 
      compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: "${log4j2Version}"
      compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: "${log4j2Version}"
      compile group: 'org.apache.logging.log4j', name: 'log4j-slf4j-impl', version: "${log4j2Version}"
      
      compile group: 'org.mariadb.jdbc', name: 'mariadb-java-client', version: '2.2.1'
      compile group: 'javax.inject', name: 'javax.inject', version: '1'
      compileOnly('org.projectlombok:lombok')
      testCompile('org.springframework.boot:spring-boot-starter-test')
}

dependencyManagement {
      imports {
            mavenBom "de.codecentric:spring-boot-admin-dependencies:${springBootAdminVersion}"
      }
}
紅色部份是為了導入 Spring Boot Admin client 而加入的。
  • 修改 application.properties (client)
spring.boot.admin.url=http://localhost:8081
spring.boot.admin.client.name=demo

endpoints.sensitive=false
endpoints.logfile.external-file=d:/tmp/demo.log
在 application.properties 中加入如上設定,前兩行指出 Spring Boot Admin Server 的網址及要顯示在監控畫面的程式名稱; 後兩行是為了將 client 的 log4j2 產生的 log file 送到監控畫面而設定的,這樣萬一監控時發現錯誤,可以立即看 log。
  • 執行 client
執行後,client 會自動連到 server 註冊,然後就會顯示在監控畫面,如上。如果想看更詳細的資料,可以按【Details】,將顯示如下畫面,其中第二項「Log」就是 log4j2 的 log file 內容。



2018年2月26日 星期一

Gradle: 編譯 spring boot application

這篇說明如何使用 Gradle 編譯 spring boot application,直接來看個例子。
buildscript {
    ext {
        springBootVersion = '1.5.10.RELEASE'
    }
    repositories {
        maven { url "http://maven.springframework.org/milestone" }
        maven { url "http://repo.maven.apache.org/maven2" }
        maven { url "http://repo1.maven.org/maven2/" }
        maven { url "http://amateras.sourceforge.jp/mvn/" }
        mavenCentral()
        jcenter()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'org.springframework.boot'

group = 'idv.steven.crawler'
sourceCompatibility = 1.8
targetCompatibility = 1.8

bootRepackage {
    mainClass = 'idv.steven.crawler.CrawlerStarter'
}

jar {
    baseName = 'my-crawler'
    version =  '1.0.0'  
}

buildDir = 'build'

repositories {
      mavenCentral()
      maven { url "http://maven.springframework.org/milestone" }
      maven { url "http://repo.maven.apache.org/maven2" }
      maven { url "http://repo1.maven.org/maven2/" }
      maven { url "http://amateras.sourceforge.jp/mvn/" }
}

configurations.all {
      exclude group: 'log4j', module: 'log4j'
      exclude group: 'org.slf4j', module: 'slf4j-log4j12'
      exclude group: 'org.slf4j', module: 'log4j-over-slf4j'
      exclude group: 'ch.qos.logback', module: 'logback-core'
      exclude group: 'ch.qos.logback', module: 'logback-classic'
}

dependencies {
      def log4j2Version = '2.10.0'
      def fasterxmlVersion = '2.9.3'
      def hibernateVersion = '5.2.13.Final'
 
      compile fileTree(dir: 'lib', include: ['*.jar'])

      compile("org.springframework.boot:spring-boot-starter-web") {
          exclude module: "spring-boot-starter-tomcat"
      }
      compile('org.springframework.boot:spring-boot-starter-aop')
      compile('org.springframework.boot:spring-boot-starter-batch')
      compile('org.springframework.boot:spring-boot-starter-data-jpa')
      compile('org.springframework.boot:spring-boot-starter-mail')
      compile("org.thymeleaf:thymeleaf-spring4")
      compile("nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect")
 
      compile group: 'org.hibernate', name: 'hibernate-core', version: "${hibernateVersion}"
      compile group: 'org.hibernate', name: 'hibernate-entitymanager', version: "${hibernateVersion}"
      compile group: 'org.hibernate', name: 'hibernate-validator', version: '6.0.7.Final'
 
      compile group: 'javax.inject', name: 'javax.inject', version: '1'
      compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: "${log4j2Version}"
      compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: "${log4j2Version}"
      compile group: 'org.apache.logging.log4j', name: 'log4j-slf4j-impl', version: "${log4j2Version}"
      compile group: 'joda-time', name: 'joda-time', version: '2.9.9'
      compile group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: "${fasterxmlVersion}"
      compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: "${fasterxmlVersion}"
      compile group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version: "${fasterxmlVersion}"
      compile group: 'org.json', name: 'json', version: '20180130'
      compile group: 'net.minidev', name: 'json-smart', version: '2.3'
      compile group: 'javax.xml.bind', name: 'jaxb-api', version: '2.3.0'
      compile group: 'javax.validation', name: 'validation-api', version: '2.0.1.Final'
      compile group: 'com.lmax', name: 'disruptor', version: '3.3.7'
 
      compileOnly "org.projectlombok:lombok:1.16.20"
 
      testCompile('org.springframework.boot:spring-boot-starter-test')
      testCompile('org.springframework.batch:spring-batch-test')
}

  • 一開始 buildscript 中紅色部份,設定 spring boot 版本,後面 dependencies (依賴) 的部份引入 spring boot 時,即會引入這個版本。
  • buildscript 的 repositories 指定了好幾個版本庫,一般都只指定 mavenCentral(),但是,有時這個版本庫裡可能沒有我們要的 jar 檔,也可能剛好掛了或被公司防火牆擋了,設定多個比較可以確保可以抓到需要的 jar 檔。
  • apply plugin 我們導入 java 和 org.springframe.boot,這樣我們就可以使用這兩個物件的 method。
  • bootRepackage (黃底) 設定這個 application 的 main 檔在那一個類別裡。
  • jar (綠底) 設定了這個 application 編譯出來的檔名及版號。
  • buildDir (紅底) 指出編譯出來的檔案要放在那個目錄下,不指定的話,預設就會放在 build 目錄下。
  • configurations.all 在這裡可以定義一些 global 的設定,以上例來說,定義了在依賴關係中,如果有依賴這些 jar 檔的,就不要引入,因為 spring boot 預設是的 log 機制是用 logback,有些其它的 framework 預設是用 log4j,但是我希望這個程式用 log4j2,所以在 這些設定排除 logback 及 log4j 的 jar 檔。
  • compile fileTree 是當有一些 jar 檔就在自己的本機,因為我有使用一些公司內部自行開發的共用函式庫的 jar 檔,這些 jar 檔放在 lib 目錄下,如此設定,就會把這些 jar 檔也包進來一起編譯。
  • exclude module (紫底) 是在設定要引入的 jar 檔時,Gradle 會自動將相依的 jar 檔包裝進來,但是,有時有些 jar 檔我們並不需要,就可以用這個方法將它排除! 在 spring boot 中,引入 web 時,預設會包含 embedded tomcat,但是這個程式不是 web 程式,只是會用到 http client,所以將 embedded tomcat 排程。這裡還有個要特別注意的,spring boot 在一開頭已經有設定版號,這些不需寫版號。
  • 有些 jar 檔只有在 compile 階段會用到,這時候就可以用 compileOnly (橘底),lombok 只是在編譯時幫程式員產生些像是 getter、setter method,或是 log 的宣告等,以簡化程式的撰寫,但是產生好讓 Gradle 編譯後,就沒它的事了,runtime 並不會用到,也不會被包入最後產生的 jar 檔裡。
  • testCompile 是用來引入單元測試用的 jar 檔,這些 jar 檔只有開發階段進行單元測試時會用到,也不會被包入最後產生的 jar 檔裡。