Featured image of post Docker-Run命令部署Nacos集群

Docker-Run命令部署Nacos集群

推荐文章:

【合集】Docker

先执行完上面的nacos单容器:部署Nacos

1
2
cd /zx/nacos/conf`
`vim cluster.conf

cluster.conf文件内容

1
2
3
101.43.123.47:8841
101.43.123.47:8842
101.43.123.47:8843

创建3个nacos容器(conf绑定同的port)

 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
# 创建三个nacos容器
docker run -d \
    --name nacos1 \
    -e SPRING_DATASOURCE_PLATFORM=mysql \
    -e MYSQL_MASTER_SERVICE_HOST=101.43.123.47 \
    -e MYSQL_MASTER_SERVICE_PORT=3306 \
    -e MYSQL_MASTER_SERVICE_USER=root \
    -e MYSQL_MASTER_SERVICE_PASSWORD=root \
    -e MYSQL_MASTER_SERVICE_DB_NAME=nacos \
    -e MYSQL_SLAVE_SERVICE_HOST=101.43.123.47 \
    -e PREFER_HOST_MODE=101.43.123.47 \
    -e MODE=cluster \
    -e NACOS_SERVER_PORT=8841 \
    -e NACOS_SERVERS="101.43.123.47:8841 101.43.123.47:8842 101.43.123.47:8843" \
    -e NACOS_SERVER_IP=101.43.123.47 \
    -e JVM_XMS=256m -e JVM_XMX=256m  \
    -v /zx/nacos1/logs:/home/nacos/logs \
    -v /zx/nacos1/init.d/custom.properties:/home/nacos/init.d/custom.properties \
    -v /zx/nacos1/conf:/home/nacos/conf \
    -p 8841:8841 \
    --restart=always \
    nacos/nacos-server:1.4.1

docker run -d \
    --name nacos2 \
    -e SPRING_DATASOURCE_PLATFORM=mysql \
    -e MYSQL_MASTER_SERVICE_HOST=101.43.123.47 \
    -e MYSQL_MASTER_SERVICE_PORT=3306 \
    -e MYSQL_MASTER_SERVICE_USER=root \
    -e MYSQL_MASTER_SERVICE_PASSWORD=root \
    -e MYSQL_MASTER_SERVICE_DB_NAME=nacos \
    -e MYSQL_SLAVE_SERVICE_HOST=101.43.123.47 \
    -e PREFER_HOST_MODE=101.43.123.47 \
    -e MODE=cluster \
    -e NACOS_SERVER_PORT=8842 \
    -e NACOS_SERVERS="101.43.123.47:8841 101.43.123.47:8842 101.43.123.47:8843" \
    -e NACOS_SERVER_IP=101.43.123.47 \
    -e JVM_XMS=256m -e JVM_XMX=256m  \
    -v /zx/nacos2/logs:/home/nacos/logs \
    -v /zx/nacos2/init.d/custom.properties:/home/nacos/init.d/custom.properties \
    -v /zx/nacos2/conf:/home/nacos/conf \
    -p 8842:8842 \
    --restart=always \
    nacos/nacos-server:1.4.1

docker run -d \
    --name nacos3 \
    -e SPRING_DATASOURCE_PLATFORM=mysql \
    -e MYSQL_MASTER_SERVICE_HOST=101.43.123.47 \
    -e MYSQL_MASTER_SERVICE_PORT=3306 \
    -e MYSQL_MASTER_SERVICE_USER=root \
    -e MYSQL_MASTER_SERVICE_PASSWORD=root \
    -e MYSQL_MASTER_SERVICE_DB_NAME=nacos \
    -e MYSQL_SLAVE_SERVICE_HOST=101.43.123.47 \
    -e PREFER_HOST_MODE=101.43.123.47 \
    -e MODE=cluster \
    -e NACOS_SERVER_PORT=8843 \
    -e NACOS_SERVERS="101.43.123.47:8841 101.43.123.47:8842 101.43.123.47:8843" \
    -e NACOS_SERVER_IP=101.43.123.47 \
    -e JVM_XMS=256m -e JVM_XMX=256m  \
    -v /zx/nacos3/logs:/home/nacos/logs \
    -v /zx/nacos3/init.d/custom.properties:/home/nacos/init.d/custom.properties \
    -v /zx/nacos3/conf:/home/nacos/conf \
    -p 8843:8843 \
    --restart=always \
    nacos/nacos-server:1.4.1

安装nginx 反向代理nacos

nginx.conf内容(新启动的容器copy过来的)

 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
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
2
3
4
5
6
docker run -id --name=nginx \
-p 80:80 \
-v /zx/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
-v /zx/nginx/logs:/var/log/nginx \
-v /zx/nginx/html:/usr/share/nginx/html \
nginx:1.8

修改conf/nginx.conf文件,配置如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
upstream nacos-cluster {
    server 101.43.123.47:8841;
    server 101.43.123.47:8842;
    server 101.43.123.47:8843;
}

server {
    listen       80;
    server_name  101.43.123.47;

    location /nacos {
        proxy_pass http://nacos-cluster;
    }
}

而后在浏览器访问:http://localhost/nacos 即可。

代码中application.yml文件配置如下:

1
2
3
4
spring:
  cloud:
    nacos:
      server-addr: localhost:80 # Nacos地址

如果项目注册不了

  1. 设置本机名称:/etc/hostname mName
  2. 在/etc/hosts里加一行 本机IP mName

还是无法解决 错误:

(目前最靠谱解释:)

由于集群模式中,配置nacos.naming.data.warmup 属性为 true,因此当集群中只有一台服务时,会导致nacos服务为不可用状态。而本地测试时,该属性没有配置(默认值为false),因此集群只有一台服务也可以正常使用。

因此在使用时,如果是开发或者测试环境,只部署一台服务时,使用单机模式就可以了,配置集群模式在刚开始使用时,会遇到许多坑。

  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
  .____ _
__ _
_
 /\\ /___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ |'_| | '_ \/_` | \ \ \ \
        \\/___)| |_)| | | | | ||(_| |))))
        '  |____| .__|_| |_|_| |_\__, | / / / /
        =========|_|==============|___/=/_/_/_/
        ::
Spring Boot ::(v2.3.9.RELEASE)

        12-20 22:25:23:135INFO 6676---[main]cn.itcast.order.OrderApplication         :
No active
        profile set, falling
back to
default profiles:default
12-20 22:25:23:549INFO 6676---[main]o.s.cloud.context.scope.GenericScope     :
BeanFactory id = 1e3d3419-6ac5-3b72-b649-6b2133cfab3e
12-20 22:25:23:730INFO 6676---[main]o.s.b.w.embedded.tomcat.TomcatWebServer  :
Tomcat initialized
with port(s):8080(http)
        12-20 22:25:23:736INFO 6676---[main]o.apache.catalina.core.StandardService   :
Starting service[
Tomcat]
        12-20 22:25:23:737INFO 6676---[main]org.apache.catalina.core.StandardEngine  :
Starting Servlet
engine:[
Apache Tomcat/9.0.43]
        12-20 22:25:23:826INFO 6676---[main]o.a.c.c.C .[Tomcat].[localhost].[/]:
Initializing Spring
embedded WebApplicationContext
12-20 22:25:23:826INFO 6676---[main]w.s.c.ServletWebServerApplicationContext :
Root WebApplicationContext:
initialization completed
in 677ms
12-20 22:25:23:972WARN 6676---[main]c.n.c.sources.URLConfigurationSource     :
No URLs
will be
polled as
dynamic configuration
sources .
12-20 22:25:23:973INFO 6676---[main]c.n.c.sources.URLConfigurationSource     :
To enable
URLs as
dynamic configuration
sources,
define System
property archaius.
configurationSource.additionalUrls or
make config.
properties available
on classpath.
        12-20 22:25:23:975WARN 6676---[main]c.n.c.sources.URLConfigurationSource     :
No URLs
will be
polled as
dynamic configuration
sources .
12-20 22:25:23:975INFO 6676---[main]c.n.c.sources.URLConfigurationSource     :
To enable
URLs as
dynamic configuration
sources,
define System
property archaius.
configurationSource.additionalUrls or
make config.
properties available
on classpath.
        12-20 22:25:24:060INFO 6676---[main]o.s.s.concurrent.ThreadPoolTaskExecutor  :
Initializing ExecutorService 'applicationTaskExecutor'
        12-20 22:25:24:232INFO 6676---[main]o.s.s.c.ThreadPoolTaskScheduler          :
Initializing ExecutorService 'Nacos-Watch-Task-Scheduler'
        12-20 22:25:24:702INFO 6676---[main]o.s.b.w.embedded.tomcat.TomcatWebServer  :
Tomcat started
on port(s):8080(http)
with context
path ''
        12-20 22:25:24:797ERROR 6676---[main]c.a.c.n.registry.NacosServiceRegistry    :
        nacos registry, orderServer
register failed...
NacosRegistration {
    nacosDiscoveryProperties = NacosDiscoveryProperties {
        serverAddr = '101.43.123.47:8841', endpoint = '', namespace = '', watchDelay = 30000, logName = '', service = 'orderServer', weight = 1.0, clusterName = 'DEFAULT', group = 'DEFAULT_GROUP', namingLoadCacheAtStart = 'false', metadata = {preserved.register.source = SPRING_CLOUD}, registerEnabled = true, ip = '192.168.124.25', networkInterface = '', port = 8080, secure = false, accessKey = '', secretKey = '', heartBeatInterval = null, heartBeatTimeout = null, ipDeleteTimeout = null
    }
},

com.alibaba.nacos.api.exception.NacosException:
failed to
req API:/nacos/v1/ns/
instance after
all servers([101.43.123.47:8841]) tried:ErrCode:503,ErrMsg:
server is
        DOWN now, please try
again later!
at com.alibaba.nacos.client.naming.net.NamingProxy.
reqApi(NamingProxy.Java:556) ~[nacos-client-1.4.1.jar:na]
at com.alibaba.nacos.client.naming.net.NamingProxy.
reqApi(NamingProxy.Java:498) ~[nacos-client-1.4.1.jar:na]
at com.alibaba.nacos.client.naming.net.NamingProxy.
reqApi(NamingProxy.Java:493) ~[nacos-client-1.4.1.jar:na]
at com.alibaba.nacos.client.naming.net.NamingProxy.
registerService(NamingProxy.Java:246) ~[nacos-client-1.4.1.jar:na]
at com.alibaba.nacos.client.naming.NacosNamingService.
registerInstance(NacosNamingService.Java:212) ~[nacos-client-1.4.1.jar:na]
at com.alibaba.cloud.nacos.registry.NacosServiceRegistry.
register(NacosServiceRegistry.Java:74) ~[spring-cloud-starter-alibaba-nacos-discovery-2.2.5.RELEASE.jar:2.2.5.RELEASE]
at org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration.
register(AbstractAutoServiceRegistration.Java:239) [spring-cloud-commons-2.2.7.RELEASE.jar:2.2.7.RELEASE]
at com.alibaba.cloud.nacos.registry.NacosAutoServiceRegistration.
register(NacosAutoServiceRegistration.Java:78) [spring-cloud-starter-alibaba-nacos-discovery-2.2.5.RELEASE.jar:2.2.5.RELEASE]
at org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration.
start(AbstractAutoServiceRegistration.Java:138) [spring-cloud-commons-2.2.7.RELEASE.jar:2.2.7.RELEASE]
at org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration.
bind(AbstractAutoServiceRegistration.Java:101) [spring-cloud-commons-2.2.7.RELEASE.jar:2.2.7.RELEASE]
at org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration.
onApplicationEvent(AbstractAutoServiceRegistration.Java:88) [spring-cloud-commons-2.2.7.RELEASE.jar:2.2.7.RELEASE]
at org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration.
onApplicationEvent(AbstractAutoServiceRegistration.Java:47) [spring-cloud-commons-2.2.7.RELEASE.jar:2.2.7.RELEASE]
at org.springframework.context.event.SimpleApplicationEventMulticaster.
doInvokeListener(SimpleApplicationEventMulticaster.Java:172) [spring-context-5.2.13.RELEASE.jar:5.2.13.RELEASE]
at org.springframework.context.event.SimpleApplicationEventMulticaster.
invokeListener(SimpleApplicationEventMulticaster.Java:165) [spring-context-5.2.13.RELEASE.jar:5.2.13.RELEASE]
at org.springframework.context.event.SimpleApplicationEventMulticaster.
multicastEvent(SimpleApplicationEventMulticaster.Java:139) [spring-context-5.2.13.RELEASE.jar:5.2.13.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.
publishEvent(AbstractApplicationContext.Java:404) [spring-context-5.2.13.RELEASE.jar:5.2.13.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.
publishEvent(AbstractApplicationContext.Java:361) [spring-context-5.2.13.RELEASE.jar:5.2.13.RELEASE]
at org.springframework.boot.web.servlet.context.WebServerStartStopLifecycle.
start(WebServerStartStopLifecycle.Java:46) [spring-boot-2.3.9.RELEASE.jar:2.3.9.RELEASE]
at org.springframework.context.support.DefaultLifecycleProcessor.
doStart(DefaultLifecycleProcessor.Java:182) [spring-context-5.2.13.RELEASE.jar:5.2.13.RELEASE]
at org.springframework.context.support.DefaultLifecycleProcessor.
access$200(DefaultLifecycleProcessor.Java:53) [spring-context-5.2.13.RELEASE.jar:5.2.13.RELEASE]
at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.
start(DefaultLifecycleProcessor.Java:360) [spring-context-5.2.13.RELEASE.jar:5.2.13.RELEASE]
at org.springframework.context.support.DefaultLifecycleProcessor.
startBeans(DefaultLifecycleProcessor.Java:158) [spring-context-5.2.13.RELEASE.jar:5.2.13.RELEASE]
at org.springframework.context.support.DefaultLifecycleProcessor.
onRefresh(DefaultLifecycleProcessor.Java:122) [spring-context-5.2.13.RELEASE.jar:5.2.13.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.
finishRefresh(AbstractApplicationContext.Java:895) [spring-context-5.2.13.RELEASE.jar:5.2.13.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.
refresh(AbstractApplicationContext.Java:554) [spring-context-5.2.13.RELEASE.jar:5.2.13.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.
refresh(ServletWebServerApplicationContext.Java:143) [spring-boot-2.3.9.RELEASE.jar:2.3.9.RELEASE]
at org.springframework.boot.SpringApplication.
refresh(SpringApplication.Java:758) [spring-boot-2.3.9.RELEASE.jar:2.3.9.RELEASE]
at org.springframework.boot.SpringApplication.
refresh(SpringApplication.Java:750) [spring-boot-2.3.9.RELEASE.jar:2.3.9.RELEASE]
at org.springframework.boot.SpringApplication.
refreshContext(SpringApplication.Java:405) [spring-boot-2.3.9.RELEASE.jar:2.3.9.RELEASE]
at org.springframework.boot.SpringApplication.
run(SpringApplication.Java:315) [spring-boot-2.3.9.RELEASE.jar:2.3.9.RELEASE]
at org.springframework.boot.SpringApplication.
run(SpringApplication.Java:1237) [spring-boot-2.3.9.RELEASE.jar:2.3.9.RELEASE]
at org.springframework.boot.SpringApplication.
run(SpringApplication.Java:1226) [spring-boot-2.3.9.RELEASE.jar:2.3.9.RELEASE]
at cn.itcast.order.OrderApplication.
main(OrderApplication.Java:17) [classes/:na]

        12-20 22:25:24:797WARN 6676---[main]ConfigServletWebServerApplicationContext :
Exception encountered
during context
initialization -
cancelling refresh
attempt:org.springframework.context.ApplicationContextException:
Failed to
start bean 'webServerStartStop';
nested exception
is Java.lang.reflect.UndeclaredThrowableException
皖ICP备2024056275号-1
发表了78篇文章 · 总计149.56k字
本站已稳定运行