mirror of https://gitee.com/maxjhandsome/pig
✨ Introducing new features. xxl-job 客户端支持基于服务注册发现的自动化配置 xxl-job-admin https://gitee.com/log4j/pig/issues/I24HWM
This commit is contained in:
parent
c387461fb2
commit
612ffd4a9a
|
@ -18,7 +18,7 @@
|
|||
| 依赖 | 版本 |
|
||||
| ---------------------- | ------------- |
|
||||
| Spring Boot | 2.3.6.RELEASE |
|
||||
| Spring Cloud | Hoxton.SR8 |
|
||||
| Spring Cloud | Hoxton.SR9 |
|
||||
| Spring Cloud Alibaba | 2.2.3.RELEASE |
|
||||
| Spring Security OAuth2 | 2.3.6 |
|
||||
| Mybatis Plus | 3.4.1 |
|
||||
|
|
|
@ -18,6 +18,7 @@ package com.pig4cloud.pig.common.core.config;
|
|||
|
||||
import cn.hutool.core.date.DatePattern;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
|
||||
import com.pig4cloud.pig.common.core.jackson.PigJavaTimeModule;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
|
@ -51,6 +52,7 @@ public class JacksonConfiguration {
|
|||
builder.locale(Locale.CHINA);
|
||||
builder.timeZone(TimeZone.getTimeZone(ZoneId.systemDefault()));
|
||||
builder.simpleDateFormat(DatePattern.NORM_DATETIME_PATTERN);
|
||||
builder.serializerByType(Long.class, ToStringSerializer.instance);
|
||||
builder.modules(new PigJavaTimeModule());
|
||||
};
|
||||
}
|
||||
|
|
|
@ -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>
|
||||
|
|
|
@ -3,9 +3,13 @@ package com.pig4cloud.pig.common.job;
|
|||
import com.pig4cloud.pig.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;
|
||||
|
||||
/**
|
||||
* xxl-job自动装配
|
||||
|
@ -18,10 +22,21 @@ import org.springframework.context.annotation.Configuration;
|
|||
@ComponentScan("com.pig4cloud.pig.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());
|
||||
|
@ -29,6 +44,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;
|
||||
}
|
||||
|
||||
|
|
|
@ -14,6 +14,6 @@ public class XxlAdminProperties {
|
|||
/**
|
||||
* 调度中心部署跟地址 [选填]:如调度中心集群部署存在多个地址则用逗号分隔。 执行器将会使用该地址进行"执行器心跳注册"和"任务结果回调";为空则关闭自动注册;
|
||||
*/
|
||||
private String addresses = "http://pig-job:5004/xxl-job-admin";
|
||||
private String addresses;
|
||||
|
||||
}
|
||||
|
|
|
@ -13,6 +13,18 @@
|
|||
|
||||
<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>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
|
|
|
@ -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: 5004
|
||||
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:pig_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: 5004
|
||||
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@
|
Loading…
Reference in New Issue