Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
mica
mica
提交
597c24dc
mica
项目概览
mica
/
mica
通知
10
Star
2
Fork
1
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
mica
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
597c24dc
编写于
4月 30, 2020
作者:
如梦技术
🐛
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
✨
开源 mica 2.0 mica-jobs 模块.
上级
63e7172a
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
271 addition
and
1 deletion
+271
-1
build.gradle
build.gradle
+1
-0
mica-bom/build.gradle
mica-bom/build.gradle
+1
-0
mica-jobs/README.md
mica-jobs/README.md
+34
-0
mica-jobs/build.gradle
mica-jobs/build.gradle
+7
-0
mica-jobs/src/main/java/net/dreamlu/mica/jobs/config/XxlJobClientAutoConfiguration.java
...eamlu/mica/jobs/config/XxlJobClientAutoConfiguration.java
+128
-0
mica-jobs/src/main/java/net/dreamlu/mica/jobs/properties/XxlJobClientProperties.java
.../dreamlu/mica/jobs/properties/XxlJobClientProperties.java
+99
-0
settings.gradle
settings.gradle
+1
-1
未找到文件。
build.gradle
浏览文件 @
597c24dc
...
...
@@ -7,6 +7,7 @@ ext {
swaggerAnnotationsVersion
=
"1.5.22"
okhttpVersion
=
"3.14.4"
jsoupVersion
=
"1.13.1"
xxlJobVersion
=
"2.2.0"
lombokVersion
=
"1.18.12"
findbugsVersion
=
"3.0.2"
}
...
...
mica-bom/build.gradle
浏览文件 @
597c24dc
...
...
@@ -21,6 +21,7 @@ dependencyManagement {
dependency
"net.dreamlu:mica-laytpl:${VERSION}"
dependency
"net.dreamlu:mica-swagger:${VERSION}"
dependency
"net.dreamlu:mica-captcha:${VERSION}"
dependency
"net.dreamlu:mica-jobs:${VERSION}"
// commons
dependency
"com.google.code.findbugs:jsr305:${findbugsVersion}"
dependency
"io.swagger:swagger-annotations:${swaggerAnnotationsVersion}"
...
...
mica-jobs/README.md
0 → 100644
浏览文件 @
597c24dc
# xxl-job stater
xxl-job 官方地址:https://github.com/xuxueli/xxl-job
xxl-job 官方文档:https://www.xuxueli.com/xxl-job/
## 添加依赖
### maven
```
xml
<dependency>
<groupId>
net.dreamlu
</groupId>
<artifactId>
mica-jobs
</artifactId>
<version>
${version}
</version>
</dependency>
```
### gradle
```
groovy
compile
(
"net.dreamlu:mica-jobs:${version}"
)
```
## 配置
| 配置项 | 默认值 | 说明 |
| ----- | ------ | ------ |
| xxl.job.enabled | true | 是否启用分布式调度任务,默认:开启 |
| xxl.job.admin.address | | 调度中心地址,如调度中心集群部署存在多个地址则用逗号分隔。执行器将会使用该地址进行"执行器心跳注册"和"任务结果回调";为空则关闭自动注册;支持配置,{@code lb:// + ${service_name}} 从注册中心动态获取地址 |
| xxl.job.admin.access-token | | 与调度中心交互的accessToken,非空时启用 |
| xxl.job.admin.context-path | | job admin 的 context-path |
| xxl.job.executor.app-name | | 执行器名称,执行器心跳注册分组依据;为空则关闭自动注册 |
| xxl.job.executor.ip | | 执行器 IP,默认为空表示自动获取IP,多网卡时可手动设置指定IP,该IP不会绑定Host仅作为通讯实用;地址信息用于 "执行器注册" 和 "调度中心请求并触发任务" |
| xxl.job.executor.log-path | | 执行器日志位置 |
| xxl.job.executor.log-retention-days | -1 | 执行器日志保留天数,默认值:-1,值大于3时生效,启用执行器Log文件定期清理功能,否则不生效 |
| xxl.job.executor.port | -1 | 执行器端口,小于等于0则自动获取;默认端口为9999,单机部署多个执行器时,注意要配置不同执行器端口 |
mica-jobs/build.gradle
0 → 100644
浏览文件 @
597c24dc
dependencies
{
api
"com.xuxueli:xxl-job-core:${xxlJobVersion}"
api
"org.springframework.cloud:spring-cloud-commons"
implementation
"org.springframework.boot:spring-boot-autoconfigure"
compileOnly
"org.springframework.cloud:spring-cloud-context"
annotationProcessor
"net.dreamlu:mica-auto:${micaAutoVersion}"
}
mica-jobs/src/main/java/net/dreamlu/mica/jobs/config/XxlJobClientAutoConfiguration.java
0 → 100644
浏览文件 @
597c24dc
/*
* Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & www.dreamlu.net).
* <p>
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* 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.
*/
package
net.dreamlu.mica.jobs.config
;
import
com.xxl.job.core.executor.impl.XxlJobSpringExecutor
;
import
lombok.extern.slf4j.Slf4j
;
import
net.dreamlu.mica.jobs.properties.XxlJobClientProperties
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
;
import
org.springframework.boot.context.properties.EnableConfigurationProperties
;
import
org.springframework.cloud.client.ServiceInstance
;
import
org.springframework.cloud.client.discovery.DiscoveryClient
;
import
org.springframework.cloud.commons.util.InetUtils
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.core.env.Environment
;
import
org.springframework.util.Assert
;
import
org.springframework.util.StringUtils
;
import
java.net.URI
;
import
java.util.Optional
;
import
java.util.stream.Collectors
;
import
java.util.stream.Stream
;
import
static
net
.
dreamlu
.
mica
.
jobs
.
properties
.
XxlJobClientProperties
.
XxlJobAdminProps
;
import
static
net
.
dreamlu
.
mica
.
jobs
.
properties
.
XxlJobClientProperties
.
XxlJobExecutorProps
;
/**
* xxl-job client config
*
* @author L.cm
*/
@Slf4j
@Configuration
@EnableConfigurationProperties
(
XxlJobClientProperties
.
class
)
public
class
XxlJobClientAutoConfiguration
{
private
static
final
String
LB_PREFIX
=
"lb://"
;
@Bean
@ConditionalOnProperty
(
prefix
=
"xxl.job"
,
value
=
"enabled"
,
havingValue
=
"true"
)
public
XxlJobSpringExecutor
xxlJobExecutor
(
XxlJobClientProperties
properties
,
DiscoveryClient
discoveryClient
,
Environment
environment
,
InetUtils
inetUtils
)
{
log
.
info
(
">>>>>>>>>>> xxl-job client config init."
);
XxlJobSpringExecutor
xxlJobSpringExecutor
=
new
XxlJobSpringExecutor
();
XxlJobAdminProps
admin
=
properties
.
getAdmin
();
xxlJobSpringExecutor
.
setAccessToken
(
admin
.
getAccessToken
());
// 获取 admin 管理端的地址
xxlJobSpringExecutor
.
setAdminAddresses
(
getAdminServiceUrl
(
discoveryClient
,
admin
));
XxlJobExecutorProps
executor
=
properties
.
getExecutor
();
// 微服务 服务名
String
serviceId
=
getAppName
(
environment
);
xxlJobSpringExecutor
.
setAppname
(
getExecutorName
(
executor
,
serviceId
));
String
ipAddress
=
executor
.
getIp
();
if
(
StringUtils
.
isEmpty
(
ipAddress
))
{
ipAddress
=
inetUtils
.
findFirstNonLoopbackHostInfo
().
getIpAddress
();
}
xxlJobSpringExecutor
.
setIp
(
ipAddress
);
xxlJobSpringExecutor
.
setPort
(
executor
.
getPort
());
xxlJobSpringExecutor
.
setLogPath
(
getLogPath
(
executor
,
environment
,
serviceId
));
xxlJobSpringExecutor
.
setLogRetentionDays
(
executor
.
getLogRetentionDays
());
return
xxlJobSpringExecutor
;
}
private
static
String
getAdminServiceUrl
(
DiscoveryClient
discoveryClient
,
XxlJobAdminProps
admin
)
{
String
addresses
=
admin
.
getAddress
();
Assert
.
hasText
(
addresses
,
"xxl-job admin addresses is empty."
);
// 处理 context-path,trim,补充前缀
String
contextPath
=
Optional
.
ofNullable
(
admin
.
getContextPath
())
.
map
(
String:
:
trim
)
.
map
(
path
->
path
.
startsWith
(
"/"
)
?
path
:
"/"
+
path
)
.
orElse
(
""
);
return
StringUtils
.
commaDelimitedListToSet
(
addresses
).
stream
()
.
flatMap
(
address
->
{
// lb://xxx
if
(
address
.
startsWith
(
LB_PREFIX
))
{
return
discoveryClient
.
getInstances
(
address
.
substring
(
LB_PREFIX
.
length
()))
.
stream
()
.
map
(
ServiceInstance:
:
getUri
)
.
map
(
URI:
:
toString
);
}
// ip:port,补充 http:// 前缀
if
(!
address
.
startsWith
(
"http"
))
{
address
=
"http://"
+
address
;
}
// 其他 protocol://ip:port
return
Stream
.
of
(
address
);
}).
map
(
url
->
url
+
contextPath
)
.
collect
(
Collectors
.
joining
(
","
));
}
private
static
String
getExecutorName
(
XxlJobExecutorProps
executor
,
String
serviceId
)
{
String
appName
=
executor
.
getAppName
();
if
(
StringUtils
.
hasText
(
appName
))
{
return
appName
;
}
return
serviceId
;
}
private
static
String
getLogPath
(
XxlJobExecutorProps
executor
,
Environment
environment
,
String
serviceId
)
{
String
logPath
=
executor
.
getLogPath
();
if
(!
StringUtils
.
hasText
(
logPath
))
{
logPath
=
environment
.
getProperty
(
"LOGGING_PATH"
,
"logs"
)
.
concat
(
"/"
).
concat
(
serviceId
).
concat
(
"/jobs"
);
}
return
logPath
;
}
private
static
String
getAppName
(
Environment
environment
)
{
return
environment
.
getProperty
(
"spring.application.name"
,
""
);
}
}
mica-jobs/src/main/java/net/dreamlu/mica/jobs/properties/XxlJobClientProperties.java
0 → 100644
浏览文件 @
597c24dc
/*
* Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & www.dreamlu.net).
* <p>
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* 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.
*/
package
net.dreamlu.mica.jobs.properties
;
import
lombok.Getter
;
import
lombok.Setter
;
import
org.springframework.boot.context.properties.ConfigurationProperties
;
import
org.springframework.cloud.context.config.annotation.RefreshScope
;
/**
* <p>
* xxl-job 配置类
* </p>
*
* @author yangkai.shen
* @author L.cm
*/
@Getter
@Setter
@RefreshScope
@ConfigurationProperties
(
prefix
=
"xxl.job"
)
public
class
XxlJobClientProperties
{
/**
* 是否启用分布式调度任务,默认:开启
*/
private
boolean
enabled
=
true
;
/**
* 调度中心配置
*/
private
XxlJobAdminProps
admin
=
new
XxlJobAdminProps
();
/**
* 执行器配置
*/
private
XxlJobExecutorProps
executor
=
new
XxlJobExecutorProps
();
@Getter
@Setter
public
static
class
XxlJobAdminProps
{
/**
* 调度中心地址,如调度中心集群部署存在多个地址则用逗号分隔。执行器将会使用该地址进行"执行器心跳注册"和"任务结果回调";为空则关闭自动注册;支持配置,{@code lb:// + ${service_name}} 从注册中心动态获取地址
*/
private
String
address
;
/**
* 与调度中心交互的accessToken,非空时启用
*/
private
String
accessToken
;
/**
* job admin 的 context-path
*/
private
String
contextPath
;
}
@Getter
@Setter
public
static
class
XxlJobExecutorProps
{
/**
* 执行器名称,执行器心跳注册分组依据;为空则关闭自动注册
*/
private
String
appName
;
/**
* 执行器 IP,默认为空表示自动获取IP,多网卡时可手动设置指定IP,该IP不会绑定Host仅作为通讯实用;地址信息用于 "执行器注册" 和 "调度中心请求并触发任务"
*/
private
String
ip
;
/**
* 执行器端口,小于等于0则自动获取;默认端口为9999,单机部署多个执行器时,注意要配置不同执行器端口
*/
private
int
port
=
-
1
;
/**
* 执行器日志位置
*/
private
String
logPath
;
/**
* 执行器日志保留天数,默认值:-1,值大于3时生效,启用执行器Log文件定期清理功能,否则不生效
*/
private
int
logRetentionDays
=
-
1
;
}
}
settings.gradle
浏览文件 @
597c24dc
...
...
@@ -6,4 +6,4 @@ include "mica-spider"
include
"mica-laytpl"
include
"mica-swagger"
include
"mica-captcha"
include
"mica-jobs"
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录