乐趣区

关于nacos:Nacos源码学习系列第2篇服务搭建集群模式

明天咱们搭建一个 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.<init>(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.<init>(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

查看控制台

浏览器关上控制台

http://127.0.0.1:8050/nacos

登录信息 (nacos/nacos)

退出移动版