Try   HackMD

HAPI FHIR 伺服器建置指南(新手向)

Prerequisites

這份指南將會使用 Rocky Linux 8.4 做為教學用的系統,Rocky Linux 8 是 CentOS 8 的替代版本,用起來一模一樣,如果你沒看過,現在讓你看看:

Rocky Linux 8 官方網站https://rockylinux.org/zh-tw/

如果你有在使用 VMWare、VirtualBox 等虛擬化平台,可以考慮改用 Proxmox VE。他是一個開放原始碼的虛擬化平台,並提供一個類似 VMWare vCloud 的 Web 管理工具。你可以在上面開 QEMU VM 跟LXC Container,也可以在上面實現 Ceph、HA Cluster 等機制,全部都滑鼠點一點就好。

Proxmox VE 上面已經有 Rocky Linux 8 的 LXC Image,直接抓下來開就行了,請參考以下說明,或是你也可以參考 Jason(Proxmox VE 專家,江湖人稱"節神")的系列文章:

Proxmox VE 官方網站https://pve.proxmox.com/wiki/Main_Page
節省工具箱部落格https://blog.jason.tools/

說明

這份筆記是給完全沒接觸過 HAPI FHIR 架設的初學者使用的(當然建議讀者還是要知道怎麼操作 Linux 作業系統),在接下來的教學中,你將學會如何從官方的 GitHub Repository 把 HAPI FHIR 伺服器抓下來,設定好 PostgreSQL 資料庫後,直接把伺服器跑起來。

至於打包成 Docker 之類的進階操作,請參考另一份筆記: FHIR 伺服器建置指南

環境設定與安裝

安裝 EPEL 8 擴展套件庫

安裝 EPEL8 擴展套件庫並更新系統,中間會跳出提示,直接 y 到底就好

$ dnf install epel-release -y $ dnf update -y

安裝 PostgreSQL 14

安裝 PostgreSQL 套件庫,中間會跳出提示,直接 y 到底就好

$ dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm $ dnf update -y

把內建的 PostgreSQL 套件庫停用

$ dnf -qy module disable postgresql

安裝 PostgreSQL 14 伺服器(寫這篇文的時候剛好 14 stable 版本 release 出來了,就直接上)

$ dnf install postgresql14 postgresql14-server -y

安裝好之後確認一下 PostgreSQL 版本對不對

$ psql -V

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

開始之前要先初始化 PostgreSQL 14 資料庫

$ /usr/pgsql-14/bin/postgresql-14-setup initdb

確認版本沒問題後,就可以啟動並設定開機自動啟用

$ systemctl enable postgresql-14 $ systemctl start postgresql-14

檢查一下 PostgreSQL 是否正常啟動

$ systemctl status postgresql-14

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

設定 PostgreSQL

安裝好後,PostgreSQL 預設會幫你建立一個叫 postgres 的使用者,記得先改密碼

$ passwd postgres

切換到 postgres 這個使用者

$ su - postgres

改資料庫管理員 postgres 的預設密碼

$ psql -c "ALTER USER postgres WITH PASSWORD '新密碼';"

顯示 ALTER ROLE 代表有改成功

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

注意:這邊密碼設 1234 是只做教學展示用途,好孩子請不要學,否則你的資料庫會被打到不要不要的 ˊ_>ˋ

新建 FHIR 用的資料庫

連線進去資料庫

$ psql

進來後你會看到這樣的提示:

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

新建使用者

$ create user fhir with encrypted password 'hapifhir'; # 語法長這樣 $ create user 使用者名稱 with encrypted password '密碼';

注意:還是要強調,密碼請設定強密碼,否則你的資料庫會被打到連你老板都認不出來 ˊ_>ˋ

新建資料庫

$ create database hapifhir with owner fhir template template0; # 語法長這樣 $ create database 資料庫名稱 with owner 資料庫擁有者名稱 template template0;

將所有權限給這個使用者

$ grant ALL privileges on database hapifhir to fhir; # 語法長這樣 $ grant ALL privileges on database 資料庫名稱 to 使用者名稱;

設定資料庫時區為臺灣時區

$ alter database hapifhir set timezone to 'ROC'; # 語法長這樣 $ alter database 資料庫名稱 set timezone to '時區代碼';

上面全部設定完之後就退出 PostgreSQL 並退出 postgres 帳號

postgres=# \q $ exit

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

安裝 HAPI FHIR 伺服器

我們會直接使用最新版的 HAPI FHIR 伺服器,並設定使用 R4(FHIR 4.0.1) 版本。

因為這個是用 Java 寫的,所以會需要準備 Java 跟 Maven 環境。另外各版本 HAPI FHIR 支援的 Java 版本都不一樣,記得參考以下網站:
https://hapifhir.io/hapi-fhir/docs/getting_started/versions.html

在最新版本的 HAPI FHIR 裡面,官方雖然將最小 Java 版本訂為 Java 8,不過實際上 Java 8 會編譯失敗,因此要改用 Java 11

安裝必要套件

安裝必要套件

$ dnf install -y vim git make gcc gcc-c++ tmux

安裝 Java 11(你也可以試試看 Java 16) 跟 Maven

$ dnf install java-11-openjdk-devel maven

由於 Maven 3.5.4 預設使用 Java 8,因此要把版本切換回 Java 11

$ alternatives --config java $ alternatives --config javac

讓 Maven 預設使用 Java 11

$ vim /etc/profile

最下面加入這行

export JAVA_HOME=/usr/lib/jvm/java-11-openjdk

讓他生效

$ source /etc/profile

下載並設定 HAPI FHIR 伺服器

從 GitHub 把 Repository 抓下來

$ git clone https://github.com/hapifhir/hapi-fhir-jpaserver-starter.git

設定 HAPI FHIR Server 組態

$ cd hapi-fhir-jpaserver-starter/ $ vim src/main/resources/application.yaml

把資料庫改為 PostgreSQL 並設定連線資訊

spring: datasource: url: 'jdbc:postgresql://localhost:5432/hapifhir' username: fhir password: hapifhir driverClassName: org.postgresql.Driver jpa: hibernate.dialect: ca.uhn.fhir.jpa.model.dialect.HapiFhirPostgres94Dialect

設定使用 FHIR R4 版本:

hapi: fhir: ### This is the FHIR version. Choose between, DSTU2, DSTU3, R4 or R5 fhir_version: R4 server_address: http://<伺服器URL>/fhir # 設定伺服器 URL default_encoding: JSON # 預設使用 JSON default_pretty_print: true # 預設格式化輸出 default_page_size: 20 # 預設搜尋結果分頁數量 graphql_enabled: true # 啟用 GraphQL cors: # 啟用 CORS 設定 allow_Credentials: true allowed_origin: - '*' tester: home: name: Local Tester server_address: 'http://<伺服器URL>/fhir' # 設定伺服器位址 refuse_to_fetch_third_party_urls: false fhir_version: R4 validation: # 設定伺服器端驗證 requests_enabled: true responses_enabled: true

編譯並執行

進入 tmux 虛擬視窗

$ tmux

編譯成 war 可執行檔,編譯候會放在 target/ROOT.war 裡面

$ mvn clean package spring-boot:repackage -Pboot

上面這條指令預設以 Single Thread 模式執行,如果你的主機是多核心,也可以加入-T <thread>參數,幾核心就打多少數字,像這樣:

$ mvn clean package spring-boot:repackage -Pboot -T 20

看到 BUILD SUCCESS 就代表編譯成功了

用 Java 執行

$ java -jar target/ROOT.war

只要不跳錯誤,並且看到 Completed initialization in ....ms 這行就代表啟動成功了

伺服器預設會開在 8080 Port,請直接打開瀏覽器輸入 http://<IP>:8080即可看到 HAPI FHIR 頁面