浏览代码

spring batch init

wy 1 年之前
当前提交
6e25d54eae

+ 19 - 0
.gitignore

@@ -0,0 +1,19 @@
+# IDE specific files
+/.idea/
+*.iml
+*.log
+*.gz
+
+# log
+/logs/
+
+# Compiler generated files
+target/
+
+
+# jrebel
+rebel.xml
+
+
+#应用库
+/application/

+ 56 - 0
SpringBatchServiceClient/pom.xml

@@ -0,0 +1,56 @@
+<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+
+    <parent>
+        <groupId>com.github.microservice</groupId>
+        <artifactId>PParent</artifactId>
+        <version>1.0.0-SNAPSHOT</version>
+        <relativePath>../../../super/PParent</relativePath>
+    </parent>
+
+    <groupId>com.zswl.cloud.SpringBatchService</groupId>
+        <artifactId>SpringBatchServiceClient</artifactId>
+    <name>SpringBatchServiceClient</name>
+
+    <properties>
+        <java.version>11</java.version>
+    </properties>
+
+    <dependencies>
+        <dependency>
+            <groupId>com.github.microservice</groupId>
+            <artifactId>PCore</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+
+        <!-- 通用basedata -->
+        <dependency>
+            <groupId>com.github.microservice.components</groupId>
+            <artifactId>BaseData</artifactId>
+            <version>${Project.version}</version>
+        </dependency>
+
+        <!--应用中心客户端-->
+        <dependency>
+            <groupId>com.github.microservice.components</groupId>
+            <artifactId>ApplicationClient</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+
+
+        <!-- 文档 -->
+        <dependency>
+            <groupId>com.github.microservice.components</groupId>
+            <artifactId>Swagger2</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+
+        <dependency>
+            <groupId>cn.hutool</groupId>
+            <artifactId>hutool-core</artifactId>
+            <version>5.8.18</version>
+        </dependency>
+    </dependencies>
+</project>

+ 4 - 0
SpringBatchServiceClient/src/main/java/com/zswl/cloud/springBatch/cline/service/TestService.java

@@ -0,0 +1,4 @@
+package com.zswl.cloud.springBatch.cline.service;
+
+public interface TestService {
+}

+ 126 - 0
SpringBatchServiceServer/pom.xml

@@ -0,0 +1,126 @@
+<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+
+    <parent>
+        <groupId>com.github.microservice</groupId>
+        <artifactId>PParent</artifactId>
+        <version>1.0.0-SNAPSHOT</version>
+        <relativePath>../../../super/PParent</relativePath>
+    </parent>
+
+    <groupId>com.zswl.cloud.SpringBatchService</groupId>
+    <artifactId>SpringBatchServiceServer</artifactId>
+    <name>SpringBatchServiceServer</name>
+
+    <properties>
+        <java.version>11</java.version>
+    </properties>
+
+    <dependencies>
+        <dependency>
+            <groupId>com.github.microservice</groupId>
+            <artifactId>PCore</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+
+        <!--应用中心客户端-->
+        <dependency>
+            <groupId>com.github.microservice.components</groupId>
+            <artifactId>ApplicationClient</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+
+        <dependency>
+            <groupId>com.zswl.cloud.bdb</groupId>
+            <artifactId>zswl-cloud-bdb-client</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+
+        <!-- JDBC 驱动-->
+        <dependency>
+            <groupId>com.mysql</groupId>
+            <artifactId>mysql-connector-j</artifactId>
+            <version>8.0.33</version>
+        </dependency>
+
+        <dependency>
+            <groupId>com.alibaba</groupId>
+            <artifactId>fastjson</artifactId>
+            <version>2.0.23</version>
+        </dependency>
+
+        <dependency>
+            <groupId>cn.hutool</groupId>
+            <artifactId>hutool-all</artifactId>
+            <version>5.8.18</version>
+        </dependency>
+
+        <dependency>
+            <groupId>com.github.xiaoymin</groupId>
+            <artifactId>knife4j-spring-boot-starter</artifactId>
+            <version>3.0.3</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-batch</artifactId>
+            <version>2.7.3</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-web</artifactId>
+            <version>2.7.3</version>
+        </dependency>
+
+    </dependencies>
+
+    <build>
+
+        <plugins>
+            <plugin>
+                <groupId>org.springframework.boot</groupId>
+                <artifactId>spring-boot-maven-plugin</artifactId>
+                <configuration>
+                    <fork>true</fork>
+                </configuration>
+                <executions>
+                    <execution>
+                        <goals>
+                            <goal>repackage</goal>
+                        </goals>
+                    </execution>
+                </executions>
+            </plugin>
+
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-jar-plugin</artifactId>
+                <configuration>
+                    <excludes>
+                        <!-- This is where the exclusion occurs -->
+                        <exclude>**/application-*.yml</exclude>
+                    </excludes>
+                </configuration>
+            </plugin>
+
+        </plugins>
+
+        <!-- properties 取 pom 配置 -->
+        <resources>
+            <resource>
+                <directory>src/main/java</directory>
+                <includes>
+                    <include>**/*.xml</include>
+                </includes>
+            </resource>
+            <resource>
+                <directory>src/main/resources</directory>
+                <filtering>true</filtering>
+            </resource>
+        </resources>
+
+    </build>
+</project>

+ 20 - 0
SpringBatchServiceServer/src/main/java/com/zswl/cloud/springBtach/server/boot/SpringBatchApplication.java

@@ -0,0 +1,20 @@
+package com.zswl.cloud.springBtach.server.boot;
+
+import com.github.microservice.app.annotation.EnableApplicationClient;
+import com.github.microservice.core.boot.ApplicationBootSuper;
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.context.annotation.ComponentScan;
+
+/**
+ * @author Wy
+ */
+@SpringBootApplication
+@EnableApplicationClient
+@ComponentScan("com.zswl.cloud.springBtach.server.core")
+public class SpringBatchApplication extends ApplicationBootSuper {
+
+    public static void main(String[] args) {
+        SpringApplication.run(SpringBatchApplication.class, args);
+    }
+}

+ 13 - 0
SpringBatchServiceServer/src/main/java/com/zswl/cloud/springBtach/server/core/config/ApiConfig.java

@@ -0,0 +1,13 @@
+package com.zswl.cloud.springBtach.server.core.config;
+
+import com.github.microservice.components.swagger.config.SwaggerConfiguration;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.Import;
+
+/**
+ * @author wy
+ */
+@Configuration
+@Import(SwaggerConfiguration.class)
+public class ApiConfig {
+}

+ 30 - 0
SpringBatchServiceServer/src/main/java/com/zswl/cloud/springBtach/server/core/config/CorsConfig.java

@@ -0,0 +1,30 @@
+package com.zswl.cloud.springBtach.server.core.config;
+
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.web.cors.CorsConfiguration;
+import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
+import org.springframework.web.filter.CorsFilter;
+
+/**
+ * @Author: wy
+ * @Date: 2023/6/26 14:29
+ */
+@Configuration
+public class CorsConfig {
+
+    private CorsConfiguration buildConfig() {
+        CorsConfiguration corsConfiguration = new CorsConfiguration();
+        corsConfiguration.addAllowedOrigin("*");
+        corsConfiguration.addAllowedHeader("*");
+        corsConfiguration.addAllowedMethod("*");
+        return corsConfiguration;
+    }
+
+    @Bean
+    public CorsFilter corsFilter() {
+        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
+        source.registerCorsConfiguration("/**", buildConfig());
+        return new CorsFilter(source);
+    }
+}

+ 44 - 0
SpringBatchServiceServer/src/main/java/com/zswl/cloud/springBtach/server/core/config/Knife4jConfiguration.java

@@ -0,0 +1,44 @@
+package com.zswl.cloud.springBtach.server.core.config;
+
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import springfox.documentation.builders.ApiInfoBuilder;
+import springfox.documentation.builders.PathSelectors;
+import springfox.documentation.builders.RequestHandlerSelectors;
+import springfox.documentation.service.ApiInfo;
+import springfox.documentation.spi.DocumentationType;
+import springfox.documentation.spring.web.plugins.Docket;
+import springfox.documentation.swagger2.annotations.EnableSwagger2;
+
+/**
+ * @Author: wy
+ * @Date: 2023/8/18 10:30
+ */
+@Configuration
+@EnableSwagger2
+public class Knife4jConfiguration {
+
+    @Bean(value = "SpringBatch")
+    public Docket defaultApiV1() {
+        String groupName = "SpringBatch基础服务";
+        return new Docket(DocumentationType.SWAGGER_2)
+                .apiInfo(getApiInfo())
+                //分组名称
+                .host("https://www.baidu.com")
+                .groupName(groupName)
+                .select()
+                //这里指定Controller扫描包路径
+                .apis(RequestHandlerSelectors.basePackage("com.zswl.cloud.springBatch.server.core.controller"))
+                .paths(PathSelectors.any())
+                .build();
+    }
+
+    private ApiInfo getApiInfo(){
+        return new ApiInfoBuilder()
+                //.title("swagger-bootstrap-ui-demo RESTful APIs")
+                .description("# swagger-bootstrap-ui-demo RESTful APIs")
+                .termsOfServiceUrl("http://www.xx.com/")
+                .version("1.0")
+                .build();
+    }
+}

+ 14 - 0
SpringBatchServiceServer/src/main/java/com/zswl/cloud/springBtach/server/core/config/MVCConfig.java

@@ -0,0 +1,14 @@
+package com.zswl.cloud.springBtach.server.core.config;
+
+import com.github.microservice.core.mvc.MVCConfiguration;
+import com.github.microservice.core.mvc.MVCResponseConfiguration;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.Import;
+
+/**
+ * @author wy
+ */
+@Configuration
+@Import({MVCConfiguration.class, MVCResponseConfiguration.class})
+public class MVCConfig {
+}

+ 164 - 0
SpringBatchServiceServer/src/main/java/com/zswl/cloud/springBtach/server/core/config/SpringBatchConfig.java

@@ -0,0 +1,164 @@
+package com.zswl.cloud.springBtach.server.core.config;
+
+import org.springframework.batch.core.Job;
+import org.springframework.batch.core.Step;
+import org.springframework.batch.core.StepContribution;
+import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
+import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
+import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
+import org.springframework.batch.core.job.builder.FlowBuilder;
+import org.springframework.batch.core.job.flow.Flow;
+import org.springframework.batch.core.launch.support.RunIdIncrementer;
+import org.springframework.batch.core.scope.context.ChunkContext;
+import org.springframework.batch.core.step.tasklet.Tasklet;
+import org.springframework.batch.repeat.RepeatStatus;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+import javax.annotation.Resource;
+
+/**
+ * @author Wy
+ */
+@EnableBatchProcessing
+@Configuration
+public class SpringBatchConfig {
+
+    //job构造工厂---用于构建job对象
+    @Resource
+    private JobBuilderFactory jobBuilderFactory;
+
+    //step 构造工厂--用于构造step对象
+    @Resource
+    private StepBuilderFactory stepBuilderFactory;
+
+    //构造一个step对象执行的任务(逻辑对象)
+    @Bean
+    public Tasklet taskletA(){
+        return new Tasklet() {
+            @Override
+            public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
+                System.out.println("----------------taskletA---------------" + chunkContext.getStepContext().getJobParameters().get("name"));
+                return RepeatStatus.FINISHED;
+            }
+        };
+    }
+
+    @Bean
+    public Tasklet taskletB1(){
+        return new Tasklet() {
+            @Override
+            public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
+                System.out.println("------------stepB----taskletB1---------------");
+                return RepeatStatus.FINISHED;
+            }
+        };
+    }
+
+    @Bean
+    public Tasklet taskletB2(){
+        return new Tasklet() {
+            @Override
+            public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
+                System.out.println("------------stepB----taskletB2---------------");
+                return RepeatStatus.FINISHED;
+            }
+        };
+    }
+    @Bean
+    public Tasklet taskletB3(){
+        return new Tasklet() {
+            @Override
+            public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
+                System.out.println("------------stepB----taskletB3---------------");
+                return RepeatStatus.FINISHED;
+            }
+        };
+    }
+
+    @Bean
+    public Tasklet taskletC(){
+        return new Tasklet() {
+            @Override
+            public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
+                System.out.println("----------------taskletC---------------");
+                return RepeatStatus.FINISHED;
+            }
+        };
+    }
+
+    //构造一个step对象
+    @Bean
+    public Step stepA(){
+        //tasklet 执行step逻辑, 类似 Thread()--->可以执行runable接口
+        return stepBuilderFactory.get("stepA")
+                .tasklet(taskletA())
+                .build();
+    }
+
+    @Bean
+    public Step stepB1(){
+        //tasklet 执行step逻辑, 类似 Thread()--->可以执行runable接口
+        return stepBuilderFactory.get("stepB1")
+                .tasklet(taskletB1())
+                .build();
+    }
+
+    @Bean
+    public Step stepB2(){
+        //tasklet 执行step逻辑, 类似 Thread()--->可以执行runable接口
+        return stepBuilderFactory.get("stepB2")
+                .tasklet(taskletB2())
+                .build();
+    }
+
+    @Bean
+    public Step stepB3(){
+        //tasklet 执行step逻辑, 类似 Thread()--->可以执行runable接口
+        return stepBuilderFactory.get("stepB3")
+                .tasklet(taskletB3())
+                .build();
+    }
+
+    //构造一个流式步骤
+    @Bean
+    public Flow flowB(){
+        return new FlowBuilder<Flow>("flowB")
+                .start(stepB1())
+                .next(stepB2())
+                .next(stepB3())
+                .build();
+    }
+
+    //job 没有现有的flowStep步骤操作方法, 必须使用step进行封装之后再执行
+    @Bean
+    public Step stepB(){
+        //tasklet 执行step逻辑, 类似 Thread()--->可以执行runable接口
+        return stepBuilderFactory
+                .get("stepB")
+                .flow(flowB())
+                .build();
+    }
+
+    //构造一个step对象
+    @Bean
+    public Step stepC(){
+        //tasklet 执行step逻辑, 类似 Thread()--->可以执行runable接口
+        return stepBuilderFactory.get("stepC")
+                .tasklet(taskletC())
+                .build();
+    }
+
+    //如果firstStep 执行成功:下一步执行successStep 否则是failStep
+    @Bean
+    public Job job(){
+        return jobBuilderFactory
+                .get("flow-step-job")
+                .start(stepA())
+                .next(stepB())
+                .next(stepC())
+                .incrementer(new RunIdIncrementer())
+                .build();
+    }
+
+}

+ 38 - 0
SpringBatchServiceServer/src/main/java/com/zswl/cloud/springBtach/server/core/controller/TestSpringBatch.java

@@ -0,0 +1,38 @@
+package com.zswl.cloud.springBtach.server.core.controller;
+
+import org.springframework.batch.core.*;
+import org.springframework.batch.core.explore.JobExplorer;
+import org.springframework.batch.core.launch.JobLauncher;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import javax.annotation.Resource;
+
+/**
+ * @author wy
+ */
+@RestController
+public class TestSpringBatch {
+
+    @Resource
+    private JobLauncher launcher;
+
+    @Resource
+    private Job job;
+
+    @Resource
+    private JobExplorer jobExplorer;
+
+    @GetMapping("/job/start")
+    public ExitStatus startJob(String name) throws Exception {
+
+        //启动job作业
+        JobParameters parameters = new JobParametersBuilder(jobExplorer)
+                .getNextJobParameters(job)
+                .addString("name", name)
+                .toJobParameters();
+
+        JobExecution jobExet = launcher.run(job, parameters);
+        return jobExet.getExitStatus();
+    }
+}

+ 35 - 0
SpringBatchServiceServer/src/main/resources/application-dev.yml

@@ -0,0 +1,35 @@
+spring:
+  datasource:
+    url: jdbc:mysql://192.168.110.241:3306/spring_batch?verifyServerCertificate=false&useSSL=false&requireSSL=false
+    username: root
+    password: c7ix0bJv2GvyhbkRw6
+    driver-class-name: com.mysql.jdbc.Driver
+  cloud:
+    consul:
+      discovery:
+        prefer-ip-address: false
+        hostname: 192.168.110.241
+  batch:
+    job:
+      enabled: false
+  sql:
+    init:
+      schema-locations: classpath:org/springframework/batch/core/schema-mysql.sql
+      #      mode: always
+      mode: never
+
+swagger:
+  packageName: "com.zswl.cloud.springBatch.server.core.controller"
+  title: "中数未来"
+  description: "springBatch"
+  version: "1.0"
+  enable: true
+
+
+#logging:
+#  level:
+#    org:
+#      springframework:
+#        web: trace
+#debug: true
+#trace: true

+ 48 - 0
SpringBatchServiceServer/src/main/resources/application.yml

@@ -0,0 +1,48 @@
+#Web服务器端口
+server:
+  port: 10086
+
+#允许消息总线
+spring:
+  cloud:
+    consul:
+      discovery:
+        prefer-ip-address: true # ip自动注册
+        hostname: localhost    # 配置实例地址
+        port: ${server.port}
+        health-check-path: /actuator/health  # 检测实例健康
+        health-check-interval: 10s   # 每隔10s检查
+        register: true  # 自动注册
+        service-name: ${spring.application.name} # 实例名称
+        instance-id: ${spring.application.name}:${server.port}
+      host: ${ApplicationCenter.url}
+      port: ${ApplicationCenter.port}
+    #kafka
+    stream:
+      kafka:
+        binder:
+          brokers: ${Kafka.brokers}
+          replication-factor: 1
+      function:
+        definition: userLogStreamConsumer
+      bindings:
+        userLogStreamConsumer-in-0:
+          destination: UserLogStream
+          group: ${project.artifactId}
+    bus:
+      enabled: true
+  #配置zipkin链路追踪客户端
+  zipkin:
+    base-url: ${Zipkin.baseUrl}
+    enabled: true
+    sender:
+      type: web
+  sleuth:
+    sampler:
+      probability: ${Zipkin.sampler.probability}
+
+#调试或者输出日志
+logging:
+  file:
+    name: logs/${project.artifactId}.log
+#debug: true

+ 21 - 0
SpringBatchServiceServer/src/main/resources/bootstrap.yml

@@ -0,0 +1,21 @@
+###需拷贝到每一个模块下
+spring:
+  mvc:
+    pathmatch:
+      matching-strategy: ant_path_matcher
+  main: #允许循环引用
+    allow-circular-references: true
+  # 应用名
+  application:
+    name: SpringBatchService
+  # 当前读取配置文件的类型
+  profiles:
+    active: dev
+  #配置中心
+  cloud:
+    config:
+      uri: ${ConfigCenter.url}
+      name: ${project.artifactId},ConfigCenter
+  autoconfigure:
+    exclude:
+      - org.springframework.cloud.netflix.ribbon.RibbonAutoConfiguration

+ 22 - 0
pom.xml

@@ -0,0 +1,22 @@
+<?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>
+
+    <groupId>com.zswl.cloud.SpringBatchService</groupId>
+    <artifactId>SpringBatchService</artifactId>
+    <packaging>pom</packaging>
+    <version>1.0-SNAPSHOT</version>
+
+    <properties>
+        <maven.compiler.source>11</maven.compiler.source>
+        <maven.compiler.target>11</maven.compiler.target>
+    </properties>
+
+    <modules>
+        <module>SpringBatchServiceClient</module>
+        <module>SpringBatchServiceServer</module>
+    </modules>
+
+</project>