2020-03-12 21:20:17,866 INFO Nacos started successfully in cluster mode. use external storage 2020-03-12 21:20:18,117 INFO Initializing Spring DispatcherServlet 'dispatcherServlet' 2020-03-12 21:20:18,117 INFO Initializing Servlet 'dispatcherServlet' 2020-03-12 21:20:18,141 INFO Completed initialization in 23 ms
3.1、Nacos 集群模式测试
3.1.1、调用 OPEN API
1 2 3 4 5 6 7 8 9 10 11
# 发布配置 $ curl -X POST "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=nacos.cfg.dataId&group=test&content=helloWorld"
# 获取配置 $ curl -X GET "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=nacos.cfg.dataId&group=test"
# 服务注册 $ curl -X POST 'http://127.0.0.1:8848/nacos/v1/ns/instance?serviceName=nacos.naming.serviceName&ip=20.18.7.10&port=8080'
# 服务发现 $ curl -X GET 'http://127.0.0.1:8848/nacos/v1/ns/instance/list?serviceName=nacos.naming.serviceName'
grafana /run.sh Up 0.0.0.0:3000->3000/tcp mysql docker-entrypoint.sh mysqld Exit 1 nacos-standalone-mysql bin/docker-startup.sh Up 0.0.0.0:8848->8848/tcp, 0.0.0.0:9555->9555/tcp prometheus /bin/prometheus --config.f ... Up 0.0.0.0:9090->9090/tcp
# 查看容器的运行状态 $ docker-compose -f example/standalone-mysql-5.7.yaml logs mysql
mysql | Database initialized mysql | Initializing certificates mysql | Generating a RSA private key mysql | ..+++++ mysql | ..................................+++++ mysql | unable to write 'random state' mysql | writing new private key to 'ca-key.pem' mysql | ----- mysql | Generating a RSA private key mysql | .......................+++++ mysql | ......+++++ mysql | unable to write 'random state' mysql | writing new private key to 'server-key.pem' mysql | ----- mysql | Generating a RSA private key mysql | ..............................................................+++++ mysql | ...............+++++ mysql | unable to write 'random state' mysql | writing new private key to 'client-key.pem' mysql | ----- mysql | mysql_ssl_rsa_setup: Can't change permissions of the file 'ca-key.pem' (Errcode: 1 - Operation not permitted) mysql | 2020-03-12 22:13:39 [ERROR] Error setting file permissions forca-key.pem and ca.pem mysql | mysql_ssl_rsa_setup: Can't change permissions of the file 'server-key.pem' (Errcode: 1 - Operation not permitted) mysql | 2020-03-12 22:13:39 [ERROR] Error setting file permissions forserver-key.pem and server-cert.pem mysql | mysql_ssl_rsa_setup: Can't change permissions of the file 'client-key.pem' (Errcode: 1 - Operation not permitted) mysql | 2020-03-12 22:13:39 [ERROR] Error setting file permissions forclient-key.pem and client-cert.pem
grafana /run.sh Up 0.0.0.0:3000->3000/tcp mysql docker-entrypoint.sh mysqld Up 0.0.0.0:3309->3306/tcp, 33060/tcp nacos-standalone-mysql bin/docker-startup.sh Up 0.0.0.0:8848->8848/tcp, 0.0.0.0:9555->9555/tcp prometheus /bin/prometheus --config.f ... Up 0.0.0.0:9090->9090/tcp
5.1、Nacos 启动失败
5.1.1、原因分析
容器虽然都启动成功了,但浏览器无法访问 http://127.0.0.1:8848/nacos,查看 Nacos 的日志文件,发现有 No DataSource set 相关的异常信息,也就是 Nacos Server 无法正常连接到 MySQL 数据库
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)
经过排查,发现可能是以下几方面原因造成:
1)开启系统防火墙导致
2)在 MySQL 的配置文件 /etc/mysql/mysql.conf.d/mysqld.cnf 里,开启了 bind 127.0.0.1 配置项,导致外部无法连接
3)在 MySQL 数据库里不存在 nacos 用户,这是因为在 env 目录下的 Docker-Compose 环境配置文件中,指定了 Nacos 默认连接 MySQL 时使用的用户名和密码为 nacos/nacos
4)在 MySQL 数据库里不存在 nacos_devtest 数据库,这是因为在 env 目录下的 Docker-Compose 环境配置文件中,指定了 Nacos 默认连接的 MySQL 数据库为 nacos_devtest
分析结果:各种假设经过逐一验证后,最终发现导致 Nacos 无法连接 MySQL 数据库的原因,是因为在 MySQL 数据库中,不存在 nacos 用户,同时也不存在 nacos_devtest 数据库,即 MySQL 数据库里没有被初始化过
2020-03-12 23:01:50,062 INFO Initializing ExecutorService 'taskScheduler' 2020-03-12 23:01:50,092 INFO Exposing 16 endpoint(s) beneath base path '/actuator' 2020-03-12 23:01:50,240 INFO Tomcat started on port(s): 8848 (http) with context path '/nacos' 2020-03-12 23:01:50,245 INFO Started Nacos in 12.964 seconds (JVM running for 13.92) 2020-03-12 23:01:50,246 INFO Nacos Log files: /home/nacos/logs 2020-03-12 23:01:50,247 INFO Nacos Log files: /home/nacos/conf 2020-03-12 23:01:50,247 INFO Nacos Log files: /home/nacos/data 2020-03-12 23:01:50,248 INFO Nacos started successfully in stand alone mode. use external storage 2020-03-12 23:01:55,147 INFO Initializing Spring DispatcherServlet 'dispatcherServlet' 2020-03-12 23:01:55,147 INFO Initializing Servlet 'dispatcherServlet' 2020-03-12 23:01:55,169 INFO Completed initialization in 22 ms