nacos分布式程序开发实例

时间:2024-04-09 07:30:48

1.通过windows docker desktop 完成 nacos 的安装/启动/配置

(1)先安装docker desktop

docker-toolbox-windows-docker-for-windows-stable安装包下载_开源镜像站-阿里云

(2)配置docker 国内镜像源

Docker 镜像加速 |

阿里云登录 - 欢迎登录阿里云,安全稳定的云计算服务平台

(3)拉取nacos的image,部署到本地docker

PS C:\Users\admin> docker pull nacos/nacos-server
Using default tag: latest
latest: Pulling from nacos/nacos-server
5ad559c5ae16: Pull complete
5746ca7cf180: Pull complete
d709fe221c89: Pull complete
e88fdcf257b1: Pull complete
eb573b28173c: Pull complete
a71625257ced: Pull complete
26e7e7836838: Pull complete
30f7d6851c4a: Pull complete
d565cd94c625: Pull complete
Digest: sha256:87a3d8b78ec24c253a4db7c093097a7b256327eb5117cd9498e289b896918153
Status: Downloaded newer image for nacos/nacos-server:latest
docker.io/nacos/nacos-server:latest

What's Next?
  View summary of image vulnerabilities and recommendations → docker scout quickview nacos/nacos-server
PS C:\Users\admin> docker run --name nacos -e MODE=standalone -p 8848:8848 -d nacos/nacos-server
8d17121aac278dc1353e342020183c412a8a4400a7a8fea293e5f5836b876f64

docker run --name nacos后,nacos即启动成功,在docker desktop界面上可以看到运行情况

(4)将现网nacos的yaml配置导出,并导入本地nacos中

导出的yaml文件会以zip压缩包形式下载到本地

将zip包导入本地nacos中即可(本地nacos登录地址http://127.0.0.1:8848/nacos

至此,nacos安装启动配置完毕。

2.在windows docker desktop中,现在最新的redis镜像(image),并用下载的镜像创建容器(container)

(1)下载redis镜像

PS C:\Users\admin> docker pull redis
Using default tag: latest
latest: Pulling from library/redis
a2abf6c4d29d: Pull complete
c7a4e4382001: Pull complete
4044b9ba67c9: Pull complete
c8388a79482f: Pull complete
413c8bb60be2: Pull complete
1abfd3011519: Pull complete
Digest: sha256:db485f2e245b5b3329fdc7eff4eb00f913e09d8feb9ca720788059fdc2ed8339
Status: Downloaded newer image for redis:latest
docker.io/library/redis:latest

What's Next?
  View summary of image vulnerabilities and recommendations → docker scout quickview redis

(2)查看本地镜像

PS C:\Users\admin> docker image ls
REPOSITORY                                             TAG          IMAGE ID       CREATED         SIZE
frappe/erpnext                                         v15.10.1     a3b490c6e849   2 months ago    1.71GB
postgres                                               15           d366292ba654   3 months ago    419MB
odoo                                                   latest       a5e2a8d99256   3 months ago    1.81GB
redis                                                  6.2-alpine   0e8d64a9df81   4 months ago    30.3MB
mariadb                                                10.6         d1d924d97091   4 months ago    396MB
mysql                                                  8.0          96bc8cf3633b   5 months ago    582MB
redis                                                  <none>       7c4b517da47d   7 months ago    153MB
mysql                                                  5.7          a5b7ceed4074   8 months ago    581MB
traefik                                                v2.6         22c6901de2be   22 months ago   102MB
redis                                                  latest       7614ae9453d1   2 years ago     113MB
nacos/nacos-server                                     latest       bdf60dc2ada3   2 years ago     1.05GB
rohitbasu77/oracle11g                                  latest       06b948774586   4 years ago     5.35GB
elasticsearch                                          6.8.3        1d0fd79266e6   4 years ago     800MB
mobz/elasticsearch-head                                5            b19a5c98e43b   7 years ago     824MB
registry.cn-hangzhou.aliyuncs.com/helowin/oracle_11g   latest       3fa112fd3642   8 years ago     6.85GB
jaspeen/oracle-11g                                     latest       0c8711fe4f0f   8 years ago     281MB

(3)创建容器(命令行)

PS C:\Users\admin> docker run -p 6379:6379 --name redis-gzw -v D:\code\wx\conf\redis.conf:/etc/redis/redis.conf -v D:\code\wx\dockerdata:/data -d redis:aliyunlast redis-server --appendonly yes
a9dfa639b1be5ece6178709016350124999db9467f6f73b8aa05b0bad41a46fc

------------------------------------------------------------------------------------------
第一次创建时,下面的报错是因为有redis容器正在运行,6379端口被占用,提示启动失败,但容器创建成功

docker: Error response from daemon: driver failed programming external connectivity on endpoint redis-gzw (231f0fb67ea730a060682083c7662e114189acad547729d002ee5457184d12d9): Bind for 0.0.0.0:6379 failed: port is already allocated.
PS C:\Users\admin> docker run -p 6379:6379 --name redis-gzw -v D:\code\wx\conf\redis.conf:/etc/redis/redis.conf -v D:\code\wx\dockerdata:/data -d redis:aliyunlast redis-server --appendonly yes

------------------------------------------------------------------------------------------
第二次创建时,下面的报错是因为容器名redis-gzw的容器已存在,不可重复创建。

docker: Error response from daemon: Conflict. The container name "/redis-gzw" is already in use by container "a9dfa639b1be5ece6178709016350124999db9467f6f73b8aa05b0bad41a46fc". You have to remove (or rename) that container to be able to reuse that name.
See 'docker run --help'.

-p 6379:6379 端口映射:前表示主机部分,:后表示容器部分。
–name redis-gzw 指定该容器名称,查看和进行操作都比较方便。


-v 挂载目录,规则与端口映射相同。

-v D:\code\wx\conf\redis.conf:/etc/redis/redis.conf D:\code\wx\conf\redis.conf 挂载配置文件实体机和虚拟机映射

表示实体机路径(本例使用的是windows主机),冒号后面的/etc/redis/redis.conf表示容器中的路径,可以理解为虚拟机路径

-v D:\code\wx\dockerdata:/data 挂载持久化存储目录的实体机和虚拟机映射


-d 是docker run命令的一个选项,它用于在后台(detached mode)运行容器。

redis:aliyunlast 表示使用指定的redis:aliyunlast镜像创建容器,是docker基本语法:

docker run [OPTIONS] IMAGE[:TAG|@DIGEST] [COMMAND] [ARG...]


redis-server 是Redis数据库的服务器进程(也是命令),当您运行Redis容器时,需要指定要运行的命令。redis-server命令用于启动Redis服务器。通过在docker run命令中指定redis-server作为容器的命令,您可以在Redis容器中启动Redis服务器进程。


--appendonly yes 是redis-server命令的参数,标识开启redis 持久化

windows|nacos 安装Redis_nacos配置redis-CSDN博客

gpt详细的解释:

https://poe.com/s/3jY7OrWD973byObFPGv5

创建后容器自动启动,如果没有启动成功,可以手动启动

(4)修改镜像的标签(tag)

PS C:\Users\admin> docker tag 7614ae9453d1d87e740a2056257a6de7135c84037c367e1fffa92ae922784631 redis:aliyunlast

docker tag IMAGEID(镜像id) REPOSITORY:TAG(仓库:标签)

(5)测试验证(redisinsight)

redisinsight:

Download | Redis