かんがるーさんの日記

最近自分が興味をもったものを調べた時の手順等を書いています。今は Spring Boot をいじっています。

Spring Boot 2.0.x の Web アプリを 2.1.x へバージョンアップする ( その6 )( Thymeleaf テンプレートの html タグの属性を変更し、メトリックスのタグの定義場所を変更する )

概要

記事一覧はこちらです。

Spring Boot 2.0.x の Web アプリを 2.1.x へバージョンアップする ( その5 )( テストが大量に失敗する原因を解消する2 ) の続きです。

  • 今回の手順で確認できるのは以下の内容です。
    • Thymeleaf テンプレートの html タグに記述している属性から不要なものを削除し、xmlns:sec の記述を変更します。
    • メトリックスの application label を定義する場所を変更します。

参照したサイト・書籍

  1. Google「言語指定にlangは使うな」/公式発言
    https://seopack.jp/seoblog/20161111-use-hreflang/

  2. What is the difference between HTML lang tag and HTML hreflang tag?
    https://webmasters.stackexchange.com/questions/105998/what-is-the-difference-between-html-lang-tag-and-html-hreflang-tag

  3. 多言語サイトはrel=”altrenate” hreflang=”x”で管理しよう
    https://seopack.jp/internal-seo/crawler-measures/multilingual-site-hreflang.php

  4. Search Console ヘルプ - ページのローカライズ版について Google に知らせる
    https://support.google.com/webmasters/answer/189077?hl=ja

  5. xmlns:sec is no longer available
    https://github.com/thymeleaf/thymeleaf-extras-springsecurity/issues/52

  6. Spring Boot 2.1 Release Notes - Micrometer
    https://github.com/spring-projects/spring-booT/wiki/Spring-Boot-2.1-Release-Notes#micrometer

目次

  1. Thymeleaf テンプレートの xmlns="http://www.w3.org/1999/xhtml" を削除する
  2. xmlns:sec の記述を xmlns:sec="http://www.thymeleaf.org/extras/spring-security" に変更する
  3. メトリックスの application label を application.properties の management.metrics.tags.application で定義するよう変更する

手順

Thymeleaf テンプレートの xmlns="http://www.w3.org/1999/xhtml" を削除する

現在 Thymeleaf テンプレートの html タグに以下のように属性を記述しているのですが、

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">

xmlns="http://www.w3.org/1999/xhtml" は Thymeleaf のバージョンが 2 系のころの記述で 3 系になったら不要になったので削除します。

また IntelliJ IDEA で html を作成すると以下のように記述されたファイルが作成されるので lang="ja" を記述しようと思ったのですが、

<!DOCTYPE html>
<html lang="en">

"html lang" で Google で検索してみたら Google「言語指定にlangは使うな」/公式発言 というページを見つけました。他にも調べてみたところ、

  • lang 属性は信頼性がないので Google は参照していない。
  • 多言語ページを作成しているならば <link rel="alternate" href="..." hreflang="...(ja 等)" /> を記述する。ただし ccTLD(国別コードトップレベルドメイン)の場合は不要。

らしいので lang="ja" は記述しないことにします。

xmlns:sec の記述を xmlns:sec="http://www.thymeleaf.org/extras/spring-security" に変更する

xmlns:sec の記述について src/main/resources/templates/loginsuccess.html では、

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.w3.org/1999/xhtml">

src/main/resources/templates/common/mainparts.html では、

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">

と記述しています。おそらく前者は IntelliJ IDEA で自動補完したもの、後者はどこかの Web サイトを見てコピペしたものでしょう。

IntelliJ IDEA でタグの候補が表示されるのは xmlns:sec="http://www.thymeleaf.org/extras/spring-security" なので、この記述に変更します。変更すると以下のように候補が表示されます。

f:id:ksby:20190217163552p:plain

メトリックスの application label を application.properties の management.metrics.tags.application で定義するよう変更する

Spring Boot Statistics でメトリックスを表示する時に使用する application label を

f:id:ksby:20190217193527p:plain

今は docker/prometheus/prometheus.yml で以下のように定義していますが、

- job_name: 'spring-actuator'
  metrics_path: '/actuator/prometheus'
  scrape_interval: 15s
  basic_auth:
    username: actuator
    password: xxxxxxxx
  static_configs:
  # Docker で起動した Prometheus からローカルPCで起動している Spring Boot のアプリケーション
  # にアクセスする。docker-compose.yml の extra_hosts に定義した PC のホスト名 app を設定する。
  - targets: ['app:8080']
    labels:
      application: 'lending'

Spring Boot 2.1 Release Notes - Micrometer を見ると application.properties に management.metrics.tags.~= の形式で定義できるようになったので、定義場所を変更します。

docker/prometheus/prometheus.yml から labels の定義を削除した後、

- job_name: 'spring-actuator'
  metrics_path: '/actuator/prometheus'
  scrape_interval: 15s
  basic_auth:
    username: actuator
    password: xxxxxxxx
  static_configs:
  # Docker で起動した Prometheus からローカルPCで起動している Spring Boot のアプリケーション
  # にアクセスする。docker-compose.yml の extra_hosts に定義した PC のホスト名 app を設定する。
  - targets: ['app:8080']

src/main/resources/application.properties に management.metrics.tags.application=lending の定義を追加します。

host.ip.address=172.31.16.1
doma.dialect=org.seasar.doma.jdbc.dialect.PostgresDialect

management.endpoints.web.exposure.include=health,info,loggers,prometheus
management.metrics.tags.application=lending

docker-compose up -d でサーバを起動した後、Tomcat を起動してから Grafana を見ると Application に「lending」が表示されています。

f:id:ksby:20190217194918p:plain

履歴

2019/02/17
初版発行。