先日のBuildでService ConnectorがGAとなったようです。
ドキュメントをみてもあまりピンとこなかったので、実際に試してみました。
サンプルアプリ
サンプルアプリとしては、Springが公開しているMySQLを使ったサンプルアプリを使ってみます。
MySQLはflexible serverで作成します。今回の本論とは関係ありませんが、Azure Portal上でデータベースを作成できることを知りました。
JavaアプリはAzure Web Appsで動かします。Azure Portal上で「サービス コネクタ」というものがあるので、そこから「Create」ボタンを押します。
試した時点ではご覧のサービスがサポートされているようです。
現れる各項目に対して必要な情報を入れたらOKです。一番最後の「Client type」に「SpringBoot」というものがあったのでそれを選択しておきます。
保存したらデーターベース接続に必要な情報が登録されていました。
環境変数にも登録されていました。
ここらへんの挙動は、以下のドキュメントに記載がありました。
以上の設定でアプリも無事動きました。Azure Web Appsのログストリームに流れているログをみると、データベースに接続できていることが確認できます。
2022-06-05 08:16:59.160 INFO 167 --- [main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2022-06-05 08:16:59.168 INFO 167 --- [main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 7966 ms 2022-06-05 08:17:00.154 INFO 167 --- [main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default] 2022-06-05 08:17:00.481 INFO 167 --- [main] org.hibernate.Version : HHH000412: Hibernate ORM core version 5.6.9.Final 2022-06-05 08:17:01.613 INFO 167 --- [main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.1.2.Final} 2022-06-05 08:17:02.168 INFO 167 --- [main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting... 2022-06-05 08:17:03.737 INFO 167 --- [main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed. 2022-06-05 08:17:03.952 INFO 167 --- [main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.MySQL8Dialect 2022-06-05 08:17:07.249 INFO 167 --- [main] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform] 2022-06-05 08:17:07.399 INFO 167 --- [main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default' 2022-06-05T08:17:11.320Z INFO - Waiting for response to warmup request for container xxxxxxxxxxxxxx_b9b31849. Elapsed time = 30.6847269 sec 2022-06-05 08:17:08.882 WARN 167 --- [main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning 2022-06-05 08:17:10.823 INFO 167 --- [main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 80 (http) with context path '' 2022-06-05 08:17:10.903 INFO 167 --- [main] .g.m.a.AccessingDataWithMysqlApplication : Started AccessingDataWithMysqlApplication in 23.331 seconds (JVM running for 25.611) 2022-06-05T08:17:12.302Z INFO - Container xxxxxxxxxxxxxx_b9b31849 for site app-miyohide-mysql initialized successfully and is ready to serve requests. 2022-06-05 08:17:11.627 INFO 167 --- [http-nio-80-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet' 2022-06-05 08:17:11.657 INFO 167 --- [http-nio-80-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet' 2022-06-05 08:17:11.664 INFO 167 --- [http-nio-80-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 1 ms
アプリも無事データを取得できていることが確認できました。