diff --git a/README.md b/README.md index 102451e5..66b43434 100644 --- a/README.md +++ b/README.md @@ -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 | diff --git a/pig-common/pig-common-core/src/main/java/com/pig4cloud/pig/common/core/config/JacksonConfiguration.java b/pig-common/pig-common-core/src/main/java/com/pig4cloud/pig/common/core/config/JacksonConfiguration.java index 1925ec75..af85fef6 100755 --- a/pig-common/pig-common-core/src/main/java/com/pig4cloud/pig/common/core/config/JacksonConfiguration.java +++ b/pig-common/pig-common-core/src/main/java/com/pig4cloud/pig/common/core/config/JacksonConfiguration.java @@ -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()); }; } diff --git a/pig-common/pig-common-job/pom.xml b/pig-common/pig-common-job/pom.xml index 7764fa0c..156c71f6 100755 --- a/pig-common/pig-common-job/pom.xml +++ b/pig-common/pig-common-job/pom.xml @@ -39,5 +39,10 @@ xxl-job-core ${xxl-job.version} + + + org.springframework.cloud + spring-cloud-commons + diff --git a/pig-common/pig-common-job/src/main/java/com/pig4cloud/pig/common/job/XxlJobAutoConfiguration.java b/pig-common/pig-common-job/src/main/java/com/pig4cloud/pig/common/job/XxlJobAutoConfiguration.java index 1d59f2b8..b46d6675 100644 --- a/pig-common/pig-common-job/src/main/java/com/pig4cloud/pig/common/job/XxlJobAutoConfiguration.java +++ b/pig-common/pig-common-job/src/main/java/com/pig4cloud/pig/common/job/XxlJobAutoConfiguration.java @@ -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; } diff --git a/pig-common/pig-common-job/src/main/java/com/pig4cloud/pig/common/job/properties/XxlAdminProperties.java b/pig-common/pig-common-job/src/main/java/com/pig4cloud/pig/common/job/properties/XxlAdminProperties.java index 10769cb8..3ad1e5ea 100644 --- a/pig-common/pig-common-job/src/main/java/com/pig4cloud/pig/common/job/properties/XxlAdminProperties.java +++ b/pig-common/pig-common-job/src/main/java/com/pig4cloud/pig/common/job/properties/XxlAdminProperties.java @@ -14,6 +14,6 @@ public class XxlAdminProperties { /** * 调度中心部署跟地址 [选填]:如调度中心集群部署存在多个地址则用逗号分隔。 执行器将会使用该地址进行"执行器心跳注册"和"任务结果回调";为空则关闭自动注册; */ - private String addresses = "http://pig-job:5004/xxl-job-admin"; + private String addresses; } diff --git a/pig-visual/pig-xxl-job-admin/pom.xml b/pig-visual/pig-xxl-job-admin/pom.xml index 5f39e154..8949cf2d 100644 --- a/pig-visual/pig-xxl-job-admin/pom.xml +++ b/pig-visual/pig-xxl-job-admin/pom.xml @@ -13,6 +13,18 @@ + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-discovery + + + + + org.springframework.boot + spring-boot-starter-validation + + org.springframework.boot diff --git a/pig-visual/pig-xxl-job-admin/src/main/java/com/xxl/job/admin/controller/interceptor/PermissionInterceptor.java b/pig-visual/pig-xxl-job-admin/src/main/java/com/xxl/job/admin/controller/interceptor/PermissionInterceptor.java index bdc64b2e..520092d9 100644 --- a/pig-visual/pig-xxl-job-admin/src/main/java/com/xxl/job/admin/controller/interceptor/PermissionInterceptor.java +++ b/pig-visual/pig-xxl-job-admin/src/main/java/com/xxl/job/admin/controller/interceptor/PermissionInterceptor.java @@ -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; diff --git a/pig-visual/pig-xxl-job-admin/src/main/resources/application.yml b/pig-visual/pig-xxl-job-admin/src/main/resources/application.yml index b3bf3338..e36e355f 100644 --- a/pig-visual/pig-xxl-job-admin/src/main/resources/application.yml +++ b/pig-visual/pig-xxl-job-admin/src/main/resources/application.yml @@ -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 \ No newline at end of file diff --git a/pig-visual/pig-xxl-job-admin/src/main/resources/bootstrap.yml b/pig-visual/pig-xxl-job-admin/src/main/resources/bootstrap.yml new file mode 100644 index 00000000..15e5c900 --- /dev/null +++ b/pig-visual/pig-xxl-job-admin/src/main/resources/bootstrap.yml @@ -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@ \ No newline at end of file