# 端口
server.port = 8080
# 应用名
spring.application.name = cloud-three-code
# 启用编码
spring.http.encoding.enabled = true
# 缺省编码(这里只是在客户端没设置编码时起效,如果要强制编码,请配置force)
spring.http.encoding.charset = UTF-8
# 时区(也可以使用fastjson,就不用配置时区了。不配置时区,接收时间参数时会少8个小时。)
spring.jackson.time-zone = GMT+8
# 调接口超时时间,10秒即可
ribbon.ConnectTimeout = 10000
ribbon.ReadTimeout = 10000
# 启用hystrix
feign.hystrix.enabled = true
# 启用hystrix 超时时间
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds = 8000
# feign 超时时间
feign.client.config.default.connect-timeout = 10000
feign.client.config.default.read-timeout = 10000
# eureka配置
eureka.instance.appname = ${spring.application.name}
eureka.instance.instance-id = ${spring.cloud.client.ip-address}:${server.port}
eureka.instance.prefer-ip-address = true
eureka.client.service-url.defaultZone = http://${eureka.user.name}:${eureka.user.password}@域名/eureka/
eureka.user.name = 用户名
eureka.user.password = 密码
# 数据源配置
spring.datasource.driver-class-name = com.mysql.jdbc.Driver
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://url:port/db_name?useUnicode=true&characterEncoding=utf8&autoReconnect=true&useSSL=false&allowMultiQueries=true
spring.datasource.username = 用户名
spring.datasource.password = 密码
spring.datasource.initialSize = 5
spring.datasource.minIdle = 5
spring.datasource.maxActive = 50
spring.datasource.maxWait = 60000
spring.datasource.timeBetweenEvictionRunsMillis = 60000
spring.datasource.minEvictableIdleTimeMillis = 300000
spring.datasource.validationQuery = SELECT 1 FROM DUAL
spring.datasource.testWhileIdle = true
spring.datasource.testOnBorrow = false
spring.datasource.testOnReturn = false
spring.datasource.poolPreparedStatements = true
spring.datasource.maxPoolPreparedStatementPerConnectionSize = 20
spring.datasource.filters = stat,wall,log4j
spring.datasource.connectionProperties = druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
mybatis-plus.mapper-locations = classpath:mybatis/mapper/*.xml
mybatis-plus.config-location = classpath:mybatis/mybatis-config.xml
# redis配置
spring.redis.host =url
spring.redis.port = 6379
spring.redis.password = 密码
spring.redis.database = 0
spring.redis.pool.max-active = 32
spring.redis.pool.max-wait = -1
spring.redis.pool.max-idle = 8
spring.redis.pool.min-idle = 2
spring.redis.timeout = 0
# 使用jedis pool配置
spring:
redis:
# Redis 服务器地址
host: 192.168.142.128
# Redis 服务器连接端口
port: 6379
# Redis 服务器连接密码(默认为空)
password:
# Redis 数据库索引(默认为0)
database: 1
jedis:
pool:
# 连接池最大连接数(使用负值表示没有限制)
max-active: 8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
max-wait: -1
# 连接池中的最大空闲连接
max-idle: 8
# 屏蔽掉虚拟机网卡
spring:
cloud:
inetutils:
ignored-interfaces: ['VMware.*', '本地连接*']
# 配置项目使用fastjson
/**
* 使用fastjson代替jackson
*/
@Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
//先把JackSon的消息转换器删除.
//(1)源码分析可知,返回json的过程为:
// Controller调用结束后返回一个数据对象,for循环遍历conventers,找到支持application/json的HttpMessageConverter,然后将返回的数据序列化成json。
// 具体参考org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor的writeWithMessageConverters方法
//(2)由于是list结构,我们添加的fastjson在最后。因此必须要将jackson的转换器删除,不然会先匹配上jackson,导致没使用fastjson
for (Iterator<HttpMessageConverter<?>> iterator = converters.iterator(); iterator.hasNext(); ) {
HttpMessageConverter httpMessageConverter = iterator.next();
if (httpMessageConverter instanceof MappingJackson2HttpMessageConverter) {
iterator.remove();
}
}
//自定义fastjson配置
FastJsonConfig config = new FastJsonConfig();
config.setSerializerFeatures(
// 是否输出值为null的字段,默认为false,我们将它打开
SerializerFeature.WriteMapNullValue,
// 将Collection类型字段的字段空值输出为[]
SerializerFeature.WriteNullListAsEmpty,
// 将字符串类型字段的空值输出为空字符串
SerializerFeature.WriteNullStringAsEmpty,
SerializerFeature.WriteDateUseDateFormat,
// 禁用循环引用
SerializerFeature.DisableCircularReferenceDetect
);
// 添加支持的MediaTypes;不添加时默认为*/*,也就是默认支持全部
// 但是MappingJackson2HttpMessageConverter里面支持的MediaTypes为application/json
List<MediaType> fastMediaTypes = new ArrayList<>();
fastMediaTypes.add(MediaType.APPLICATION_JSON);
fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
fastJsonHttpMessageConverter.setFastJsonConfig(config);
fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes);
converters.add(fastJsonHttpMessageConverter);
}
# 配置项目使用fastjson - feign 配置
@Configuration
public class FeignConfig {
@Bean
Logger.Level feignLoggerLevel() {
//这里记录所有,根据实际情况选择合适的日志level
return Logger.Level.FULL;
}
/**
* 支持提交文件的编码格式
*/
@Bean
public Encoder feignEncoder() {
return new SpringFormEncoder(new SpringEncoder(feignHttpMessageConverter()));
}
@Bean
public Decoder feignDecoder() {
return new SpringDecoder(feignHttpMessageConverter());
}
/**
* 设置解码器为fastjson
*
* @return
*/
private ObjectFactory<HttpMessageConverters> feignHttpMessageConverter() {
final HttpMessageConverters httpMessageConverters = new HttpMessageConverters(this.getFastJsonConverter());
return () -> httpMessageConverters;
}
private FastJsonHttpMessageConverter getFastJsonConverter() {
FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
List<MediaType> supportedMediaTypes = new ArrayList<>();
supportedMediaTypes.add(MediaType.APPLICATION_JSON);
supportedMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
converter.setSupportedMediaTypes(supportedMediaTypes);
FastJsonConfig config = new FastJsonConfig();
config.getSerializeConfig().put(JSON.class, new SwaggerJsonSerializer());
config.setSerializerFeatures(
// 是否输出值为null的字段,默认为false,我们将它打开
SerializerFeature.WriteMapNullValue,
// 将Collection类型字段的字段空值输出为[]
SerializerFeature.WriteNullListAsEmpty,
// 将字符串类型字段的空值输出为空字符串
SerializerFeature.WriteNullStringAsEmpty,
SerializerFeature.WriteDateUseDateFormat,
// 禁用循环引用
SerializerFeature.DisableCircularReferenceDetect);
converter.setFastJsonConfig(config);
return converter;
}
}
一个好用的打印参数的过滤器
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.Ordered;
import org.springframework.web.filter.OncePerRequestFilter;
import org.springframework.web.util.ContentCachingRequestWrapper;
import org.springframework.web.util.ContentCachingResponseWrapper;
import org.springframework.web.util.WebUtils;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Enumeration;
@Slf4j
public class LogParamFilter extends OncePerRequestFilter implements Ordered {
// put filter at the end of all other filters to make sure we are processing after all others
private int order = Ordered.LOWEST_PRECEDENCE - 100;
public static final String SPLIT_STRING_M = "=";
public static final String SPLIT_STRING_DOT = ", ";
@Override
public int getOrder() {
return order;
}
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
FilterChain filterChain) throws ServletException, IOException {
ContentCachingRequestWrapper wrapperRequest = new ContentCachingRequestWrapper(request);
ContentCachingResponseWrapper wrapperResponse = new ContentCachingResponseWrapper(response);
String urlParams = getRequestParams(request);
String url = request.getRequestURI();
filterChain.doFilter(wrapperRequest, wrapperResponse);
String requestBodyStr = getRequestBody(wrapperRequest);
String responseBodyStr = getResponseBody(wrapperResponse);
if (!url.contains("/ping")) {
log.info("请求参数 = [{}] | request body:{}", urlParams, requestBodyStr);
log.info("接口返回 body = {}", responseBodyStr);
}
wrapperResponse.copyBodyToResponse();
}
/**
* 打印请求参数
*
* @param request
*/
private String getRequestBody(ContentCachingRequestWrapper request) {
ContentCachingRequestWrapper wrapper = WebUtils.getNativeRequest(request, ContentCachingRequestWrapper.class);
if (wrapper != null) {
byte[] buf = wrapper.getContentAsByteArray();
if (buf.length > 0) {
String payload;
try {
payload = new String(buf, 0, buf.length, wrapper.getCharacterEncoding());
} catch (UnsupportedEncodingException e) {
payload = "[unknown]";
}
return payload.replaceAll("\\n", "");
}
}
return "";
}
/**
* 打印返回参数
*
* @param response
*/
private String getResponseBody(ContentCachingResponseWrapper response) {
ContentCachingResponseWrapper wrapper = WebUtils.getNativeResponse(response,
ContentCachingResponseWrapper.class);
if (wrapper != null) {
byte[] buf = wrapper.getContentAsByteArray();
if (buf.length > 0) {
String payload;
try {
payload = new String(buf, 0, buf.length, wrapper.getCharacterEncoding());
} catch (UnsupportedEncodingException e) {
payload = "[unknown]";
}
return payload;
}
}
return "";
}
/**
* 获取请求地址上的参数
*
* @param request
* @return
*/
public static String getRequestParams(HttpServletRequest request) {
StringBuilder sb = new StringBuilder();
Enumeration<String> enu = request.getParameterNames();
//获取请求参数
while (enu.hasMoreElements()) {
String name = enu.nextElement();
sb.append(name + SPLIT_STRING_M).append(request.getParameter(name));
if (enu.hasMoreElements()) {
sb.append(SPLIT_STRING_DOT);
}
}
return sb.toString();
}
}
import com.xxx.LogParamFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class FilterConfig {
@Bean
public FilterRegistrationBean registFilter() {
FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setFilter(new LogParamFilter());
registration.addUrlPatterns("/*");
registration.setName("LogParamFilter");
return registration;
}
}