結論からいうと、ClearDBサービス(MySQL)を用意してアプリにバインドすると自動でユーザー、マスワード、JDBC-URLを用意してくれました。
このデータソース情報を使うために、src/main/resourcesにapplication-cloud.propertiesを用意します。ローカル環境では、application.propertiesを使い、クラウド環境ではapplication-cloud.propertiesを使うようになるそうです。
中身は、以下のように、spring.jpa.hibernate.ddl-auto=updateで、テーブルがなかったらエンティティクラス情報から作るという指定でよいです。
|
#spring.jpa.hibernate.ddl-auto=create spring.jpa.hibernate.ddl-auto=update #logging.level.org.hibernate.SQL=TRACE #spring.datasource.initialize=false |
https://docs.spring.io/spring-boot/docs/current/reference/html/howto-database-initialization.html
cf envでデータソースの確認
コマンドラインから、cf env アプリ名でユーザ、パスワード、jdbcUrlが確認できます。(*以下は、ユーザー名などをxyzなどのようにダミーにしてます。)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
f> cf env rariru Getting env variables for app rariru in org dev00xxxxxx-org / space development as dev00xxxxxxx@gmail.com... OK System-Provided: { "VCAP_SERVICES": { "cleardb": [ { "binding_name": null, "credentials": { "hostname": "us-cdbr-iron-east-00abc.cleardb.net", "jdbcUrl": "jdbc:mysql://us-cdbr-iron-east-04.cleardb.net/ad_xyz?user=1111\u0026password=0000", "name": "ad_xyz", "password": "0000", "port": "3306", "uri": "mysql://b4:xxxx@us-cdbr-iron-east-04.cleardb.net:3306/ad_xyz?reconnect=true", "username": "b4" }, "instance_name": "rariru-db", "label": "cleardb", "name": "rariru-db", "plan": "spark", |
その他・参考にしたチュートリアル
TAGVOTE「タグボート」の SpringBoot入門 Spring公式のチュートリアルである Accessing data with MySQL や Building an Application with Spring Boot
まとめ
ローカルにMySQLとSTSを用意したので、PCFでMySQLを設定する方法が分からずいろいろ調べましたが簡単でした。