默认情况下,Nacos使用嵌入式数据库实现数据的存储。所以如果启动多个默认配置下的Nacos节点,数据存储是存在一致性问题的。为了解决这个问题,Nacos采用了集中式存储的方式来支持集群化部署,但是Nacos目前支持MySQL的存储。

创建数据库并且执行nacos的sql脚本

创建一个MySQL的database

  1. CREATE DATABASE nacos;
  2. USE nacos;

执行nacos的sql脚本,位置在Linux下的 “nacos/conf/nacos-mysql.sql”,

image.png

修改nacos配置文件持久化信息到mysql中

修改 “/nacos/conf/application.properties”

如果application.properties有下面的内容就直接修改,如果application.properties没有下面的内容就在最底下新加一下连接MySQL的配置

  1. # nacos将数据持久化到MySQL中
  2. spring.datasource.platform=mysql
  3. db.num=1
  4. db.url.0=jdbc:mysql://zjj101:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true
  5. db.user=root
  6. db.password=root

重启nacos

停止nacos命令: 在bin目录下执行: sh shutdown.sh

启动nacos命令: 在bin目录下执行: sh startup.sh -m standalone

  1. [root@zjj101 bin]# ls
  2. derby.log logs shutdown.cmd shutdown.sh startup.cmd startup.sh work
  3. [root@zjj101 bin]# sh shutdown.sh
  4. The nacosServer(78692) is running...
  5. Send shutdown request to nacosServer(78692) OK
  6. [root@zjj101 bin]# sh startup.sh -m standalone
  7. /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.302.b08-0.el7_9.x86_64/bin/java -Xms512m -Xmx512m -Xmn256m -Dnacos.standalone=true -Djava.ext.dirs=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.302.b08-0.el7_9.x86_64/jre/lib/ext:/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.302.b08-0.el7_9.x86_64/lib/ext:/root/soft/nacos/plugins/cmdb:/root/soft/nacos/plugins/mysql -Xloggc:/root/soft/nacos/logs/nacos_gc.log -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=10 -XX:GCLogFileSize=100M -Dnacos.home=/root/soft/nacos -Dloader.path=/root/soft/nacos/plugins/health -jar /root/soft/nacos/target/nacos-server.jar --spring.config.location=classpath:/,classpath:/config/,file:./,file:./config/,file:/root/soft/nacos/conf/ --logging.config=/root/soft/nacos/conf/nacos-logback.xml --server.max-http-header-size=524288
  8. nacos is starting with standalone
  9. nacos is startingyou can check the /root/soft/nacos/logs/start.out

查看启动日志

在bin目录下执行: tail -500 ../logs/nacos.log
好像也没啥明显的报错的信息

  1. [root@zjj101 bin]# tail -500 ../logs/nacos.log
  2. 2022-02-05 15:10:12,381 INFO Expired JWT token.
  3. 2022-02-05 15:10:12,385 ERROR Responding with unauthorized error. Message - Full authentication is required to access this resource
  4. 2022-02-05 15:16:28,494 INFO [capacityManagement] start correct usage
  5. 2022-02-05 15:16:28,495 INFO [capacityManagement] end correct usage, cost: 0s
  6. 2022-02-05 15:16:28,706 WARN clearConfigHistory start
  7. 2022-02-05 15:26:28,496 INFO [capacityManagement] start correct usage
  8. 2022-02-05 15:26:28,497 INFO [capacityManagement] end correct usage, cost: 0s
  9. 2022-02-05 15:26:28,708 WARN clearConfigHistory start
  10. 2022-02-05 15:35:24,098 INFO Shutting down ExecutorService 'taskScheduler'
  11. 2022-02-05 15:35:24,103 INFO Shutting down ExecutorService 'applicationTaskExecutor'
  12. 2022-02-05 15:35:57,077 INFO Starting Nacos v1.1.4 on zjj101.localdomain with PID 5262 (/root/soft/nacos/target/nacos-server.jar started by root in /root/soft/nacos/bin)
  13. 2022-02-05 15:35:57,080 INFO The following profiles are active: standalone
  14. 2022-02-05 15:35:59,045 INFO Bean 'org.springframework.security.config.annotation.configuration.ObjectPostProcessorConfiguration' of type [org.springframework.security.config.annotation.configuration.ObjectPostProcessorConfiguration$$EnhancerBySpringCGLIB$$92dcb77a] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
  15. 2022-02-05 15:35:59,142 INFO Bean 'objectPostProcessor' of type [org.springframework.security.config.annotation.configuration.AutowireBeanFactoryObjectPostProcessor] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
  16. 2022-02-05 15:35:59,145 INFO Bean 'org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler@723ca036' of type [org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
  17. 2022-02-05 15:35:59,148 INFO Bean 'org.springframework.security.config.annotation.method.configuration.GlobalMethodSecurityConfiguration' of type [org.springframework.security.config.annotation.method.configuration.GlobalMethodSecurityConfiguration$$EnhancerBySpringCGLIB$$b7b15a2c] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
  18. 2022-02-05 15:35:59,153 INFO Bean 'methodSecurityMetadataSource' of type [org.springframework.security.access.method.DelegatingMethodSecurityMetadataSource] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
  19. 2022-02-05 15:35:59,709 INFO Tomcat initialized with port(s): 8848 (http)
  20. 2022-02-05 15:35:59,739 INFO Starting service [Tomcat]
  21. 2022-02-05 15:35:59,739 INFO Starting Servlet Engine: Apache Tomcat/9.0.13
  22. 2022-02-05 15:35:59,752 INFO The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib]
  23. 2022-02-05 15:35:59,844 INFO Initializing Spring embedded WebApplicationContext
  24. 2022-02-05 15:35:59,844 INFO Root WebApplicationContext: initialization completed in 2712 ms
  25. 2022-02-05 15:36:00,607 INFO Use Mysql as the driver
  26. 2022-02-05 15:36:02,075 INFO Initializing ExecutorService 'applicationTaskExecutor'
  27. 2022-02-05 15:36:02,264 INFO Adding welcome page: class path resource [static/index.html]
  28. 2022-02-05 15:36:02,628 INFO Creating filter chain: Ant [pattern='/'], []
  29. 2022-02-05 15:36:02,628 INFO Creating filter chain: Ant [pattern='/**/*.css'], []
  30. 2022-02-05 15:36:02,628 INFO Creating filter chain: Ant [pattern='/**/*.js'], []
  31. 2022-02-05 15:36:02,628 INFO Creating filter chain: Ant [pattern='/**/*.html'], []
  32. 2022-02-05 15:36:02,629 INFO Creating filter chain: Ant [pattern='/**/*.map'], []
  33. 2022-02-05 15:36:02,629 INFO Creating filter chain: Ant [pattern='/**/*.svg'], []
  34. 2022-02-05 15:36:02,629 INFO Creating filter chain: Ant [pattern='/**/*.png'], []
  35. 2022-02-05 15:36:02,629 INFO Creating filter chain: Ant [pattern='/**/*.ico'], []
  36. 2022-02-05 15:36:02,629 INFO Creating filter chain: Ant [pattern='/console-fe/public/**'], []
  37. 2022-02-05 15:36:02,629 INFO Creating filter chain: Ant [pattern='/v1/auth/login'], []
  38. 2022-02-05 15:36:02,629 INFO Creating filter chain: Ant [pattern='/v1/console/health/**'], []
  39. 2022-02-05 15:36:02,629 INFO Creating filter chain: Ant [pattern='/v1/cs/**'], []
  40. 2022-02-05 15:36:02,629 INFO Creating filter chain: Ant [pattern='/v1/ns/**'], []
  41. 2022-02-05 15:36:02,629 INFO Creating filter chain: Ant [pattern='/v1/cmdb/**'], []
  42. 2022-02-05 15:36:02,629 INFO Creating filter chain: Ant [pattern='/actuator/**'], []
  43. 2022-02-05 15:36:02,629 INFO Creating filter chain: Ant [pattern='/v1/console/server/**'], []
  44. 2022-02-05 15:36:02,663 INFO Creating filter chain: any request, [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@72ea6193, org.springframework.security.web.context.SecurityContextPersistenceFilter@2a7686a7, org.springframework.security.web.header.HeaderWriterFilter@1d0d6318, org.springframework.security.web.authentication.logout.LogoutFilter@246f8b8b, com.alibaba.nacos.console.filter.JwtAuthenticationTokenFilter@31aa3ca5, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@7ec3394b, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@312ab28e, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@45905bff, org.springframework.security.web.session.SessionManagementFilter@4409e975, org.springframework.security.web.access.ExceptionTranslationFilter@f001896, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@55342f40]
  45. 2022-02-05 15:36:02,763 INFO Exposing 2 endpoint(s) beneath base path '/actuator'
  46. 2022-02-05 15:36:02,799 INFO Initializing ExecutorService 'taskScheduler'
  47. 2022-02-05 15:36:02,934 INFO Tomcat started on port(s): 8848 (http) with context path '/nacos'
  48. 2022-02-05 15:36:02,940 INFO Started Nacos in 7.98 seconds (JVM running for 8.819)
  49. 2022-02-05 15:36:02,940 INFO Nacos Log files: /root/soft/nacos/logs/
  50. 2022-02-05 15:36:02,940 INFO Nacos Conf files: /root/soft/nacos/conf/
  51. 2022-02-05 15:36:02,940 INFO Nacos Data files: /root/soft/nacos/data/
  52. 2022-02-05 15:36:02,940 INFO Nacos started successfully in stand alone mode.
  53. 2022-02-05 15:36:04,394 INFO Initializing Spring DispatcherServlet 'dispatcherServlet'
  54. 2022-02-05 15:36:04,394 INFO Initializing Servlet 'dispatcherServlet'
  55. 2022-02-05 15:36:04,554 INFO Completed initialization in 160 ms

此时再次访问nacos

发现配置列表没有配置了,因为nacos把数据持久化到mysql里面,需要重新编写配置文件,并且修改bootstrap.proerties的命名空间id