linux 使用 nginx 搭建 nacos 集群模式

linux 搭建 nacos 集群,步骤如下

首先准备3台服务器,笔者这里的三台服务器 ip 地址分别是 192.168.0.119;192.168.0.123;192.168.0.114,后面简称为119,123,114

然后为3台服务器配置 java 环境,笔者这里的3台服务器已经成功安装 java 环境,故安装 java 的过程这里就不赘述了,没有配 java 环境的朋友可以参考:https://blog.csdn.net/wsjzzcbq/article/details/82818826

1、官网下载 nacos

官网地址:https://nacos.io/zh-cn/

进入文档,快速开始,下载 最新稳定版本

linux 使用 nginx 搭建 nacos 集群模式_第1张图片

笔者这里下载的版本是最新的 2.1.0-BETA

linux 使用 nginx 搭建 nacos 集群模式_第2张图片

 2、nacos 集群搭建

2.1、安装 nacos

成功下载安装包后,将 nacos 安装包分别上传到3台服务器

笔者这里将 nacos 安装包上传到 119,然后通过 scp 命令将安装包复制到另外2台服务器

scp 命令

scp /root/nacos-server-2.1.0-BETA.tar.gz [email protected]:/root
scp /root/nacos-server-2.1.0-BETA.tar.gz [email protected]:/root

linux 使用 nginx 搭建 nacos 集群模式_第3张图片

linux 使用 nginx 搭建 nacos 集群模式_第4张图片 

3台服务器都上传成功后,在3台服务器上分别创建 nacos 安装目录

mkdir -p /usr/local/nacos

然后分别将 nacos 安装包解压到新建的 nacos 安装目录

tar -zxvf nacos-server-2.1.0-BETA.tar.gz -C /usr/local/nacos

linux 使用 nginx 搭建 nacos 集群模式_第5张图片

 

2.2、配置集群信息

解压完成后,分别进入3台机器的 nacos 的 conf 目录,编辑 cluster.conf 文件,配置集群信息

cd /usr/local/nacos/nacos/conf/

进入conf目录后,将 cluster.conf.example 文件修改为 cluster.conf

这里直接复制修改文件名

cp cluster.conf.example cluster.conf

linux 使用 nginx 搭建 nacos 集群模式_第6张图片

 编辑 cluster.conf 文件

vi cluster.conf

添加集群配置

读者可修改成自己服务器的 ip 地址

192.168.0.119:8848
192.168.0.123:8848
192.168.0.114:8848

 linux 使用 nginx 搭建 nacos 集群模式_第7张图片

保存

linux 使用 nginx 搭建 nacos 集群模式_第8张图片

 

2.3、配置数据库

笔者这里使用一台数据库,放在119机器上

新建数据库,取名为 nacos

linux 使用 nginx 搭建 nacos 集群模式_第9张图片

 建表 sql

从 nacos 安装目录下的 conf 目录上获取 nacos-mysql.sql 

linux 使用 nginx 搭建 nacos 集群模式_第10张图片

 获取将压缩包解压,直接复制

linux 使用 nginx 搭建 nacos 集群模式_第11张图片

 

将 nacos-mysql.sql 的内容在 navicat 中执行

linux 使用 nginx 搭建 nacos 集群模式_第12张图片

建表成功

下面为3台服务器分别配置数据库信息

编辑 application.properties 文件

vi application.properties

添加数据库配置信息

db.num=1
db.url.0=jdbc:mysql://192.168.0.119:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true
db.user=root
db.password=123456

linux 使用 nginx 搭建 nacos 集群模式_第13张图片

 

3、启动集群

在启动集群之前,需要在3台服务器的防火墙上分别开放3个端口,分别是8848、9848 和 9849

开放 8848,web页面访问需要

firewall-cmd --zone=public --add-port=8848/tcp --permanent

linux 使用 nginx 搭建 nacos 集群模式_第14张图片

 

开放 9848 和 9849

Nacos2.0版本相比1.X新增了gRPC的通信方式,因此需要增加2个端口

linux 使用 nginx 搭建 nacos 集群模式_第15张图片

 

firewall-cmd --zone=public --add-port=9848/tcp --permanent
firewall-cmd --zone=public --add-port=9849/tcp --permanent

开放后,更新防火墙规则(无需断开连接,动态添加规则)

firewall-cmd --reload

linux 使用 nginx 搭建 nacos 集群模式_第16张图片

 

防火墙端口开放成功后,进入bin目录,启动集群

分别进入3台机器的 nacos 的 bin 目录,依次执行启动命令

cd /usr/local/nacos/nacos/bin

linux 使用 nginx 搭建 nacos 集群模式_第17张图片

 启动 nacos

sh startup.sh

linux 使用 nginx 搭建 nacos 集群模式_第18张图片

 

4、配置 nginx

在 nginx 中添加如下配置

upstream nacos {
    server 192.168.0.119:8848;
    server 192.168.0.123:8848;
    server 192.168.0.114:8848;
}
	
server {
    listen 80;
    server_name cloudnacos;
    location / {
        proxy_pass http://nacos;
    }
}

修改 hosts 文件,添加域名 cloudnacos

hosts 文件在 C:\Windows\System32\drivers\etc 目录下

在 hosts 文件中添加 127.0.0.1   cloudnacos

127.0.0.1   cloudnacos

linux 使用 nginx 搭建 nacos 集群模式_第19张图片

 配置完成后,启动 nginx

浏览器访问:http://cloudnacos/nacos

用户名密码默认都是:nacos

查看集群节点

linux 使用 nginx 搭建 nacos 集群模式_第20张图片

 

5、服务注册

nginx 配置后,启动成功

新建 nacos 项目,在 bootstrap.yml 文件中添加配置

项目创建过程这里不赘述了

server:
  port: 8090

spring:
  application:
    name: cloud-learn-system
  cloud:
    nacos:
      discovery:
        server-addr: http://cloudnacos
      config:
        server-addr: http://cloudnacos
        file-extension: yaml

linux 使用 nginx 搭建 nacos 集群模式_第21张图片

 

然后启动项目

项目启动成功后,分别访问 http://192.168.0.119:8848/nacos;http://192.168.0.123:8848/nacos;http://192.168.0.114:8848/nacos 查看服务是否注册成功和是否数据同步

linux 使用 nginx 搭建 nacos 集群模式_第22张图片

 

nacos 集群搭建成功

6、其他

nacos 集群关闭命令

sh shutdown.sh

如果 nacos 集群启动后,查看  /usr/local/nacos/nacos/logs/start.out 发现报错

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'memoryMonitor' defined in URL [jar:file:/usr/local/nacos/nacos/target/nacos-server.jar!/BOOT-INF/lib/nacos-config-2.1.0-BETA.jar!/com/alibaba/nacos/config/server/monitor/MemoryMonitor.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'asyncNotifyService': Unsatisfied dependency expressed through field 'dumpService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'externalDumpService': Invocation of init method failed; nested exception is ErrCode:500, ErrMsg:Nacos Server did not start because dumpservice bean construction failure :
No DataSource set
	at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:769)
	at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:218)
	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.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:866)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550)
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141)
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:744)
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:391)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:312)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1204)
	at com.alibaba.nacos.Nacos.main(Nacos.java:35)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49)
	at org.springframework.boot.loader.Launcher.launch(Launcher.java:108)
	at org.springframework.boot.loader.Launcher.launch(Launcher.java:58)
	at org.springframework.boot.loader.PropertiesLauncher.main(PropertiesLauncher.java:467)
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'asyncNotifyService': Unsatisfied dependency expressed through field 'dumpService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'externalDumpService': Invocation of init method failed; nested exception is ErrCode:500, ErrMsg:Nacos Server did not start because dumpservice bean construction failure :
No DataSource set
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:598)
	at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:376)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1402)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:591)
	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)
	... 27 common frames omitted
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'externalDumpService': Invocation of init method failed; nested exception is ErrCode:500, ErrMsg:Nacos Server did not start because dumpservice bean construction failure :
No DataSource set
	at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:139)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:413)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1761)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:592)
	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.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:595)
	... 41 common frames omitted
Caused by: com.alibaba.nacos.api.exception.NacosException: Nacos Server did not start because dumpservice bean construction failure :
No DataSource set
	at com.alibaba.nacos.config.server.service.dump.DumpService.dumpOperate(DumpService.java:225)
	at com.alibaba.nacos.config.server.service.dump.ExternalDumpService.init(ExternalDumpService.java:52)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:363)
	at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:307)
	at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:136)
	... 53 common frames omitted
Caused by: java.lang.IllegalStateException: No DataSource set
	at org.springframework.util.Assert.state(Assert.java:73)
	at org.springframework.jdbc.support.JdbcAccessor.obtainDataSource(JdbcAccessor.java:77)
	at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:371)
	at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:452)
	at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:462)
	at org.springframework.jdbc.core.JdbcTemplate.queryForObject(JdbcTemplate.java:473)
	at org.springframework.jdbc.core.JdbcTemplate.queryForObject(JdbcTemplate.java:480)
	at com.alibaba.nacos.config.server.service.repository.extrnal.ExternalStoragePersistServiceImpl.findConfigMaxId(ExternalStoragePersistServiceImpl.java:674)
	at com.alibaba.nacos.config.server.service.dump.processor.DumpAllProcessor.process(DumpAllProcessor.java:51)
	at com.alibaba.nacos.config.server.service.dump.DumpService.dumpConfigInfo(DumpService.java:282)
	at com.alibaba.nacos.config.server.service.dump.DumpService.dumpOperate(DumpService.java:195)
	... 61 common frames omitted

很可能是数据库没连接上的问题,可以将数据库放在集群节点之外,保持集群与数据库间良好的网络通信

至此完

你可能感兴趣的:(spring,cloud,spring,cloud,分布式)