欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

解决elastic-job-ui在使用druid作为数据库连接池时作业维度报错问题,

来源: javaer 分享于  点击 11264 次 点评:123

解决elastic-job-ui在使用druid作为数据库连接池时作业维度报错问题,


目录
  • 问题说明
  • 解决
  • 实操
    • 1、引入依赖和配置
    • 2、打包
    • 3、访问
  • 总结

    问题说明

    我们项目中使用到了elastic-job,然后自己封装了个sdk,

    方便使用,里面的数据源配置是常用的druid+mysql的组合,在操作中,发现elastic-job-ui可视化控制台会报错无法使用。

    深究其原因是因为,各个服务把定时任务注册到了zk中,包括数据库配置类的一些信息,

    但是elastic-job-ui源码中没有引入对应的pom依赖,

    导致他在去zk获取了定时任务的配置类信息后,需要想这些信息转换成对应的类对象操作时,没法成功转换。

    解决

    处理也很简单,

    一种是项目中包装的sdk不使用druid连接池即可,可以使用HikariCP,实测是没问题

    另一种更简单,下载elastic-job-ui源码,在pom依赖中引入druid依赖接口(我们用这个,省的改项目代码,引用的地方太多了)

    实操

    下载源码,使用的示最新的3.0.2版本

    https://github.com/apache/shardingsphere-elasticjob-ui/tree/3.0.2

    1、引入依赖和配置

    引入druid依赖,顺便mysql的也引入和配置下application,省的我们手动配置mysql依赖文件了

    另外他这个build也调整下,不然你直接对他打包才几兆,不完整

    <?xml version="1.0" encoding="UTF-8"?>
    <!--
      ~ Licensed to the Apache Software Foundation (ASF) under one or more
      ~ contributor license agreements.  See the NOTICE file distributed with
      ~ this work for additional information regarding copyright ownership.
      ~ The ASF licenses this file to You under the Apache License, Version 2.0
      ~ (the "License"); you may not use this file except in compliance with
      ~ the License.  You may obtain a copy of the License at
      ~
      ~     http://www.apache.org/licenses/LICENSE-2.0
      ~
      ~ Unless required by applicable law or agreed to in writing, software
      ~ distributed under the License is distributed on an "AS IS" BASIS,
      ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      ~ See the License for the specific language governing permissions and
      ~ limitations under the License.
      -->
    
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.apache.shardingsphere</groupId>
        <artifactId>shardingsphere-elasticjob-lite-ui</artifactId>
        <version>3.0.2</version>
    </parent>
    <artifactId>shardingsphere-elasticjob-lite-ui-backend</artifactId>
    <name>${project.artifactId}</name>
    
    <dependencies>
        <dependency>
            <groupId>org.apache.shardingsphere.elasticjob</groupId>
            <artifactId>elasticjob-lite-lifecycle</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.openjpa</groupId>
            <artifactId>openjpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-dbcp2</artifactId>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.activation</groupId>
            <artifactId>javax.activation-api</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.13</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.22</version>
        </dependency>
    
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-core</artifactId>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-impl</artifactId>
        </dependency>
        <dependency>
            <groupId>com.auth0</groupId>
            <artifactId>java-jwt</artifactId>
        </dependency>
    </dependencies>
    
    <build>
        <finalName>shardingsphere-elasticjob-lite-ui</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <configuration>
                    <encoding>${project.build.sourceEncoding}</encoding>
                    <outputDirectory>${project.build.outputDirectory}</outputDirectory>
                    <resources>
                        <resource>
                            <targetPath>${project.build.directory}/classes/public</targetPath>
                            <directory>${project.parent.basedir}/shardingsphere-elasticjob-lite-ui-frontend/dist</directory>
                        </resource>
                        <resource>
                            <targetPath>${project.build.directory}/classes</targetPath>
                            <directory>src/main/resources</directory>
                        </resource>
                    </resources>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring-boot.version}</version>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    

    application.properties文件直接加入mysql配置选择

    ## Uncomment the following property to allow adding DataSource dynamically.
    dynamic.datasource.allowed-driver-classes={'org.h2.Driver','org.postgresql.Driver','com.mysql.cj.jdbc.Driver'}
    

    2、打包

    直接用这个打好的jar包,直接运行即可,jar里面打包包含了前端页面

    3、访问

    java -jar运行完这个jar包后,直接访问 http://127.0.0.1:8088/#/ 即可看到可视化控制台

    账号密码都是默认的root

    总结

    以上为个人经验,希望能给大家一个参考,也希望大家多多支持3672js教程。

    您可能感兴趣的文章:
    • Druid连接池未关闭导致内存泄漏问题
    • Druid连接池的自定义过滤功能实现方法
    • druid连接池的参数配置示例全面解析
    • java连接池Druid连接回收DestroyConnectionThread&DestroyTask
    • java连接池Druid获取连接getConnection示例详解
    相关栏目:

    用户点评