原创

CentOS下nacos安装

1.下载

  • 下载地址[.tar.gz] https://github.com/alibaba/nacos/releases

解压并移动

tar -zxvf nacos-server-1.1.4.tar.gz 
mv nacos /usr/local

单机模式启动

cd /usr/local/nacos/bin
sh startup.sh -m standalone

或启动

bash startup.sh -m standalone

启动时出现报错

错误一

/usr/java/jdk1.8.0_181/bin/java  -Xms512m -Xmx512m -Xmn256m -Dnacos.standalone=true -Djava.ext.dirs=/usr/java/jdk1.8.0_181/jre/lib/ext:/usr/java/jdk1.8.0_181/lib/ext:/usr/local/nacos/plugins/cmdb:/usr/local/nacos/plugins/mysql -Xloggc:/usr/local/nacos/logs/nacos_gc.log -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=10 -XX:GCLogFileSize=100M -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8000 -Dnacos.home=/usr/local/nacos -Dloader.path=/usr/local/nacos/plugins/health -jar /usr/local/nacos/target/nacos-server.jar  --spring.config.location=classpath:/,classpath:/config/,file:./,file:./config/,file:/usr/local/nacos/conf/ --logging.config=/usr/local/nacos/conf/nacos-logback.xml --server.max-http-header-size=524288
nacos is starting with standalone
nacos is starting,you can check the /usr/local/nacos/logs/start.out

查看/usr/local/nacos/logs/start.out,看文件中提示具体什么错误

如报下面错误:

java.io.FileNotFoundException: /usr/local/nacos/conf/cluster.conf (没有那个文件或目录)
	at java.io.FileInputStream.open0(Native Method)
	at java.io.FileInputStream.open(FileInputStream.java:195)
	at java.io.FileInputStream.<init>(FileInputStream.java:138)
	at com.alibaba.nacos.core.utils.SystemUtils.readClusterConf(SystemUtils.java:124)
	at com.alibaba.nacos.core.listener.StartingSpringApplicationRunListener.logClusterConf(StartingSpringApplicationRunListener.java:141)
	at com.alibaba.nacos.core.listener.StartingSpringApplicationRunListener.contextPrepared(StartingSpringApplicationRunListener.java:91)
	at org.springframework.boot.SpringApplicationRunListeners.contextPrepared(SpringApplicationRunListeners.java:60)
	at org.springframework.boot.SpringApplication.prepareContext(SpringApplication.java:374)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:314)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248)
	at com.alibaba.nacos.Nacos.main(Nacos.java:33)
	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:48)
	at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
	at org.springframework.boot.loader.Launcher.launch(Launcher.java:50)
	at org.springframework.boot.loader.PropertiesLauncher.main(PropertiesLauncher.java:593)

解决方案

  • 由于启动的默认由cluster改成了standalone单机启动模式,但是启动仍然是集群模式启动,并且抛出上述找不到cluster.conf文件
bash startup.sh -m standalone

错误二

systemctl start nacos.service

报如下错误:

nacos.service
   Loaded: loaded (/usr/lib/systemd/system/nacos.service; bad; vendor preset: disabled)
   Active: failed (Result: exit-code) since 一 2019-10-28 22:31:25 CST; 19s ago
  Process: 2847 ExecStart=/usr/local/nacos/bin/startup.sh -m standalone (code=exited, status=1/FAILURE)

10月 28 22:31:25 localhost.localdomainP1 startup.sh[2847]: which: no javac in (/usr/...
10月 28 22:31:25 localhost.localdomainP1 startup.sh[2847]: readlink: 缺少操作数
10月 28 22:31:25 localhost.localdomainP1 startup.sh[2847]: Try 'readlink --help' for...
10月 28 22:31:25 localhost.localdomainP1 systemd[1]: nacos.service: control process...1
10月 28 22:31:25 localhost.localdomainP1 startup.sh[2847]: dirname: 缺少操作数
10月 28 22:31:25 localhost.localdomainP1 startup.sh[2847]: Try 'dirname --help' for ...
10月 28 22:31:25 localhost.localdomainP1 systemd[1]: Failed to start nacos.service.
10月 28 22:31:25 localhost.localdomainP1 startup.sh[2847]: ERROR: Please set the JAV...
10月 28 22:31:25 localhost.localdomainP1 systemd[1]: Unit nacos.service entered fai....
10月 28 22:31:25 localhost.localdomainP1 systemd[1]: nacos.service failed.

解决方案

需要更改相应启动文件的JDK配置
将如下三行注释掉,并将第一行的配置修改为JDK的位置。

如何找到JDK位置

whereis java

找到JDK的位置为: /usr/java/jdk1.8.0_181

vim startup.sh

原文为:

[ ! -e "$JAVA_HOME/bin/java" ] && JAVA_HOME=$HOME/jdk/java
[ ! -e "$JAVA_HOME/bin/java" ] && JAVA_HOME=/usr/java
[ ! -e "$JAVA_HOME/bin/java" ] && JAVA_HOME=/opt/taobao/java
[ ! -e "$JAVA_HOME/bin/java" ] && unset JAVA_HOME

修改后

[ ! -e "$JAVA_HOME/bin/java" ] && JAVA_HOME=/usr/java/jdk1.8.0_181
#[ ! -e "$JAVA_HOME/bin/java" ] && JAVA_HOME=/usr/java
#[ ! -e "$JAVA_HOME/bin/java" ] && JAVA_HOME=/opt/taobao/java
#[ ! -e "$JAVA_HOME/bin/java" ] && unset JAVA_HOME


访问web地址

curl http://127.0.0.1:8848/nacos/index.html

http://127.0.0.1:8848/nacos/index.html
登录的用户名和密码默认的都是nacos

注意防火墙的端口8848

集群

本例的安装目录为/usr/local/nacos

参考链接: https://www.cnblogs.com/lhlucky/p/nacoscluster.html

1.配置 cluster.conf文件

  • 在文件里添加相关服务器IP,三台机器都做相同的配置
cd /usr/local/nacos/conf
cp cluster.conf.example cluster.conf
vim cluster.conf

cluster.conf文件内容为:

#it is ip
#example
#10.10.109.214
#11.16.128.34
#11.16.128.36
192.168.47.128:8848
192.168.47.129:8848
192.168.47.130:8848

2.创建数据库

  • 脚本位置 /usr/local/nacos/conf/nacos-mysql.sql
  • 将脚本里的SQL语句直接导入既可

3.配置application.properties

cd /usr/local/nacos/conf
vim application.properties

增加内容为:

spring.datasource.platform=mysql
db.num=1
db.url.0=jdbc:mysql://192.168.47.128:5186/nacos_config?serverTimezone=GMT%2B8&characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true
db.user=cici
db.password=123123

4.分别启动三台服务器

cd /usr/local/nacos/bin
sh startup.sh

观察日志,看是否有异常打印【cat或者tailf命令】

  • /usr/local/nacos/logs/nacos.log
  • /usr/local/nacos/logs/naming-raft.log
  • /usr/local/nacos/logs/start.out

启动成功后访问,观察集群

  • http://192.168.47.128:8848/nacos/index.html

  • http://192.168.47.129:8848/nacos/index.html

  • http://192.168.47.130:8848/nacos/index.html

5.遇到的问题一

5.1 mysql8问题

直接下载的稳定版本nacos编译后的文件,不支持mysql8及其以上版本,本例中使用的为1.1.4版本

-参考链接
https://www.cnblogs.com/gyli20170901/p/11245270.html

解决方案

  • 下载nacos的源码并更改mysql驱动的版本
5.1.1 下载地址

https://github.com/alibaba/nacos

5.1.2 修改

修改最外层pom.xml 中 mysql驱动版本,我这边使用的是8.0.16

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.16</version>
</dependency>
5.1.3 修改naming这个项目 com.alibaba.nacos.naming.healthcheck 包下的 MysqlHealthCheckProcessor 类的第24行导包为
import com.mysql.cj.jdbc.MysqlDataSource;
5.1.4修改console项目下的配置文件

-由于mysql8及其以上版本需要带时区,所以还需要修改 console这项目 resources/META-INF下 nacos-default.properties这个文件中的db.url

db.url.0=jdbc:mysql://11.162.196.161:3306/diamond_devtest?serverTimezone=GMT%2B8&characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true
db.url.1=jdbc:mysql://11.163.152.91:3306/diamond_devtest?serverTimezone=GMT%2B8&characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true
5.1.5 打包

cmd命令窗口进入项目根目录执行

mvn -Prelease-nacos clean install -U
5.1.6 打包完成后,再次安装nacos,步骤按照上面安装的说明
  • 包的地址[根目录\distribution\target]
5.1.7 再次修改application.properties配置文件
cd /usr/local/nacos/conf
vim application.properties

修改为

spring.datasource.platform=mysql
db.num=1
db.url.0=jdbc:mysql://192.168.47.128:5186/nacos_config?serverTimezone=GMT%2B8&characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true
db.user=cici
db.password=123123

5.2 unable to find local peer: 192.168.47.128:8848

  • 参考链接 https://www.wandouip.com/t5i278697/
java.lang.IllegalStateException: unable to find local peer: 172.16.26.250:8848, all peers: [120.79.167.88:8848, 119.23.104.130:8848, 47.101.47.127:8848]
	at com.alibaba.nacos.naming.consistency.persistent.raft.RaftPeerSet.local(RaftPeerSet.java:224)
	at com.alibaba.nacos.naming.monitor.PerformanceLoggerThread.collectMetrics(PerformanceLoggerThread.java:100)
	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.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:84)
	at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
	at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:93)
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at java.lang.Thread.run(Thread.java:748)
2019-10-18 14:06:45,000 ERROR Unexpected error occurred in scheduled task.

因为我部署的三台服务器是虚拟机上的,属于内网,都是在一个网段的。

解决方案

编辑启动文件 /usr/local/nacos/bin/startup.sh

vim /usr/local/nacos/bin/startup.sh

依次修改三台服务器的启动文件

# 单机模式对应的启动参数
if [[ "${MODE}" == "standalone" ]]; then
    JAVA_OPT="${JAVA_OPT} -Xms512m -Xmx512m -Xmn256m"
    JAVA_OPT="${JAVA_OPT} -Dnacos.standalone=true"
else
# 集群模式对应的启动参数
    JAVA_OPT="${JAVA_OPT} -server -Xms2g -Xmx2g -Xmn1g -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m"
    JAVA_OPT="${JAVA_OPT} -XX:-OmitStackTraceInFastThrow -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=${BASE_DIR}/logs/java_heapdump.hprof"
    JAVA_OPT="${JAVA_OPT} -XX:-UseLargePages"
    
    # *新增以下参数设置本机ip地址*
    JAVA_OPT="${JAVA_OPT} -Dnacos.server.ip=192.168.47.128"

fi

if [[ "${FUNCTION_MODE}" == "config" ]]; then
    JAVA_OPT="${JAVA_OPT} -Dnacos.functionMode=config"
elif [[ "${FUNCTION_MODE}" == "naming" ]]; then
    JAVA_OPT="${JAVA_OPT} -Dnacos.functionMode=naming"
fi

修改之后再次启动即可

#关闭服务

cd /usr/local/nacos/bin
./shutdown.sh
#启动服务

sh startup.sh
正文到此结束