本地Windows10开发环境,下载Nacos作为微服务项目的注册中心和配置中心
下载版本为:nacos-server-2.1.2
下载好nacos-server,按照官方文档中的部署手册,修改conf/application.properties文件,增加数据源配置(如下)。
- #*************** Config Module Related Configurations ***************#
- ### If use MySQL as datasource:
- spring.datasource.platform=mysql
-
- ### Count of DB:
- db.num=1
-
- ### Connect URL of DB:
- db.url.0=jdbc:mysql://localhost:3306/数据库名?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC
- db.user=数据库用户名
- db.password=数据库密码
启动后报错,报错信息如下:
- org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat
- at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:163)
- at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:577)
- at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145)
- at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:745)
- at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:420)
- at org.springframework.boot.SpringApplication.run(SpringApplication.java:307)
- at org.springframework.boot.SpringApplication.run(SpringApplication.java:1317)
- at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306)
- 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.boot.web.server.WebServerException: Unable to start embedded Tomcat
- at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.initialize(TomcatWebServer.java:142)
- at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.<init>(TomcatWebServer.java:104)
- at org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory.getTomcatWebServer(TomcatServletWebServerFactory.java:479)
- at org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory.getWebServer(TomcatServletWebServerFactory.java:211)
- at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:182)
- at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:160)
- ... 16 common frames omitted
Nacos支持三种部署模式,其默认设置为集群模式。由于自己粗心,只看了Nacos官网文档中的部署手册,修改了数据源配置,但未添加集群配置,所以导致启动报错。

解决方案:
解决方案有两种:
第一种方案,将启动模式改为单机模式。
修改Nacos启动文件,将配置 set MODE="cluster" 改为 set MODE="standalone" 。
重新启动,可以看到服务以单机模式启动成功。
第二种方案,添加集群配置,依旧使用集群模式启动。
将 cluster.conf.example文件拷贝一份到当前目录,命名为 cluster.conf
修改 cluster.conf配置内容,配置内容如下:
注意:我这里只开启一个服务,所以只配置了一行,通常集群是有多台机器开启服务,在每行按格式添加相应的服务器ip和端口号即可。
重新启动,可以看到服务以集群模式启动成功。