前言 市面上MQTT Server 有很多種,例如: EMQX Mosquitto 這次我們透過docker來建立IOT傳輸用的MQTT Sever EMQX 步驟 建立一個新的folder,命名emqx 在emqx目錄底下,建立名為data資料夾,用來存放eqmx資料 在emqx目錄底下,建立docker-compose.yml docker-compose.yml 底下輸入 version: '3' services: emqx: image: emqx:5.0.20 container_name: emqx healthcheck: test: ["CMD", "/opt/emqx/bin/emqx_ctl", "status"] interval: 5s timeout: 25s retries: 5 ports: - 1883:1883 ## tcp - 8083:8083 ## websockeet - 8084:8084 ## websocket ssl - 8883:8883 ## ssl - 18083:18083 ## web volumes: - ./data:/opt/emqx/data 執行docker-compose up -d,即可啟動emqx 打開browser,在localhost:18083即可看到啟動好的emqx,如下圖 預設帳密為: admin/public 參考連結 EQMX
前言 redis exporter是用來監控Redis一個Prometheus Client,提供相關redis的監控metrics 步驟 建立一個新的folder,命名redis 在redis目錄底下,建立docker-compose.yml docker-compose.yml 底下輸入 version: '3' services: redis: restart: always container_name: redis image: redis:6.2.7-alpine ports: - 6379:6379 redis-exporter: restart: always image: oliver006/redis_exporter:v1.45.0-alpine container_name: redis-exporter ports: - 9121:9121 command: - '--redis.addr=redis://redis-server:6379' 執行docker-compose up -d,redis服務與redis exporter即可啟動
確認redis exporter服務,已經在執行輸入curl http://localhost:9121/metrics
λ curl http://localhost:9121/metrics # HELP go_gc_duration_seconds A summary of the pause duration of garbage collection cycles. # TYPE go_gc_duration_seconds summary go_gc_duration_seconds{quantile="0"} 0 go_gc_duration_seconds{quantile="0.25"} 0 go_gc_duration_seconds{quantile="0.5"} 0 go_gc_duration_seconds{quantile="0.75"} 0 go_gc_duration_seconds{quantile="1"} 0 go_gc_duration_seconds_sum 0 go_gc_duration_seconds_count 0 # HELP go_goroutines Number of goroutines that currently exist....
介紹 Redis 是一個開源的in memory key value cache,常用來降低服務內的延遲情況
Redis 支援的data type
Strings Lists Sets Hash Sroted sets Streams Geospatial HyperLogLog Bitmaps Bitfields 現在讓我們開始安裝Redis
建立 Redis 建立一個新的folder,命名redis-server 在redis-server目錄底下,建立docker-compose.yml docker-compose.yml 底下輸入 version: '3' services: redis: restart: always container_name: redis image: redis:6.2.7-alpine ports: - 6379:6379 執行docker-compose up -d,redis服務即可啟動
確認redis服務,已經在執行
λ docker exec -it redis sh /data # redis-cli 127.0.0.1:6379> ping PONG 127.0.0.1:6379> exit /data # redis-cli ping PONG /data # 小結 redis 已經安裝完成,下一章節將會開始使用java連接redis 參考連結 Redis
前言 使用到 SSH Tunnel 情境
這裡有一台Server,上面架設一個Postgres,因為公司資安的關係,並不對外開啟port:5432 我們目前在Local開發一個服務,需要連線到Server上的Postgres,不太可能每次改完程式,部署上去測試,這樣來回時間會拉很長
主要內容 ssh -L [local_ip]:[local_port]:[host_ip]:[host_port] username@host local_ip :本地ip位置,預設綁定本機 local_port:本地開啟port host_ip:遠端Server ip host_port:遠端要轉發port L: 建立Tunnel Example ssh -L 5432:localhost:5432 tony@123.456.789.10 參考連結 SSH Tunnel 突破防火牆限制
前言 node exporter是用來監控Linux vm的一個Prometheus Client 提供: CPU 使用率、硬碟使用率… metrics 步驟 安裝Node Exporter在Ubuntu20.04 #!/bin/bash version="${VERSION:-1.2.2}" arch="${ARCH:-linux-amd64}" bin_dir="${BIN_DIR:-/usr/local/bin}" wget "https://github.com/prometheus/node_exporter/releases/download/v$version/node_exporter-$version.$arch.tar.gz" \ -O /tmp/node_exporter.tar.gz mkdir -p /tmp/node_exporter cd /tmp || { echo "ERROR! No /tmp found.."; exit 1; } tar xfz /tmp/node_exporter.tar.gz -C /tmp/node_exporter || { echo "ERROR! Extracting the node_exporter tar"; exit 1; } sudo cp "/tmp/node_exporter/node_exporter-$version.$arch/node_exporter" "$bin_dir" chown root:staff "$bin_dir/node_exporter" sudo cat <<EOF > /etc/systemd/system/node_exporter.service [Unit] Description=Node Exporter After=network.target [Service] Type=simple ExecStart=/usr/local/bin/node_exporter [Install] WantedBy=multi-user....
步驟 安裝Prometheus/Grafana 設定Prometheus設定向我們spring boot application位置 # my global config
global:
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: 'My-Spring-Boot-Application'
# Override the global default and scrape targets from this job every 5 seconds....
前言 常在河邊走那裡不失鞋,當一個後端工程師,常常會遇到上線Application server突然無法壞掉的問題,因此我們需要一個監控的Server來輔助我們上線的Application,本篇將使用Prometheus監控server當作範例來做為展示 步驟 Spring boot 安裝Prometheus Client 在pom.xml上新增兩個dependency spring-boot-starter-actuator: spring boot內建的監控library micrometer-registry-prometheus: 在spring boot上提供prometheus metrics格式 <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency> Spring boot 調整設定檔 在applcation.yml 新增下列設定 management:
endpoint:
health:
show-details: always
prometheus:
enabled: true
endpoints:
web:
exposure:
include: prometheus,health
metrics:
tags:
application: ${spring.application.name}
health:
diskspace:
enabled: true
defaults:
enabled: false 設定好,啟動spring boot即可在http://:/actuator/prometheus看到metrics 參考連結 Spring boot Spring boot Actuator Micrometer Registry Prometheus
介紹 在本機上開發的時候常常遇到需要https和domain name的狀況,例如:Line Bot的webhook測試,這時候使用ngrok就可以解決這個問題,ngork賦予你一個暫時domain name和https 安裝 Windows choco install ngrok Mac brew install ngrok 在本機上登入ngork(使用token認證) ngrok authtoken <token> 使用 ngrok http <Port> 參考連結 ngork
介紹 在工作上不免要定期備份database的資料與還原datdabase的資料 備份步驟 連線進入mongo mongo -u <username> -p <password> 切換db use admin 磁碟同步與上鎖 db.runCommand({fsync:1,lock:1}) 確認是否上鎖 db.currentOp() 備份資料 mongodump --host <host-ip> --port 27017 --db <database> --authenticationDatabase admin --username <username> --password <password> --out <Path> 解鎖 db.fsyncUnlock() 還原步驟 還原 mongorestore --host <host-ip> --port 27017 --username <username> --password <password> --db <database> <backup file path> 參考連結 Mongo
介紹 SDKMAN是一款基於Linux/Mac的SDK管理工具,可以管理多個版本JDK,也可以安裝與JDK相容的套件 安裝SDKMAN $ curl -s "https://get.sdkman.io" | bash
$ source "$HOME/.sdkman/bin/sdkman-init.sh"
sdk version
> sdkman 5.0.0+51 羅列Java版本 sdk list java 安裝JAVA sdk install java x.y.z-open 移除 sdk uninstall java ... 指定預設JAVA sdk default java 11 參考連結 SDKMAN