• Docker部署Nacos2.0单机版+mysql8


    Docker部署Nacos

    环境

    docker 19.03.13(其余docker版本均可)

    # 前置环境准备
       
    Mysql 8.0
    
    # 下载内容
    docker pull nacos/nacos-server:2.0.3
    
    # 目录创建
    mkdir -p /home/jamel/docker/software/nacos/logs/
    mkdir -p /home/jamel/docker/software/nacos/init.d/
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    数据库初始化(注意事项)

    Mysql 8.0 版本Sql

    执行这个官方标准sql:https://github.com/alibaba/nacos/blob/2.0.3/distribution/conf/nacos-mysql.sql

    如果链接打不开请直接安排下面的脚本

    /*
     * Copyright 1999-2018 Alibaba Group Holding Ltd.
     *
     * Licensed 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.
     */
    
    CREATE SCHEMA nacos AUTHORIZATION nacos;
    
    CREATE TABLE config_info (
      id bigint NOT NULL generated by default as identity,
      data_id varchar(255) NOT NULL,
      group_id varchar(128) NOT NULL,
      tenant_id varchar(128) default '',
      app_name varchar(128),
      content CLOB,
      md5 varchar(32) DEFAULT NULL,
      gmt_create timestamp NOT NULL DEFAULT '2010-05-05 00:00:00',
      gmt_modified timestamp NOT NULL DEFAULT '2010-05-05 00:00:00',
      src_user varchar(128) DEFAULT NULL,
      src_ip varchar(50) DEFAULT NULL,
      c_desc varchar(256) DEFAULT NULL,
      c_use varchar(64) DEFAULT NULL,
      effect varchar(64) DEFAULT NULL,
      type varchar(64) DEFAULT NULL,
      c_schema LONG VARCHAR DEFAULT NULL,
      constraint configinfo_id_key PRIMARY KEY (id),
      constraint uk_configinfo_datagrouptenant UNIQUE (data_id,group_id,tenant_id));
    
    CREATE INDEX configinfo_dataid_key_idx ON config_info(data_id);
    CREATE INDEX configinfo_groupid_key_idx ON config_info(group_id);
    CREATE INDEX configinfo_dataid_group_key_idx ON config_info(data_id, group_id);
    
    CREATE TABLE his_config_info (
      id bigint NOT NULL,
      nid bigint NOT NULL generated by default as identity,
      data_id varchar(255) NOT NULL,
      group_id varchar(128) NOT NULL,
      tenant_id varchar(128) default '',
      app_name varchar(128),
      content CLOB,
      md5 varchar(32) DEFAULT NULL,
      gmt_create timestamp NOT NULL DEFAULT '2010-05-05 00:00:00.000',
      gmt_modified timestamp NOT NULL DEFAULT '2010-05-05 00:00:00.000',
      src_user varchar(128),
      src_ip varchar(50) DEFAULT NULL,
      op_type char(10) DEFAULT NULL,
      constraint hisconfiginfo_nid_key PRIMARY KEY (nid));
    
    CREATE INDEX hisconfiginfo_dataid_key_idx ON his_config_info(data_id);
    CREATE INDEX hisconfiginfo_gmt_create_idx ON his_config_info(gmt_create);
    CREATE INDEX hisconfiginfo_gmt_modified_idx ON his_config_info(gmt_modified);
    
    
    CREATE TABLE config_info_beta (
      id bigint NOT NULL generated by default as identity,
      data_id varchar(255) NOT NULL,
      group_id varchar(128) NOT NULL,
      tenant_id varchar(128) default '',
      app_name varchar(128),
      content CLOB,
      beta_ips varchar(1024),
      md5 varchar(32) DEFAULT NULL,
      gmt_create timestamp NOT NULL DEFAULT '2010-05-05 00:00:00',
      gmt_modified timestamp NOT NULL DEFAULT '2010-05-05 00:00:00',
      src_user varchar(128),
      src_ip varchar(50) DEFAULT NULL,
      constraint configinfobeta_id_key PRIMARY KEY (id),
      constraint uk_configinfobeta_datagrouptenant UNIQUE (data_id,group_id,tenant_id));
    
    CREATE TABLE config_info_tag (
      id bigint NOT NULL generated by default as identity,
      data_id varchar(255) NOT NULL,
      group_id varchar(128) NOT NULL,
      tenant_id varchar(128) default '',
      tag_id varchar(128) NOT NULL,
      app_name varchar(128),
      content CLOB,
      md5 varchar(32) DEFAULT NULL,
      gmt_create timestamp NOT NULL DEFAULT '2010-05-05 00:00:00',
      gmt_modified timestamp NOT NULL DEFAULT '2010-05-05 00:00:00',
      src_user varchar(128),
      src_ip varchar(50) DEFAULT NULL,
      constraint configinfotag_id_key PRIMARY KEY (id),
      constraint uk_configinfotag_datagrouptenanttag UNIQUE (data_id,group_id,tenant_id,tag_id));
    
    CREATE TABLE config_info_aggr (
      id bigint NOT NULL generated by default as identity,
      data_id varchar(255) NOT NULL,
      group_id varchar(128) NOT NULL,
      tenant_id varchar(128) default '',
      datum_id varchar(255) NOT NULL,
      app_name varchar(128),
      content CLOB,
      gmt_modified timestamp NOT NULL DEFAULT '2010-05-05 00:00:00',
      constraint configinfoaggr_id_key PRIMARY KEY (id),
      constraint uk_configinfoaggr_datagrouptenantdatum UNIQUE (data_id,group_id,tenant_id,datum_id));
    
    CREATE TABLE app_list (
     id bigint NOT NULL generated by default as identity,
     app_name varchar(128) NOT NULL,
     is_dynamic_collect_disabled smallint DEFAULT 0,
     last_sub_info_collected_time timestamp DEFAULT '1970-01-01 08:00:00.0',
     sub_info_lock_owner varchar(128),
     sub_info_lock_time timestamp DEFAULT '1970-01-01 08:00:00.0',
     constraint applist_id_key PRIMARY KEY (id),
     constraint uk_appname UNIQUE (app_name));
    
    CREATE TABLE app_configdata_relation_subs (
      id bigint NOT NULL generated by default as identity,
      app_name varchar(128) NOT NULL,
      data_id varchar(255) NOT NULL,
      group_id varchar(128) NOT NULL,
      gmt_modified timestamp DEFAULT '2010-05-05 00:00:00',
      constraint configdatarelationsubs_id_key PRIMARY KEY (id),
      constraint uk_app_sub_config_datagroup UNIQUE (app_name, data_id, group_id));
    
    
    CREATE TABLE app_configdata_relation_pubs (
      id bigint NOT NULL generated by default as identity,
      app_name varchar(128) NOT NULL,
      data_id varchar(255) NOT NULL,
      group_id varchar(128) NOT NULL,
      gmt_modified timestamp DEFAULT '2010-05-05 00:00:00',
      constraint configdatarelationpubs_id_key PRIMARY KEY (id),
      constraint uk_app_pub_config_datagroup UNIQUE (app_name, data_id, group_id));
    
    CREATE TABLE config_tags_relation (
      id bigint NOT NULL,
      tag_name varchar(128) NOT NULL,
      tag_type varchar(64) DEFAULT NULL,
      data_id varchar(255) NOT NULL,
      group_id varchar(128) NOT NULL,
      tenant_id varchar(128) DEFAULT '',
      nid bigint NOT NULL generated by default as identity,
      constraint config_tags_id_key PRIMARY KEY (nid),
      constraint uk_configtagrelation_configidtag UNIQUE (id, tag_name, tag_type));
    
    CREATE INDEX config_tags_tenant_id_idx ON config_tags_relation(tenant_id);
    
    CREATE TABLE group_capacity (
      id bigint NOT NULL generated by default as identity,
      group_id varchar(128) DEFAULT '',
      quota int DEFAULT 0,
      usage int DEFAULT 0,
      max_size int DEFAULT 0,
      max_aggr_count int DEFAULT 0,
      max_aggr_size int DEFAULT 0,
      max_history_count int DEFAULT 0,
      gmt_create timestamp DEFAULT '2010-05-05 00:00:00',
      gmt_modified timestamp DEFAULT '2010-05-05 00:00:00',
      constraint group_capacity_id_key PRIMARY KEY (id),
      constraint uk_group_id UNIQUE (group_id));
    
    CREATE TABLE tenant_capacity (
      id bigint NOT NULL generated by default as identity,
      tenant_id varchar(128) DEFAULT '',
      quota int DEFAULT 0,
      usage int DEFAULT 0,
      max_size int DEFAULT 0,
      max_aggr_count int DEFAULT 0,
      max_aggr_size int DEFAULT 0,
      max_history_count int DEFAULT 0,
      gmt_create timestamp DEFAULT '2010-05-05 00:00:00',
      gmt_modified timestamp DEFAULT '2010-05-05 00:00:00',
      constraint tenant_capacity_id_key PRIMARY KEY (id),
      constraint uk_tenant_id UNIQUE (tenant_id));
    
    CREATE TABLE tenant_info (
      id bigint NOT NULL generated by default as identity,
      kp varchar(128) NOT NULL,
      tenant_id varchar(128)  DEFAULT '',
      tenant_name varchar(128)  DEFAULT '',
      tenant_desc varchar(256)  DEFAULT NULL,
      create_source varchar(32) DEFAULT NULL,
      gmt_create bigint NOT NULL,
      gmt_modified bigint NOT NULL,
      constraint tenant_info_id_key PRIMARY KEY (id),
      constraint uk_tenant_info_kptenantid UNIQUE (kp,tenant_id));
    CREATE INDEX tenant_info_tenant_id_idx ON tenant_info(tenant_id);
    
    CREATE TABLE users (
    	username varchar(50) NOT NULL PRIMARY KEY,
    	password varchar(500) NOT NULL,
    	enabled boolean NOT NULL DEFAULT true
    );
    
    CREATE TABLE roles (
    	username varchar(50) NOT NULL,
    	role varchar(50) NOT NULL,
    	constraint uk_username_role UNIQUE (username,role)
    );
    
    CREATE TABLE permissions (
        role varchar(50) NOT NULL,
        resource varchar(512) NOT NULL,
        action varchar(8) NOT NULL,
        constraint uk_role_permission UNIQUE (role,resource,action)
    );
    
    INSERT INTO users (username, password, enabled) VALUES ('nacos', '$2a$10$EuWPZHzz32dJN7jexM34MOeYirDdFAZm2kuWj7VEOJhhZkDrxfvUu', TRUE);
    
    INSERT INTO roles (username, role) VALUES ('nacos', 'ROLE_ADMIN');
    
    
    /******************************************/
    /*   ipv6 support   */
    /******************************************/
    ALTER TABLE `config_info_tag`
    MODIFY COLUMN `src_ip` varchar(50) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT 'source ip' AFTER `src_user`;
    
    ALTER TABLE `his_config_info`
    MODIFY COLUMN `src_ip` varchar(50) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL AFTER `src_user`;
    
    ALTER TABLE `config_info`
    MODIFY COLUMN `src_ip` varchar(50) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT 'source ip' AFTER `src_user`;
    
    ALTER TABLE `config_info_beta`
    MODIFY COLUMN `src_ip` varchar(50) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT 'source ip' AFTER `src_user`;
    
    • 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
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228

    custom.properties

    vim /home/jamel/docker/software/nacos/application.properties
    【更换其中的数据源链接即可】

    # spring
    server.servlet.contextPath=${SERVER_SERVLET_CONTEXTPATH:/nacos}
    server.contextPath=/nacos
    server.port=${NACOS_APPLICATION_PORT:8848}
    spring.datasource.platform=${SPRING_DATASOURCE_PLATFORM:"mysql"}
    nacos.cmdb.dumpTaskInterval=3600
    nacos.cmdb.eventTaskInterval=10
    nacos.cmdb.labelTaskInterval=300
    nacos.cmdb.loadDataAtStart=false
    db.num=${MYSQL_DATABASE_NUM:1}
    db.url.0=jdbc:mysql://192.168.110.xx:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&serverTimezone=UTC
    #db.url.1=jdbc:mysql://${MYSQL_SERVICE_HOST}:${MYSQL_SERVICE_PORT:3306}/${MYSQL_SERVICE_DB_NAME}?${MYSQL_SERVICE_DB_PARAM:characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true}
    db.user=${MYSQL_SERVICE_USER:root}
    db.password=${MYSQL_SERVICE_PASSWORD:xx}
    ### The auth system to use, currently only 'nacos' is supported:
    nacos.core.auth.system.type=${NACOS_AUTH_SYSTEM_TYPE:nacos}
    
    
    ### The token expiration in seconds:
    nacos.core.auth.default.token.expire.seconds=${NACOS_AUTH_TOKEN_EXPIRE_SECONDS:18000}
    
    ### The default token:
    nacos.core.auth.default.token.secret.key=${NACOS_AUTH_TOKEN:SecretKey012345678901234567890123456789012345678901234567890123456789}
    
    ### Turn on/off caching of auth information. By turning on this switch, the update of auth information would have a 15 seconds delay.
    nacos.core.auth.caching.enabled=${NACOS_AUTH_CACHE_ENABLE:false}
    nacos.core.auth.enable.userAgentAuthWhite=${NACOS_AUTH_USER_AGENT_AUTH_WHITE_ENABLE:false}
    nacos.core.auth.server.identity.key=${NACOS_AUTH_IDENTITY_KEY:serverIdentity}
    nacos.core.auth.server.identity.value=${NACOS_AUTH_IDENTITY_VALUE:security}
    server.tomcat.accesslog.enabled=${TOMCAT_ACCESSLOG_ENABLED:false}
    server.tomcat.accesslog.pattern=%h %l %u %t "%r" %s %b %D
    # default current work dir
    server.tomcat.basedir=
    ## spring security config
    ### turn off security
    nacos.security.ignore.urls=${NACOS_SECURITY_IGNORE_URLS:/,/error,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-fe/public/**,/v1/auth/**,/v1/console/health/**,/actuator/**,/v1/console/server/**}
    # metrics for elastic search
    management.metrics.export.elastic.enabled=false
    management.metrics.export.influx.enabled=false
    
    nacos.naming.distro.taskDispatchThreadCount=10
    nacos.naming.distro.taskDispatchPeriod=200
    nacos.naming.distro.batchSyncKeyCount=1000
    nacos.naming.distro.initDataRatio=0.9
    nacos.naming.distro.syncRetryDelay=5000
    nacos.naming.data.warmup=true
    
    
    • 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

    启动

    docker run -d -it \
    	-e MODE=standalone  \
    	-p 8848:8848 -p 9848:9848  \
        -m 2048m \
        --memory-swap=2312m \
    	-v /home/jamel/docker/software/nacos/application.properties:/home/nacos/conf/application.properties \
        -v /home/jamel/docker/software/nacos/logs:/home/nacos/logs \
    	--restart=always \
    	--name nacos-2.0.3 nacos/nacos-server:2.0.3
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    效果图
    在这里插入图片描述

    使用

    1.进入服务器内部
        docker exec -it nacos-1.4.1  bash
    2.查看启动日志
        less /home/jamel/docker/software/nacos/logs/start.out
    3.访问管理页面
        http://47.107.76.32:8848/nacos
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    nacos使用规范

    namespace使用

    空间名作用
    public(默认命名空间:public,默认分组:DEFAULT_GROUP)
    dev开发者环境

    推荐yml配置说明

    DataId作用
    common.yml全局配置(swagger、接口权限、接口防刷、feign机制、日志级别)
    mysql.yml数据源配置(路径扫描、分布式事务、mybatis-plus功能)
    rocketmq.yml消息中间件(开关、模式选择)
    redis.yml数据中间件(redis配置、J2Cache配置)
    aaa-bbb-server.yml项目配置(aaa:项目类型,bbb项目名称)

    问题

    1. 容器互连时访问宿主机出现“No route to host”
    # 开启防火墙,设置端口开放
    systemctl start firewalld
    firewall-cmd --zone=public --add-port=3306/tcp --permanent
    firewall-cmd --reload
    
    • 1
    • 2
    • 3
    • 4

    2.日志清理(谨慎操作)

    rm rf  /home/jamel/docker/software/nacos/logs/*
    
    • 1

    3.项目启动时如何找到对应服务的yml配置文件?

    基于 spring.application.name (@project.artifactId@) 确认服务配置文件
    
    • 1

    感谢加参考

    https://nacos.io/zh-cn/docs/quick-start-docker.html

  • 相关阅读:
    VirtualBox安装Centos
    【题解】JZOJ6703 tree
    Android几种定时任务实现方式汇总
    maven聚合工程的创建
    Spring配置 + xml的IOC控制反转 + Setter注入
    Docker镜像详解(手拉手教你上传至阿里云,发布到私有库)
    Idea集成Docker
    深入理解AQS--jdk层面管程实现【管程详解的补充】
    Linux Head命令如何使用?LinuxSRE课程
    SpringBoot的搭建(两种方式)
  • 原文地址:https://blog.csdn.net/Jamel_LiToo/article/details/126248915