Prechádzať zdrojové kódy

增加线程工具、包版本管理、公共启动类、SpringUtil封装类

yucheng 4 rokov pred
rodič
commit
18cd0868cc

+ 22 - 0
README.md

@@ -0,0 +1,22 @@
+FM基础组件
+============ 
+
+模块
+---------------
+* fm-parent 包管理模块
+* [fm-common](/fm-common/README.md) 基础工具
+* [fm-server](/fm-server/README.md) 开发web服务模块
+
+最新变化
+---------------
+* 开发方式采用各业务的微服务,采用独立发布的方式,版本由服务自己控制
+
+环境搭建
+---------------
+
+开发示例
+---------------
+
+相关文档
+---------------
+

+ 8 - 0
fm-common/README.md

@@ -0,0 +1,8 @@
+fm-common 通用工具
+============ 
+
+说明
+---------------
+
+最新变化
+---------------

+ 5 - 6
fm-common/pom.xml

@@ -3,18 +3,17 @@
          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">
     <parent>
-        <artifactId>integrated-platform</artifactId>
+        <artifactId>fm-parent</artifactId>
         <groupId>com.persagy</groupId>
-        <version>1.0.0</version>
+        <version>3.0.0</version>
+        <relativePath>../fm-parent</relativePath>
     </parent>
     <modelVersion>4.0.0</modelVersion>
     <artifactId>fm-common</artifactId>
-    <packaging>jar</packaging>
     <dependencies>
         <dependency>
-            <groupId>org.projectlombok</groupId>
-            <artifactId>lombok</artifactId>
-            <optional>true</optional>
+            <groupId>com.persagy</groupId>
+            <artifactId>integrated-common-core</artifactId>
         </dependency>
     </dependencies>
 </project>

+ 103 - 0
fm-common/src/main/java/com/persagy/fm/common/helper/ExecutorHelper.java

@@ -0,0 +1,103 @@
+package com.persagy.fm.common.helper;
+
+import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
+
+import java.util.Collection;
+import java.util.List;
+import java.util.concurrent.*;
+
+/**
+ * 线程池助手
+ * @author Charlie Yu
+ * @version 1.0 2021-03-02
+ */
+public final class ExecutorHelper {
+
+    /**
+     * 线程池
+     */
+    private static ExecutorService executor = null;
+
+    /**
+     * 通过线程池执行任务,无需同步
+     * @param run 可运行的对象
+     */
+    public static void execute(Runnable run) {
+        getExecutor().execute(run);
+    }
+
+    /**
+     * 通过线程池执行任务,并返回同步点
+     * @param task 线程对象
+     * @return 同步点对象,同步点只能返回null,
+     */
+    public static Future<?> submitRunable(Runnable task) {
+        return getExecutor().submit(task);
+    }
+
+    /**
+     * 通过线程池执行任务,并返回同步点
+     * @param task 可回调对象
+     * @return 同步点对象,可以取得异步返回结果,
+     */
+    public static <T> Future<T> submit(Callable<T> task) {
+        return getExecutor().submit(task);
+    }
+
+    /**
+     * 通过线程池执行任务列表,并返回同步点列表
+     * @param tasks 任务列表
+     * @return 同步点对象,可以取得异步返回结果,
+     * @throws InterruptedException 中断异常
+     */
+    public static <T> List<Future<T>> submitAll(Collection<Callable<T>> tasks) throws InterruptedException {
+        return getExecutor().invokeAll(tasks);
+    }
+
+    /**
+     * 通过线程池执行任务列表,并返回第一个成功的结果
+     * @param tasks 任务列表
+     * @param <T> 结果泛型
+     * @return 一个任务的结果对象
+     * @throws InterruptedException
+     * @throws ExecutionException
+     */
+    public static <T> T submitAny(Collection<Callable<T>> tasks) throws InterruptedException, ExecutionException {
+        return getExecutor().invokeAny(tasks);
+    }
+
+    /**
+     * 取得线程池
+     * @return 线程池服务
+     */
+    public static ExecutorService getExecutor() {
+        if (executor == null) {
+            init();
+        }
+        return executor;
+    }
+
+    /**
+     * 初始化线程池服务
+     */
+    public static synchronized void init() {
+        if (executor != null) {
+            return;
+        }
+        ThreadPoolTaskExecutor poolTaskExecutor = SpringHelper.getBean(
+                SpringHelper.getProperty("threadpool.name", "threadPoolTaskExecutor"), ThreadPoolTaskExecutor.class);
+        if (poolTaskExecutor != null) {
+            executor = poolTaskExecutor.getThreadPoolExecutor();
+        } else {
+            //改成无限长的线程池,看看有没有问题
+            executor = Executors.newCachedThreadPool();
+            //executor = Executors.newWorkStealingPool();
+
+            /*ThreadPoolExecutor executorTest = new ThreadPoolExecutor(10,
+                    AppConfig.getInstance().getInt("restcaller.poolnum", 20), 10, TimeUnit.SECONDS,
+                    new SynchronousQueue<Runnable>());
+            executorTest.allowCoreThreadTimeOut(true);
+            executor = executorTest;*/
+        }
+    }
+}

+ 68 - 0
fm-common/src/main/java/com/persagy/fm/common/helper/SpringHelper.java

@@ -0,0 +1,68 @@
+package com.persagy.fm.common.helper;
+
+import cn.hutool.extra.spring.SpringUtil;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.context.ApplicationContext;
+
+import java.util.Map;
+
+public class SpringHelper {
+
+    /**
+     * spring上下文
+     * @return spring上下文
+     */
+    public static ApplicationContext getContext(){
+        return SpringUtil.getApplicationContext();
+    }
+
+    /**
+     * 根据类名搜索相应的bean对象
+     * @param <T> 类模板标识返回值类型
+     * @param clazz 模板类
+     * @return bean对象
+     */
+    public static <T>T getBean(Class<T> clazz){
+        return SpringUtil.getBean(clazz);
+    }
+
+    /**
+     * 根据类型取得所有
+     * @param type 接口类
+     * @return 取得所有的实现bean
+     */
+    public static <T> Map<String, T> getBeansOfType(Class<T> type){
+        return SpringUtil.getBeansOfType(type);
+    }
+
+    /**
+     * 根据bean名称和bean类型搜索相应的bean对象
+     * @param <T> 类模板标识返回值类型
+     * @param beanName bean名称
+     * @param clazz 模板类
+     * @return bean对象
+     */
+    public static <T>T getBean(String beanName,Class<T> clazz){
+        return SpringUtil.getBean(beanName, clazz);
+    }
+
+    /**
+     * 根据bean名称搜索相应的bean对象
+     * @param beanName bean名称
+     * @return bean对象
+     */
+    public static <T>T getBean(String beanName){
+        return SpringUtil.getBean(beanName);
+    }
+
+    /**
+     * 获取配置
+     * @param key 配置标识
+     * @param defaultValue 默认值
+     * @return
+     */
+    public static String getProperty(String key, String defaultValue) {
+        String value = SpringUtil.getProperty(key);
+        return StringUtils.isBlank(value) ? defaultValue : value;
+    }
+}

+ 8 - 0
fm-parent/README.md

@@ -0,0 +1,8 @@
+fm-parent 包版本管理
+============ 
+
+说明
+---------------
+
+最新变化
+---------------

+ 277 - 0
fm-parent/pom.xml

@@ -0,0 +1,277 @@
+<?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.persagy</groupId>
+    <artifactId>fm-parent</artifactId>
+    <version>3.0.0</version>
+    <packaging>pom</packaging>
+
+    <parent>
+        <groupId>org.springframework.boot</groupId>
+        <artifactId>spring-boot-starter-parent</artifactId>
+        <version>2.1.14.RELEASE</version>
+    </parent>
+    <properties>
+        <fm.version>3.0.0</fm.version>
+        <platform.version>1.0.0</platform.version>
+    </properties>
+
+    <dependencyManagement>
+        <dependencies>
+            <dependency>
+                <groupId>com.persagy</groupId>
+                <artifactId>fm-server</artifactId>
+                <version>${fm.version}</version>
+            </dependency>
+            <dependency>
+                <groupId>com.persagy</groupId>
+                <artifactId>integrated-common-core</artifactId>
+                <version>${platform.version}</version>
+            </dependency>
+            <dependency>
+                <groupId>com.persagy</groupId>
+                <artifactId>integrated-log-spring-boot-starter</artifactId>
+                <version>${platform.version}</version>
+            </dependency>
+        </dependencies>
+    </dependencyManagement>
+
+    <!-- 插件配置 -->
+    <build>
+        <pluginManagement>
+            <plugins>
+                <!-- compiler插件, 设定JDK版本 -->
+                <plugin>
+                    <groupId>org.apache.maven.plugins</groupId>
+                    <artifactId>maven-compiler-plugin</artifactId>
+                    <version>3.7.0</version>
+                    <configuration>
+                        <source>${java.version}</source>
+                        <target>${java.version}</target>
+                        <showWarnings>true</showWarnings>
+                    </configuration>
+                </plugin>
+                <plugin>
+                    <groupId>org.scala-tools</groupId>
+                    <artifactId>maven-scala-plugin</artifactId>
+                    <version>2.15.2</version>
+                    <executions>
+                        <execution>
+                            <id>scala-compile-first</id>
+                            <phase>process-resources</phase>
+                            <goals>
+                                <goal>compile</goal>
+                            </goals>
+                        </execution>
+                        <execution>
+                            <id>scala-test-compile</id>
+                            <phase>process-test-resources</phase>
+                            <goals>
+                                <goal>testCompile</goal>
+                            </goals>
+                        </execution>
+                    </executions>
+                </plugin>
+                <!-- resource插件 -->
+                <plugin>
+                    <groupId>org.apache.maven.plugins</groupId>
+                    <artifactId>maven-resources-plugin</artifactId>
+                    <version>3.0.2</version>
+                </plugin>
+
+                <!-- test插件, 仅测试名称为*Test的类, 使用支持分组测试的surefire-junit47 driver -->
+                <plugin>
+                    <groupId>org.apache.maven.plugins</groupId>
+                    <artifactId>maven-surefire-plugin</artifactId>
+                    <version>2.20.1</version>
+                    <configuration>
+                        <includes>
+                            <include>**/*Test.java</include>
+                            <include>**/*Test.scala</include>
+                        </includes>
+                        <skipTests>true</skipTests>
+                    </configuration>
+                    <dependencies>
+                        <dependency>
+                            <groupId>org.apache.maven.surefire</groupId>
+                            <artifactId>surefire-junit4</artifactId>
+                            <version>2.18.1</version>
+                        </dependency>
+                    </dependencies>
+                </plugin>
+
+                <!-- 增加更多的Source和Test Source目录插件 -->
+                <plugin>
+                    <groupId>org.codehaus.mojo</groupId>
+                    <artifactId>build-helper-maven-plugin</artifactId>
+                    <version>3.0.0</version>
+                </plugin>
+
+                <!-- cobertura 测试覆盖率统计插插件 -->
+                <plugin>
+                    <groupId>org.codehaus.mojo</groupId>
+                    <artifactId>cobertura-maven-plugin</artifactId>
+                    <version>2.7</version>
+                </plugin>
+
+                <!-- war打包插件, 设定war包名称不带版本号 -->
+                <plugin>
+                    <groupId>org.apache.maven.plugins</groupId>
+                    <artifactId>maven-war-plugin</artifactId>
+                    <version>3.2.0</version>
+                    <configuration>
+                        <warName>${project.artifactId}${chassis.version}</warName>
+                        <failOnMissingWebXml>false</failOnMissingWebXml>
+                    </configuration>
+                </plugin>
+
+                <!-- jar打包相关插件 -->
+                <plugin>
+                    <groupId>org.apache.maven.plugins</groupId>
+                    <artifactId>maven-jar-plugin</artifactId>
+                    <version>3.1.0</version>
+                    <executions>
+                        <execution>
+                            <goals>
+                                <goal>test-jar</goal>
+                            </goals>
+                        </execution>
+                    </executions>
+                    <configuration>
+                        <archive>
+                            <manifest>
+                                <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
+                            </manifest>
+                        </archive>
+                    </configuration>
+                </plugin>
+                <plugin>
+                    <groupId>org.apache.maven.plugins</groupId>
+                    <artifactId>maven-source-plugin</artifactId>
+                    <version>3.0.1</version>
+                    <configuration>
+                        <attach>true</attach>
+                    </configuration>
+                    <executions>
+                        <execution>
+                            <phase>compile</phase>
+                            <goals>
+                                <goal>jar</goal>
+                            </goals>
+                        </execution>
+                    </executions>
+                </plugin>
+
+                <!-- clean插件 -->
+                <plugin>
+                    <groupId>org.apache.maven.plugins</groupId>
+                    <artifactId>maven-clean-plugin</artifactId>
+                    <version>3.0.0</version>
+                </plugin>
+
+                <!-- install插件 -->
+                <plugin>
+                    <groupId>org.apache.maven.plugins</groupId>
+                    <artifactId>maven-install-plugin</artifactId>
+                    <version>2.5.2</version>
+                </plugin>
+
+                <!-- enforcer插件, 避免被依赖的依赖引入过期的jar包 -->
+                <plugin>
+                    <groupId>org.apache.maven.plugins</groupId>
+                    <artifactId>maven-enforcer-plugin</artifactId>
+                    <version>1.4.1</version>
+                    <executions>
+                        <execution>
+                            <id>enforce-banned-dependencies</id>
+                            <goals>
+                                <goal>enforce</goal>
+                            </goals>
+                            <configuration>
+                                <rules>
+                                    <requireMavenVersion>
+                                        <version>3.0.3</version>
+                                    </requireMavenVersion>
+                                    <requireJavaVersion>
+                                        <version>1.7</version>
+                                    </requireJavaVersion>
+                                    <bannedDependencies>
+                                        <searchTransitive>true</searchTransitive>
+                                        <excludes>
+                                            <exclude>commons-logging</exclude>
+                                            <exclude>aspectj:aspectj*</exclude>
+                                            <exclude>org.springframework</exclude>
+                                        </excludes>
+                                        <includes>
+                                            <include>org.springframework:*:4.3.*</include>
+                                        </includes>
+                                    </bannedDependencies>
+                                </rules>
+                                <fail>true</fail>
+                            </configuration>
+                        </execution>
+                    </executions>
+                </plugin>
+                <!-- dependency相关插件 -->
+                <plugin>
+                    <groupId>org.apache.maven.plugins</groupId>
+                    <artifactId>maven-dependency-plugin</artifactId>
+                    <version>2.10</version>
+                </plugin>
+                <!-- ant插件 -->
+                <plugin>
+                    <groupId>org.apache.maven.plugins</groupId>
+                    <artifactId>maven-antrun-plugin</artifactId>
+                    <version>1.8</version>
+                </plugin>
+
+                <!-- exec java 插件 -->
+                <plugin>
+                    <groupId>org.codehaus.mojo</groupId>
+                    <artifactId>exec-maven-plugin</artifactId>
+                    <version>1.6.0</version>
+                </plugin>
+                <plugin>
+                    <groupId>org.apache.maven.plugins</groupId>
+                    <artifactId>maven-release-plugin</artifactId>
+                    <version>2.5.3</version>
+                    <configuration>
+                        <!--<tagBase>https://192.168.1.100:8443/svn/myapp/tags/</tagBase>-->
+                    </configuration>
+                </plugin>
+                <plugin>
+                    <groupId>org.eclipse.m2e</groupId>
+                    <artifactId>lifecycle-mapping</artifactId>
+                    <version>1.0.0</version>
+                    <configuration>
+                        <lifecycleMappingMetadata>
+                            <pluginExecutions>
+                                <pluginExecution>
+                                    <pluginExecutionFilter>
+                                        <groupId>org.apache.maven.plugins</groupId>
+                                        <artifactId>maven-dependency-plugin</artifactId>
+                                        <versionRange>[2.0,)</versionRange>
+                                        <goals>
+                                            <goal>copy-dependencies</goal>
+                                        </goals>
+                                    </pluginExecutionFilter>
+                                    <action>
+                                        <ignore/>
+                                    </action>
+                                </pluginExecution>
+                            </pluginExecutions>
+                        </lifecycleMappingMetadata>
+                    </configuration>
+                </plugin>
+            </plugins>
+        </pluginManagement>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-source-plugin</artifactId>
+            </plugin>
+        </plugins>
+    </build>
+</project>

+ 8 - 0
fm-server/README.md

@@ -0,0 +1,8 @@
+fm-server 开发web服务模块
+============ 
+
+说明
+---------------
+
+最新变化
+---------------

+ 19 - 0
fm-server/pom.xml

@@ -0,0 +1,19 @@
+<?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">
+    <parent>
+        <artifactId>fm-parent</artifactId>
+        <groupId>com.persagy</groupId>
+        <version>3.0.0</version>
+        <relativePath>../fm-parent</relativePath>
+    </parent>
+    <modelVersion>4.0.0</modelVersion>
+    <artifactId>fm-server</artifactId>
+    <dependencies>
+        <dependency>
+            <groupId>com.persagy</groupId>
+            <artifactId>integrated-common-core</artifactId>
+        </dependency>
+    </dependencies>
+</project>

+ 40 - 0
fm-server/src/main/java/com/persagy/fm/server/ServerApplication.java

@@ -0,0 +1,40 @@
+package com.persagy.fm.server;
+
+import com.persagy.log.annotation.EnableControllerLog;
+import org.springframework.boot.Banner;
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.boot.autoconfigure.aop.AopAutoConfiguration;
+import org.springframework.boot.context.event.ApplicationStartedEvent;
+import org.springframework.cache.annotation.EnableCaching;
+import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
+import org.springframework.cloud.openfeign.EnableFeignClients;
+import org.springframework.context.ApplicationListener;
+import org.springframework.context.annotation.ComponentScan;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.scheduling.annotation.EnableScheduling;
+
+/**
+ * springboot服务器
+ * 同时启动Eureka和Feign
+ * @author Charlie Yu
+ * @version 1.0 2021-03-02
+ */
+@EnableCaching
+@EnableFeignClients
+@EnableControllerLog
+@EnableDiscoveryClient
+@Configuration
+@EnableScheduling
+@SpringBootApplication(exclude = AopAutoConfiguration.class)
+@ComponentScan(basePackages = {"com.persagy.fm"})
+public class ServerApplication {
+
+    public static void main(String[] args) {
+        SpringApplication application = new SpringApplication(ServerApplication.class);
+        application.setBannerMode(Banner.Mode.OFF);
+        application.addListeners((ApplicationListener<ApplicationStartedEvent>) event ->
+                System.out.println("\033[1;93;44m>>>>>> " + event.getApplicationContext().getApplicationName().substring(1) + " has started!!! \033[m"));
+        application.run(args);
+    }
+}

+ 7 - 4
pom.xml

@@ -2,16 +2,19 @@
 <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>
-        <artifactId>integrated-platform</artifactId>
         <groupId>com.persagy</groupId>
-        <version>1.0.0</version>
+        <artifactId>fm-parent</artifactId>
+        <version>3.0.0</version>
+        <relativePath>fm-parent</relativePath>
     </parent>
-    <modelVersion>4.0.0</modelVersion>
+    <artifactId>fm-basics</artifactId>
     <packaging>pom</packaging>
     <description>FM组件项目,只提供jar,不提供服务</description>
-    <artifactId>fm-basics</artifactId>
     <modules>
+        <module>fm-parent</module>
+        <module>fm-server</module>
         <module>fm-common</module>
     </modules>
 </project>