Quellcode durchsuchen

增加cloud模块

wei vor 5 Jahren
Ursprung
Commit
d88290cc12
23 geänderte Dateien mit 563 neuen und 109 gelöschten Zeilen
  1. 10 0
      base-springframework-starter/pom.xml
  2. 16 0
      base-springframework/base-springframework-cloud/src/main/java/com/wei/base/springframework/cloud/config/FeignConfigure.java
  3. 85 0
      base-springframework/base-springframework-cloud/src/main/java/com/wei/base/springframework/cloud/env/InitializeCustomConfiguration.java
  4. 45 0
      base-springframework/base-springframework-cloud/src/main/java/com/wei/base/springframework/cloud/feign/FeginErrorDecoder.java
  5. 34 0
      base-springframework/base-springframework-cloud/src/main/java/com/wei/base/springframework/cloud/feign/FeignDecoder.java
  6. 59 0
      base-springframework/base-springframework-cloud/src/main/java/com/wei/base/springframework/cloud/feign/FeignInterceptor.java
  7. 178 0
      base-springframework/base-springframework-cloud/src/main/resources/logback-spring.xml
  8. 43 12
      base-springframework/base-springframework-core/src/main/java/com/wei/base/springframework/core/env/InitializeCustomConfiguration.java
  9. 3 3
      base-springframework/base-springframework-core/src/main/java/com/wei/base/springframework/core/http/converter/FastJsonConverter.java
  10. 0 2
      base-springframework/base-springframework-core/src/main/resources/META-INF/spring.factories
  11. 0 0
      base-springframework/base-springframework-core/src/main/resources/application-common.yml
  12. 43 12
      base-springframework/base-springframework-mysql/src/main/java/com/wei/base/springframework/mysql/env/InitializeCustomConfiguration.java
  13. 0 0
      base-springframework/base-springframework-mysql/src/main/resources/application-mysql.yml
  14. 1 0
      base-springframework/pom.xml
  15. 3 1
      pom.xml
  16. 18 0
      wei-test-cloud/wei-test-cloud-app/src/main/java/com/wei/cloud/api/impl/ApiServiceImpl.java
  17. 5 0
      wei-test-cloud/wei-test-cloud-app/src/main/java/com/wei/cloud/service/CloudService.java
  18. 13 0
      wei-test-cloud/wei-test-cloud-app/src/main/java/com/wei/cloud/service/impl/CloudServiceImpl.java
  19. 7 0
      wei-test/pom.xml
  20. 0 17
      wei-test/src/main/java/com/wei/test/TestApplication.java
  21. 0 29
      wei-test/src/main/java/com/wei/test/controller/TestController.java
  22. 0 33
      wei-test/src/main/java/com/wei/test/vo/UserVO.java
  23. 0 0
      wei-test/src/main/resources/application.yml

+ 10 - 0
base-springframework-starter/pom.xml

@@ -7,6 +7,11 @@
     <packaging>pom</packaging>
     <version>0.0.1-SNAPSHOT</version>
 
+    <modules>
+        <module>../wei-test</module>
+        <module>../wei-test-cloud</module>
+    </modules>
+
     <properties>
         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
         <java.version>1.8</java.version>
@@ -25,6 +30,11 @@
             <artifactId>base-springframework-core</artifactId>
             <version>${project.version}</version>
         </dependency>
+        <dependency>
+            <groupId>com.wei</groupId>
+            <artifactId>base-springframework-cloud</artifactId>
+            <version>${project.version}</version>
+        </dependency>
     </dependencies>
 
     <dependencyManagement>

+ 16 - 0
base-springframework/base-springframework-cloud/src/main/java/com/wei/base/springframework/cloud/config/FeignConfigure.java

@@ -0,0 +1,16 @@
+package com.wei.base.springframework.cloud.config;
+
+import lombok.Data;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.context.annotation.Configuration;
+
+@Data
+@ConfigurationProperties(prefix = "spring.response-body-advice")
+@Configuration
+public class FeignConfigure {
+
+    /**
+     * 过滤header参数传递字段
+     */
+    private String headerTransmitFilter = "Filter-Enabled";
+}

+ 85 - 0
base-springframework/base-springframework-cloud/src/main/java/com/wei/base/springframework/cloud/env/InitializeCustomConfiguration.java

@@ -0,0 +1,85 @@
+package com.wei.base.springframework.cloud.env;
+
+import com.google.common.collect.Lists;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.env.EnvironmentPostProcessor;
+import org.springframework.boot.env.YamlPropertySourceLoader;
+import org.springframework.core.env.ConfigurableEnvironment;
+import org.springframework.core.env.MutablePropertySources;
+import org.springframework.core.env.PropertiesPropertySource;
+import org.springframework.core.env.PropertySource;
+import org.springframework.core.io.ClassPathResource;
+import org.springframework.core.io.Resource;
+
+import java.io.IOException;
+import java.util.List;
+import java.util.Properties;
+
+/**
+ * 加载自定义配置文件
+ *
+ * @author : weierming
+ * @date : 2020/7/13
+ */
+public class InitializeCustomConfiguration implements EnvironmentPostProcessor {
+
+    //自定义配置文件地址
+    private static final List<String> PROFILES = Lists.newArrayList("application-cloud.yml");
+
+    @Override
+    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
+        PROFILES.stream().forEach(profile -> {
+            //从classpath路径下面查找文件
+            Resource resource = new ClassPathResource(profile);
+            if (resource == null || !resource.exists()) {
+                throw new IllegalArgumentException("资源" + resource + "不存在");
+            }
+
+            MutablePropertySources mutablePropertySources = environment.getPropertySources();
+            //加载成PropertySource对象,并添加到Environment环境中
+            switch (StringUtils.substringAfterLast(profile, ".")) {
+                case "yml":
+                    List<PropertySource<?>> propertySources = loadYmlProfiles(resource);
+                    propertySources.stream().forEach(propertySource -> {
+                        mutablePropertySources.addLast(propertySource);
+                    });
+                    break;
+                default:
+                    mutablePropertySources.addLast(loadProfiles(resource));
+                    break;
+            }
+        });
+    }
+
+    /**
+     * 加载单个配置文件
+     *
+     * @param resource
+     * @return
+     */
+    private PropertiesPropertySource loadProfiles(Resource resource) {
+        try {
+            Properties properties = new Properties();
+            properties.load(resource.getInputStream());
+            return new PropertiesPropertySource(resource.getFilename(), properties);
+        } catch (IOException ex) {
+            throw new IllegalStateException("加载配置文件失败" + resource, ex);
+        }
+    }
+
+    /**
+     * 加载yml格式配置文件
+     *
+     * @param resource
+     * @return
+     */
+    private List<PropertySource<?>> loadYmlProfiles(Resource resource) {
+        try {
+            YamlPropertySourceLoader yamlPropertySourceLoader = new YamlPropertySourceLoader();
+            return yamlPropertySourceLoader.load(resource.getFilename(), resource);
+        } catch (IOException ex) {
+            throw new IllegalStateException("加载配置文件失败" + resource, ex);
+        }
+    }
+}

+ 45 - 0
base-springframework/base-springframework-cloud/src/main/java/com/wei/base/springframework/cloud/feign/FeginErrorDecoder.java

@@ -0,0 +1,45 @@
+package com.wei.base.springframework.cloud.feign;
+
+import com.google.common.base.Charsets;
+import feign.FeignException;
+import feign.Response;
+import feign.Util;
+import feign.codec.ErrorDecoder;
+import lombok.extern.slf4j.Slf4j;
+
+import java.io.IOException;
+
+import static feign.FeignException.errorStatus;
+
+/**
+ * feign错误拦截器
+ *
+ * @author : weierming
+ * @date : 2020/7/24
+ */
+@Slf4j
+public class FeginErrorDecoder implements ErrorDecoder {
+
+    @Override
+    public Exception decode(String methodKey, Response response) {
+        try {
+            String body = Util.toString(response.body().asReader(Charsets.UTF_8));
+            log.error("body:{}", body);
+        } catch (IOException e) {
+            log.error("读取body失败!", e);
+        }
+
+        FeignException e = errorStatus(methodKey, response);
+        log.error("request:{}", response.request(), e);
+        Exception exception = null;
+        switch (response.status()) {
+            case 404:
+                exception = new Exception("服务不存在!");
+
+            default:
+                break;
+        }
+
+        return exception;
+    }
+}

+ 34 - 0
base-springframework/base-springframework-cloud/src/main/java/com/wei/base/springframework/cloud/feign/FeignDecoder.java

@@ -0,0 +1,34 @@
+package com.wei.base.springframework.cloud.feign;
+
+import feign.FeignException;
+import feign.Response;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.ObjectFactory;
+import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
+import org.springframework.cloud.openfeign.support.SpringDecoder;
+import org.springframework.context.annotation.Configuration;
+
+import java.io.IOException;
+import java.lang.reflect.Type;
+
+/**
+ * feign出参返回值
+ *
+ * @author : weierming
+ * @date : 2020/7/24
+ */
+@Slf4j
+@Configuration
+public class FeignDecoder extends SpringDecoder {
+
+    public FeignDecoder(ObjectFactory<HttpMessageConverters> messageConverters) {
+        super(messageConverters);
+    }
+
+    @Override
+    public Object decode(Response response, Type type) throws IOException, FeignException {
+        String rString = String.valueOf(super.decode(response, String.class));
+        log.info("url:{}, body:{}", response.request().url(), rString);
+        return rString;
+    }
+}

+ 59 - 0
base-springframework/base-springframework-cloud/src/main/java/com/wei/base/springframework/cloud/feign/FeignInterceptor.java

@@ -0,0 +1,59 @@
+package com.wei.base.springframework.cloud.feign;
+
+import com.wei.base.springframework.cloud.config.FeignConfigure;
+import feign.RequestInterceptor;
+import feign.RequestTemplate;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.web.context.request.RequestAttributes;
+import org.springframework.web.context.request.RequestContextHolder;
+import org.springframework.web.context.request.ServletRequestAttributes;
+
+import javax.servlet.http.HttpServletRequest;
+import java.util.Collection;
+import java.util.Enumeration;
+import java.util.Map;
+
+/**
+ * feign拦截器,主要用于往header中添加参数
+ *
+ * @author : weierming
+ * @date : 2020/7/23
+ */
+@Configuration
+public class FeignInterceptor implements RequestInterceptor {
+
+    @Autowired
+    private FeignConfigure feignConfigure;
+
+    /**
+     * 封装fegin header参数传递
+     *
+     * @param template
+     */
+    @Override
+    public void apply(RequestTemplate template) {
+        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
+
+        if (requestAttributes != null) {
+            ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes;
+            HttpServletRequest httpServletRequest = servletRequestAttributes.getRequest();
+            Map<String, Collection<String>> headers = template.headers();
+
+            //有些第三方接口有做安全校验如果header传递的话会导致报错
+            String headerTransmitFilter = feignConfigure.getHeaderTransmitFilter();
+            if (headers.containsKey(headerTransmitFilter)) {
+                Collection<String> key = headers.get(headerTransmitFilter);
+                if (key.contains("true")) {
+                    return;
+                }
+            }
+
+            Enumeration<String> headNames = httpServletRequest.getHeaderNames();
+            while (headNames.hasMoreElements()) {
+                String headName = headNames.nextElement();
+                template.header(headName, httpServletRequest.getHeader(headName));
+            }
+        }
+    }
+}

+ 178 - 0
base-springframework/base-springframework-cloud/src/main/resources/logback-spring.xml

@@ -0,0 +1,178 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!-- 日志级别从低到高分为TRACE < DEBUG < INFO < WARN < ERROR < FATAL,如果设置为WARN,则低于WARN的信息都不会输出 -->
+<!-- scan:当此属性设置为true时,配置文件如果发生改变,将会被重新加载,默认值为true -->
+<!-- scanPeriod:设置监测配置文件是否有修改的时间间隔,如果没有给出时间单位,默认单位是毫秒。当scan为true时,此属性生效。默认的时间间隔为1分钟。 -->
+<!-- debug:当此属性设置为true时,将打印出logback内部日志信息,实时查看logback运行状态。默认值为false。 -->
+<configuration scan="true" scanPeriod="10 seconds">
+
+    <define name="logPath" class="com.wei.base.springframework.core.log.config.LogProperty"/>
+    <property name="log.path" value="/data/release/logs/${logPath}"/>
+    <!--<property name="log.path" value="/data/release/logs"/>-->
+    <property name="log.lever" value="debug"/>
+
+    <!-- 彩色日志 -->
+    <!-- 彩色日志依赖的渲染类 -->
+    <conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter"/>
+    <conversionRule conversionWord="wex"
+                    converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter"/>
+    <conversionRule conversionWord="wEx"
+                    converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter"/>
+    <!-- 彩色日志格式 -->
+    <property name="CONSOLE_LOG_PATTERN"
+              value="${CONSOLE_LOG_PATTERN:-%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>
+
+    <!--输出到控制台-->
+    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
+        <!--此日志appender是为开发使用,只配置最底级别,控制台输出的日志级别是大于或等于此级别的日志信息-->
+        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
+            <level>info</level>
+        </filter>
+        <encoder>
+            <Pattern>${CONSOLE_LOG_PATTERN}</Pattern>
+            <!-- 设置字符集 -->
+            <charset>UTF-8</charset>
+        </encoder>
+    </appender>
+
+    <!--输出到文件-->
+    <!-- 时间滚动输出 level为 DEBUG 日志 -->
+    <appender name="DEBUG_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
+        <!-- 正在记录的日志文件的路径及文件名 -->
+        <file>${log.path}/debug.log</file>
+        <!--日志文件输出格式-->
+        <encoder>
+            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
+            <charset>UTF-8</charset> <!-- 设置字符集 -->
+        </encoder>
+        <!-- 日志记录器的滚动策略,按日期,按大小记录 -->
+        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
+            <!-- 日志归档 -->
+            <fileNamePattern>${log.path}/debug/log-debug-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
+            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
+                <maxFileSize>100MB</maxFileSize>
+            </timeBasedFileNamingAndTriggeringPolicy>
+            <!--日志文件保留天数-->
+            <maxHistory>15</maxHistory>
+        </rollingPolicy>
+        <!-- 此日志文件只记录debug级别的 -->
+        <filter class="ch.qos.logback.classic.filter.LevelFilter">
+            <level>debug</level>
+            <onMatch>ACCEPT</onMatch>
+            <onMismatch>DENY</onMismatch>
+        </filter>
+    </appender>
+
+    <!-- 时间滚动输出 level为 INFO 日志 -->
+    <appender name="INFO_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
+        <!-- 正在记录的日志文件的路径及文件名 -->
+        <file>${log.path}/info.log</file>
+        <!--日志文件输出格式-->
+        <encoder>
+            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
+            <charset>UTF-8</charset>
+        </encoder>
+        <!-- 日志记录器的滚动策略,按日期,按大小记录 -->
+        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
+            <!-- 每天日志归档路径以及格式 -->
+            <fileNamePattern>${log.path}/info/log-info-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
+            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
+                <maxFileSize>100MB</maxFileSize>
+            </timeBasedFileNamingAndTriggeringPolicy>
+            <!--日志文件保留天数-->
+            <maxHistory>15</maxHistory>
+        </rollingPolicy>
+        <!-- 此日志文件只记录info级别的 -->
+        <filter class="ch.qos.logback.classic.filter.LevelFilter">
+            <level>info</level>
+            <onMatch>ACCEPT</onMatch>
+            <onMismatch>DENY</onMismatch>
+        </filter>
+    </appender>
+
+    <!-- 时间滚动输出 level为 WARN 日志 -->
+    <appender name="WARN_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
+        <!-- 正在记录的日志文件的路径及文件名 -->
+        <file>${log.path}/warn.log</file>
+        <!--日志文件输出格式-->
+        <encoder>
+            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
+            <charset>UTF-8</charset> <!-- 此处设置字符集 -->
+        </encoder>
+        <!-- 日志记录器的滚动策略,按日期,按大小记录 -->
+        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
+            <fileNamePattern>${log.path}/warn/log-warn-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
+            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
+                <maxFileSize>100MB</maxFileSize>
+            </timeBasedFileNamingAndTriggeringPolicy>
+            <!--日志文件保留天数-->
+            <maxHistory>15</maxHistory>
+        </rollingPolicy>
+        <!-- 此日志文件只记录warn级别的 -->
+        <filter class="ch.qos.logback.classic.filter.LevelFilter">
+            <level>warn</level>
+            <onMatch>ACCEPT</onMatch>
+            <onMismatch>DENY</onMismatch>
+        </filter>
+    </appender>
+
+
+    <!-- 时间滚动输出 level为 ERROR 日志 -->
+    <appender name="ERROR_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
+        <!-- 正在记录的日志文件的路径及文件名 -->
+        <file>${log.path}/error.log</file>
+        <!--日志文件输出格式-->
+        <encoder>
+            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
+            <charset>UTF-8</charset> <!-- 此处设置字符集 -->
+        </encoder>
+        <!-- 日志记录器的滚动策略,按日期,按大小记录 -->
+        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
+            <fileNamePattern>${log.path}/error/log-error-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
+            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
+                <maxFileSize>100MB</maxFileSize>
+            </timeBasedFileNamingAndTriggeringPolicy>
+            <!--日志文件保留天数-->
+            <maxHistory>15</maxHistory>
+        </rollingPolicy>
+        <!-- 此日志文件只记录ERROR级别的 -->
+        <filter class="ch.qos.logback.classic.filter.LevelFilter">
+            <level>ERROR</level>
+            <onMatch>ACCEPT</onMatch>
+            <onMismatch>DENY</onMismatch>
+        </filter>
+    </appender>
+
+    <!--
+        <logger>用来设置某一个包或者具体的某一个类的日志打印级别、
+        以及指定<appender>。<logger>仅有一个name属性,
+        一个可选的level和一个可选的addtivity属性。
+        name:用来指定受此logger约束的某一个包或者具体的某一个类。
+        level:用来设置打印级别,大小写无关:TRACE, DEBUG, INFO, WARN, ERROR, ALL 和 OFF,
+              还有一个特俗值INHERITED或者同义词NULL,代表强制执行上级的级别。
+              如果未设置此属性,那么当前logger将会继承上级的级别。
+        addtivity:是否向上级logger传递打印信息。默认是true。
+    -->
+    <!--<logger name="org.springframework.web" level="info"/>-->
+    <!--<logger name="org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor" level="INFO"/>-->
+    <!--
+        使用mybatis的时候,sql语句是debug下才会打印,而这里我们只配置了info,所以想要查看sql语句的话,有以下两种操作:
+        第一种把<root level="info">改成<root level="DEBUG">这样就会打印sql,不过这样日志那边会出现很多其他消息
+        第二种就是单独给dao下目录配置debug模式,代码如下,这样配置sql语句会打印,其他还是正常info级别:
+     -->
+
+
+    <!--
+        root节点是必选节点,用来指定最基础的日志输出级别,只有一个level属性
+        level:用来设置打印级别,大小写无关:TRACE, DEBUG, INFO, WARN, ERROR, ALL 和 OFF,
+        不能设置为INHERITED或者同义词NULL。默认是DEBUG
+        可以包含零个或多个元素,标识这个appender将会添加到这个logger。
+    -->
+
+    <root level="info">
+        <appender-ref ref="CONSOLE"/>
+        <appender-ref ref="DEBUG_FILE"/>
+        <appender-ref ref="INFO_FILE"/>
+        <appender-ref ref="WARN_FILE"/>
+        <appender-ref ref="ERROR_FILE"/>
+    </root>
+</configuration>

+ 43 - 12
base-springframework/base-springframework-core/src/main/java/com/wei/base/springframework/core/env/InitializeCustomConfiguration.java

@@ -1,9 +1,12 @@
 package com.wei.base.springframework.core.env;
 
 import com.google.common.collect.Lists;
+import org.apache.commons.lang3.StringUtils;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.env.EnvironmentPostProcessor;
+import org.springframework.boot.env.YamlPropertySourceLoader;
 import org.springframework.core.env.ConfigurableEnvironment;
+import org.springframework.core.env.MutablePropertySources;
 import org.springframework.core.env.PropertiesPropertySource;
 import org.springframework.core.env.PropertySource;
 import org.springframework.core.io.ClassPathResource;
@@ -21,32 +24,60 @@ import java.util.Properties;
  */
 public class InitializeCustomConfiguration implements EnvironmentPostProcessor {
 
-    //Properties对象
-    private final Properties PROPERTIES = new Properties();
-
     //自定义配置文件地址
-    private static final List<String> PROFILES = Lists.newArrayList("common-application.yml");
+    private static final List<String> PROFILES = Lists.newArrayList("application-common.yml");
 
     @Override
     public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
         PROFILES.stream().forEach(profile -> {
             //从classpath路径下面查找文件
             Resource resource = new ClassPathResource(profile);
+            if (resource == null || !resource.exists()) {
+                throw new IllegalArgumentException("资源" + resource + "不存在");
+            }
+
+            MutablePropertySources mutablePropertySources = environment.getPropertySources();
             //加载成PropertySource对象,并添加到Environment环境中
-            environment.getPropertySources().addLast(loadProfiles(resource));
+            switch (StringUtils.substringAfterLast(profile, ".")) {
+                case "yml":
+                    List<PropertySource<?>> propertySources = loadYmlProfiles(resource);
+                    propertySources.stream().forEach(propertySource -> {
+                        mutablePropertySources.addLast(propertySource);
+                    });
+                    break;
+                default:
+                    mutablePropertySources.addLast(loadProfiles(resource));
+                    break;
+            }
         });
     }
 
-    //加载单个配置文件
-    private PropertySource<?> loadProfiles(Resource resource) {
-        if (resource == null || !resource.exists()) {
-            throw new IllegalArgumentException("资源" + resource + "不存在");
+    /**
+     * 加载单个配置文件
+     *
+     * @param resource
+     * @return
+     */
+    private PropertiesPropertySource loadProfiles(Resource resource) {
+        try {
+            Properties properties = new Properties();
+            properties.load(resource.getInputStream());
+            return new PropertiesPropertySource(resource.getFilename(), properties);
+        } catch (IOException ex) {
+            throw new IllegalStateException("加载配置文件失败" + resource, ex);
         }
+    }
 
+    /**
+     * 加载yml格式配置文件
+     *
+     * @param resource
+     * @return
+     */
+    private List<PropertySource<?>> loadYmlProfiles(Resource resource) {
         try {
-            //从输入流中加载一个Properties对象
-            PROPERTIES.load(resource.getInputStream());
-            return new PropertiesPropertySource(resource.getFilename(), PROPERTIES);
+            YamlPropertySourceLoader yamlPropertySourceLoader = new YamlPropertySourceLoader();
+            return yamlPropertySourceLoader.load(resource.getFilename(), resource);
         } catch (IOException ex) {
             throw new IllegalStateException("加载配置文件失败" + resource, ex);
         }

+ 3 - 3
base-springframework/base-springframework-core/src/main/java/com/wei/base/springframework/core/http/converter/FastJsonConverter.java

@@ -7,10 +7,10 @@ import com.google.common.base.Charsets;
 import com.google.common.collect.Lists;
 import com.wei.base.springframework.core.web.config.ResponseHandlerConfigure;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 import org.springframework.http.MediaType;
-import org.springframework.http.converter.HttpMessageConverter;
 
 @Configuration
 public class FastJsonConverter {
@@ -19,7 +19,7 @@ public class FastJsonConverter {
     private ResponseHandlerConfigure responseHandlerConfigure;
 
     @Bean
-    public HttpMessageConverter<Object> configureMessageConverters() {
+    public HttpMessageConverters configureMessageConverters() {
         FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
 
         FastJsonConfig fastJsonConfig = new FastJsonConfig();
@@ -33,6 +33,6 @@ public class FastJsonConverter {
 
         fastConverter.setFastJsonConfig(fastJsonConfig);
         fastConverter.setSupportedMediaTypes(Lists.newArrayList(MediaType.APPLICATION_JSON));
-        return fastConverter;
+        return new HttpMessageConverters(fastConverter);
     }
 }

+ 0 - 2
base-springframework/base-springframework-core/src/main/resources/META-INF/spring.factories

@@ -1,6 +1,4 @@
 org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
 	com.wei.base.springframework.core.CoreConfiguration
-#org.springframework.boot.env.EnvironmentPostProcessor=\
-#	org.springframework.core.web.config.Test
 org.springframework.boot.env.EnvironmentPostProcessor=\
   com.wei.base.springframework.core.env.InitializeCustomConfiguration

+ 0 - 0
base-springframework/base-springframework-core/src/main/resources/common-application.yml → base-springframework/base-springframework-core/src/main/resources/application-common.yml


+ 43 - 12
base-springframework/base-springframework-mysql/src/main/java/com/wei/base/springframework/mysql/env/InitializeCustomConfiguration.java

@@ -1,9 +1,12 @@
 package com.wei.base.springframework.mysql.env;
 
 import com.google.common.collect.Lists;
+import org.apache.commons.lang3.StringUtils;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.env.EnvironmentPostProcessor;
+import org.springframework.boot.env.YamlPropertySourceLoader;
 import org.springframework.core.env.ConfigurableEnvironment;
+import org.springframework.core.env.MutablePropertySources;
 import org.springframework.core.env.PropertiesPropertySource;
 import org.springframework.core.env.PropertySource;
 import org.springframework.core.io.ClassPathResource;
@@ -21,32 +24,60 @@ import java.util.Properties;
  */
 public class InitializeCustomConfiguration implements EnvironmentPostProcessor {
 
-    //Properties对象
-    private final Properties PROPERTIES = new Properties();
-
     //自定义配置文件地址
-    private static final List<String> PROFILES = Lists.newArrayList("mysql-application.yml");
+    private static final List<String> PROFILES = Lists.newArrayList("application-mysql.yml");
 
     @Override
     public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
         PROFILES.stream().forEach(profile -> {
             //从classpath路径下面查找文件
             Resource resource = new ClassPathResource(profile);
+            if (resource == null || !resource.exists()) {
+                throw new IllegalArgumentException("资源" + resource + "不存在");
+            }
+
+            MutablePropertySources mutablePropertySources = environment.getPropertySources();
             //加载成PropertySource对象,并添加到Environment环境中
-            environment.getPropertySources().addLast(loadProfiles(resource));
+            switch (StringUtils.substringAfterLast(profile, ".")) {
+                case "yml":
+                    List<PropertySource<?>> propertySources = loadYmlProfiles(resource);
+                    propertySources.stream().forEach(propertySource -> {
+                        mutablePropertySources.addLast(propertySource);
+                    });
+                    break;
+                default:
+                    mutablePropertySources.addLast(loadProfiles(resource));
+                    break;
+            }
         });
     }
 
-    //加载单个配置文件
-    private PropertySource<?> loadProfiles(Resource resource) {
-        if (resource == null || !resource.exists()) {
-            throw new IllegalArgumentException("资源" + resource + "不存在");
+    /**
+     * 加载单个配置文件
+     *
+     * @param resource
+     * @return
+     */
+    private PropertiesPropertySource loadProfiles(Resource resource) {
+        try {
+            Properties properties = new Properties();
+            properties.load(resource.getInputStream());
+            return new PropertiesPropertySource(resource.getFilename(), properties);
+        } catch (IOException ex) {
+            throw new IllegalStateException("加载配置文件失败" + resource, ex);
         }
+    }
 
+    /**
+     * 加载yml格式配置文件
+     *
+     * @param resource
+     * @return
+     */
+    private List<PropertySource<?>> loadYmlProfiles(Resource resource) {
         try {
-            //从输入流中加载一个Properties对象
-            PROPERTIES.load(resource.getInputStream());
-            return new PropertiesPropertySource(resource.getFilename(), PROPERTIES);
+            YamlPropertySourceLoader yamlPropertySourceLoader = new YamlPropertySourceLoader();
+            return yamlPropertySourceLoader.load(resource.getFilename(), resource);
         } catch (IOException ex) {
             throw new IllegalStateException("加载配置文件失败" + resource, ex);
         }

+ 0 - 0
base-springframework/base-springframework-mysql/src/main/resources/mysql-application.yml → base-springframework/base-springframework-mysql/src/main/resources/application-mysql.yml


+ 1 - 0
base-springframework/pom.xml

@@ -11,6 +11,7 @@
         <module>base-springframework-util</module>
         <module>base-springframework-core</module>
         <module>base-springframework-mysql</module>
+        <module>base-springframework-cloud</module>
     </modules>
 
     <properties>

+ 3 - 1
pom.xml

@@ -10,8 +10,10 @@
     <name>基础框架</name>
 
     <modules>
-        <module>base-springframework-starter</module>
         <module>base-springframework</module>
+        <module>base-springframework-starter</module>
+        <module>wei-test</module>
+        <module>wei-test-cloud</module>
     </modules>
 
     <properties>

+ 18 - 0
wei-test-cloud/wei-test-cloud-app/src/main/java/com/wei/cloud/api/impl/ApiServiceImpl.java

@@ -0,0 +1,18 @@
+package com.wei.cloud.api.impl;
+
+import com.wei.cloud.api.ApiService;
+import com.wei.cloud.service.CloudService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.RestController;
+
+@RestController
+public class ApiServiceImpl implements ApiService {
+
+    @Autowired
+    private CloudService cloudService;
+
+    @Override
+    public String test(String b) {
+        return cloudService.test();
+    }
+}

+ 5 - 0
wei-test-cloud/wei-test-cloud-app/src/main/java/com/wei/cloud/service/CloudService.java

@@ -0,0 +1,5 @@
+package com.wei.cloud.service;
+
+public interface CloudService {
+    String test();
+}

+ 13 - 0
wei-test-cloud/wei-test-cloud-app/src/main/java/com/wei/cloud/service/impl/CloudServiceImpl.java

@@ -0,0 +1,13 @@
+package com.wei.cloud.service.impl;
+
+import com.wei.cloud.service.CloudService;
+import org.springframework.stereotype.Service;
+
+@Service
+public class CloudServiceImpl implements CloudService {
+
+    @Override
+    public String test() {
+        return "test";
+    }
+}

+ 7 - 0
wei-test/pom.xml

@@ -8,7 +8,14 @@
         <version>0.0.1-SNAPSHOT</version>
     </parent>
 
+    <packaging>pom</packaging>
     <artifactId>wei-test</artifactId>
+
+    <modules>
+        <module>wei-test-api</module>
+        <module>wei-test-app</module>
+    </modules>
+
     <build>
         <plugins>
             <plugin>

+ 0 - 17
wei-test/src/main/java/com/wei/test/TestApplication.java

@@ -1,17 +0,0 @@
-package com.wei.test;
-
-import lombok.SneakyThrows;
-import org.springframework.boot.SpringApplication;
-import org.springframework.boot.autoconfigure.SpringBootApplication;
-import org.springframework.boot.context.properties.EnableConfigurationProperties;
-
-
-@EnableConfigurationProperties
-@SpringBootApplication
-public class TestApplication {
-
-    @SneakyThrows
-    public static void main(String[] args) {
-        SpringApplication.run(TestApplication.class, args);
-    }
-}

+ 0 - 29
wei-test/src/main/java/com/wei/test/controller/TestController.java

@@ -1,29 +0,0 @@
-package com.wei.test.controller;
-
-import com.wei.base.springframework.core.vo.RestfulVO;
-import com.wei.test.vo.UserVO;
-import org.springframework.web.bind.annotation.*;
-
-@RestController
-public class TestController {
-
-    private String test = "123456";
-
-    @GetMapping("/test")
-    public RestfulVO<String> test(@RequestParam("test") String test1) {
-        System.err.println(test1);
-        return new RestfulVO("test");
-    }
-
-    @PostMapping("/test1")
-    public RestfulVO<String> test(@RequestBody UserVO userVO) {
-        System.err.println(userVO.toString());
-        return new RestfulVO("test");
-    }
-
-    @PostMapping("/test2/{ttt}")
-    public RestfulVO<String> test2(@PathVariable("ttt") String ttt) {
-        System.err.println(ttt);
-        return new RestfulVO("test");
-    }
-}

+ 0 - 33
wei-test/src/main/java/com/wei/test/vo/UserVO.java

@@ -1,33 +0,0 @@
-package com.wei.test.vo;
-
-import lombok.AllArgsConstructor;
-import lombok.Data;
-import lombok.NoArgsConstructor;
-
-import java.io.Serializable;
-import java.util.Date;
-import java.util.List;
-
-/**
- * 统一返回值
- *
- * @author : weierming
- * @date : 2020/7/10
- */
-@Data
-@NoArgsConstructor
-@AllArgsConstructor
-public class UserVO implements Serializable {
-
-    private static final long serialVersionUID = 6488177830405564354L;
-
-    private Long userId;
-
-    private String userName;
-
-    private Date date;
-
-    private List<UserVO> list;
-
-    private UserVO userVO;
-}

+ 0 - 0
wei-test/src/main/resources/application.yml