Skip to content

Commit 2f562a0

Browse files
committed
本例主要演示如何通过编码的方式手动触发xxl-job执行器
1 parent abc0470 commit 2f562a0

File tree

15 files changed

+742
-0
lines changed

15 files changed

+742
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,3 +656,7 @@
656656
/springboot-mybatisplus-tenant/target/classes/application-jdbc.yml
657657
/springboot-mybatisplus-tenant/target/classes/demo_test.sql
658658
/springboot-mybatisplus-tenant/springboot-mybatisplus-tenant.iml
659+
/springboot-generate-sqlscript/target/classes/
660+
/springboot-mybatisplus-tenant/target/classes/META-INF/
661+
/springboot-xxl-job-executor/target/classes/
662+
/springboot-xxl-job-executor/springboot-xxl-job-executor.iml

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,19 @@ springboot实战系列暂时先更新到这边,下边是对之前springboot实
2626
[springboot实战之ORM整合(mybatis篇)](https://mp.weixin.qq.com/s?__biz=MzI1MTY1Njk4NQ==&mid=2247487718&idx=1&sn=eff0d92671875d8acbf2a676a00b091c&chksm=e9eefe90de997786adfdf769644014c2c182b925e73e8c40702a1819b02f60d22b4a9afac33c&scene=21#wechat_redirect)
2727

2828

29+
[mybatis-plus多租户使用采坑记](https://mp.weixin.qq.com/s?__biz=MzI1MTY1Njk4NQ==&mid=2247494620&idx=1&sn=73d471e8c9f351124a64f062a3285c81&chksm=e9ed05aade9a8cbc8be138816f4cfe6181f1b325eb55830288af73d2655ac4c6d15cce2c8fb1&token=1644107503&lang=zh_CN#rd)
30+
31+
32+
[如何解决mybatis-plus自动填充字段不生效问题](https://mp.weixin.qq.com/s?__biz=MzI1MTY1Njk4NQ==&mid=2247494797&idx=1&sn=9b7593788b8c001c64d2dd0e89fa82e6&chksm=e9ed02fbde9a8bedc6108e7be8dfa60fa9f3ffb115f0a2c8d0637dedeaad9083d7a9f7f42150&token=1644107503&lang=zh_CN#rd)
33+
2934

3035
[springboot实战之ORM整合(JPA篇)](https://mp.weixin.qq.com/s?__biz=MzI1MTY1Njk4NQ==&mid=2247487674&idx=1&sn=2f33664e18858409a55e07cdffd8878b&chksm=e9eefeccde9977da4e15df6e8bfa69ceb83417f32c82a00dae50dabe176bf046e0847b010c8f&scene=21#wechat_redirect)
3136

3237

3338

39+
40+
41+
3442
> 4、数据库性能提升篇
3543
3644
[springboot实战之mysql读写分离](https://mp.weixin.qq.com/s?__biz=MzI1MTY1Njk4NQ==&mid=2247487921&idx=1&sn=e3495542b67e131914b90d6c7e9157b5&chksm=e9eeffc7de9976d19989a01c74b449674b05735642b6046a114bf4ca9a368a30f2bc3f681d45&scene=21#wechat_redirect)
@@ -92,6 +100,7 @@ springboot实战系列暂时先更新到这边,下边是对之前springboot实
92100
> 9、部署篇
93101
94102
[springboot实战之docker部署](https://mp.weixin.qq.com/s?__biz=MzI1MTY1Njk4NQ==&mid=2247488493&idx=1&sn=4b429bbe734d303c14adabda758c1882&chksm=e9eefd9bde99748d295ede5423164421ef5ff86977aa6025512f0fe1e72acf074d9ad67169b8&scene=21#wechat_redirect)
103+
[springboot项目在docker容器中如何优雅关闭](https://mp.weixin.qq.com/s?__biz=MzI1MTY1Njk4NQ==&mid=2247494541&idx=1&sn=96b485733f0750ee424147bc395f7faf&chksm=e9ed05fbde9a8ceddc0c739a86f44f169345380ef38abf122c62f9a1859220d6675327629eb7&token=1644107503&lang=zh_CN#rd)
95104

96105

97106

@@ -108,3 +117,5 @@ springboot实战系列暂时先更新到这边,下边是对之前springboot实
108117

109118
[如何使用自定义类加载器防止代码被反编译破解](https://mp.weixin.qq.com/s?__biz=MzI1MTY1Njk4NQ==&mid=2247493416&idx=1&sn=2c4cc888793e88df15396ff6fb3dbda0&chksm=e9ed095ede9a8048522c77d9a01d0675ccfd134d2db2f4adccc2eb9c5e85d998edc3c9da2097&token=1482534083&lang=zh_CN#rd)
110119

120+
[如何解决springboot参数传中文乱码](https://mp.weixin.qq.com/s?__biz=MzI1MTY1Njk4NQ==&mid=2247494264&idx=1&sn=521b198092b4a11971f6fe00d6c8f1ae&chksm=e9ed040ede9a8d18594af836f72b4a78cc3e8a87ff6b887fdf2f16eb9a2edf79969805a8d0b3&token=1644107503&lang=zh_CN#rd)
121+

pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
<module>springboot-chaincontext</module>
3737
<module>springboot-generate-sqlscript</module>
3838
<module>springboot-mybatisplus-tenant</module>
39+
<module>springboot-xxl-job-executor</module>
3940
</modules>
4041
<parent>
4142
<groupId>org.springframework.boot</groupId>
@@ -99,6 +100,7 @@
99100
<spring-kafka.version>2.2.6.RELEASE</spring-kafka.version>
100101
<hutool.version>5.4.1</hutool.version>
101102
<spring-cloud.version>Greenwich.RC2</spring-cloud.version>
103+
<xxl-job.version>2.2.0</xxl-job.version>
102104
</properties>
103105

104106
<dependencyManagement>
@@ -468,6 +470,12 @@
468470
<version>${hutool.version}</version>
469471
</dependency>
470472

473+
<dependency>
474+
<groupId>com.xuxueli</groupId>
475+
<artifactId>xxl-job-core</artifactId>
476+
<version>${xxl-job.version}</version>
477+
</dependency>
478+
471479

472480

473481
</dependencies>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
本例主要演示如何通过编码的方式手动触发xxl-job执行器
2+
3+
核心点:com.xxl.job.core.biz.ExecutorBiz
4+
5+
6+
7+
8+
9+
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>springboot-learning</artifactId>
7+
<groupId>com.github.lybgeek</groupId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>springboot-xxl-job-executor</artifactId>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>org.springframework.boot</groupId>
17+
<artifactId>spring-boot-starter-web</artifactId>
18+
</dependency>
19+
20+
<dependency>
21+
<groupId>org.springframework.boot</groupId>
22+
<artifactId>spring-boot-configuration-processor</artifactId>
23+
<optional>true</optional>
24+
</dependency>
25+
<dependency>
26+
<groupId>org.projectlombok</groupId>
27+
<artifactId>lombok</artifactId>
28+
<optional>true</optional>
29+
</dependency>
30+
<dependency>
31+
<groupId>org.springframework.boot</groupId>
32+
<artifactId>spring-boot-starter-test</artifactId>
33+
<scope>test</scope>
34+
</dependency>
35+
36+
<dependency>
37+
<groupId>com.xuxueli</groupId>
38+
<artifactId>xxl-job-core</artifactId>
39+
</dependency>
40+
41+
<!--swagger在线接口文档-->
42+
<dependency>
43+
<groupId>io.springfox</groupId>
44+
<artifactId>springfox-swagger-ui</artifactId>
45+
</dependency>
46+
<dependency>
47+
<groupId>io.springfox</groupId>
48+
<artifactId>springfox-swagger2</artifactId>
49+
</dependency>
50+
51+
<dependency>
52+
<groupId>io.swagger</groupId>
53+
<artifactId>swagger-annotations</artifactId>
54+
</dependency>
55+
<dependency>
56+
<groupId>io.swagger</groupId>
57+
<artifactId>swagger-models</artifactId>
58+
</dependency>
59+
60+
<dependency>
61+
<groupId>com.alibaba</groupId>
62+
<artifactId>fastjson</artifactId>
63+
</dependency>
64+
65+
</dependencies>
66+
67+
68+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.github.lybgeek;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class SpringbootXxlJobExecutorApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(SpringbootXxlJobExecutorApplication.class, args);
11+
}
12+
13+
}
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
2+
package com.github.lybgeek.config;
3+
4+
import com.google.common.collect.Lists;
5+
import io.swagger.annotations.ApiOperation;
6+
import org.springframework.beans.factory.annotation.Value;
7+
import org.springframework.context.annotation.Bean;
8+
import org.springframework.context.annotation.Configuration;
9+
import springfox.documentation.builders.ApiInfoBuilder;
10+
import springfox.documentation.builders.ParameterBuilder;
11+
import springfox.documentation.builders.PathSelectors;
12+
import springfox.documentation.builders.RequestHandlerSelectors;
13+
import springfox.documentation.schema.ModelRef;
14+
import springfox.documentation.service.*;
15+
import springfox.documentation.spi.DocumentationType;
16+
import springfox.documentation.spi.service.contexts.SecurityContext;
17+
import springfox.documentation.spring.web.plugins.Docket;
18+
import springfox.documentation.swagger2.annotations.EnableSwagger2;
19+
20+
import java.util.ArrayList;
21+
import java.util.List;
22+
23+
24+
@Configuration
25+
@EnableSwagger2
26+
public class SwaggerConfig {
27+
28+
29+
/** 是否开启swagger */
30+
@Value("${swagger.enabled:true}")
31+
private boolean enabled;
32+
33+
/** 设置请求的统一前缀 */
34+
@Value("${swagger.pathMapping:/}")
35+
private String pathMapping;
36+
37+
/** 是否需要过滤token处理 **/
38+
@Value("${swagger.skipToken.enabled:false}")
39+
private Boolean skipTokenEnabled;
40+
41+
42+
43+
44+
45+
@Bean
46+
public Docket groupRestApi() {
47+
buildHeaderParams();
48+
return new Docket(DocumentationType.SWAGGER_2)
49+
// 是否启用Swagger
50+
.enable(enabled)
51+
// 用来创建该API的基本信息,展示在文档的页面中(自定义展示的信息)
52+
.apiInfo(groupApiInfo())
53+
// 设置哪些接口暴露给Swagger展示
54+
.select()
55+
// 扫描所有有注解的api,用这种方式更灵活
56+
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
57+
// 扫描指定包中的swagger注解
58+
// .apis(RequestHandlerSelectors.basePackage("com.github.lybgeek"))
59+
// 扫描所有 .apis(RequestHandlerSelectors.any())
60+
.paths(PathSelectors.any())
61+
.build()
62+
// 设置安全模式,swagger可以设置访问token
63+
.securityContexts(Lists.newArrayList(securityContext(),securityContext1()))
64+
.securitySchemes(Lists.<SecurityScheme>newArrayList(apiKey(),apiKey1()))
65+
.pathMapping(pathMapping)
66+
.globalOperationParameters(buildHeaderParams());
67+
68+
}
69+
70+
/**
71+
* 设置请求头信息
72+
* @return
73+
*/
74+
private List<Parameter> buildHeaderParams() {
75+
List<Parameter> parameters = new ArrayList<>();
76+
ParameterBuilder skipTokenAuthorize = new ParameterBuilder();
77+
if (skipTokenEnabled) {
78+
skipTokenAuthorize.name("skipTokenAuthorize").description("跳过token认证").modelRef(new ModelRef("string")).parameterType("header").defaultValue("true").required(false).build();
79+
parameters.add(skipTokenAuthorize.build());
80+
}
81+
return parameters;
82+
}
83+
84+
/**
85+
* 添加摘要信息
86+
*/
87+
private ApiInfo groupApiInfo(){
88+
return new ApiInfoBuilder()
89+
.title("springboot-xxl-job-executor")
90+
.description("<div style='font-size:14px;color:red;'>SPRINGBOOT-XXL-JOB-EXECUTOR RESTful APIs</div>")
91+
.termsOfServiceUrl("http://www.retail.com/")
92+
.version("1.0")
93+
.build();
94+
}
95+
96+
97+
/**
98+
* 指定header key
99+
*/
100+
private ApiKey apiKey() {
101+
return new ApiKey("Authorization", "Authorization", "header");
102+
}
103+
104+
private ApiKey apiKey1() {
105+
return new ApiKey("serviceName", "serviceName", "header");
106+
}
107+
108+
/**
109+
* 安全上下文
110+
*/
111+
private SecurityContext securityContext() {
112+
return SecurityContext.builder()
113+
.securityReferences(defaultAuth())
114+
.forPaths(PathSelectors.regex("/.*"))
115+
.build();
116+
}
117+
118+
/**
119+
* 安全模式,这里指定token通过Authorization头请求头传递
120+
*/
121+
List<SecurityReference> defaultAuth() {
122+
AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
123+
AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
124+
authorizationScopes[0] = authorizationScope;
125+
return Lists.newArrayList(new SecurityReference("Authorization", authorizationScopes));
126+
}
127+
128+
private SecurityContext securityContext1() {
129+
return SecurityContext.builder()
130+
.securityReferences(defaultAuth1())
131+
.forPaths(PathSelectors.regex("/.*"))
132+
.build();
133+
}
134+
135+
List<SecurityReference> defaultAuth1() {
136+
AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
137+
AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
138+
authorizationScopes[0] = authorizationScope;
139+
return Lists.newArrayList(new SecurityReference("serviceName", authorizationScopes));
140+
}
141+
142+
143+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.github.lybgeek.config;
2+
3+
import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
4+
import lombok.extern.slf4j.Slf4j;
5+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
6+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
7+
import org.springframework.context.annotation.Bean;
8+
import org.springframework.context.annotation.Configuration;
9+
import org.springframework.context.annotation.Profile;
10+
11+
12+
@Configuration
13+
@Slf4j
14+
@EnableConfigurationProperties(XxlJobProperty.class)
15+
@Profile("job")
16+
public class XxlJobConfig {
17+
18+
@Bean
19+
@ConditionalOnMissingBean
20+
public XxlJobSpringExecutor xxlJobExecutor(XxlJobProperty property) {
21+
log.info(">>>>>>>>>>> xxl-job config init.");
22+
XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
23+
xxlJobSpringExecutor.setAdminAddresses(property.getAdminAddresses());
24+
xxlJobSpringExecutor.setAppname(property.getExecutorAppname());
25+
xxlJobSpringExecutor.setAddress(property.getExecutorAddress());
26+
xxlJobSpringExecutor.setIp(property.getExecutorIp());
27+
xxlJobSpringExecutor.setPort(property.getExecutorPort());
28+
xxlJobSpringExecutor.setAccessToken(property.getAccessToken());
29+
xxlJobSpringExecutor.setLogPath(property.getExecutorLogPath());
30+
xxlJobSpringExecutor.setLogRetentionDays(property.getExecutorLogRetentionDays());
31+
32+
return xxlJobSpringExecutor;
33+
}
34+
35+
/**
36+
* 针对多网卡、容器内部署等情况,可借助 "spring-cloud-commons" 提供的 "InetUtils" 组件灵活定制注册IP;
37+
*
38+
* 1、引入依赖:
39+
* <dependency>
40+
* <groupId>org.springframework.cloud</groupId>
41+
* <artifactId>spring-cloud-commons</artifactId>
42+
* <version>${version}</version>
43+
* </dependency>
44+
*
45+
* 2、配置文件,或者容器启动变量
46+
* spring.cloud.inetutils.preferred-networks: 'xxx.xxx.xxx.'
47+
*
48+
* 3、获取IP
49+
* String ip_ = inetUtils.findFirstNonLoopbackHostInfo().getIpAddress();
50+
*/
51+
52+
53+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.github.lybgeek.config;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Data;
5+
import lombok.NoArgsConstructor;
6+
import org.springframework.boot.context.properties.ConfigurationProperties;
7+
8+
/**
9+
* @description: xxl-job配置
10+
*
11+
**/
12+
@Data
13+
@AllArgsConstructor
14+
@NoArgsConstructor
15+
@ConfigurationProperties(prefix = "xxl.job")
16+
public class XxlJobProperty {
17+
18+
private String adminAddresses;
19+
20+
private String accessToken;
21+
22+
private String executorAppname;
23+
24+
private String executorAddress;
25+
26+
private String executorIp;
27+
28+
private int executorPort;
29+
30+
private String executorLogPath;
31+
32+
private int executorLogRetentionDays;
33+
34+
}

0 commit comments

Comments
 (0)