Java引入docker-api(三),进阶操作之xxl-job创建

关联文章链接:

准备

  • 由于版本之间有差异,每个版本去官网下载一份properties文件模板存储到resource的xxljob/properties文件夹下。
  • 准备要操作的数据库连接参数

ps: 一般来说,如果将xxljob服务化,那么创建一个xxljob都要给新建一个数据库单独使用,通过有权限的数据库账号创建好数据库,然后运行官方指定版本的sql文件,然后将数据库地址,数据库名称,账号,密码,作为参数传递到方法内即可。因为关联性不大,下面的演示没有创建数据库的代码,如果需要评论区告诉我,我把代码粘贴过来。

还是直接上代码,包含具体注释,如果有不理解的评论区讨论

//获取客户端在引入docker-api(一)中有相关代码,这里不多余做粘贴
DockerClient client = dockerConfig.getClient();

String containerName = "xxljob-" + UUID.fastUUID().toString(true).substring(7);
//修改配置文件数据库参数 这里指定版本2.3.0 这里是封装了一工具类读取打jar包后resource下的文件
File file = JarFileUtil.file("xxljob/properties/application-2.3.0.properties");
String readUtf8String = FileUtil.readUtf8String(file.getPath());

String database = "数据库名";
//这里我将官网下载的文件做了占位符替换,具体内容粘贴在下面
readUtf8String = StrUtil.format(readUtf8String, "数据库连接地址例;localhost:3306", database, "账号密码", "账号密码");
//挂载的宿主机配置文件地址
String confPath = "/Users/tomcat/xxljob/application-2.3.0.properties";
//写入到文件中
FileUtil.writeString(readUtf8String, confPath, StandardCharsets.UTF_8);
//hostConfig对象创建,并设置始终重启策略
HostConfig hostConfig = HostConfig.newHostConfig().withRestartPolicy(RestartPolicy.alwaysRestart());

//端口绑定  宿主机端口 -> 容器端口
hostConfig.withPortBindings(new PortBinding(Ports.Binding.bindPort(8081), ExposedPort.tcp(8080)));
//绑定挂载 容器内的目录地址
Volume logPath = "/data/applogs";
Volume conf = "/application.properties";
hostConfig.withBinds(new Bind(confPath, conf), new Bind("/Users/tomcat/xxljob/applogs", logPath));
//内存配额 单位为Byte 这里举例为2GB
hostConfig.withMemory(2 * 1024 * 1024 * 1024L);

//创建容器
CreateContainerCmd containerCmd = client
        .createContainerCmd("xuxueli/xxl-job-admin:2.3.0")
        .withName(containerName)
        //内部暴露端口
        .withExposedPorts(ExposedPort.tcp(8080))
        .withHostConfig(hostConfig);
CreateContainerResponse result = null;
try {
    result = containerCmd.exec();
} catch (ConflictException e) {
    if (e.getHttpStatus() == 404) {
        throw ServiceExceptionUtil.fail(DockerErrorCodeEnum.IMAGE_NOT_FOUND);
    }
    if (e.getHttpStatus() == 409) {
        throw ServiceExceptionUtil.fail(DockerErrorCodeEnum.EXIST);
    }
} catch (Exception e) {
    ServiceException exception = ServiceExceptionUtil.fail(DockerErrorCodeEnum.CREATE_FAIL);
    exception.setTip(e.getMessage());
    throw exception;
}

这是官方2.3.0版本的配置文件,仅仅将数据库地址,数据库名,账号,密码做了占位符替换

### web
server.port=8080
server.servlet.context-path=/xxl-job-admin

### actuator
management.server.servlet.context-path=/actuator
management.health.mail.enabled=false

### resources
spring.mvc.servlet.load-on-startup=0
spring.mvc.static-path-pattern=/static/**
spring.resources.static-locations=classpath:/static/

### freemarker
spring.freemarker.templateLoaderPath=classpath:/templates/
spring.freemarker.suffix=.ftl
spring.freemarker.charset=UTF-8
spring.freemarker.request-context-attribute=request
spring.freemarker.settings.number_format=0.##########

### mybatis
mybatis.mapper-locations=classpath:/mybatis-mapper/*Mapper.xml
#mybatis.type-aliases-package=com.xxl.job.admin.core.model

### xxl-job, datasource
spring.datasource.url=jdbc:mysql://{}/{}?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&useSSL=false&serverTimezone=Asia/Shanghai
spring.datasource.username={}
spring.datasource.password={}
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

### datasource-pool
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.hikari.minimum-idle=10
spring.datasource.hikari.maximum-pool-size=30
spring.datasource.hikari.auto-commit=true
spring.datasource.hikari.idle-timeout=30000
spring.datasource.hikari.pool-name=HikariCP
spring.datasource.hikari.max-lifetime=900000
spring.datasource.hikari.connection-timeout=10000
spring.datasource.hikari.connection-test-query=SELECT 1
spring.datasource.hikari.validation-timeout=1000

### xxl-job, email
spring.mail.host=smtp.qq.com
spring.mail.port=25
spring.mail.username=xxx@qq.com
spring.mail.from=xxx@qq.com
spring.mail.password=xxx
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory

### xxl-job, access token
xxl.job.accessToken=

### xxl-job, i18n (default is zh_CN, and you can choose "zh_CN", "zh_TC" and "en")
xxl.job.i18n=zh_CN

## xxl-job, triggerpool max size
xxl.job.triggerpool.fast.max=200
xxl.job.triggerpool.slow.max=100

### xxl-job, log retention days
xxl.job.logretentiondays=30


#spring.config.location=/application.properties

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值