nacos切换mysql8.0数据库
一、应用环境
Nacos:1.2.0
MySQL:8.0.16
二、Nacos安装配置
1、解压(默认端口8848),
2、\conf\application.properties:
最上方增加server.contextPath=/nacos
配置MySQL数据库连接
3、导入数据库脚本
4、默认不支持mysql8:替换\target\nacos-server.jar
5、启动\bin\startup.cmd
6、启动成功后窗口显示Nacos版本、模式、端口、进程号、访问地址、官网信息,进入
http://localhost:8848/nacos/index.html
默认账户nacos/nacos
7、建立命名空间
点击左侧菜单“命名空间”、右上角“新建命名空间”按钮,创建命名空间
命名空间信息:查找数据库表tenant_info,tenant_id(命名空间ID)、tenant_name(命名空间名称)、tenant_desc(命名空间描述)
三、nacos-server.jar
1、nacos1.2.0默认只支持MySQL5,不支持MySQL8,所以需要修改源代码自己编译
2、源代码地址:https://github.com/alibaba/nacos.git
3、修改代码:
(1)pom.xml
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.34</version>
</dependency>
改为
<dependency>
<groupId>org.apache.commmons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.16</version>
</dependency>
(2)api/pom.xml、client/pom.xml、common/pom.xml
<configuration>
<source>6</source>
<target>6</target>
</configuration>
改为
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
(3)config/pom.xml
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
</dependency>
改为
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
</dependency>
(4)com.alibaba.nacos.config.server.service.BaseDataSourceServiceImpl.java、com.alibaba.nacos.config.server.service.LocalDataSourceServiceImpl.java
import org.apache.commons.dbcp.BasicDataSource; ds.setMaxActive ds.setMaxWait
改为
import org.apache.commons.dbcp2.BasicDataSource; ds.setMaxTotal ds.setMaxWaitMillis
(5)console/src/main/resources/META-INFO/nacos-default.properties
db.url.0=jdbc:mysql://11.162.196.161:3306/diamond_devtest?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true
db.url.1=jdbc:mysql://11.163.152.91:3306/diamond_devtest?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true
改为
db.url.0=jdbc:mysql://11.162.196.161:3306/diamond_devtest?serverTimezone=GMT%2B8characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true
db.url.1=jdbc:mysql://11.163.152.91:3306/diamond_devtest?serverTimezone=GMT%2B8characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true
(6)com.alibaba.nacos.naming.healthcheck.MysqlHealthCheckProcessor.java
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
改为
import com.mysql.cj.jdbc.MysqlDataSource;
4、编译打包
根目录下的BUILDING文件有详细说明。
mvn -Prelease-nacos -Dmaven.test.skip=true clean install -U
编译成功后,从distribution/target/nacos-server-1.2.0/nacos/target文件夹拷取目标文件nacos-server.jar
四、常见问题
1、如果在Eclipse中修改完上述代码后仍有如下编译错误,错误代码:
The type 'XXX' is not API...
修改Windows->Preferences->Java->Compiler->Errors/Warning->Deprecated And restricted API->Forbidden reference选项为Warning
2、如果项目启动时报错如下
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured. Reason: Failed to determine a suitable driver class Action: Consider the following: If you want an embedded database (H2, HSQL or Derby), please put it on the classpath. If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).
网上常见的解决方案:
(1)启动类XXXApplication加上注解:@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})
(2)修改pom文件在build节中加入resource配置,确保build时加载到配置文件
我的情况及解决方案:
(1)项目不是刚创建的,而是在其他人开发的,所以认为已经过测试,代码没有问题
(2)从错误代码分析,是数据库驱动类报错,项目此时未连接数据库,所以判断是nacos连接数据库错误
(3)排队配置的环境变量未生效,已重启所有cmd窗口、eclipse环境,确保环境变量正确加载
(4)排除应用启动顺序错误问题
(5)查验数据库版本一致性,最终确认,开发用的数据库为8.0.12,而我的数据库是8.0.16,所以需要重新打包nacos-server.jar
原文链接:https://blog.csdn.net/mch111666/article/details/105159213/