docker安装mysql和nacos集群加nginx代理

安装mysql nacos

1、拉取mysql镜像

docker pull mysql:8.0.27

2、定义挂载目录

在/home/mysql目录下新建两个文件夹,一个叫data另一个叫conf

命令形式:

mkdir -p /home/mysql/{data,conf}

3、配置my.cnf(my.ini)

vim /home/mysql/conf/my.cnf

[mysqld]
#Mysql服务的唯一编号 每个mysql服务Id需唯一
server-id=1

#服务端口号 默认3306
port=3306

#mysql安装根目录(default /usr)
#basedir=/usr/local/mysql

#mysql数据文件所在位置
datadir=/var/lib/mysql

#pid
pid-file=/var/run/mysqld/mysqld.pid

#设置socke文件所在目录
socket=/var/lib/mysql/mysql.sock

#设置临时目录
#tmpdir=/tmp

# 用户
user=mysql

# 允许访问的IP网段
bind-address=0.0.0.0

# 跳过密码登录
#skip-grant-tables

#主要用于MyISAM存储引擎,如果多台服务器连接一个数据库则建议注释下面内容
#skip-external-locking

#只能用IP地址检查客户端的登录,不用主机名
#skip_name_resolve=1

#事务隔离级别,默认为可重复读,mysql默认可重复读级别(此级别下可能参数很多间隙锁,影响性能)
#transaction_isolation=READ-COMMITTED

#数据库默认字符集,主流字符集支持一些特殊表情符号(特殊表情符占用4个字节)
character-set-server=utf8mb4

#数据库字符集对应一些排序等规则,注意要和character-set-server对应
collation-server=utf8mb4_general_ci

#设置client连接mysql时的字符集,防止乱码
init_connect='SET NAMES utf8mb4'

#是否对sql语句大小写敏感,1表示不敏感
lower_case_table_names=1

#最大连接数
max_connections=400

#最大错误连接数
max_connect_errors=1000

#TIMESTAMP如果没有显示声明NOT NULL,允许NULL值
explicit_defaults_for_timestamp=true

#SQL数据包发送的大小,如果有BLOB对象建议修改成1G
max_allowed_packet=128M

#MySQL连接闲置超过一定时间后(单位:秒)将会被强行关闭
#MySQL默认的wait_timeout  值为8个小时, interactive_timeout参数需要同时配置才能生效
interactive_timeout=1800
wait_timeout=1800

#内部内存临时表的最大值 ,设置成128M。
#比如大数据量的group by ,order by时可能用到临时表,
#超过了这个值将写入磁盘,系统IO压力增大
tmp_table_size=134217728
max_heap_table_size=134217728

#禁用mysql的缓存查询结果集功能
#后期根据业务情况测试决定是否开启
#大部分情况下关闭下面两项
#query_cache_size = 0
#query_cache_type = 0
 
#数据库错误日志文件
#log-error=/var/log/mysqld.log

#慢查询sql日志设置
#slow_query_log=1
#slow_query_log_file=/var/log/mysqld_slow.log

#检查未使用到索引的sql
log_queries_not_using_indexes=1

#针对log_queries_not_using_indexes开启后,记录慢sql的频次、每分钟记录的条数
log_throttle_queries_not_using_indexes=5

#作为从库时生效,从库复制中如何有慢sql也将被记录
log_slow_slave_statements=1

#慢查询执行的秒数,必须达到此值可被记录
long_query_time=8

#检索的行数必须达到此值才可被记为慢查询
min_examined_row_limit=100

#mysql binlog日志文件保存的过期时间,过期后自动删除
#expire_logs_days=5
binlog_expire_logs_seconds=604800

4、开启mysql容器

https://nacos.io/zh-cn/docs/v2/guide/user/auth.html 鉴权的问题

docker run -itd -p 3306:3306 --name mysql -v /home/mysql/conf/my.cnf:/etc/my.cnf -v /home/mysql/data:/var/lib/mysql  --privileged=true --restart=always  -e MYSQL_ROOT_PASSWORD=123456 mysql:8.0.27

5、用navicat连接/DBeaver 工具连接远程端口

6、nacos数据库配置,创建一个nacos数据库,运行一下语句

https://github.com/alibaba/nacos/blob/master/distribution/conf/mysql-schema.sql

官网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.
 */

/******************************************/
/*   表名称 = config_info                  */
/******************************************/
CREATE TABLE `config_info` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'id',
  `data_id` varchar(255) NOT NULL COMMENT 'data_id',
  `group_id` varchar(128) DEFAULT NULL COMMENT 'group_id',
  `content` longtext NOT NULL COMMENT 'content',
  `md5` varchar(32) DEFAULT NULL COMMENT 'md5',
  `gmt_create` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `gmt_modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '修改时间',
  `src_user` text COMMENT 'source user',
  `src_ip` varchar(50) DEFAULT NULL COMMENT 'source ip',
  `app_name` varchar(128) DEFAULT NULL COMMENT 'app_name',
  `tenant_id` varchar(128) DEFAULT '' COMMENT '租户字段',
  `c_desc` varchar(256) DEFAULT NULL COMMENT 'configuration description',
  `c_use` varchar(64) DEFAULT NULL COMMENT 'configuration usage',
  `effect` varchar(64) DEFAULT NULL COMMENT '配置生效的描述',
  `type` varchar(64) DEFAULT NULL COMMENT '配置的类型',
  `c_schema` text COMMENT '配置的模式',
  `encrypted_data_key` text NOT NULL COMMENT '密钥',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_configinfo_datagrouptenant` (`data_id`,`group_id`,`tenant_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='config_info';

/******************************************/
/*   表名称 = config_info_aggr             */
/******************************************/
CREATE TABLE `config_info_aggr` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'id',
  `data_id` varchar(255) NOT NULL COMMENT 'data_id',
  `group_id` varchar(128) NOT NULL COMMENT 'group_id',
  `datum_id` varchar(255) NOT NULL COMMENT 'datum_id',
  `content` longtext NOT NULL COMMENT '内容',
  `gmt_modified` datetime NOT NULL COMMENT '修改时间',
  `app_name` varchar(128) DEFAULT NULL COMMENT 'app_name',
  `tenant_id` varchar(128) DEFAULT '' COMMENT '租户字段',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_configinfoaggr_datagrouptenantdatum` (`data_id`,`group_id`,`tenant_id`,`datum_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='增加租户字段';


/******************************************/
/*   表名称 = config_info_beta             */
/******************************************/
CREATE TABLE `config_info_beta` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'id',
  `data_id` varchar(255) NOT NULL COMMENT 'data_id',
  `group_id` varchar(128) NOT NULL COMMENT 'group_id',
  `app_name` varchar(128) DEFAULT NULL COMMENT 'app_name',
  `content` longtext NOT NULL COMMENT 'content',
  `beta_ips` varchar(1024) DEFAULT NULL COMMENT 'betaIps',
  `md5` varchar(32) DEFAULT NULL COMMENT 'md5',
  `gmt_create` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `gmt_modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '修改时间',
  `src_user` text COMMENT 'source user',
  `src_ip` varchar(50) DEFAULT NULL COMMENT 'source ip',
  `tenant_id` varchar(128) DEFAULT '' COMMENT '租户字段',
  `encrypted_data_key` text NOT NULL COMMENT '密钥',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_configinfobeta_datagrouptenant` (`data_id`,`group_id`,`tenant_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='config_info_beta';

/******************************************/
/*   表名称 = config_info_tag              */
/******************************************/
CREATE TABLE `config_info_tag` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'id',
  `data_id` varchar(255) NOT NULL COMMENT 'data_id',
  `group_id` varchar(128) NOT NULL COMMENT 'group_id',
  `tenant_id` varchar(128) DEFAULT '' COMMENT 'tenant_id',
  `tag_id` varchar(128) NOT NULL COMMENT 'tag_id',
  `app_name` varchar(128) DEFAULT NULL COMMENT 'app_name',
  `content` longtext NOT NULL COMMENT 'content',
  `md5` varchar(32) DEFAULT NULL COMMENT 'md5',
  `gmt_create` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `gmt_modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '修改时间',
  `src_user` text COMMENT 'source user',
  `src_ip` varchar(50) DEFAULT NULL COMMENT 'source ip',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_configinfotag_datagrouptenanttag` (`data_id`,`group_id`,`tenant_id`,`tag_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='config_info_tag';

/******************************************/
/*   表名称 = config_tags_relation         */
/******************************************/
CREATE TABLE `config_tags_relation` (
  `id` bigint(20) NOT NULL COMMENT 'id',
  `tag_name` varchar(128) NOT NULL COMMENT 'tag_name',
  `tag_type` varchar(64) DEFAULT NULL COMMENT 'tag_type',
  `data_id` varchar(255) NOT NULL COMMENT 'data_id',
  `group_id` varchar(128) NOT NULL COMMENT 'group_id',
  `tenant_id` varchar(128) DEFAULT '' COMMENT 'tenant_id',
  `nid` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'nid, 自增长标识',
  PRIMARY KEY (`nid`),
  UNIQUE KEY `uk_configtagrelation_configidtag` (`id`,`tag_name`,`tag_type`),
  KEY `idx_tenant_id` (`tenant_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='config_tag_relation';

/******************************************/
/*   表名称 = group_capacity               */
/******************************************/
CREATE TABLE `group_capacity` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `group_id` varchar(128) NOT NULL DEFAULT '' COMMENT 'Group ID,空字符表示整个集群',
  `quota` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '配额,0表示使用默认值',
  `usage` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '使用量',
  `max_size` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '单个配置大小上限,单位为字节,0表示使用默认值',
  `max_aggr_count` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '聚合子配置最大个数,,0表示使用默认值',
  `max_aggr_size` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '单个聚合数据的子配置大小上限,单位为字节,0表示使用默认值',
  `max_history_count` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '最大变更历史数量',
  `gmt_create` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `gmt_modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '修改时间',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_group_id` (`group_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='集群、各Group容量信息表';

/******************************************/
/*   表名称 = his_config_info              */
/******************************************/
CREATE TABLE `his_config_info` (
  `id` bigint(20) unsigned NOT NULL COMMENT 'id',
  `nid` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'nid, 自增标识',
  `data_id` varchar(255) NOT NULL COMMENT 'data_id',
  `group_id` varchar(128) NOT NULL COMMENT 'group_id',
  `app_name` varchar(128) DEFAULT NULL COMMENT 'app_name',
  `content` longtext NOT NULL COMMENT 'content',
  `md5` varchar(32) DEFAULT NULL COMMENT 'md5',
  `gmt_create` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `gmt_modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '修改时间',
  `src_user` text COMMENT 'source user',
  `src_ip` varchar(50) DEFAULT NULL COMMENT 'source ip',
  `op_type` char(10) DEFAULT NULL COMMENT 'operation type',
  `tenant_id` varchar(128) DEFAULT '' COMMENT '租户字段',
  `encrypted_data_key` text NOT NULL COMMENT '密钥',
  PRIMARY KEY (`nid`),
  KEY `idx_gmt_create` (`gmt_create`),
  KEY `idx_gmt_modified` (`gmt_modified`),
  KEY `idx_did` (`data_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='多租户改造';


/******************************************/
/*   表名称 = tenant_capacity              */
/******************************************/
CREATE TABLE `tenant_capacity` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `tenant_id` varchar(128) NOT NULL DEFAULT '' COMMENT 'Tenant ID',
  `quota` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '配额,0表示使用默认值',
  `usage` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '使用量',
  `max_size` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '单个配置大小上限,单位为字节,0表示使用默认值',
  `max_aggr_count` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '聚合子配置最大个数',
  `max_aggr_size` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '单个聚合数据的子配置大小上限,单位为字节,0表示使用默认值',
  `max_history_count` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '最大变更历史数量',
  `gmt_create` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `gmt_modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '修改时间',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_tenant_id` (`tenant_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='租户容量信息表';


CREATE TABLE `tenant_info` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'id',
  `kp` varchar(128) NOT NULL COMMENT 'kp',
  `tenant_id` varchar(128) default '' COMMENT 'tenant_id',
  `tenant_name` varchar(128) default '' COMMENT 'tenant_name',
  `tenant_desc` varchar(256) DEFAULT NULL COMMENT 'tenant_desc',
  `create_source` varchar(32) DEFAULT NULL COMMENT 'create_source',
  `gmt_create` bigint(20) NOT NULL COMMENT '创建时间',
  `gmt_modified` bigint(20) NOT NULL COMMENT '修改时间',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_tenant_info_kptenantid` (`kp`,`tenant_id`),
  KEY `idx_tenant_id` (`tenant_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='tenant_info';

CREATE TABLE `users` (
    `username` varchar(50) NOT NULL PRIMARY KEY COMMENT 'username',
    `password` varchar(500) NOT NULL COMMENT 'password',
    `enabled` boolean NOT NULL COMMENT 'enabled'
);

CREATE TABLE `roles` (
    `username` varchar(50) NOT NULL COMMENT 'username',
    `role` varchar(50) NOT NULL COMMENT 'role',
    UNIQUE INDEX `idx_user_role` (`username` ASC, `role` ASC) USING BTREE
);

CREATE TABLE `permissions` (
    `role` varchar(50) NOT NULL COMMENT 'role',
    `resource` varchar(128) NOT NULL COMMENT 'resource',
    `action` varchar(8) NOT NULL COMMENT 'action',
    UNIQUE INDEX `uk_role_permission` (`role`,`resource`,`action`) USING BTREE
);

INSERT INTO users (username, password, enabled) VALUES ('nacos', '$2a$10$EuWPZHzz32dJN7jexM34MOeYirDdFAZm2kuWj7VEOJhhZkDrxfvUu', TRUE);

INSERT INTO roles (username, role) VALUES ('nacos', 'ROLE_ADMIN');

二、nacos服务部署以及单个nacos服务搭建

直接使用集群搭建

1、拉取镜像

docker pull nacos/nacos-server

2、启动nacos

公网ip:111.231.74.191

docker run -itd \
-e PREFER_HOST_MODE=ip \
-e MODE=standalone \
-e SPRING_DATASOURCE_PLATFORM=mysql \ (要依赖的容器)
-e MYSQL_SERVICE_HOST=111.231.74.191 \ (当前mysql容器IP地址)
-e MYSQL_SERVICE_PORT=3306 \
-e MYSQL_SERVICE_DB_NAME=nacos \(要连接的数据库)
-e MYSQL_SERVICE_USER=root \
-e MYSQL_SERVICE_PASSWORD=password \(用root用户登录连接虚拟机时的密码)
-p 9901:8848 \
--name nacos \
--restart=always \
nacos/nacos-server

注意:当打上以上命名时,有几个点需要注意,已用红色字体表明

1、172.17.0.4 \ (当前mysql容器IP地址):

2、nacos \(要n连接的数据库)

注意:以上命令没写正确,就会报错

如果出现No DataSource set

  • 检查ip是否对应
  • 是否在同一网段下
  • 检查数据库名称
  • 检查root与密码
  • 检查数据库中是否存在对应的表(是否执行脚本)

三、nacos集群搭建

1、先移除所有的容器

docker ps -a

2、创建一个以自定义网路的mysql容器

删除

Pv4 内网 IP 地址范围:

  • 10.0.0.0 到 10.255.255.255(CIDR 表示法:10.0.0.0/8)
  • 172.16.0.0 到 172.31.255.255(CIDR 表示法:172.16.0.0/12)
  • 192.168.0.0 到 192.168.255.255(CIDR 表示法:192.168.0.0/16)

这些地址范围被保留用于内部网络,并且不会被分配给公共互联网上的设备。

自己的172.18.1.0 已经有了 所以改成19

docker network rm mynet
docker network create --subnet 172.19.1.0/24 --gateway 172.19.1.1 mynet

因为当mysql容器进行关闭后,再次打开时,mysql容器的端口号可能会进行改变

(这是mysql的ip,下面会进行创建两个服务,必须在同一网段下)

docker run -itd -p 3306:3306 \
--name mysql \
--net mynet --ip 172.19.1.30 \
-v /home/mysql/conf/my.cnf:/etc/my.cnf \
-v /home/mysql/data:/var/lib/mysql \
--privileged=true \
--restart=always \
-e MYSQL_ROOT_PASSWORD=root123 \
mysql:8.0.27

3、创建集群中的服务

nacos01:

(不要加d,方便查看是否报错加d是后台运行,)

docker run -it \
-e PREFER_HOST_MODE=ip \
-e MODE=cluster \
-e NACOS_SERVERS="172.19.1.41:8848 172.19.1.42:8848" \
-e SPRING_DATASOURCE_PLATFORM=mysql \
-e MYSQL_SERVICE_HOST=172.19.1.30 \
-e MYSQL_SERVICE_PORT=3306 \
-e MYSQL_SERVICE_DB_NAME=nacos \
-e MYSQL_SERVICE_USER=root \
-e MYSQL_SERVICE_PASSWORD=123456 \
-p 9901:8848 \
--name nacos01 \
--net mynet --ip 172.19.1.41 \
--restart=always \
nacos/nacos-server
nacos02:
docker run -it \
-e PREFER_HOST_MODE=ip \
-e MODE=cluster \
-e NACOS_SERVERS="172.19.1.41:8848 172.19.1.42:8848" \
-e SPRING_DATASOURCE_PLATFORM=mysql \
-e MYSQL_SERVICE_HOST=172.19.1.30 \
-e MYSQL_SERVICE_PORT=3306 \
-e MYSQL_SERVICE_DB_NAME=nacos \
-e MYSQL_SERVICE_USER=root \
-e MYSQL_SERVICE_PASSWORD=123456 \
-p 9902:8848 \
--name nacos02 \
--net mynet --ip 172.19.1.42 \
--restart=always \
nacos/nacos-server

可以查看到集群中有多少台服务器

增加了鉴权

报错

https://nacos.io/zh-cn/docs/v2/guide/user/auth.html

处理方法。

https://blog.csdn.net/pengpeng_xin/article/details/130906652

//拿掉 e NACOS_AUTH_ENABLE=true

解决办法-修改配置

1.通过docker进入nacos容器

docker exec -it nacos01 /bin/bash
docker exec -it nacos02 /bin/bash

2.进入配置文件夹

cd conf/

3.修改配置文件

vim application.properties

4、配置 覆盖

### The default token:
nacos.core.auth.plugin.nacos.token.secret.key=${NACOS_AUTH_TOKEN:SecretKey012345678901234567890123456789012345678901234567890123456789}
### open
nacos.core.auth.enabled=true

nacos.core.auth.server.identity.key=${NACOS_AUTH_IDENTITY_KEY:example}
nacos.core.auth.server.identity.value=${NACOS_AUTH_IDENTITY_VALUE:example}
其中

SecretKey01234567890123456789012345345678999987654901234567890123456789

可以随意修改,长度保持差不多或者长一点就可以。

ps:如果太短,你就有理由看日志为什么报错了。也是锻炼的机会哦🐶。

5.重启nacos

docker stop nacos
docker start nacos

暂时看不到。使用nginx做反向代理,负载均衡。

为什么要实现反向代理?

安装nginx代理nacos

因为一个集群中两台服务之间没有联系,必须给这两台服务进行建立联系,才能登一个账号可以进行同步操作

4.1、安装nginx代理nacos

方式一

Docker 安装 Nginx

docker pull nginx

创建映射文件夹

将容器内的文件夹映射到主机的 /opt/ 文件夹中(也可以不创建,运行容器时会自动创建,这里只是为了说明文件夹作用):

# 配置文件路径
mkdir -p /opt/nginx/conf/conf.d
# 静态资源路径
mkdir -p /opt/nginx/html
# 日志文件路径
mkdir -p /opt/nginx/logs

拷贝默认配置

# 启动容器
docker run -d --name nginx -p 80:80 nginx
# 获取容器 ID
docker ps | grep nginx
# 拷贝配置文件,81f10c94e08a 为上一步查到的容器 ID
docker cp 81f10c94e08a:/etc/nginx/nginx.conf /opt/nginx/conf/nginx.conf
docker cp 81f10c94e08a:/etc/nginx/conf.d /opt/nginx/conf
docker cp 81f10c94e08a:/usr/share/nginx/html /opt/nginx/html
# 强制删除容器
docker rm -f nginx

修改配置

# 修改配置文件,使用反向代理及负载均衡
vim /opt/nginx/conf/nginx.conf

修改如下:

    #gzip  on;

    upstream nacosCluster {
        server 172.19.1.41:8848 weight=1;
        server 172.19.1.42:8848 weight=2;
    }

    server {
        listen  80;
        server_name  111.231.74.191;

        location / {
            root   /etc/nginx/html/;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {
            root   /usr/share/nginx/html;
        }

        location /nacos {
            add_header backendIP $upstream_addr; # 添加 HTTP 响应头,以便知道负载到哪台服务器上
            add_header backendCode $upstream_status; # 响应码
            proxy_pass  http://nacosCluster;
        }
    }

启动容器

docker run -d --name nginx -p 80:80 \
--restart=always \
-v /opt/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
-v /opt/nginx/conf/conf.d:/etc/nginx/conf.d \
-v /opt/nginx/html:/usr/share/nginx/html \
-v /opt/nginx/logs:/var/log/nginx \
nginx

浏览器访问 ,按下 F12 即可看到请求落到哪台 Nacos 服务器上了。

还需要增加

docker network connect mynet nginx

http://111.231.74.191/nacos

方式二

user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
  worker_connections 1024;
}
http {
  include /etc/nginx/mime.types;
  default_type application/octet-stream;
  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;
  sendfile on;
  #tcp_nopush     on;
  keepalive_timeout 65;
  #gzip  on;
include /etc/nginx/conf.d/*.conf;
#1这里是需要添加的
  #命名时需要注意,名称不能带下划线"_",否则会报错
  upstream nacosCluster {
    server 172.19.1.41:8848;
    server 172.19.1.42:8848;
  }
  server {
    listen 8878;
    server_name localhost;
    location / {
      proxy_pass http://nacosCluster/;
    }
  }
  #1到这里结束
}
#2这里是需要添加的,与http同级
stream {
#命名时需要注意,名称不能带下划线"_",否则会报错
  upstream nacosGrpc {
    server 172.23.144.1:9848;
    server 172.23.144.1:9858;
    server 172.23.144.1:9868;
  }
  server {
    listen 9878; #nginx监听偏移1000的端口,因为nacos客户端会链接 8878+1000 这个端口,代理的是nacos服务端偏移后的端口
    proxy_pass nacosGrpc;
  }
}
#2到这里结束

https://blog.csdn.net/langmeng110/article/details/133986672

8858端口:Nacos Server的HTTP通信端口。该端口用于提供HTTP协议的API接口,用于配置管理、服务注册与发现等功能的访问和操作。

9858端口:Nacos Server的TCP通信端口。该端口用于提供TCP协议的API接口,用于服务注册与发现的高可用功能。

9859端口:Nacos Server的UDP通信端口。该端口用于提供UDP协议的API接口,用于服务注册与发现的高可用功能。

nacos的集群二配置

说明: 本次搭建均在同一台服务器上,以IP 172.23.144.1为例,以目录/data目录为例,且目录下创建3个文件夹分别对应3个nacos应用数据目录

mkdir /data/{nacos8848,nacos8858,nacos8868}
IP服务端口gRpc端口
172.23.144.188489848
172.23.144.188589858
172.23.144.188689868
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值