Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加了对postgresql数据库的支持 #1052

Closed
wants to merge 3 commits into from

Conversation

nextll
Copy link

@nextll nextll commented Aug 6, 2019

你好,我们公司使用的是postgrsql数据库。由于xxl_job只支持mysql,在生产环境再搭建一套mysql增加了运维的工作量。因此我fork了XXL_JOB的代码,并增加了对PG的支持。

感谢XXL-JOB团队提供了非常便捷实用的定时任务系统,由于项目需要我会一直使用PG数据库,因此如果后续我使用时发现BUG会继续pull request消除BUG。

主要修改点如下:

  1. 在xxl-job-admin子工程的resources目录下,增加了新的mybatis-mapper-postgresql目录。目录中mapper文件的SQL,用PG的SQL语法、函数做了调增。
  2. 在doc目录中,增加了xxl_job_postgresql.sql数据库初始化文件。同mysql的初始化语句相比,除了建表语句外,还增加了序列创建(PG的主键自增需要创建序列,和oracle类似)、触发器创建(PG不支持 default on update,因此udpate时update_time列更新,只能靠触发器)。

将程序切换到PG,只需要做如下配置调整:

  1. 数据库初始化时,使用“doc/db/xxl_job_postgresql.sql”文件。
  2. 修改application.properties配置文件,修改位置如下所示:

### mybatis
# mysql
mybatis.mapper-locations=classpath:/mybatis-mapper/*Mapper.xml
# postgresql  ## 使用PG数据库时,请注释上一行mysql配置,打开下行的PG配置
#mybatis.mapper-locations=classpath:/mybatis-mapper-postgresql/*Mapper.xml

### xxl-job, datasource
# mysql configuration
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/xxl_job?Unicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=root_pwd
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

# postgresql configuration   ## 使用PG数据库时,请注释前四行的mysql配置,打开下面四行的PG配置
#spring.datasource.url=jdbc:postgresql://10.10.10.231:5432/postgres?currentSchema=xxl_job
#spring.datasource.username=postgres
#spring.datasource.password=123456
#spring.datasource.driver-class-name=org.postgresql.Driver

@nextll
Copy link
Author

nextll commented Aug 18, 2019

老大,这个PR过还是不过啊?

1 similar comment
@miRUAcleInjoker
Copy link

老大,这个PR过还是不过啊?

@power-man
Copy link

有点问题,报类型转换的错,有小部分代码需要修改驼峰映射。

@jssngz
Copy link

jssngz commented Sep 3, 2019

完美,我正准备切换到postgresql呢.谢谢@nextll

@nextll
Copy link
Author

nextll commented Sep 3, 2019

有点问题,报类型转换的错,有小部分代码需要修改驼峰映射。

你好,能把错误贴出来吗?谢谢

@jssngz
Copy link

jssngz commented Sep 3, 2019

List<Map<String, Object>> triggerCountMapAll = xxlJobLogDao.triggerCountByDay(startDate, endDate); 这一行有个bug。
postgres sql应该是大小写不敏感,全部给转化为小写了。
mysql 却是大小写敏感的
我在sql哪里全部改成小写下划线分割就好了。
XxlJobLogMapper.xml中triggerCountByDay sql修改为
<select id="triggerCountByDay" resultType="java.util.Map" > SELECT to_char(trigger_time,'yyyy-mm-dd') as trigger_day, COUNT(handle_code) as trigger_day_count, SUM(CASE WHEN (trigger_code in (0, 200) and handle_code = 0) then 1 else 0 end) as trigger_day_count_running, SUM(CASE WHEN handle_code = 200 then 1 else 0 end) as trigger_day_count_suc FROM xxl_job_log WHERE trigger_time BETWEEN #{from} and #{to} GROUP BY trigger_day ORDER BY trigger_day </select>
XxlJobServiceImpl.chartInfo方法中修改为
String day = String.valueOf(item.get("trigger_day")); int triggerDayCount = Integer.valueOf(String.valueOf(item.get("trigger_day_count"))); int triggerDayCountRunning = Integer.valueOf(String.valueOf(item.get("trigger_day_count_running"))); int triggerDayCountSuc = Integer.valueOf(String.valueOf(item.get("trigger_day_count_suc"))); int triggerDayCountFail = triggerDayCount - triggerDayCountRunning - triggerDayCountSuc;

@nextll
Copy link
Author

nextll commented Sep 3, 2019

谢谢,看到了。

List<Map<String, Object>> triggerCountMapAll = xxlJobLogDao.triggerCountByDay(startDate, endDate); 这一行有个bug。
postgres sql应该是大小写不敏感,全部给转化为小写了。
mysql 却是大小写敏感的

谢谢,看到了。我这边处理一下。

-------问题已经修复------

…默认都是小写,增加双引号来规定返回列名的大小写
@nextll
Copy link
Author

nextll commented Sep 3, 2019

List<Map<String, Object>> triggerCountMapAll = xxlJobLogDao.triggerCountByDay(startDate, endDate); 这一行有个bug。
postgres sql应该是大小写不敏感,全部给转化为小写了。
mysql 却是大小写敏感的
我在sql哪里全部改成小写下划线分割就好了。
XxlJobLogMapper.xml中triggerCountByDay sql修改为
<select id="triggerCountByDay" resultType="java.util.Map" > SELECT to_char(trigger_time,'yyyy-mm-dd') as trigger_day, COUNT(handle_code) as trigger_day_count, SUM(CASE WHEN (trigger_code in (0, 200) and handle_code = 0) then 1 else 0 end) as trigger_day_count_running, SUM(CASE WHEN handle_code = 200 then 1 else 0 end) as trigger_day_count_suc FROM xxl_job_log WHERE trigger_time BETWEEN #{from} and #{to} GROUP BY trigger_day ORDER BY trigger_day </select>
XxlJobServiceImpl.chartInfo方法中修改为
String day = String.valueOf(item.get("trigger_day")); int triggerDayCount = Integer.valueOf(String.valueOf(item.get("trigger_day_count"))); int triggerDayCountRunning = Integer.valueOf(String.valueOf(item.get("trigger_day_count_running"))); int triggerDayCountSuc = Integer.valueOf(String.valueOf(item.get("trigger_day_count_suc"))); int triggerDayCountFail = triggerDayCount - triggerDayCountRunning - triggerDayCountSuc;

hi,不用这么麻烦,看我修改的代码,只要在SQL中,对返回字段加双引号就可以了,这样Java代码还是PG、mysql通吃的。详细看commit.

@xuxueli
Copy link
Owner

xuxueli commented Oct 21, 2019

你好,为了控制维护成本,主分支以Mysql为主进行迭代。
各分支版本可以单独建立仓库,在这里分享:#154

@xuxueli xuxueli closed this Oct 21, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants