Skip to content

Commit

Permalink
support postgresql store meta data instead of h2 database (#828)
Browse files Browse the repository at this point in the history
* support postgresql store meta data instead of h2 database
add docker-compose file for postgresql
add related docs

* Update home/docs/start/postgresql-change.md

Co-authored-by: tomsun28 <tomsun28@outlook.com>
Signed-off-by: vinci <55838224+vinci-897@users.noreply.github.com>

* Update home/docs/start/postgresql-change.md

Co-authored-by: tomsun28 <tomsun28@outlook.com>
Signed-off-by: vinci <55838224+vinci-897@users.noreply.github.com>

* Update home/docs/start/postgresql-change.md

Co-authored-by: tomsun28 <tomsun28@outlook.com>
Signed-off-by: vinci <55838224+vinci-897@users.noreply.github.com>

* Update home/docs/start/postgresql-change.md

Co-authored-by: tomsun28 <tomsun28@outlook.com>
Signed-off-by: vinci <55838224+vinci-897@users.noreply.github.com>

---------

Signed-off-by: vinci <55838224+vinci-897@users.noreply.github.com>
Co-authored-by: tomsun28 <tomsun28@outlook.com>
  • Loading branch information
leo-934 and tomsun28 authored Apr 1, 2023
1 parent d9ffaed commit 04b7399
Show file tree
Hide file tree
Showing 8 changed files with 471 additions and 0 deletions.
73 changes: 73 additions & 0 deletions home/docs/start/postgresql-change.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
---
id: postgresql-change
title: Use PostgreSQL Replace H2 Database to Store Metadata(Optional)
sidebar_label: Use PostgreSQL Instead of H2
---
PostgreSQL is a RDBMS emphasiing extensibility and SQL compliance.. In addition to default built-in H2 database, HertzBeat allow you to use PostgreSQL to store structured relational data such as monitoring information, alarm information and configuration information.

> If you have the PostgreSQL environment, can be directly to database creation step.
### Install PostgreSQL via Docker
1. Download and install the Docker environment
Docker tools download refer to [Docker official document](https://docs.docker.com/get-docker/)
After the installation you can check if the Docker version normally output at the terminal.
```
$ docker -v
Docker version 20.10.12, build e91ed57
```
2. Install PostgreSQL with Docker
```
$ docker run -d --name postgresql -p 5432:5432 -e POSTGRES_USER=root -e POSTGRES_PASSWORD=123456 -e TZ=Asia/Shanghai postgresql:15
```
use```$ docker ps```to check if the database started successfully
3. Create database in container manually or with [script](https://github.com/dromara/hertzbeat/tree/master/script/docker-compose/hertzbeat-postgresql-iotdb/conf/sql/schema.sql).

### Database creation
1. Enter postgreSQL or use the client to connect postgreSQL service
```
su - postgres
psql
```
2. Create database named hertzbeat
`CREATE DATABASE hertzbeat;`
3. Check if hertzbeat database has been successfully created
`\l`

### Modify hertzbeat's configuration file application.yml and switch data source

1. Configure HertzBeat's configuration file
Modify `hertzbeat/config/application.yml` configuration file
Note⚠️The docker container way need to mount application.yml file locally,while you can use installation package way to unzip and modify `hertzbeat/config/application.yml`
Replace `spring.database` data source parameters, URL account and password.
```yaml
spring:
datasource:
driver-class-name: org.h2.Driver
username: sa
password: 123456
url: jdbc:h2:./data/hertzbeat;MODE=MYSQL
```
Specific replacement parameters is as follows and you need to configure account, ip, port according to the postgresql environment:
```yaml
spring:
config:
activate:
on-profile: prod
datasource:
driver-class-name: org.postgresql.Driver
username: root
password: 123456
url: jdbc:postgresql://127.0.0.1:5432/hertzbeat
hikari:
max-lifetime: 120000

jpa:
database: postgresql
hibernate:
ddl-auto: update
properties:
hibernate:
dialect: org.hibernate.dialect.PostgreSQLDialect
```
**Start HertzBeat visit http://ip:1157/ on the browser You can use HertzBeat monitoring alarm, default account and password are admin/hertzbeat**
2 changes: 2 additions & 0 deletions script/docker-compose/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

- Use Mysql + IoTDB as Hertzbeat dependent storage -> [HertzBeat+Mysql+IoTDB Solution](hertzbeat-mysql-iotdb)
- Use Mysql + Tdengine as Hertzbeat dependent storage -> [HertzBeat+Mysql+Tdengine Solution](hertzbeat-mysql-tdengine)
- Use Postgresql + IoTDB as Hertzbeat dependent storage -> [HertzBeat+PostgreSQL+IoTDB Solution](hertzbeat-postgresql-iotdb)


## Docker-Compose 部署方案

- 使用 Mysql + IoTDB 作为 Hertzbeat 依赖存储服务 -> [HertzBeat+Mysql+IoTDB 方案](hertzbeat-mysql-iotdb)
- 使用 Mysql + Tdengine 作为 Hertzbeat 依赖存储服务 -> [HertzBeat+Mysql+Tdengine 方案](hertzbeat-mysql-tdengine)
- 使用 PostgreSQL + IoTDB 作为 Hertzbeat 依赖存储服务 -> [HertzBeat+PostgreSQL+IoTDB 方案](hertzbeat-postgresql-iotdb)
28 changes: 28 additions & 0 deletions script/docker-compose/hertzbeat-postgresql-iotdb/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
## docker-compose deployment HertzBeat+PostgreSQL+IoTDB solution

> The docker-compose deployment scheme uses PostgreSQL + IoTDB as the dependent storage service of Hertzbeat.
> This solution will start three container services PostgreSQL, IoTDB, HertzBeat
##### Install Docker & Docker-compose

1. Download and install docker environment & docker-compose environment
Please refer to [Docker official website documentation](https://docs.docker.com/get-docker/), [Compose installation](https://docs.docker.com/compose/install/)
```
$ docker -v
Docker version 20.10.12, build e91ed57
```

##### docker compose deploys hertzbeat and its dependent services

1. Download the hertzbeat-docker-compose installation deployment script file
The script file is located in `script/docker-compose/hertzbeat-postgresql-iotdb` link [script/docker-compose](https://gitee.com/dromara/hertzbeat/tree/master/script/docker-compose/hertzbeat-postgresql-iotdb)


2. Enter the deployment script docker-compose directory, execute

`docker compose up -d`


##### Start exploring HertzBeat

Browser access `localhost:1157` to start, the default account password `admin/hertzbeat`
35 changes: 35 additions & 0 deletions script/docker-compose/hertzbeat-postgresql-iotdb/README_CN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
## docker-compose部署 HertzBeat+Postgresql+IoTDB 方案

- 如果不想部署而是直接使用,我们提供SAAS监控云-[TanCloud探云](https://console.tancloud.cn),即刻 **[登录注册](https://console.tancloud.cn)** 免费使用。
- 如果想自己本地快速部署的话,可以参考下面进行操作。

> docker-compose 部署方案使用了 PostgreSQL + IoTDB 作为 Hertzbeat 依赖存储服务。
> 此方案会启动三个容器服务 PostgreSQL, IoTDB, HertzBeat
##### 安装Docker & Docker-compose

1. 下载安装 docker 环境 & docker-compose 环境
请参考 [Docker官网文档](https://docs.docker.com/get-docker/), [Compose安装](https://docs.docker.com/compose/install/)
```
$ docker -v
Docker version 20.10.12, build e91ed57
```

##### docker compose部署hertzbeat及其依赖服务

1. 下载hertzbeat-docker-compose安装部署脚本文件
脚本文件位于代码仓库下`script/docker-compose/hertzbeat-postgre-iotdb` 链接 [script/docker-compose](https://gitee.com/dromara/hertzbeat/tree/master/script/docker-compose/hertzbeat-postgre-iotdb)


2. 进入部署脚本 docker-compose 目录, 执行

`docker compose up -d`


##### 开始探索HertzBeat

浏览器访问 `localhost:1157` 即可开始,默认账号密码 `admin/hertzbeat`

---

怎么样是不是很简单,只要几分钟就可以部署完成,赶紧试试吧!
132 changes: 132 additions & 0 deletions script/docker-compose/hertzbeat-postgresql-iotdb/conf/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

server:
port: 1157
spring:
application:
name: ${HOSTNAME:@hertzbeat@}${PID}
profiles:
active: prod
mvc:
static-path-pattern: /**
jackson:
default-property-inclusion: ALWAYS
web:
resources:
static-locations:
- classpath:/dist/
- classpath:../dist/
thymeleaf:
prefix: classpath:/templates/
check-template-location: true
cache: true
suffix: .html
mode: HTML

management:
health:
mail:
enabled: off
endpoints:
web:
exposure:
include:
- 'metrics'
- 'health'
- 'env'
enabled-by-default: on

sureness:
auths:
- digest
- basic
- jwt
jwt:
secret: 'CyaFv0bwq2Eik0jdrKUtsA6bx3sDJeFV643R
LnfKefTjsIfJLBa2YkhEqEGtcHDTNe4CU6+9
8tVt4bisXQ13rbN0oxhUZR73M6EByXIO+SV5
dKhaX0csgOCTlCxq20yhmUea6H6JIpSE2Rwp'

---
spring:
config:
activate:
on-profile: prod
datasource:
driver-class-name: org.postgresql.Driver
username: root
password: 123456
url: jdbc:postgresql://postgresql:5432/hertzbeat
hikari:
max-lifetime: 120000

jpa:
database: postgresql
hibernate:
ddl-auto: update
properties:
hibernate:
dialect: org.hibernate.dialect.PostgreSQLDialect

# Not Require, Please config if you need email notify
# 非必填:不使用邮箱作为警告通知可以去掉spring.mail配置
# mail:
# # Attention: this is mail server address.
# # 请注意此为邮件服务器地址:qq邮箱为 smtp.qq.com qq企业邮箱为 smtp.exmail.qq.com
# host: smtp.qq.com
# username: example@qq.com
# # Attention: this is not email account password, this requires an email authorization code
# # 请注意此非邮箱账户密码 此需填写邮箱授权码
# password: xxqzvuqbnqvbbdac
# port: 465
# default-encoding: UTF-8
# properties:
# mail:
# smtp:
# socketFactoryClass: javax.net.ssl.SSLSocketFactory
# ssl:
# enable: true

warehouse:
store:
# store history metrics data, enable only one below
# 存储历史数据方式, 下方只能enabled启用一种方式
jpa:
enabled: false
expire-time: 1h
iot-db:
enabled: true
host: iotdb
rpc-port: 6667
username: root
password: root
# org.dromara.hertzbeat.warehouse.config.IotDbVersion: V_0_13 || V_1_0
# 如果你使用docker-compose-1.0.yaml, 不要忘记修i该version为V_1_0
version: V_0_13
query-timeout-in-ms: -1
# 数据存储时间:默认'7776000000'(90天,单位为毫秒,-1代表永不过期)
# data expire time, unit:ms, default '7776000000'(90 days, -1:never expire)
expire-time: '7776000000'
# store real-time metrics data, enable only one below
# 存储实时数据方式, 下方只能enabled启用一种方式
memory:
enabled: true
init-size: 1024
redis:
enabled: false
host: 127.0.0.1
port: 6379
password: 123456
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
-- Licensed to the Apache Software Foundation (ASF) under one
-- or more contributor license agreements. See the NOTICE file
-- distributed with this work for additional information
-- regarding copyright ownership. The ASF licenses this file
-- to you under the Apache License, Version 2.0 (the
-- "License"); you may not use this file except in compliance
-- with the License. You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.

-- this file works for PostgreSQL.

CREATE EXTENSION IF NOT EXISTS dblink;

DO $$
BEGIN
PERFORM dblink_exec('', 'CREATE DATABASE hertzbeat');
EXCEPTION WHEN duplicate_database THEN RAISE NOTICE '%, skipping', SQLERRM USING ERRCODE = SQLSTATE;
END
$$;
Loading

0 comments on commit 04b7399

Please sign in to comment.