nacos 服務端部署鏡像
單點模式下的docker-compose
version: "3.7"
services:
nacos:
image: nacos/nacos-server:2.0.3
environment:
PREFER_HOST_MODE: hostname
MODE: standalone
volumes:
- /home/nacos/standalone-logs/:/home/nacos/logs
- /home/nacos/init.d/custom.properties:/home/nacos/init.d/custom.properties
ports:
- "8848:8848"
- "9848:9848"
prometheus:
image: prom/prometheus:latest
volumes:
- /etc/prometheus/prometheus-standalone.yaml:/etc/prometheus/prometheus.yml
ports:
- "9090:9090"
depends_on:
- nacos
restart: always
grafana:
image: grafana/grafana:latest
ports:
- 3000:3000
restart: always
custom.properties
#spring.security.enabled=false
#management.security=false
#security.basic.enabled=false
#nacos.security.ignore.urls=/**
#management.metrics.export.elastic.host=http://localhost:9200
# metrics for prometheus
management.endpoints.web.exposure.include=*
# metrics for elastic search
#management.metrics.export.elastic.enabled=false
#management.metrics.export.elastic.host=http://localhost:9200
# metrics for influx
#management.metrics.export.influx.enabled=false
#management.metrics.export.influx.db=springboot
#management.metrics.export.influx.uri=http://localhost:8086
#management.metrics.export.influx.auto-create-db=true
#management.metrics.export.influx.consistency=one
#management.metrics.export.influx.compressed=true
prometheus-standalone.yaml
# 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_timeout is set to the global default (10s).
# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets:
# - alertmanager:9093
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: 'prometheus'
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets: ['localhost:9090']
- job_name: 'nacos'
metrics_path: '/nacos/actuator/prometheus'
static_configs:
- targets: ['nacos:8848']
nacos docker鏡像支持的環(huán)境變量
名稱 | 描述 | 默認值 |
---|---|---|
MODE | cluster模式/standalone模式 | cluster/standalone default cluster |
NACOS_SERVERS | nacos cluster地址 | eg. ip1,ip2,ip3 |
PREFER_HOST_MODE | 是否支持hostname hostname/ip | default ip |
NACOS_SERVER_PORT | nacos服務器端口 | default 8848 |
NACOS_SERVER_IP | 多網卡下的自定義nacos服務器IP | |
SPRING_DATASOURCE_PLATFORM | standalone 支持 mysql | mysql / empty default empty |
MYSQL_MASTER_SERVICE_HOST | mysql 主節(jié)點host | |
MYSQL_MASTER_SERVICE_PORT | mysql 主節(jié)點端口 | default : 3306 |
MYSQL_MASTER_SERVICE_DB_NAME | mysql 主節(jié)點數(shù)據(jù)庫 | |
MYSQL_MASTER_SERVICE_USER | 數(shù)據(jù)庫用戶名 | |
MYSQL_MASTER_SERVICE_PASSWORD | 數(shù)據(jù)庫密碼 | |
MYSQL_SLAVE_SERVICE_HOST | mysql從節(jié)點host | |
MYSQL_SLAVE_SERVICE_PORT | mysql從節(jié)點端口 | default :3306 |
MYSQL_DATABASE_NUM | 數(shù)據(jù)庫數(shù)量 | default :2 |
JVM_XMS | -Xms | default :2g |
JVM_XMX | -Xmx | default :2g |
JVM_XMN | -Xmn | default :1g |
JVM_MS | -XX:MetaspaceSize | default :128m |
JVM_MMS | -XX:MaxMetaspaceSize | default :320m |
NACOS_DEBUG | 開啟遠程調試 y/n | default :n |
TOMCAT_ACCESSLOG_ENABLED | server.tomcat.accesslog.enabled | default :false |
結合prometheus和grafana監(jiān)控Nacos
Nacos 0.8.0版本之后支持通過暴露metrics數(shù)據(jù)接入第三方監(jiān)控系統(tǒng)監(jiān)控Nacos運行狀態(tài)
搭建nacos并暴露metrics數(shù)據(jù)
按上文所示,搭建好環(huán)境后,修改 custom.properties 文件喊废,暴露監(jiān)控端點,隨后訪問 http:{ip}:8848/nacos/actuator/prometheus 可以看到數(shù)據(jù)
management.endpoints.web.exposure.include=*
通過prometheus采集Nacos metrics數(shù)據(jù)
訪問 http://{ip}:9090/graph 溃肪,搜索 nacos_monitor 查看數(shù)據(jù)是否采集成功
通過grafana展示數(shù)據(jù)
訪問 grafana: http://{ip}:3000 ,配置數(shù)據(jù)
-
配置prometheus數(shù)據(jù)源
QQ截圖20210916165617.png -
導入Nacos grafana監(jiān)控模版
QQ截圖20210916165827.png -
導入成功后即可看到監(jiān)控面板
QQ截圖20210916165914.png
nacos 開發(fā)端接入
- 添加依賴
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>2.2.6.RELEASE</version>
</dependency>
注意 spring-cloud-alibaba-dependencies 與 spring-cloud-dependencies 有對應關系,阿里提供了畢業(yè)版本推薦依賴關系。
注意:版本 2.1.x.RELEASE 對應的是 Spring Boot 2.1.x 版本册倒。版本 2.0.x.RELEASE 對應的是 Spring Boot 2.0.x 版本,版本 1.5.x.RELEASE 對應的是 Spring Boot 1.5.x 版本磺送。
Spring Cloud Version | Spring Cloud Alibaba Version | Spring Boot Version |
---|---|---|
Spring Cloud 2020.0.1 | 2021.1 | 2.4.2 |
Spring Cloud Hoxton.SR9 | 2.2.6.RELEASE | 2.3.2.RELEASE |
Spring Cloud Greenwich.SR6 | 2.1.4.RELEASE | 2.1.13.RELEASE |
Spring Cloud Hoxton.SR3 | 2.2.1.RELEASE | 2.2.5.RELEASE |
Spring Cloud Hoxton.RELEASE | 2.2.0.RELEASE | 2.2.X.RELEASE |
Spring Cloud Greenwich | 2.1.2.RELEASE | 2.1.X.RELEASE |
- 在 application.yml 中配置 Nacos server 的地址和應用名
spring:
cloud:
nacos:
discovery:
enabled: true
service: {serviceName}
server-addr: {ip:8848}
- 在啟動類上增加注解
@EnableDiscoveryClient
開啟服務注冊發(fā)現(xiàn)功能 - 給RestTemplate實例添加
@LoadBalanced
注解驻子,可以開啟負載均衡