前言:公司数据库统一切换为oracle,减少部署mysql,现需要将Apollo的数据库做oracle适配,当前使用版本为Apollo2.0.0,网上找到最新版本的适配oracle的版本也仅为1.4.0,现决定自己适配。
部分参考了官方介绍的改造文档:https://github.com/apolloconfig/apollo/compare/v0.8.0...vanpersl:db-oracle
Apollo版本 2.0.0
Oracle版本:12c
添加俩用户
APOLLOPORTALDB 默认数据库为APOLLOPORTALDB
APOLLOCONFIGDB 默认数据库为APOLLOCONFIGDB
搜索引入mysql驱动的地方,删除后添加oracle驱动,
com.oracle.database.jdbc
ojdbc8
12.2.0.1
# DataSource
#spring.datasource.hikari.connectionInitSql=set names utf8mb4
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.Oracle12cDialect
3.1 因为oracle不加双引号默认字段和表名会转成大写,所以干脆全转成大写
3.2 oracle索引全局不能重复,但是Apollo不同表索引名有重复的,给每个表索引名称加表名前缀
3.3 oracle不支持自增,SPRING_SESSION表用到了自增,所以需要通过oracle的序列实现自增,这个官方有说明 https://github.com/spring-projects/spring-session/blob/faee8f1bdb8822a5653a81eba838dddf224d92d6/spring-session-jdbc/src/main/resources/org/springframework/session/jdbc/schema-oracle.sql
最终修改完的SQL会最后上传
因一个数据库共用一个序列,插入数据的时候可能会出现一个表自增不连续的情况,不影响
解决原先字段中包括oracle关键字问题
添加类
package com.ctrip.framework.apollo.common.utils;
import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
public class OracleUpperCaseStrategy extends PhysicalNamingStrategyStandardImpl {
private static final long serialVersionUID = 1383021413247872469L;
@Override
public Identifier toPhysicalTableName(Identifier name, JdbcEnvironment context) {
// 将表名全部转换成大写,加双引号解决字段或表名是oracle关键字
String tableName = "\"" + name.getText().toUpperCase() + "\"";
return name.toIdentifier(tableName);
}
@Override
public Identifier toPhysicalColumnName(Identifier name, JdbcEnvironment jdbcEnvironment) {
// 将字段全部转换成大写,加双引号解决字段或表名是oracle关键字
String columnName = "\"" + name.getText().toUpperCase() + "\"";
return name.toIdentifier(columnName);
}
}
添加common中application.properties配置,关联上面的转换类
spring.jpa.hibernate.naming.physical-strategy=com.ctrip.framework.apollo.common.utils.OracleUpperCaseStrategy
spring.jpa.hibernate.globally_quoted_identifiers=true
spring.jpa.properties.hibernate.globally_quoted_identifiers=true
查找JdbcUserDetailsManager,将 ` 符号全部删除。因为oracle不识别
绝大多数entity的实体类继承自BaseEntity,修改其自增ID为从序列获取
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequence")
@SequenceGenerator(name = "sequence", sequenceName = "ID_SEQ", allocationSize = 1)
@Column(name = "Id")
private long id;
其他部分entity不继承自BaseEntity,这里不一一备注,可以查看git提交记录查看或者全局搜索 @Id 手动替换
apollo-common的application.properties添加以下(仅仅方便调试,后期有遗漏地方方便再次修改)
spring.jpa.show-sql=true
#全局DEBUG
#logging.level.root=DEBUG
以上基本修改完毕,如有以后未标注的以提交的代码为准
附idea本地开发调试的vm参数
AdminServiceApplication
-Dapollo_profile=github
-Dspring.datasource.url=jdbc:oracle:thin:@192.168.80.73:1521/orcl
-Dspring.datasource.username=APOLLOCONFIGDB
-Dspring.datasource.password=APOLLOCONFIGDB
ConfigServiceApplication
-Dapollo_profile=github
-Dspring.datasource.url=jdbc:oracle:thin:@192.168.80.73:1521/orcl
-Dspring.datasource.username=APOLLOCONFIGDB
-Dspring.datasource.password=APOLLOCONFIGDB
PortalApplication
-Dapollo_profile=github,auth
-Ddev_meta=http://localhost:8080/
-Dserver.port=8070
-Dspring.datasource.url=jdbc:oracle:thin:@192.168.80.73:1521/orcl
-Dspring.datasource.username=APOLLOPORTALDB
-Dspring.datasource.password=APOLLOPORTALDB
执行\scripts下build.sh
将build好的target下zip包,按图上格式存放,执行build命令
Dockerfile文件
apollo-adminservice
FROM openjdk:8-jdk-alpine
#jdk采用openjdk8,操作系统采用alpine,减小体积
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
#修改镜像源为阿里源
ENV LANG=en_US.UTF-8
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN echo 'Asia/Shanghai' >/etc/timezone
#修改时区及默认字符集
RUN apk add --update --no-cache ttf-dejavu fontconfig busybox-extras iproute2 curl net-tools tcpdump wget bash-doc bash-completion && \
rm -rf /var/cache/apk/*
#添加常用工具及字符集字体等
ENV APOLLO_RUN_MODE=Docker
ENV SERVER_PORT=8090
EXPOSE 8090
COPY ./apollo-adminservice /apollo-adminservice
#CMD ["/bin/sh", "-c", "tail -f /dev/null"]
CMD ["/apollo-adminservice/scripts/startup.sh"]
apollo-configservice
FROM openjdk:8-jdk-alpine
#jdk采用openjdk8,操作系统采用alpine,减小体积
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
#修改镜像源为阿里源
ENV LANG=en_US.UTF-8
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN echo 'Asia/Shanghai' >/etc/timezone
#修改时区及默认字符集
RUN apk add --update --no-cache ttf-dejavu fontconfig busybox-extras iproute2 curl net-tools tcpdump wget bash-doc bash-completion && \
rm -rf /var/cache/apk/*
#添加常用工具及字符集字体等
ENV APOLLO_RUN_MODE=Docker
ENV SERVER_PORT=8080
EXPOSE 8080
COPY ./apollo-configservice /apollo-configservice
#CMD ["/bin/sh", "-c", "tail -f /dev/null"]
CMD ["/apollo-configservice/scripts/startup.sh"]
apollo-portal
FROM openjdk:8-jdk-alpine
#jdk采用openjdk8,操作系统采用alpine,减小体积
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
#修改镜像源为阿里源
ENV LANG=en_US.UTF-8
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN echo 'Asia/Shanghai' >/etc/timezone
#修改时区及默认字符集
RUN apk add --update --no-cache ttf-dejavu fontconfig busybox-extras iproute2 curl net-tools tcpdump wget bash-doc bash-completion && \
rm -rf /var/cache/apk/*
#添加常用工具及字符集字体等
ENV APOLLO_RUN_MODE=Docker
ENV SERVER_PORT=8070
EXPOSE 8070
COPY ./apollo-portal /apollo-portal
#CMD ["/bin/sh", "-c", "tail -f /dev/null"]
CMD ["/apollo-portal/scripts/startup.sh"]
运行命令
docker run -p 8090:8090 \
-e SPRING_DATASOURCE_URL="jdbc:oracle:thin:@192.168.80.73:1521/orcl" \
-e SPRING_DATASOURCE_USERNAME=APOLLOCONFIGDB -e SPRING_DATASOURCE_PASSWORD=APOLLOCONFIGDB \
-d -v /tmp/logs:/opt/logs --name apollo-adminservice apollo-adminservice-oracle:2.0.0
docker run -p 8080:8080 \
-e SPRING_DATASOURCE_URL="jdbc:oracle:thin:@192.168.80.73:1521/orcl" \
-e SPRING_DATASOURCE_USERNAME=APOLLOCONFIGDB -e SPRING_DATASOURCE_PASSWORD=APOLLOCONFIGDB \
-d -v /tmp/logs:/opt/logs --name apollo-configservice apollo-configservice-oracle:2.0.0
docker run -p 8070:8070 \
-e SPRING_DATASOURCE_URL="jdbc:oracle:thin:@192.168.80.73:1521/orcl" \
-e SPRING_DATASOURCE_USERNAME=APOLLOPORTALDB -e SPRING_DATASOURCE_PASSWORD=APOLLOPORTALDB \
-e APOLLO_PORTAL_ENVS=dev,pro \
-e DEV_META=http://192.168.1.152:8080 -e PRO_META=http://192.168.1.152:8080 \
-d -v /tmp/logs:/opt/logs --name apollo-portal apollo-portal-oracle:2.0.0
花费约2天时间修改以上
以上代码及sql均提交到github
GitHub - lililill/apollo-oracle at 2.0.0-oracle
https://github.com/apolloconfig/apollo/compare/v2.0.0...lililill:apollo-oracle:2.0.0-oracle
注意:eureka.service.url需要修改为对应的ip地址或者域名
目前测试未发现问题,如有问题可留言