目录

springcloudAlibaba_nacos 集群和持久化配置

官网说明

集群部署架构图

因此开源的时候推荐用户把所有服务列表放到一个vip下面,然后挂到一个域名下面

  • http://ip1:port/openAPI 直连ip模式,机器挂则需要修改ip才可以使用。

  • http://VIP:port/openAPI 挂载VIP模式,直连vip即可,下面挂server真实ip,可读性不好。

  • http://nacos.com:port/openAPI 域名 + VIP模式,可读性好,而且换ip方便,推荐模式

http://img.cana.space/picStore/20201114165354.png

此处的 VIP 指代的是 Virtual IP(虚拟IP)的意思,官方文档说明不太清楚。通常情况下指代的是 Nginx。

Nacos集群真实架构图

http://img.cana.space/picStore/20201114171447.png

部署说明

Nacos支持三种部署模式

  • 单机模式 - 用于测试和单机试用。
  • 集群模式 - 用于生产环境,确保高可用。
  • 多集群模式 - 用于多数据中心场景。

单机模式支持mysql

在0.7版本之前,在单机模式时nacos使用嵌入式数据库derby实现数据的存储,不方便观察数据存储的基本情况。0.7版本增加了支持mysql数据源能力,具体的操作步骤:

  • 1.安装数据库,版本要求:5.6.5+
  • 2.初始化mysql数据库,数据库初始化文件:nacos-mysql.sql
  • 3.修改conf/application.properties文件,增加支持mysql数据源配置(目前只支持mysql),添加mysql数据源的url、用户名和密码。
1
2
3
4
5
6
spring.datasource.platform=mysql

db.num=1
db.url.0=jdbc:mysql://11.162.196.16:3306/nacos_devtest?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true
db.user=nacos_devtest
db.password=youdontknow

再以单机模式启动nacos,nacos所有写嵌入式数据库的数据都写到了mysql

Nacos持久化配置

配置说明

Nacos默认自带的是嵌入式数据库derby

这一点可以从nacos/config/pom.xml中看出,包含

1
2
3
4
        <dependency>
            <groupId>org.apache.derby</groupId>
            <artifactId>derby</artifactId>
        </dependency>

derby到mysql切换配置步骤

  1. 启动mysql(我的版本是5.7,官方要求:5.6.5+

  2. 执行脚本(nacos/conf/nacos-mysql.sql)

  3. 配置数据库信息(nacos/conf/application.properties)

    加入如下内容

    1
    2
    3
    4
    5
    6
    7
    
    #*************** Mysql Configurations ***************#
    spring.datasource.platform=mysql
       
    db.num=1
    db.url.0=jdbc:mysql://localhost:3306/nacos_config?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true
    db.user=root
    db.password=333
    
  4. 再以单机模式启动nacos

    1
    
    sh startup.sh -m standalone
    
  5. 验证

    启动nacos,可以看到是个全新的空记录界面,以前是记录进derby,现在是记录进mysql,新建一个配置,查看数据库变化

    http://img.cana.space/picStore/20201114182254.png

集群配置

需要:1个ngix+3个nacos注册中心+1个mysql

使用docker安装example集群模式

  1. clone项目

    1
    2
    
    git clone https://github.com/nacos-group/nacos-docker.git
    cd nacos-docker
    
  2. 集群模式启动

    注意,JVM参数根据自己服务器调整。nacos默认就2G,不设置的话容器就会不断重启。还非常卡。

    1
    2
    3
    4
    5
    6
    
    # david @ Davids-Macbook-Pro in ~/docker/compose-config/nacos-docker/env on git:master x [19:40:17]
    $ vim nacos-hostname.env
    #jvm
    JVM_XMS=256m
    JVM_XMX=256m
    JVM_XMN=256m
    

    启动集群模式

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    
    # david @ Davids-Macbook-Pro in ~/docker/compose-config/nacos-docker on git:master x [18:54:26]
    $ docker-compose -f example/cluster-hostname.yaml up -d
    Starting mysql ... done
    Starting nacos3 ... done
    Starting nacos1 ... done
    Starting nacos2 ... done
       
    # david @ Davids-Macbook-Pro in ~/docker/compose-config/nacos-docker on git:master x [18:54:42]
    $ docker-compose -f example/cluster-hostname.yaml ps
     Name              Command             State                       Ports
    ---------------------------------------------------------------------------------------------
    mysql    docker-entrypoint.sh mysqld   Up      0.0.0.0:3306->3306/tcp, 33060/tcp
    nacos1   bin/docker-startup.sh         Up      0.0.0.0:8848->8848/tcp, 0.0.0.0:9555->9555/tcp
    nacos2   bin/docker-startup.sh         Up      0.0.0.0:8849->8848/tcp
    nacos3   bin/docker-startup.sh         Up      0.0.0.0:8850->8848/tcp
    

    其中yaml如下:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    
    version: "3"
    services:
      nacos1:
        hostname: nacos1
        container_name: nacos1
        image: nacos/nacos-server:latest
        volumes:
          - ./cluster-logs/nacos1:/home/nacos/logs
          - ./init.d/custom.properties:/home/nacos/init.d/custom.properties
        ports:
          - "8848:8848"
          - "9555:9555"
        env_file:
          - ../env/nacos-hostname.env
        restart: always
        depends_on:
          - mysql
       
      nacos2:
        hostname: nacos2
        image: nacos/nacos-server:latest
        container_name: nacos2
        volumes:
          - ./cluster-logs/nacos2:/home/nacos/logs
          - ./init.d/custom.properties:/home/nacos/init.d/custom.properties
        ports:
          - "8849:8848"
        env_file:
          - ../env/nacos-hostname.env
        restart: always
        depends_on:
          - mysql
      nacos3:
        hostname: nacos3
        image: nacos/nacos-server:latest
        container_name: nacos3
        volumes:
          - ./cluster-logs/nacos3:/home/nacos/logs
          - ./init.d/custom.properties:/home/nacos/init.d/custom.properties
        ports:
          - "8850:8848"
        env_file:
          - ../env/nacos-hostname.env
        restart: always
        depends_on:
          - mysql
      mysql:
        container_name: mysql
        image: nacos/nacos-mysql:5.7
        env_file:
          - ../env/mysql.env
        volumes:
          - ./mysql:/var/lib/mysql
        ports:
          - "3306:3306"
    
  3. 随便选一台实例进行访问

    http://localhost:8848/nacos

    http://img.cana.space/picStore/20201114194702.png

  4. 配置nginx

    直接在cluster-hostname.yaml中加入nginx实例的配置,如下:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    
    nginx:
        restart: always
        image: nginx
        container_name: nginx
        environment:
          - TZ=Asia/Shanghai
        ports:
          - 8888:80
        volumes:
          - './nginx/nginx.conf:/etc/nginx/nginx.conf:ro'
          - './nginx/conf.d:/etc/nginx/conf.d:ro'
          - './nginx/www:/usr/share/nginx/html:ro'
          - './nginx/log:/var/log/nginx'
        depends_on:
          - nacos1
          - nacos2
          - nacos3
    
  5. nginx.conf如下

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    
    user  nginx;
    worker_processes  1;
       
    error_log  /var/log/nginx/error.log warn;
    pid        /var/run/nginx.pid;
       
       
    events {
        worker_connections  1024;
    }
       
    http {
        include       mime.types;
        default_type  application/octet-stream;
        sendfile        on;
        keepalive_timeout  65;
        charset utf-8;        # 设置编码格式
       
        log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                          '$status $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" "$http_x_forwarded_for"';
       
        access_log  /var/log/nginx/access.log  main;
       
       
        #设定负载均衡的服务器列表
        # nacos集群
        upstream nacos-cluster {
            server 172.31.0.5:8848;
            server 172.31.0.4:8848;
            server 172.31.0.3:8848;
        }
        
        server{
            listen 80;
            # ipv6
            listen  [::]:80;
            server_name localhost;
       
            location / {
                proxy_pass http://nacos-cluster;
            }
       
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   /usr/share/nginx/html;
            }
        }
    }
    
  6. 创建nginx容器

    1
    2
    3
    4
    5
    6
    7
    
    $ docker-compose -f cluster-hostname.yaml up -d
    Removing nginx
    mysql is up-to-date
    nacos2 is up-to-date
    nacos1 is up-to-date
    nacos3 is up-to-date
    Starting 7ca28809a930_nginx ... done
    
  7. 查看容器ip

    1
    2
    3
    4
    5
    6
    
    $ docker_ip
    /7ca28809a930_nginx - 172.31.0.6
    /nacos3 - 172.31.0.5
    /nacos1 - 172.31.0.3
    /nacos2 - 172.31.0.4
    /mysql - 172.31.0.2
    
  8. 第一次启动时 将nacos1、nacos2、nacos3局域网ip填到nginx配置文件服务器列表位置,重建nginx容器

    1
    
    $ docker-compose -f cluster-hostname.yaml up -d
    
  9. 访问nginx端口8888:http://localhost:8888/nacos

    http://img.cana.space/picStore/20201114233610.png

  10. 跟单机环境一样增加一条配置

    http://img.cana.space/picStore/20201114234956.png

  11. 查看数据库

    数据库用户名密码在nacos-docker/env/mysql.env

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    
    $ docker ps
    CONTAINER ID        IMAGE                       COMMAND                  CREATED             STATUS              PORTS                                            NAMES
    7ca28809a930        nginx                       "/docker-entrypoint.…"   25 minutes ago      Up 13 minutes       0.0.0.0:8888->80/tcp                             7ca28809a930_nginx
    5d45e579ca8e        nacos/nacos-server:latest   "bin/docker-startup.…"   33 minutes ago      Up 33 minutes       0.0.0.0:8850->8848/tcp                           nacos3
    8bc5e234e55d        nacos/nacos-server:latest   "bin/docker-startup.…"   33 minutes ago      Up 33 minutes       0.0.0.0:8848->8848/tcp, 0.0.0.0:9555->9555/tcp   nacos1
    eb1c21d0b161        nacos/nacos-server:latest   "bin/docker-startup.…"   33 minutes ago      Up 33 minutes       0.0.0.0:8849->8848/tcp                           nacos2
    4ff504ae567c        nacos/nacos-mysql:5.7       "docker-entrypoint.s…"   34 minutes ago      Up 33 minutes       0.0.0.0:3306->3306/tcp, 33060/tcp                mysql
    $ de 4f bash
    root@4ff504ae567c:/# mysql -uroot -proot
    mysql> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mysql              |
    | nacos_devtest      |
    | performance_schema |
    | sys                |
    +--------------------+
    5 rows in set (0.06 sec)
        
    mysql> use nacos_devtest;
    Reading table information for completion of table and column names
    You can turn off this feature to get a quicker startup with -A
        
    Database changed
    mysql> show tables;
    +-------------------------+
    | Tables_in_nacos_devtest |
    +-------------------------+
    | config_info             |
    | config_info_aggr        |
    | config_info_beta        |
    | config_info_tag         |
    | config_tags_relation    |
    | group_capacity          |
    | his_config_info         |
    | permissions             |
    | roles                   |
    | tenant_capacity         |
    | tenant_info             |
    | users                   |
    +-------------------------+
    12 rows in set (0.01 sec)
    mysql> select data_id,group_id,content from config_info;
    +-----------------------------------------------+---------------+------------------------------------------------------------------------------------------+
    | data_id                                       | group_id      | content                                                                                  |
    +-----------------------------------------------+---------------+------------------------------------------------------------------------------------------+
    | cloudalibaba-config-nacos-client3377-dev.yaml | DEFAULT_GROUP | config:
        info: cloudalibaba-config-nacos-client3377,DEFAULT_GROUP,cluster,version=1.0 |
    +-----------------------------------------------+---------------+------------------------------------------------------------------------------------------+
    1 row in set (0.01 sec)
    
  12. 客户端修改yml,改成nginx地址

    1
    
    spring.cloud.nacos.config.server-addr: localhost:8888
    
  13. 再次访问:http://localhost:3377/info

    http://img.cana.space/picStore/20201114235252.png

  14. 至此,说明基于docker的nacos集群环境成功搭建