• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Nacos2.1.2源码修改支持高斯,postresql

武飞扬头像
LOYURU
帮助2

1、下载代码
git clone https://github.com/alibaba/nacos.git -b 2.1.2

git clone https://github.com/alibaba/nacos.git

2、maven命令执行下试试能不能打包
mvn -Prelease-nacos -Dmaven.test.skip=true -Drat.skip=true clean install -U

mvn -Prelease-nacos ‘-Dmaven.test.skip=true’ ‘-Drat.skip=true’ clean install -U

如果com.alibaba.nacos.consistency.ProtoMessageUtil编译报错,或提示com.谷歌.protobuf:protoc不存在之类的,手动下载protoc-3.16.3-win64.zip文件,把protoc.exe改成截图名字,放到maven路径中
下载路径:
https://github.com/protocolbuffers/protobuf/releases(如果不对可以百度下,白天不稳定,晚上还行)
学新通
打包的结果在distribution中

3、添加驱动jar包
打包完成后,试试能不能启动,程序能不能注册,然后开始改代码支持高斯或postresql,本文以高斯为例

① /pom.xml
postgresql的

<properties>
···
<postgresql.version>42.3.3</postgresql.version>
···
</properties>

···
 <dependencyManagement>
 <dependencies>
 ···
<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>${postgresql.version}</version>
</dependency>
···
      </dependencies>
  </dependencyManagement>
学新通

高斯的

<properties>
···
<opengauss.version>2.0.0</opengauss.version>
···
</properties>

···
 <dependencyManagement>
 <dependencies>
 ···
<dependency>
                <groupId>org.opengauss</groupId>
                <artifactId>opengauss-jdbc</artifactId>
                <version>${opengauss.version}</version>
 </dependency>
···
      </dependencies>
  </dependencyManagement>
学新通

② /config/pom.xml
postgresql的

<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
</dependency>

高斯的

  <dependency>
            <groupId>org.opengauss</groupId>
            <artifactId>opengauss-jdbc</artifactId>
  </dependency>

③ /naming/pom.xml
postgresql的

<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
</dependency>

高斯的

        <dependency>
            <groupId>org.opengauss</groupId>
            <artifactId>opengauss-jdbc</artifactId>
        </dependency>

4、 添加PostgreSQL驱动代码
高斯的驱动代码和PostgreSQL是一模一样的,这里只写postgresql的
① PropertiesConstant.java

public static final String MYSQL = "mysql";

public static final String POSTGRESQL = "postgresql";

② PropertyUtil.java#loadSetting

学新通

③ ExternalDataSourceProperties.java

 if (PropertiesConstant.POSTGRESQL.equalsIgnoreCase(
                EnvUtil.getProperty(PropertiesConstant.SPRING_DATASOURCE_PLATFORM))) {
            driverClassName = JDBC_DRIVER_NAME_POSTGRESQL;
        }

学新通

学新通

④ StartingApplicationListener.java

    private static final String DATABASE_POSTGRESQL = "postgresql";

学新通
以下为#judgeStorageMode方法内

学新通

5、 兼容PostgreSQL 语句
① 主键

# 全局替换:
Statement.RETURN_GENERATED_KEYS 替换为 new String[]{"id"}

# 由于postgresql无法通过Statement.RETURN_GENERATED_KEYS获取主键,因此只能显示的指定要寻找的主键

学新通

学新通
学新通

学新通
② LIKE

当前2.1.2版本只有两个地方,都在/plugin-default-impl/src/main/java/com/alibaba/nacos/plugin/auth/impl/persistence下
① ExternalRolePersistServiceImpl#findRolesLikeRoleName
② ExternalUserPersistServiceImpl#findUserLikeUsername

学新通

#如上图类似替换
LIKE '%' ? '%' 替换成 LIKE ?
new String[] {username} 替换成 new String[] {String.format("%%%s%%", username)}

# LIKE '%' '' '%'的方式,mysql可用,但并非标准sql
# LIKE '%%'的方式,这是标准sql,因此mysql与postgresql都可使用

学新通
学新通
③ LIMIT

# 全局替换:
LIMIT ?, ?    替换为 OFFSET ? LIMIT ?
LIMIT ?,?     替换为 OFFSET ? LIMIT ?
LIMIT \\?,\\? 替换为 OFFSET \\? LIMIT \\?

# LIMIT ?,? 是mysql可用,但并非标准sql
# OFFSET ? LIMIT ? 是sql,因此mysql与postgresql都可使用

还有一个较为特殊
ExternalStoragePaginationHelperImpl#fetchPage

String selectSql;
if (isDerby()) {
    selectSql = sqlFetchRows   " OFFSET "   startRow   " ROWS FETCH NEXT "   pageSize   " ROWS ONLY";
} else if (lastMaxId != null) {
    // PostgreSQL support
    selectSql = sqlFetchRows   " AND id > "   lastMaxId   " ORDER BY id ASC"   " LIMIT "   pageSize   " OFFSET "   0;
} else {
    // PostgreSQL support
    selectSql = sqlFetchRows   " LIMIT "   pageSize   " OFFSET "   startRow;
}

学新通
学新通

学新通

学新通

学新通
学新通

6、配置文件
剩下的就是

application.properties
nacos-postgresql.sql
的问题了

① application.properties

spring.datasource.platform=postgresql

db.url.0=jdbc:postgresql://127.0.0.1:5432/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC
db.user.0=postgres
db.password.0=nacos

② nacos-postgresql.sql

参考文档:
nacos如何以集群模式启动

nacos添加PostgreSQL支持

制作镜像

Unknown lifecycle phase “.test.skip=true“. You must specify a valid lifecycl

解决git 中 error 10053 问题

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfkfbaj
系列文章
更多 icon
同类精品
更多 icon
继续加载