✨ Introducing new features. #1998 xxl-job 客户端支持基于服务注册发现的自动化配置 xxl-job-admin

✨ Introducing new features. #1998 xxl-job 客户端支持基于服务注册发现的自动化配置 xxl-job-admin
This commit is contained in:
lengleng committed 2020-11-09 16:25:03 +08:00
1 parent 16b5ae6c64
commit 575a0be70d
7 files changed
+84 -12

No files matched your search

+5
View File
@@ -39,5 +39,10 @@
<artifactId>xxl-job-core</artifactId>
<version>${xxl.job.version}</version>
</dependency>
<!--提供服务发现能力-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-commons</artifactId>
</dependency>
</dependencies>
</project>
@@ -20,13 +20,17 @@ package com.pig4cloud.pigx.common.job;
import com.pig4cloud.pigx.common.job.properties.XxlJobProperties;
import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;
import java.util.stream.Collectors;
/**
* @author lengleng
* @date 2019-09-18
* @date 2020-11-09
* <p>
* xxl 初始化
*/
@@ -35,10 +39,21 @@ import org.springframework.context.annotation.Configuration;
@ComponentScan("com.pig4cloud.pigx.common.job.properties")
public class XxlJobAutoConfiguration {
/**
* 服务名称 包含 XXL_JOB_ADMIN 则说明是 Admin
*/
private static final String XXL_JOB_ADMIN = "xxl-job-admin";
/**
* 配置xxl-job 执行器,提供自动发现 xxl-job-admin 能力
* @param xxlJobProperties xxl 配置
* @param discoveryClient 注册发现客户端
* @return
*/
@Bean
public XxlJobSpringExecutor xxlJobSpringExecutor(XxlJobProperties xxlJobProperties) {
public XxlJobSpringExecutor xxlJobSpringExecutor(XxlJobProperties xxlJobProperties,
DiscoveryClient discoveryClient) {
XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
xxlJobSpringExecutor.setAdminAddresses(xxlJobProperties.getAdmin().getAddresses());
xxlJobSpringExecutor.setAppname(xxlJobProperties.getExecutor().getAppname());
xxlJobSpringExecutor.setAddress(xxlJobProperties.getExecutor().getAddress());
xxlJobSpringExecutor.setIp(xxlJobProperties.getExecutor().getIp());
@@ -46,6 +61,19 @@ public class XxlJobAutoConfiguration {
xxlJobSpringExecutor.setAccessToken(xxlJobProperties.getExecutor().getAccessToken());
xxlJobSpringExecutor.setLogPath(xxlJobProperties.getExecutor().getLogPath());
xxlJobSpringExecutor.setLogRetentionDays(xxlJobProperties.getExecutor().getLogRetentionDays());
// 如果配置为空则获取注册中心的服务列表 "http://pigx-xxl:9080/xxl-job-admin"
if (StringUtils.isEmpty(xxlJobProperties.getAdmin().getAddresses())) {
String serverList = discoveryClient.getServices().stream().filter(s -> s.contains(XXL_JOB_ADMIN))
.flatMap(s -> discoveryClient.getInstances(s).stream()).map(instance -> String
.format("http://%s:%s/%s", instance.getHost(), instance.getPort(), XXL_JOB_ADMIN))
.collect(Collectors.joining(","));
xxlJobSpringExecutor.setAddress(serverList);
}
else {
xxlJobSpringExecutor.setAddress(xxlJobProperties.getAdmin().getAddresses());
}
return xxlJobSpringExecutor;
}
@@ -31,6 +31,6 @@ public class XxlAdminProperties {
/**
* 调度中心部署跟地址 [选填]:如调度中心集群部署存在多个地址则用逗号分隔。 执行器将会使用该地址进行"执行器心跳注册"和"任务结果回调";为空则关闭自动注册;
*/
private String addresses = "http://pigx-xxl:9080/xxl-job-admin";
private String addresses;
}
+11
View File
@@ -12,6 +12,17 @@
<packaging>jar</packaging>
<dependencies>
<!--注册中心客户端-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--hibernate-validator 兼容JAVA11-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<!-- starter-web:spring-webmvc + autoconfigure + logback + yaml + tomcat -->
<dependency>
@@ -11,6 +11,7 @@ import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
/**
* 权限拦截
@@ -20,6 +21,11 @@ import javax.servlet.http.HttpServletResponse;
@Component
public class PermissionInterceptor extends HandlerInterceptorAdapter {
/**
* 针对 spring boot admin 对外暴露的接口
*/
private static final String[] ACTUATOR_IGNORE = { "/actuator", "/details", "/health" };
@Resource
private LoginService loginService;
@@ -31,6 +37,10 @@ public class PermissionInterceptor extends HandlerInterceptorAdapter {
return super.preHandle(request, response, handler);
}
if (Arrays.stream(ACTUATOR_IGNORE).anyMatch(s -> request.getRequestURI().contains(s))) {
return super.preHandle(request, response, handler);
}
// if need login
boolean needLogin = true;
boolean needAdminuser = false;
@@ -1,9 +1,3 @@
# 此配置只适合开发测试环境,详细配置参考: http://t.cn/A64RaHJm
server:
port: 9080
servlet:
context-path: /xxl-job-admin
# xxl
xxl:
job:
@@ -20,7 +14,7 @@ mybatis:
# spring
spring:
datasource:
url: jdbc:mysql://${MYSQL_HOST:pig-mysql}:${MYSQL_PORT:3306}/${MYSQL_DB:pigxx_job}?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true&allowPublicKeyRetrieval=true
url: jdbc:mysql://${MYSQL_HOST:pigx-mysql}:${MYSQL_PORT:3306}/${MYSQL_DB:pigxx_job}?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true&allowPublicKeyRetrieval=true
driver-class-name: com.mysql.cj.jdbc.Driver
username: ${MYSQL-PWD:root}
password: ${MYSQL-PWD:root}
@@ -46,9 +40,16 @@ spring:
ssl.enable: true
starttls.enable: false
required: false
# spring boot admin 配置
# close mail health check
management:
health:
mail:
enabled: false
endpoints:
web:
exposure:
include: '*'
endpoint:
health:
show-details: ALWAYS
@@ -0,0 +1,17 @@
# 此配置只适合开发测试环境,详细配置参考: http://t.cn/A64RaHJm
server:
port: 9080
servlet:
context-path: /xxl-job-admin
spring:
application:
name: @artifactId@
cloud:
nacos:
discovery:
server-addr: ${NACOS_HOST:pigx-register}:${NACOS_PORT:8848}
metadata:
management.context-path: ${server.servlet.context-path}/actuator
profiles:
active: @profiles.active@