今天我们搭建一个3个node的nacos 集群
项目配置
把项目的源码整体复制2份到新的文件夹【naco2】和【nacos3】
参照【Nacos源码学习系列第1篇服务搭建-单机模式】导入项目并设置启动端口分别为【8060】和【8070】
数据库连接配置3个服务可以共用一套, 不必重复创建数据库
项目启动
区别于单机模式的vm启动参数配置, VM参数需要设置为 -Dnacos.standalone=false
为每个服务创建3个独立的运行时文件目录 同时在该目录下创建一个conf子目录并创建 cluster.conf文件
文件目录结构:
【起始目录 -> nacos1 -> nacos -> conf -> cluster.conf】
【起始目录 -> nacos2 -> nacos -> conf -> cluster.conf】
【起始目录 -> nacos3 -> nacos -> conf -> cluster.conf】
文件内容统一如下:
你的ip:8860
你的ip:8850
你的ip:8870
注意: 每个项目一定要配置独立的运行目录, 共用的话服务启动会失败
分别为每个项目设置VM 参数
-Duser.home=起始目录/nacos1
-Duser.home=起始目录/nacos2
-Duser.home=起始目录/nacos3
debug 模式运行 【cosole】模块下的 【Nacos.java】
看到下面的日志说明启动成功,启动过程有点慢,大概要2分钟左右:
2022-05-21 21:51:16.486 INFO 266344 --- [acos-starting.0] c.a.n.c.l.StartingApplicationListener : Nacos is starting...
2022-05-21 21:51:17.507 INFO 266344 --- [acos-starting.0] c.a.n.c.l.StartingApplicationListener : Nacos is starting...
2022-05-21 21:51:18.566 INFO 266344 --- [acos-starting.0] c.a.n.c.l.StartingApplicationListener : Nacos is starting...
2022-05-21 21:51:19.585 INFO 266344 --- [acos-starting.0] c.a.n.c.l.StartingApplicationListener : Nacos is starting...
2022-05-21 21:51:20.590 INFO 266344 --- [acos-starting.0] c.a.n.c.l.StartingApplicationListener : Nacos is starting...
2022-05-21 21:51:21.595 INFO 266344 --- [acos-starting.0] c.a.n.c.l.StartingApplicationListener : Nacos is starting...
2022-05-21 21:51:22.345 INFO 266344 --- [ main] c.a.n.c.l.StartingApplicationListener : Nacos started successfully in cluster mode. use external storage
如果启动过程中碰到下面的错误,原因是没有找到运行目录下的集群配置文件【nacos -> conf -> cluster.conf】:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'serverMemberManager' defined in file [D:\code\nacos2\core\target\classes\com\alibaba\nacos\core\cluster\ServerMemberManager.class]: Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.alibaba.nacos.core.cluster.ServerMemberManager]: Constructor threw exception; nested exception is ErrCode:500, ErrMsg:jmenv.tbsite.net
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:304)
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:285)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1338)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1185)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:554)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:514)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:321)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:319)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:277)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1276)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1196)
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:857)
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:760)
... 90 common frames omitted
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.alibaba.nacos.core.cluster.ServerMemberManager]: Constructor threw exception; nested exception is ErrCode:500, ErrMsg:jmenv.tbsite.net
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:187)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:117)
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:300)
... 104 common frames omitted
Caused by: com.alibaba.nacos.api.exception.NacosException: java.net.UnknownHostException: jmenv.tbsite.net
at com.alibaba.nacos.core.cluster.lookup.AddressServerMemberLookup.run(AddressServerMemberLookup.java:152)
at com.alibaba.nacos.core.cluster.lookup.AddressServerMemberLookup.doStart(AddressServerMemberLookup.java:100)
at com.alibaba.nacos.core.cluster.AbstractMemberLookup.start(AbstractMemberLookup.java:55)
at com.alibaba.nacos.core.cluster.ServerMemberManager.initAndStartLookup(ServerMemberManager.java:185)
at com.alibaba.nacos.core.cluster.ServerMemberManager.init(ServerMemberManager.java:165)
at com.alibaba.nacos.core.cluster.ServerMemberManager.(ServerMemberManager.java:146)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:175)
... 106 common frames omitted
Caused by: java.net.UnknownHostException: jmenv.tbsite.net
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:184)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:606)
at sun.net.NetworkClient.doConnect(NetworkClient.java:175)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:463)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:558)
at sun.net.www.http.HttpClient.(HttpClient.java:242)
at sun.net.www.http.HttpClient.New(HttpClient.java:339)
at sun.net.www.http.HttpClient.New(HttpClient.java:357)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:1226)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1162)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1056)
at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:990)
at com.alibaba.nacos.common.http.client.request.JdkHttpClientRequest.execute(JdkHttpClientRequest.java:114)
at com.alibaba.nacos.common.http.client.NacosRestTemplate.execute(NacosRestTemplate.java:482)
at com.alibaba.nacos.common.http.client.NacosRestTemplate.get(NacosRestTemplate.java:72)
at com.alibaba.nacos.core.cluster.lookup.AddressServerMemberLookup.syncFromAddressUrl(AddressServerMemberLookup.java:175)
at com.alibaba.nacos.core.cluster.lookup.AddressServerMemberLookup.run(AddressServerMemberLookup.java:143)
... 116 common frames omitted
查看控制台
浏览器打开控制台
登录信息(nacos/nacos)