spring cloud nacos docker环境下单点部署

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=` 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镜像支持的环境变量

名称 描述 默认值
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 主节点host
MYSQL_MASTER_SERVICE_PORT mysql 主节点端口 default : 3306
MYSQL_MASTER_SERVICE_DB_NAME mysql 主节点数据库
MYSQL_MASTER_SERVICE_USER 数据库用户名
MYSQL_MASTER_SERVICE_PASSWORD 数据库密码
MYSQL_SLAVE_SERVICE_HOST mysql从节点host
MYSQL_SLAVE_SERVICE_PORT mysql从节点端口 default :3306
MYSQL_DATABASE_NUM 数据库数量 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监控Nacos

Nacos 0.8.0版本之后支持通过暴露metrics数据接入第三方监控系统监控Nacos运行状态

搭建nacos并暴露metrics数据

按上文所示,搭建好环境后,修改 custom.properties 文件,暴露监控端点,随后访问 http:{ip}:8848/nacos/actuator/prometheus 可以看到数据

management.endpoints.web.exposure.include=*

通过prometheus采集Nacos metrics数据

访问 http://{ip}:9090/graph ,搜索 nacos_monitor 查看数据是否采集成功

通过grafana展示数据

访问 grafana: http://{ip}:3000 ,配置数据

  • 配置prometheus数据源


    QQ截图20210916165617.png
  • 导入Nacos grafana监控模版

    QQ截图20210916165827.png

  • 导入成功后即可看到监控面板


    QQ截图20210916165914.png

nacos 开发端接入

  1. 添加依赖

    com.alibaba.cloud
    spring-cloud-starter-alibaba-nacos-config
    2.2.6.RELEASE

注意 spring-cloud-alibaba-dependencies 与 spring-cloud-dependencies 有对应关系,阿里提供了毕业版本推荐依赖关系。
注意:版本 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
  1. 在 application.yml 中配置 Nacos server 的地址和应用名
spring:
  cloud:
    nacos:
      discovery:
        enabled: true
        service: {serviceName}
        server-addr: {ip:8848}
  1. 在启动类上增加注解 @EnableDiscoveryClient 开启服务注册发现功能
  2. 给RestTemplate实例添加 @LoadBalanced 注解,可以开启负载均衡

你可能感兴趣的:(spring cloud nacos docker环境下单点部署)