Commit f0bed6cc authored by 曾水平's avatar 曾水平

日志

parent 50110785
......@@ -9,13 +9,16 @@ version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenLocal()
maven { url "http://nexus.dui88.com:8081/nexus/content/groups/public/" }
maven { url "https://plugins.gradle.org/m2/" }
maven { url "http://repo.spring.io/plugins-release" }
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-data-jdbc'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-data-redis'
implementation 'org.springframework.boot:spring-boot-starter-jdbc'
implementation 'org.springframework.boot:spring-boot-starter-validation'
......@@ -29,6 +32,12 @@ dependencies {
compile "com.alibaba:fastjson:1.2.60"
compile 'org.apache.commons:commons-collections4:4.1'
compile 'org.apache.commons:commons-lang3:3.4'
compile "com.google.guava:guava:23.0"
compile "org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.1"
compile 'commons-pool:commons-pool:1.6'
compile 'redis.clients:jedis'
compile 'com.caucho:hessian:4.0.38'
compile 'commons-io:commons-io:2.4'
}
test {
......
......@@ -6,8 +6,8 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class JdActivityApplication {
public static void main(String[] args) {
SpringApplication.run(JdActivityApplication.class, args);
}
public static void main(String[] args) {
SpringApplication.run(JdActivityApplication.class, args);
}
}
package cn.com.duiba.jdactivity.common.utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ConfigurableApplicationContext;
/**
* Created by gyf .
* 2018/3/29 .
*/
public final class MainApplicationContextHolder {
private MainApplicationContextHolder() {
}
private static volatile ConfigurableApplicationContext applicationContext;
/**
* 外部方法禁止调用此方法
*/
@Autowired
public static void _setApplicationContext(ConfigurableApplicationContext applicationContext) {
if (MainApplicationContextHolder.applicationContext != null &&
MainApplicationContextHolder.applicationContext.isActive()) {
throw new IllegalStateException("外部方法禁止调用_setApplicationContext");
}
MainApplicationContextHolder.applicationContext = applicationContext;
}
/**
* 获取当前应用的applicationContext
*
* @return
*/
public static ConfigurableApplicationContext getApplicationContext() {
return applicationContext;
}
}
package cn.com.duiba.jdactivity.common.utils;
import org.springframework.context.ApplicationContext;
import org.springframework.core.env.AbstractEnvironment;
import org.springframework.core.env.CompositePropertySource;
import org.springframework.core.env.EnumerablePropertySource;
import org.springframework.core.env.Environment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.env.PropertySource;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
public class SpringEnvironmentUtils {
public static final String DEV = "dev";
public static final String TEST = "test";
public static final String PRE = "pre";
public static final String PROD = "prod";
private SpringEnvironmentUtils(){}
/**
* 从spring的Environment获得扁平化的所有key value对
* @param environment
* @return
*/
public static LinkedHashMap<String, Object> getFlatEnvironments(Environment environment) {
final LinkedHashMap<String, Object> properties = new LinkedHashMap<>();
for (Iterator it = ((AbstractEnvironment) environment).getPropertySources().iterator(); it.hasNext(); ) {
PropertySource propertySource = (PropertySource) it.next();
handlePropertySource(propertySource, properties);
}
return properties;
}
private static void handlePropertySource(PropertySource propertySource, LinkedHashMap<String, Object> properties){
if (propertySource instanceof EnumerablePropertySource) {
for (String key : ((EnumerablePropertySource) propertySource).getPropertyNames()) {
properties.putIfAbsent(key, propertySource.getProperty(key));
}
}
if (propertySource instanceof PropertiesPropertySource) {
for (Map.Entry<String, Object> e : ((MapPropertySource) propertySource).getSource().entrySet()) {
properties.putIfAbsent(e.getKey(), e.getValue());
}
}
if (propertySource instanceof CompositePropertySource) {
for (Map.Entry<String, Object> e : getPropertiesInCompositePropertySource((CompositePropertySource) propertySource).entrySet()) {
properties.putIfAbsent(e.getKey(), e.getValue());
}
}
}
private static LinkedHashMap<String, Object> getPropertiesInCompositePropertySource(CompositePropertySource compositePropertySource) {
final LinkedHashMap<String, Object> properties = new LinkedHashMap<>();
compositePropertySource.getPropertySources().forEach(propertySource -> {
if (propertySource instanceof EnumerablePropertySource) {
for (String key : ((EnumerablePropertySource) propertySource).getPropertyNames()) {
properties.putIfAbsent(key, propertySource.getProperty(key));
}
}
if (propertySource instanceof MapPropertySource) {
for (Map.Entry<String, Object> e : ((MapPropertySource) propertySource).getSource().entrySet()) {
properties.putIfAbsent(e.getKey(), e.getValue());
}
}
if (propertySource instanceof CompositePropertySource) {
for (Map.Entry<String, Object> e : getPropertiesInCompositePropertySource((CompositePropertySource) propertySource).entrySet()) {
properties.putIfAbsent(e.getKey(), e.getValue());
}
}
});
return properties;
}
private static String[] getActiveProfiles(){
if(MainApplicationContextHolder.getApplicationContext() == null){
throw new IllegalStateException("应用还没有启动,你过早地调用了判断环境的方法");
}
return MainApplicationContextHolder.getApplicationContext().getEnvironment().getActiveProfiles();
}
/**
* 是否在开发环境
* @return
*/
public static boolean isDevEnv(){
for(String p : getActiveProfiles()){
if(DEV.equals(p)){
return true;
}
}
return false;
}
/**
* 是否在开发环境
* @return
*/
public static boolean isDevEnv(ApplicationContext applicationContext){
for(String p : applicationContext.getEnvironment().getActiveProfiles()){
if(DEV.equals(p)){
return true;
}
}
return false;
}
/**
* 是否在测试环境
* @return
*/
public static boolean isTestEnv(){
for(String p : getActiveProfiles()){
if(TEST.equals(p)){
return true;
}
}
return false;
}
/**
* 是否在测试环境
* @return
*/
public static boolean isTestEnv(ApplicationContext applicationContext){
for(String p : applicationContext.getEnvironment().getActiveProfiles()){
if(TEST.equals(p)){
return true;
}
}
return false;
}
/**
* 是否在预发环境
* @return
*/
public static boolean isPreEnv(){
for(String p : getActiveProfiles()){
if(p != null && p.startsWith(PRE)){
return true;
}
}
return false;
}
/**
* 是否在预发环境
* @return
*/
public static boolean isPreEnv(ApplicationContext applicationContext){
for(String p : applicationContext.getEnvironment().getActiveProfiles()){
if(p != null && p.startsWith(PRE)){
return true;
}
}
return false;
}
/**
* 是否在线上环境
* @return
*/
public static boolean isProdEnv(){
String[] activeProfiles = getActiveProfiles();
for(String p : activeProfiles){
if(p != null && p.startsWith(PROD)){
return true;
}
}
return false;
}
/**
* 是否在线上环境
* @return
*/
public static boolean isProdEnv(ApplicationContext applicationContext){
String[] activeProfiles = applicationContext.getEnvironment().getActiveProfiles();
for(String p : activeProfiles){
if(p != null && p.startsWith(PROD)){
return true;
}
}
return false;
}
/**
* 获取当前所在的环境
* @return
*/
public static String getCurrentEnv() {
if(SpringEnvironmentUtils.isDevEnv()) {
return SpringEnvironmentUtils.DEV;
}
if(SpringEnvironmentUtils.isTestEnv()) {
return SpringEnvironmentUtils.TEST;
}
if(SpringEnvironmentUtils.isPreEnv()) {
return SpringEnvironmentUtils.PRE;
}
if(SpringEnvironmentUtils.isProdEnv()) {
return SpringEnvironmentUtils.PROD;
}
throw new IllegalStateException("没有获取系统环境");
}
/**
* 获取当前所在的环境
* @return
*/
public static String getCurrentEnv(ApplicationContext applicationContext) {
if(SpringEnvironmentUtils.isDevEnv(applicationContext)) {
return SpringEnvironmentUtils.DEV;
}
if(SpringEnvironmentUtils.isTestEnv(applicationContext)) {
return SpringEnvironmentUtils.TEST;
}
if(SpringEnvironmentUtils.isPreEnv(applicationContext)) {
return SpringEnvironmentUtils.PRE;
}
if(SpringEnvironmentUtils.isProdEnv(applicationContext)) {
return SpringEnvironmentUtils.PROD;
}
throw new IllegalStateException("没有获取系统环境");
}
}
package cn.com.duiba.jdactivity.web;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @author zsp (zengshuiping@duiba.com.cn)
* @date 2021/5/12 15:26
*/
@RestController
public class HelloController {
public static final Logger LOGGER = LoggerFactory.getLogger(HelloController.class);
@RequestMapping("/")
public String index() {
LOGGER.info("有人访问了index");
return "Greetings from Spring Boot!";
}
}
\ No newline at end of file
spring.application.name=jd-activity
spring.profiles.active=dev
#先排除一些autoconfigure
spring.autoconfigure.exclude=\
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration,\
org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration,\
org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration
# tomcat
server.port=8080
server.tomcat.accesslog.enabled=false
server.tomcat.uri-encoding=UTF-8
server.tomcat.threads.max=300
#MySQL配置
#spring.datasource.url=jdbc:mysql://172.16.10.79:3306/mytestdb?serverTimezone=UTC&useSSL=false&allowPublicKeyRetrieval=true
#spring.datasource.data-username=root
#spring.datasource.data-password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# logging
logging.file.path=${user.home}/logs/${spring.application.name}
# MyBatis 配置
mybatis.config-location=classpath:mybatis/config.xml
mybatis.mapper-locations=classpath:mybatis/*/*.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<include resource="logback/default.xml" />
</configuration>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<included>
<property name="logpath" value="${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}/}"/>
<property name="logPattern" value="%d{HH:mm:ss.SSS} %-5level [%thread] %logger{32}[%file:%line] -&gt; %msg%n"/>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${logPattern}</pattern>
<charset class="java.nio.charset.Charset">utf8</charset>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>${logpath}/application.log</File>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>${logpath}/application_%d{yyyy-MM-dd}.log</FileNamePattern>
<maxHistory>10</maxHistory>
</rollingPolicy>
<encoder>
<pattern>${logPattern}</pattern>
</encoder>
</appender>
<!-- 统一内部日志 -->
<appender name="innerLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>${logpath}/inner/inner.log</File>
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%msg%n</pattern>
</layout>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>${logpath}/inner/inner.log.%d{yyyy-MM-dd}</FileNamePattern>
<maxHistory>7</maxHistory>
</rollingPolicy>
</appender>
<!-- 统一内部日志 -->
<logger name="innerLog" level="INFO" additivity="false">
<appender-ref ref="innerLog" />
</logger>
<logger name="org.springframework" level="WARN"/>
<logger name="org.apache" level="WARN"/>
<logger name="org.quartz" level="WARN"/>
<logger name="org.mybatis" level="ERROR" />
<logger name="org.springframework.web.servlet.mvc.method.annotation" level="ERROR" />
<logger name="cn.com.duiba" level="INFO"/>
<root level="WARN">
<appender-ref ref="CONSOLE" />
<appender-ref ref="FILE" />
</root>
</included>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<typeAliases>
<typeAlias alias="Integer" type="java.lang.Integer" />
<typeAlias alias="Long" type="java.lang.Long" />
<typeAlias alias="HashMap" type="java.util.HashMap" />
<typeAlias alias="LinkedHashMap" type="java.util.LinkedHashMap" />
<typeAlias alias="ArrayList" type="java.util.ArrayList" />
<typeAlias alias="LinkedList" type="java.util.LinkedList" />
</typeAliases>
</configuration>
package cn.com.duiba.jdactivity;
import static org.hamcrest.Matchers.equalTo;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
/**
* @author zsp (zengshuiping@duiba.com.cn)
* @date 2021/5/12 15:28
*/
@SpringBootTest
@AutoConfigureMockMvc
public class HelloControllerTest {
@Autowired
private MockMvc mvc;
@Test
public void getHello() throws Exception {
mvc.perform(MockMvcRequestBuilders.get("/")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().string(equalTo("Greetings from Spring Boot!")));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment