增加starter支持

pull/32/head
杨华明 4 years ago
parent 039bd7f203
commit caf670f914

@ -0,0 +1,64 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.xuxueli</groupId>
<artifactId>xxl-job</artifactId>
<version>2.3.0</version>
</parent>
<artifactId>xxl-job-boot-starter-executor</artifactId>
<name>${project.artifactId}</name>
<description>Example executor project for spring boot.</description>
<url>https://www.xuxueli.com/</url>
<properties>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<optional>true</optional>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!-- xxl-job-core -->
<dependency>
<groupId>com.xuxueli</groupId>
<artifactId>xxl-job-core</artifactId>
<version>${project.parent.version}</version>
</dependency>
</dependencies>
</project>

@ -0,0 +1,24 @@
package com.xxl.job.core.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
/**
*
* @author cqyhm
*
*/
@Setter@Getter@ToString
@ConfigurationProperties(prefix = ApplicationProperties.PREFIX, ignoreInvalidFields = true)
public class ApplicationProperties {
/**
*
*/
public static final String PREFIX = "spring.application";
/**
*
*/
private String name;
}

@ -0,0 +1,67 @@
package com.xxl.job.core.config;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;
import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
import com.xxl.job.core.thread.ExecutorRegistryThread;
import lombok.extern.slf4j.Slf4j;
/**
*
* @author cqyhm
*/
@Slf4j
@Configuration()
@ConditionalOnClass(ExecutorRegistryThread.class)
@ConditionalOnProperty(prefix =JobConfigProperties.PREFIX ,name = "enabled", matchIfMissing = true)
@EnableConfigurationProperties(value = {JobConfigProperties.class,ApplicationProperties.class})
public class JobAutoConfiguration {
@Bean
public XxlJobSpringExecutor xxlJobExecutor(JobConfigProperties jobConfigProperties,
ApplicationProperties applicationProperties) {
log.info(">>>>>>>>>>> xxl-job config init.");
log.info("读取配置{}",applicationProperties);
log.info("读取配置{}",jobConfigProperties);
XxlJobSpringExecutor JobSpringExecutor = new XxlJobSpringExecutor();
//通讯令牌
JobSpringExecutor.setAccessToken(jobConfigProperties.getAccessToken());
//调度器设置
JobSpringExecutor.setAdminAddresses(jobConfigProperties.getAdmin().getAddresses());
//执行器设置
String name=applicationProperties.getName(); //应用程序的名称
String appName=jobConfigProperties.getExecutor().getAppname(); //调度器的名称如果没有设置取应用的名称
JobSpringExecutor.setAppname(StringUtils.hasText(appName) ? name: appName);
JobSpringExecutor.setIp(jobConfigProperties.getExecutor().getIp());
JobSpringExecutor.setPort(jobConfigProperties.getExecutor().getPort());
//执行器日志路径
JobSpringExecutor.setLogPath(jobConfigProperties.getExecutor().getLogpath());
JobSpringExecutor.setLogRetentionDays(jobConfigProperties.getExecutor().getLogretentiondays());
return JobSpringExecutor;
}
/**
* "spring-cloud-commons" "InetUtils" IP
*
* 1
* <dependency>
* <groupId>org.springframework.cloud</groupId>
* <artifactId>spring-cloud-commons</artifactId>
* <version>${version}</version>
* </dependency>
*
* 2
* spring.cloud.inetutils.preferred-networks: 'xxx.xxx.xxx.'
*
* 3IP
* String ip_ = inetUtils.findFirstNonLoopbackHostInfo().getIpAddress();
*/
}

@ -0,0 +1,66 @@
package com.xxl.job.core.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
/**
*
* @author cqyhm
*
*/
@Setter@Getter@ToString
@ConfigurationProperties(prefix = JobConfigProperties.PREFIX, ignoreInvalidFields = true)
public class JobConfigProperties {
public static final String PREFIX = "xxl.job";
/**
*
*/
private String accessToken;
/**
*
*/
private Admin admin;
/**
*
*/
private Executor executor;
@Data
public static class Admin {
/**
*
*/
private String addresses;
}
@Data
public static class Executor {
/**
* (,)
*/
private String appname;
/**
* =ip:port
*/
private String address;
/**
*
*/
private String ip="";
/**
*
*/
private Integer port=0;
/**
*
*/
private String logpath;
/**
*
*/
private Integer logretentiondays;
}
}

@ -0,0 +1,55 @@
{
"properties": [
{
"name": "xxl.job.enabled",
"type": "java.lang.Boolean",
"defaultValue": "true",
"description": "是否启用任务执行器,默认启动true"
},
{
"name": "xxl.job.accessToken",
"type": "java.lang.String",
"defaultValue": "",
"description": "调度时的通讯令牌."
},
{
"name": "xxl.job.admin.addresses",
"type": "java.lang.String",
"defaultValue": "http://localhost:8080/xxl-job-admin/",
"description": "执行器连接调度中心的地址"
},
{
"name": "xxl.job.executor.appname",
"type": "java.lang.String",
"defaultValue": "${spring.application.name}",
"description": "默认是微服务的名称"
},
{
"name": "xxl.job.executor.logpath",
"type": "java.lang.String",
"defaultValue": "执行器日志输出路径",
"description": ""
},
{
"name": "xxl.job.executor.logretentiondays",
"type": "java.lang.Integer",
"defaultValue": "7",
"description": "执行器日志保留天数"
},
{
"name": "xxl.job.executor.port",
"type": "java.lang.Integer",
"description": "执行器监控调度的端口."
},
{
"name": "xxl.job.executor.ip",
"type": "java.lang.String",
"description": "执行器所在机器的IP地址"
},
{
"name": "xxl.job.executor.address",
"type": "java.lang.String",
"description": "执行器所在机器的地址,如果为空则使用ip:port"
}
]
}

@ -0,0 +1,2 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.xxl.job.core.config.JobAutoConfiguration

@ -42,14 +42,25 @@
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<!-- xxl-job-core --> <!-- xxl-job-core -->
<dependency> <dependency>
<groupId>com.xuxueli</groupId> <groupId>com.xuxueli</groupId>
<artifactId>xxl-job-core</artifactId> <artifactId>xxl-job-boot-starter-executor</artifactId>
<version>${project.parent.version}</version> <version>${project.parent.version}</version>
</dependency> </dependency>
<!-- xxl-job-core
<dependency>
<groupId>com.xuxueli</groupId>
<artifactId>xxl-job-core</artifactId>
<version>${project.parent.version}</version>
</dependency>
-->
</dependencies> </dependencies>
<build> <build>

@ -1,78 +0,0 @@
package com.xxl.job.executor.core.config;
import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* xxl-job config
*
* @author xuxueli 2017-04-28
*/
@Configuration
public class XxlJobConfig {
private Logger logger = LoggerFactory.getLogger(XxlJobConfig.class);
@Value("${xxl.job.admin.addresses}")
private String adminAddresses;
@Value("${xxl.job.accessToken}")
private String accessToken;
@Value("${xxl.job.executor.appname}")
private String appname;
@Value("${xxl.job.executor.address}")
private String address;
@Value("${xxl.job.executor.ip}")
private String ip;
@Value("${xxl.job.executor.port}")
private int port;
@Value("${xxl.job.executor.logpath}")
private String logPath;
@Value("${xxl.job.executor.logretentiondays}")
private int logRetentionDays;
@Bean
public XxlJobSpringExecutor xxlJobExecutor() {
logger.info(">>>>>>>>>>> xxl-job config init.");
XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
xxlJobSpringExecutor.setAdminAddresses(adminAddresses);
xxlJobSpringExecutor.setAppname(appname);
xxlJobSpringExecutor.setAddress(address);
xxlJobSpringExecutor.setIp(ip);
xxlJobSpringExecutor.setPort(port);
xxlJobSpringExecutor.setAccessToken(accessToken);
xxlJobSpringExecutor.setLogPath(logPath);
xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays);
return xxlJobSpringExecutor;
}
/**
* "spring-cloud-commons" "InetUtils" IP
*
* 1
* <dependency>
* <groupId>org.springframework.cloud</groupId>
* <artifactId>spring-cloud-commons</artifactId>
* <version>${version}</version>
* </dependency>
*
* 2
* spring.cloud.inetutils.preferred-networks: 'xxx.xxx.xxx.'
*
* 3IP
* String ip_ = inetUtils.findFirstNonLoopbackHostInfo().getIpAddress();
*/
}

@ -1,11 +1,5 @@
package com.xxl.job.executor.service.jobhandler; package com.xxl.job.executor.service.jobhandler;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.DataOutputStream; import java.io.DataOutputStream;
@ -15,6 +9,13 @@ import java.net.URL;
import java.util.Arrays; import java.util.Arrays;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
/** /**
* XxlJobBean * XxlJobBean
* *

@ -1,26 +1,28 @@
# web port # web port
server.port=8081 server:
port: 8081
# no web # no web
#spring.main.web-environment=false #spring.main.web-environment=false
spring: application: name: xxl-job-executor-sample
# log config # log config
logging.config=classpath:logback.xml logging:
config: classpath:logback.xml
### xxl-job admin address list, such as "http://address" or "http://address01,http://address02" ### xxl-job admin address list, such as "http://address" or "http://address01,http://address02"
xxl.job.admin.addresses=http://127.0.0.1:8080/xxl-job-admin xxl:
job:
### xxl-job, access token admin:
xxl.job.accessToken= addresses: http://127.0.0.1:8080/xxl-job-admin
accessToken: ### xxl-job, access token
### xxl-job executor appname ### xxl-job executor appname
xxl.job.executor.appname=xxl-job-executor-sample executor:
# appname: xxl-job-executor-sample
### xxl-job executor registry-address: default use address to registry , otherwise use ip:port if address is null ### xxl-job executor registry-address: default use address to registry , otherwise use ip:port if address is null
xxl.job.executor.address= address:
### xxl-job executor server-info ### xxl-job executor server-info
xxl.job.executor.ip= ip:
xxl.job.executor.port=9999 port: 9999
### xxl-job executor log-path ### xxl-job executor log-path
xxl.job.executor.logpath=/data/applogs/xxl-job/jobhandler logpath: /data/applogs/xxl-job/jobhandler
### xxl-job executor log-retention-days ### xxl-job executor log-retention-days
xxl.job.executor.logretentiondays=30 logretentiondays: 30
Loading…
Cancel
Save