Last active
April 17, 2018 03:30
-
-
Save yangl/311cd221dddbaa1922fa7f4cae2c3c57 to your computer and use it in GitHub Desktop.
spring boot + MyBatis plus + spring data redis + swagger + Log4j2异步日志
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
server.port=18080 | |
spring.application.name=hackthonday | |
spring.aop.proxy-target-class=true | |
#### jackson相关配置 #### | |
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss | |
spring.jackson.joda-date-time-format=yyyy-MM-dd HH:mm:ss | |
spring.jackson.locale=zh_CN | |
spring.jackson.time-zone=GMT+8 | |
#### datasource相关配置 #### | |
spring.datasource.url=jdbc:mysql://xxxxx:3306/hackthonday?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true | |
spring.datasource.username=root | |
spring.datasource.password=Sf123456?/ | |
#### mybatis-plus相关配置 #### | |
mybatis-plus.mapper-locations=classpath:/mapper/**/*Mapper.xml | |
mybatis-plus.type-aliases-package=com.sf.hackthon.hackthonday.entity | |
mybatis-plus.global-config.id-type=2 | |
mybatis-plus.global-config.field-strategy=1 | |
mybatis-plus.global-config.refresh-mapper=true | |
mybatis-plus.configuration.map-underscore-to-camel-case=true | |
mybatis-plus.configuration.cache-enabled=false | |
#### redis相关配置 #### | |
spring.redis.host=xxxxxx | |
spring.redis.port=6379 | |
spring.redis.password=Sf123456?/ | |
spring.redis.database=0 | |
spring.redis.timeout=200 | |
spring.redis.pool.min-idle=1 | |
spring.redis.pool.max-active=20 | |
spring.redis.pool.max-idle=10 | |
spring.redis.pool.max-wait=500 | |
#### swagger相关配置 #### | |
spring.swagger.enabled=true | |
spring.swagger.title=编程马拉松后端服务接口 | |
spring.swagger.base-package=com.sf.hackthon.controller | |
spring.swagger.contact.name=上王者 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.google.common.base.Strings; | |
import com.sf.common.dto.WsResult; | |
import com.sf.common.exception.BusinessException; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import org.springframework.http.HttpStatus; | |
import org.springframework.validation.BindingResult; | |
import org.springframework.web.bind.MethodArgumentNotValidException; | |
import org.springframework.web.bind.annotation.ExceptionHandler; | |
import org.springframework.web.bind.annotation.ResponseStatus; | |
import org.springframework.web.bind.annotation.RestControllerAdvice; | |
/** | |
* 服务异常统一返回 | |
* | |
* @author YANGLIN | |
* @since 2017-12-15 13:55 | |
*/ | |
@RestControllerAdvice | |
public class GlobalExceptionHandler { | |
private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class); | |
@ExceptionHandler(Throwable.class) | |
@ResponseStatus(HttpStatus.OK) | |
public WsResult sysException(Exception e) { | |
WsResult rs = new WsResult(); | |
rs.setSystemError(); | |
String message = null; | |
if (e instanceof MethodArgumentNotValidException) { | |
MethodArgumentNotValidException argException = (MethodArgumentNotValidException) e; | |
rs.setParameterError(); | |
BindingResult bindingResult = argException.getBindingResult(); | |
message = bindingResult.hasErrors() ? bindingResult.getAllErrors().get(0).getDefaultMessage() | |
: null; | |
} else if (e instanceof BusinessException) { | |
message = e.getMessage(); | |
} | |
if (!Strings.isNullOrEmpty(message)) { | |
rs.setMsg(message); | |
} | |
logger.error("服务系统异常", e); | |
return rs; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.battcn.swagger.annotation.EnableSwagger2Doc; | |
import org.springframework.boot.SpringApplication; | |
import org.springframework.boot.autoconfigure.SpringBootApplication; | |
import org.springframework.scheduling.annotation.EnableScheduling; | |
/** | |
* 应用启动类 | |
* | |
* @author weijinliang | |
* @since 2017-12-15 19:08 | |
*/ | |
@EnableScheduling | |
@EnableSwagger2Doc | |
@SpringBootApplication | |
public class HackthondayApplication { | |
public static void main(String[] args) { | |
SpringApplication.run(HackthondayApplication.class, args); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<Configuration status="ERROR"> | |
<!-- 日志文件目录和压缩文件目录配置 --> | |
<Properties> | |
<Property name="fileName">/home/log</Property> | |
<Property name="fileGz">/home/log/gz</Property> | |
</Properties> | |
<Appenders> | |
<!-- 输出控制台日志的配置 --> | |
<Console name="console" target="SYSTEM_OUT"> | |
<!--控制台只输出level及以上级别的信息(onMatch),其他的直接拒绝(onMismatch)--> | |
<ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/> | |
<!-- 输出日志的格式 --> | |
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/> | |
</Console> | |
<!-- 打印出所有的信息,每次大小超过size,则这size大小的日志会自动存入按年份-月份建立的文件夹下面并进行压缩,作为存档 --> | |
<RollingRandomAccessFile fileName="${fileName}/web-info.log" filePattern="${fileGz}/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.web-info.gz" | |
immediateFlush="false" | |
name="infoFile"> | |
<PatternLayout | |
pattern="%d{yyyy-MM-dd 'at' HH:mm:ss z} [%t] %-5level %logger{36} %L %M - %msg%xEx%n"/> | |
<Policies> | |
<TimeBasedTriggeringPolicy interval="6" modulate="true"/> | |
<SizeBasedTriggeringPolicy size="50 MB"/> | |
</Policies> | |
<Filters> | |
<!-- 只记录info和warn级别信息 --> | |
<ThresholdFilter level="error" onMatch="DENY" onMismatch="NEUTRAL"/> | |
<ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/> | |
</Filters> | |
<!-- 指定每天的最大压缩包个数,默认7个,超过了会覆盖之前的 --> | |
<DefaultRolloverStrategy max="50"/> | |
</RollingRandomAccessFile> | |
<!-- 存储所有error信息 --> | |
<RollingRandomAccessFile fileName="${fileName}/web-error.log" filePattern="${fileGz}/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.web-error.gz" | |
immediateFlush="false" | |
name="errorFile"> | |
<PatternLayout | |
pattern="%d{yyyy-MM-dd 'at' HH:mm:ss z} [%t] %-5level %logger{36} %L %M - %msg%xEx%n"/> | |
<Policies> | |
<TimeBasedTriggeringPolicy interval="6" modulate="true"/> | |
<SizeBasedTriggeringPolicy size="50 MB"/> | |
</Policies> | |
<Filters> | |
<!-- 只记录error级别信息 --> | |
<ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/> | |
</Filters> | |
<!-- 指定每天的最大压缩包个数,默认7个,超过了会覆盖之前的 --> | |
<DefaultRolloverStrategy max="50"/> | |
</RollingRandomAccessFile> | |
</Appenders> | |
<!-- 全局配置,默认所有的Logger都继承此配置 --> | |
<Loggers> | |
<!-- AsyncRoot - 异步记录日志 - 需要LMAX Disruptor的支持 --> | |
<AsyncRoot additivity="false" level="info"> | |
<AppenderRef ref="console"/> | |
<AppenderRef ref="infoFile"/> | |
<AppenderRef ref="errorFile"/> | |
</AsyncRoot> | |
</Loggers> | |
</Configuration> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.baomidou.mybatisplus.plugins.PaginationInterceptor; | |
import com.baomidou.mybatisplus.plugins.PerformanceInterceptor; | |
import org.mybatis.spring.annotation.MapperScan; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.context.annotation.Profile; | |
/** | |
* mybatis-plus 配置类 | |
* | |
* @author YANGLIN | |
* @since 2017-12-15 13:55 | |
*/ | |
@Configuration | |
@MapperScan("com.sf.**.dao*") | |
public class MybatisPlusConfig { | |
/** | |
* mybatis-plus SQL执行效率插件【生产环境可以关闭】 | |
*/ | |
@Bean | |
@Profile({"dev", "test"}) | |
public PerformanceInterceptor performanceInterceptor() { | |
return new PerformanceInterceptor(); | |
} | |
/** | |
* mybatis-plus分页插件<br> 文档:http://mp.baomidou.com<br> | |
*/ | |
@Bean | |
public PaginationInterceptor paginationInterceptor() { | |
PaginationInterceptor paginationInterceptor = new PaginationInterceptor(); | |
paginationInterceptor.setLocalPage(true); | |
return paginationInterceptor; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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.sf.hackthon.pre</groupId> | |
<artifactId>hackthonday</artifactId> | |
<version>1.0.0</version> | |
<name>hackthonday</name> | |
<description>编程马拉松</description> | |
<parent> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-parent</artifactId> | |
<version>1.5.9.RELEASE</version> | |
<relativePath/> <!-- lookup parent from repository --> | |
</parent> | |
<properties> | |
<log4j2.version>2.10.0</log4j2.version> | |
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> | |
<java.version>1.8</java.version> | |
<spring-cloud.version>Edgware.RELEASE</spring-cloud.version> | |
</properties> | |
<dependencies> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-log4j2</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>com.lmax</groupId> | |
<artifactId>disruptor</artifactId> | |
<version>3.3.7</version> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-aop</artifactId> | |
<exclusions> | |
<exclusion> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-logging</artifactId> | |
</exclusion> | |
</exclusions> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.cloud</groupId> | |
<artifactId>spring-cloud-starter-hystrix</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-freemarker</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-web</artifactId> | |
<exclusions> | |
<exclusion> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-tomcat</artifactId> | |
</exclusion> | |
</exclusions> | |
</dependency> | |
<dependency> | |
<groupId>com.alibaba</groupId> | |
<artifactId>druid-spring-boot-starter</artifactId> | |
<version>1.1.6</version> | |
</dependency> | |
<!-- MP 生成代码使用 --> | |
<dependency> | |
<groupId>org.apache.velocity</groupId> | |
<artifactId>velocity-engine-core</artifactId> | |
<version>2.0</version> | |
</dependency> | |
<dependency> | |
<groupId>com.baomidou</groupId> | |
<artifactId>mybatis-plus</artifactId> | |
<version>2.1.6</version> | |
</dependency> | |
<dependency> | |
<groupId>com.baomidou</groupId> | |
<artifactId>mybatisplus-spring-boot-starter</artifactId> | |
<version>1.0.5</version> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-data-redis</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-undertow</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-test</artifactId> | |
<scope>test</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.restdocs</groupId> | |
<artifactId>spring-restdocs-mockmvc</artifactId> | |
<scope>test</scope> | |
</dependency> | |
<dependency> | |
<groupId>com.battcn</groupId> | |
<artifactId>spring-boot-starter-swagger</artifactId> | |
<version>1.4.2-RELEASE</version> | |
</dependency> | |
<dependency> | |
<groupId>mysql</groupId> | |
<artifactId>mysql-connector-java</artifactId> | |
<scope>runtime</scope> | |
</dependency> | |
<dependency> | |
<groupId>com.google.guava</groupId> | |
<artifactId>guava</artifactId> | |
<version>23.5-jre</version> | |
</dependency> | |
<dependency> | |
<groupId>com.alibaba</groupId> | |
<artifactId>fastjson</artifactId> | |
<version>1.2.43</version> | |
</dependency> | |
<dependency> | |
<groupId>org.projectlombok</groupId> | |
<artifactId>lombok</artifactId> | |
<optional>true</optional> | |
</dependency> | |
<dependency> | |
<groupId>joda-time</groupId> | |
<artifactId>joda-time</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.apache.commons</groupId> | |
<artifactId>commons-collections4</artifactId> | |
<version>4.1</version> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-boot-devtools</artifactId> | |
<optional>true</optional> | |
<scope>true</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-freemarker</artifactId> | |
</dependency> | |
</dependencies> | |
<dependencyManagement> | |
<dependencies> | |
<dependency> | |
<groupId>org.springframework.cloud</groupId> | |
<artifactId>spring-cloud-dependencies</artifactId> | |
<version>${spring-cloud.version}</version> | |
<type>pom</type> | |
<scope>import</scope> | |
</dependency> | |
</dependencies> | |
</dependencyManagement> | |
<build> | |
<plugins> | |
<plugin> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-maven-plugin</artifactId> | |
</plugin> | |
<plugin> | |
<groupId>org.apache.maven.plugins</groupId> | |
<artifactId>maven-dependency-plugin</artifactId> | |
</plugin> | |
<plugin> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-maven-plugin</artifactId> | |
<configuration> | |
<fork>true</fork> | |
</configuration> | |
</plugin> | |
</plugins> | |
</build> | |
</project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.springframework.boot.builder.SpringApplicationBuilder; | |
import org.springframework.boot.web.support.SpringBootServletInitializer; | |
/** | |
* SpringAppicationBotter | |
* | |
* @author weijinliang | |
* @since 2017-12-14 19:09 | |
*/ | |
public class ServletInitializer extends SpringBootServletInitializer { | |
@Override | |
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { | |
return application.sources(HackthondayApplication.class); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.fasterxml.jackson.databind.DeserializationFeature; | |
import com.fasterxml.jackson.databind.MapperFeature; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import java.util.List; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.http.converter.HttpMessageConverter; | |
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; | |
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; | |
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; | |
/** | |
* | |
* | |
* @author weijinliang | |
* @since 2017-12-15 19:35 | |
*/ | |
@Configuration | |
public class WebMvcConfig extends WebMvcConfigurerAdapter { | |
@Autowired | |
private ObjectMapper objectMapper; | |
@Override | |
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { | |
ObjectMapper mapper = objectMapper.copy(); | |
mapper.configure(MapperFeature.PROPAGATE_TRANSIENT_MARKER, false); | |
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); | |
converters.add(new MappingJackson2HttpMessageConverter(mapper)); | |
} | |
@Override | |
public void addResourceHandlers(ResourceHandlerRegistry registry) { | |
registry.addResourceHandler("swagger-ui.html") | |
.addResourceLocations("classpath:/META-INF/resources/"); | |
registry.addResourceHandler("/webjars/**") | |
.addResourceLocations("classpath:/META-INF/resources/webjars/"); | |
super.addResourceHandlers(registry); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment