Commit 3fa1c4e1 by QIANGLU

Merge branch 'dev'

# Conflicts:
#	common-core/pom.xml
#	common-util/pom.xml
#	config-starter/pom.xml
#	dubbo-starter/pom.xml
#	elasticsearch-starter/pom.xml
#	mongodb-starter/pom.xml
#	monitor-starter/pom.xml
#	mybatis-starter/pom.xml
#	openfeign-starter/pom.xml
#	pom.xml
#	redis-starter/pom.xml
#	rocketmq-starter/pom.xml
#	web-starter/pom.xml
parents 05c1c2de f27081bd
......@@ -25,4 +25,16 @@
* \*-starter:组件依赖
开发版本 1.0.3-DEV-SNAPSHOT
正式版本
1.0.3.RELEASE
1、增加monitor traceId监控模块
2、增加防灾冗余
3、优化文件结构
1.0.2.RELEASE
......@@ -11,6 +11,6 @@ public interface CommonConstant {
String DEV = "dev";
String TEST = "test";
String BETA = "beta";
String PROD = "prod";
String PRO = "pro";
}
}
......@@ -11,6 +11,6 @@ public class ProdEnvCondition implements Condition {
@Override
public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
String[] profiles = conditionContext.getEnvironment().getActiveProfiles();
return !ArrayUtils.contains(profiles, CommonConstant.Env.PROD);
return !ArrayUtils.contains(profiles, CommonConstant.Env.PRO);
}
}
package com.secoo.matrix.monitor.constant;
/**
* @author qianglu
*/
public interface MonitorConstant {
String DEFAULT_PORT = "0000";
String DEFAULT_IP = "127.0.0.1";
String TRACE_ID = "traceId";
String RPC_ID = "rpcId";
String APPLICATION = "application";
String INTERFACE = "interface";
String METHOD = "method";
String GROUP = "group";
String VERSION = "version";
String CONSUMER = "consumer";
String PROVIDER = "provider";
String TIMESTAMP = "timestamp";
String SUCCESS = "success";
String FAILURE = "failure";
String ROLE = "Dubbo-RPC";
String ERROR_MSG="errorMsg";
}
package com.secoo.matrix.monitor.http.intercept;
import com.secoo.matrix.monitor.utils.TraceIDUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicInteger;
/**
* @author qianglu
* <p>
* 为了在Http请求入口生成TRACE_ID,日志ID
* </p>
*/
public class TraceInterceptor extends HandlerInterceptorAdapter {
private Logger logger = LoggerFactory.getLogger(TraceInterceptor.class);
private final ConcurrentMap<String, AtomicInteger> concurrents = new ConcurrentHashMap<>();
private ThreadLocal<Long> stime = new ThreadLocal<>();
/**
* This implementation always returns {@code true}.
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
Object handler) throws Exception {
try {
TraceIDUtils.continueTraceID();
// Long startTime = System.currentTimeMillis();
// stime.set(startTime);
// // 并发计数
// getConcurrent(request).incrementAndGet();
}catch (Exception e){//容灾
}
return true;
}
/**
* This implementation is empty.
*/
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
}
/**
* This implementation is empty.
*/
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response,
Object handler, Exception ex) throws Exception {
try {
MDC.clear();
getConcurrent(request).decrementAndGet();
}catch (Exception e){
}
}
// 获取并发计数器
private AtomicInteger getConcurrent(HttpServletRequest request) {
String key = request.getRequestURI();
AtomicInteger concurrent = concurrents.get(key);
if (concurrent == null) {
concurrents.putIfAbsent(key, new AtomicInteger());
concurrent = concurrents.get(key);
}
return concurrent;
}
}
package com.secoo.matrix.monitor.rcp.filter;
import com.secoo.matrix.monitor.constant.MonitorConstant;
import com.secoo.matrix.monitor.utils.TraceIDUtils;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
/**
* @author QIANGLU
*/
@Activate(group = {MonitorConstant.CONSUMER, MonitorConstant.PROVIDER}, order = -1)
public class LoggerFilter implements Filter {
private Logger LOG = LoggerFactory.getLogger(getClass());
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
RpcContext rpcContext = RpcContext.getContext();
URL url = rpcContext.getUrl();
try {
String who = url.getParameter("side");
//CONSUMER 从MDC中获取RPCID并增加其序列,传递最新序列唯一ID到下一层
if (MonitorConstant.CONSUMER.equals(who)) {
String traceId = TraceIDUtils.continueTraceID();
rpcContext.setAttachment(MonitorConstant.TRACE_ID, traceId);
} else if (MonitorConstant.PROVIDER.equals(who)) {
MDC.remove(MonitorConstant.TRACE_ID);
MDC.remove("rpcId");
String traceId = rpcContext.getAttachment(MonitorConstant.TRACE_ID);
TraceIDUtils.childTraceID(traceId);
}
} catch (Exception e) {
}
return invoker.invoke(invocation);
}
}
package com.secoo.matrix.monitor.utils;
import com.secoo.matrix.monitor.constant.MonitorConstant;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import java.math.BigDecimal;
/**
* 服务调用追踪号工具类
* 会使用到MDC
* <p>
* 规则:首层调用开始rpcId为"0.0"(TraceID已经生成),
* 如果有跨应用调用
* 1.调用方consumer会将rpcId追加1,例如0.0->0.1,并随RPCContext传递给下一层
* 2.服务方provider收到RPCContext,取出requestId放到本地MDC中并追加本地rpcId层级
* 例如,0.1->0.1.0
* </p>
*
* @author qianglu
*/
public class TraceIDUtils {
private static Logger log = LoggerFactory.getLogger(TraceIDUtils.class);
private final static String ZERO_DOT_ZERO = "0.0";
private final static String ZERO_DOT_ONE = "0.1";
private final static String ZERO_DOT_ONE_ZERO = "0.1.0";
private final static String H_BAR = "-";//横杠
/**
* 首次创建TraceID为UUID+"0.0",RpcID的初始值为"0.0"
*/
public static String createTraceID() {
StringBuilder sb = new StringBuilder();
String requestId = sb.append(UUIDUtils.randomUUID().toString()).append(H_BAR)
.append(ZERO_DOT_ZERO).toString();
MDC.put(MonitorConstant.TRACE_ID, requestId);
MDC.put(MonitorConstant.RPC_ID, ZERO_DOT_ZERO);
log.debug("Create New TraceID is [{}],RPCID is [{}]", requestId, ZERO_DOT_ZERO);
return requestId;
}
/**
* 追踪ID子层级追加,兼顾生成追踪ID
*
* @return
*/
public static String continueTraceID() {
StringBuilder sb = new StringBuilder();
//直接从本地MDC获取requestId
String traceID = MDC.get(MonitorConstant.TRACE_ID);
/*如果为空就生成一个新的.
* 之所以没有,可能服务消费者本身没有requestId初始化入口,所以这里补足
* requestId为UUID+"0.0"从0开始并赋值到本地MDC中,因为此方法为RPC调用下一级生成ID,所以返回的是UUID+"0.1"
* rpcId从"0.1"开始
* */
if (StringUtils.isEmpty(traceID) || "null".equals(traceID)) {
traceID = UUIDUtils.randomUUID().toString();
MDC.put(MonitorConstant.TRACE_ID, traceID + H_BAR + ZERO_DOT_ZERO);
MDC.put(MonitorConstant.RPC_ID, ZERO_DOT_ONE);
log.debug("There is no [traceID], create new noe![{}] and RPCID is [{}]",
traceID + H_BAR + ZERO_DOT_ZERO, ZERO_DOT_ONE);
return traceID + H_BAR + ZERO_DOT_ONE;
}
String rpcId = MDC.get(MonitorConstant.RPC_ID);
try {
/*正常追踪ID格式应该是 " 唯一字符串(UUID)-传递层次" .*/
String[] temp = traceID.split(H_BAR);
if (temp.length > 2) {
/*其它格式*/
} else if (temp.length == 2) {
if (StringUtils.isEmpty(rpcId)) {
/*如果rpcId为空,就是用requestId后缀层级号*/
MDC.put(MonitorConstant.RPC_ID, temp[1]);
rpcId = temp[1];
}
/*将rpcId,分解,最后一位的层级数值+1*/
String[] nums = rpcId.split("\\.");
for (int i = 0; i < nums.length; i++) {
if (i == (nums.length - 1)) {
sb.append(new BigDecimal(nums[i]).add(new BigDecimal("1")));
} else {
sb.append(nums[i]).append(".");
}
}
rpcId = sb.toString();//新的+1后的rpcId
log.debug("TraceID continued ![{}],and RPCID is [{}]",
temp[0] + "-" + sb.toString(), rpcId);
MDC.put(MonitorConstant.RPC_ID, rpcId);//更新本地MDC中的rpcId
return temp[0] + H_BAR + sb.toString();
} else if (temp.length < 2) {
/*可能不包含行号*/
}
} catch (Exception e) {
log.error("ContinueTraceID is failed!!!", e);
}
return traceID;
}
/**
* RPC调用层级追加
* 子层追加使用".1"来表示
* 例如第一层为0.1,调用一次后下一个应用为0.1.1,
*
* @param traceID
* @return
*/
public static void childTraceID(String traceID) {
StringBuilder sb = new StringBuilder();
if (StringUtils.isEmpty(traceID) || "null".equals(traceID)) {
/*如果为空就生成一个新的.
* 之所以没有,是消费者没有使用相同的requestId机制,所以本地重新生成
* requestId为UUID+"0.1"并赋值到本地MDC中,因为此方法为被调用,所以一定是下一级"
* rpcId从"0.1.0"开始
* */
traceID = sb.append(UUIDUtils.randomUUID().toString()).append(H_BAR)
.append(ZERO_DOT_ONE).toString();
MDC.put(MonitorConstant.TRACE_ID, traceID);
MDC.put(MonitorConstant.RPC_ID, ZERO_DOT_ONE_ZERO);
log.debug("There is no [traceID], create new noe![{}] and RPCID is [{}]", traceID,
ZERO_DOT_ONE_ZERO);
return;
}
String rpcId = MDC.get(MonitorConstant.RPC_ID);
try {
String[] temp = traceID.split(H_BAR);
if (temp.length > 2) {
/*其它格式*/
} else if (temp.length == 2) {
if (StringUtils.isEmpty(rpcId)) {
/*如果rpcId为空,就是用requestId后缀层级号*/
MDC.put(MonitorConstant.RPC_ID, temp[1]);
rpcId = temp[1];
}
rpcId = rpcId + ".0";
//更新本地MDC值,新增TraceID
MDC.put(MonitorConstant.RPC_ID, rpcId);
MDC.put(MonitorConstant.TRACE_ID, traceID);
log.debug("RPC_ID_KEY child is ![{}]", rpcId);
} else if (temp.length < 2) {
/*可能不包含行号*/
}
} catch (Exception e) {
log.error("ChildTraceID is failed!!!", e);
}
}
// public static void main(String[] args) {
//
// Logger log = LoggerFactory.getLogger(TraceIDUtils.class);
//
// TraceIDUtils.createTraceID();
// String s = "e591a82137f04423a4933cb0cff59374-0.2";
// MDC.put("requestId", "e591a82137f04423a4933cb0cff59374-0.0");
// MDC.put("rpcId", "0.2");
// System.out.println(TraceIDUtils.continueTraceID());
// MDC.remove(RPC_ID_KEY);
// TraceIDUtils.childTraceID("");
// }
}
package com.secoo.matrix.monitor.utils;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.UUID;
public final class UUIDUtils implements java.io.Serializable,
Comparable<UUIDUtils> {
/**
* Explicit serialVersionUID for interoperability.
*/
private static final long serialVersionUID = -4856846361193249489L;
/*
* The most significant 64 bits of this UUID.
*
* @serial
*/
private final long mostSigBits;
/*
* The least significant 64 bits of this UUID.
*
* @serial
*/
private final long leastSigBits;
/*
* The random number generator used by this class to create random based
* UUIDs. In a holder class to defer initialization until needed.
*/
private static class Holder {
static final SecureRandom numberGenerator = new SecureRandom();
}
// Constructors and Factories
/*
* Private constructor which uses a byte array to construct the new UUID.
*/
private UUIDUtils(byte[] data) {
long msb = 0;
long lsb = 0;
assert data.length == 16 : "data must be 16 bytes in length";
for (int i = 0; i < 8; i++) {
msb = (msb << 8) | (data[i] & 0xff);
}
for (int i = 8; i < 16; i++) {
lsb = (lsb << 8) | (data[i] & 0xff);
}
this.mostSigBits = msb;
this.leastSigBits = lsb;
}
/**
* Constructs a new {@code UUID} using the specified data.
* {@code mostSigBits} is used for the most significant 64 bits of the
* {@code UUID} and {@code leastSigBits} becomes the least significant 64
* bits of the {@code UUID}.
*
* @param mostSigBits
* The most significant bits of the {@code UUID}
*
* @param leastSigBits
* The least significant bits of the {@code UUID}
*/
public UUIDUtils(long mostSigBits, long leastSigBits) {
this.mostSigBits = mostSigBits;
this.leastSigBits = leastSigBits;
}
/**
* Static factory to retrieve a type 4 (pseudo randomly generated) UUID.
*
* The {@code UUID} is generated using a cryptographically strong pseudo
* random number generator.
*
* @return A randomly generated {@code UUID}
*/
public static UUIDUtils randomUUID() {
SecureRandom ng = Holder.numberGenerator;
byte[] randomBytes = new byte[16];
ng.nextBytes(randomBytes);
randomBytes[6] &= 0x0f; /* clear version */
randomBytes[6] |= 0x40; /* set to version 4 */
randomBytes[8] &= 0x3f; /* clear variant */
randomBytes[8] |= 0x80; /* set to IETF variant */
return new UUIDUtils(randomBytes);
}
/**
* Static factory to retrieve a type 3 (name based) {@code UUID} based on
* the specified byte array.
*
* @param name
* A byte array to be used to construct a {@code UUID}
*
* @return A {@code UUID} generated from the specified array
*/
public static UUIDUtils nameUUIDFromBytes(byte[] name) {
MessageDigest md;
try {
md = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException nsae) {
throw new InternalError("MD5 not supported", nsae);
}
byte[] md5Bytes = md.digest(name);
md5Bytes[6] &= 0x0f; /* clear version */
md5Bytes[6] |= 0x30; /* set to version 3 */
md5Bytes[8] &= 0x3f; /* clear variant */
md5Bytes[8] |= 0x80; /* set to IETF variant */
return new UUIDUtils(md5Bytes);
}
/**
* Creates a {@code UUID} from the string standard representation as
* described in the {@link #toString} method.
*
* @param name
* A string that specifies a {@code UUID}
*
* @return A {@code UUID} with the specified value
*
* @throws IllegalArgumentException
* If name does not conform to the string representation as
* described in {@link #toString}
*
*/
public static UUIDUtils fromString(String name) {
String[] components = name.split("-");
if (components.length != 5) {
throw new IllegalArgumentException("Invalid UUID string: " + name);
}
for (int i = 0; i < 5; i++) {
components[i] = "0x" + components[i];
}
long mostSigBits = Long.decode(components[0]).longValue();
mostSigBits <<= 16;
mostSigBits |= Long.decode(components[1]).longValue();
mostSigBits <<= 16;
mostSigBits |= Long.decode(components[2]).longValue();
long leastSigBits = Long.decode(components[3]).longValue();
leastSigBits <<= 48;
leastSigBits |= Long.decode(components[4]).longValue();
return new UUIDUtils(mostSigBits, leastSigBits);
}
// Field Accessor Methods
/**
* Returns the least significant 64 bits of this UUID's 128 bit value.
*
* @return The least significant 64 bits of this UUID's 128 bit value
*/
public long getLeastSignificantBits() {
return leastSigBits;
}
/**
* Returns the most significant 64 bits of this UUID's 128 bit value.
*
* @return The most significant 64 bits of this UUID's 128 bit value
*/
public long getMostSignificantBits() {
return mostSigBits;
}
/**
* The version number associated with this {@code UUID}. The version number
* describes how this {@code UUID} was generated.
*
* The version number has the following meaning:
* <ul>
* <li>1 Time-based UUID
* <li>2 DCE security UUID
* <li>3 Name-based UUID
* <li>4 Randomly generated UUID
* </ul>
*
* @return The version number of this {@code UUID}
*/
public int version() {
// Version is bits masked by 0x000000000000F000 in MS long
return (int) ((mostSigBits >> 12) & 0x0f);
}
/**
* The variant number associated with this {@code UUID}. The variant number
* describes the layout of the {@code UUID}.
*
* The variant number has the following meaning:
* <ul>
* <li>0 Reserved for NCS backward compatibility
* <li>2 <a
* href="http://www.ietf.org/rfc/rfc4122.txt">IETF&nbsp;RFC&nbsp;4122</a>
* (Leach-Salz), used by this class
* <li>6 Reserved, Microsoft Corporation backward compatibility
* <li>7 Reserved for future definition
* </ul>
*
* @return The variant number of this {@code UUID}
*/
public int variant() {
// This field is composed of a varying number of bits.
// 0 - - Reserved for NCS backward compatibility
// 1 0 - The IETF aka Leach-Salz variant (used by this class)
// 1 1 0 Reserved, Microsoft backward compatibility
// 1 1 1 Reserved for future definition.
return (int) ((leastSigBits >>> (64 - (leastSigBits >>> 62))) & (leastSigBits >> 63));
}
/**
* The timestamp value associated with this UUID.
*
* <p>
* The 60 bit timestamp value is constructed from the time_low, time_mid,
* and time_hi fields of this {@code UUID}. The resulting timestamp is
* measured in 100-nanosecond units since midnight, October 15, 1582 UTC.
*
* <p>
* The timestamp value is only meaningful in a time-based UUID, which has
* version type 1. If this {@code UUID} is not a time-based UUID then this
* method throws UnsupportedOperationException.
*
* @throws UnsupportedOperationException
* If this UUID is not a version 1 UUID
* @return The timestamp of this {@code UUID}.
*/
public long timestamp() {
if (version() != 1) {
throw new UnsupportedOperationException("Not a time-based UUID");
}
return (mostSigBits & 0x0FFFL) << 48
| ((mostSigBits >> 16) & 0x0FFFFL) << 32 | mostSigBits >>> 32;
}
/**
* The clock sequence value associated with this UUID.
*
* <p>
* The 14 bit clock sequence value is constructed from the clock sequence
* field of this UUID. The clock sequence field is used to guarantee
* temporal uniqueness in a time-based UUID.
*
* <p>
* The {@code clockSequence} value is only meaningful in a time-based UUID,
* which has version type 1. If this UUID is not a time-based UUID then this
* method throws UnsupportedOperationException.
*
* @return The clock sequence of this {@code UUID}
*
* @throws UnsupportedOperationException
* If this UUID is not a version 1 UUID
*/
public int clockSequence() {
if (version() != 1) {
throw new UnsupportedOperationException("Not a time-based UUID");
}
return (int) ((leastSigBits & 0x3FFF000000000000L) >>> 48);
}
/**
* The node value associated with this UUID.
*
* <p>
* The 48 bit node value is constructed from the node field of this UUID.
* This field is intended to hold the IEEE 802 address of the machine that
* generated this UUID to guarantee spatial uniqueness.
*
* <p>
* The node value is only meaningful in a time-based UUID, which has version
* type 1. If this UUID is not a time-based UUID then this method throws
* UnsupportedOperationException.
*
* @return The node value of this {@code UUID}
*
* @throws UnsupportedOperationException
* If this UUID is not a version 1 UUID
*/
public long node() {
if (version() != 1) {
throw new UnsupportedOperationException("Not a time-based UUID");
}
return leastSigBits & 0x0000FFFFFFFFFFFFL;
}
// Object Inherited Methods
/**
* Returns a {@code String} object representing this {@code UUID}.
*
* <p>
* The UUID string representation is as described by this BNF: <blockquote>
*
* <pre>
* {@code
* UUID = <time_low> "-" <time_mid> "-"
* <time_high_and_version> "-"
* <variant_and_sequence> "-"
* <node>
* time_low = 4*<hexOctet>
* time_mid = 2*<hexOctet>
* time_high_and_version = 2*<hexOctet>
* variant_and_sequence = 2*<hexOctet>
* node = 6*<hexOctet>
* hexOctet = <hexDigit><hexDigit>
* hexDigit =
* "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
* | "a" | "b" | "c" | "d" | "e" | "f"
* | "A" | "B" | "C" | "D" | "E" | "F"
* }
* </pre>
*
* </blockquote>
*
* @return A string representation of this {@code UUID}
*/
@Override
public String toString() {
return (digits(mostSigBits >> 32, 8) + digits(mostSigBits >> 16, 4)
+ digits(mostSigBits, 4) + digits(leastSigBits >> 48, 4) + digits(
leastSigBits, 12));
}
/** Returns val represented by the specified number of hex digits. */
private static String digits(long val, int digits) {
long hi = 1L << (digits * 4);
return Long.toHexString(hi | (val & (hi - 1))).substring(1);
}
/**
* Returns a hash code for this {@code UUID}.
*
* @return A hash code value for this {@code UUID}
*/
@Override
public int hashCode() {
long hilo = mostSigBits ^ leastSigBits;
return ((int) (hilo >> 32)) ^ (int) hilo;
}
/**
* Compares this object to the specified object. The result is {@code true}
* if and only if the argument is not {@code null}, is a {@code UUID}
* object, has the same variant, and contains the same value, bit for bit,
* as this {@code UUID}.
*
* @param obj
* The object to be compared
*
* @return {@code true} if the objects are the same; {@code false} otherwise
*/
@Override
public boolean equals(Object obj) {
if ((null == obj) || (obj.getClass() != UUID.class)) {
return false;
}
UUIDUtils id = (UUIDUtils) obj;
return (mostSigBits == id.mostSigBits && leastSigBits == id.leastSigBits);
}
// Comparison Operations
/**
* Compares this UUID with the specified UUID.
*
* <p>
* The first of two UUIDs is greater than the second if the most significant
* field in which the UUIDs differ is greater for the first UUID.
*
* @param val
* {@code UUID} to which this {@code UUID} is to be compared
*
* @return -1, 0 or 1 as this {@code UUID} is less than, equal to, or
* greater than {@code val}
*
*/
@Override
public int compareTo(UUIDUtils val) {
// The ordering is intentionally set up so that the UUIDs
// can simply be numerically compared as two numbers
return (this.mostSigBits < val.mostSigBits ? -1
: (this.mostSigBits > val.mostSigBits ? 1
: (this.leastSigBits < val.leastSigBits ? -1
: (this.leastSigBits > val.leastSigBits ? 1 : 0))));
}
}
logFilter=com.secoo.matrix.monitor.rcp.filter.LoggerFilter
......@@ -50,7 +50,7 @@ public class MybatisConfig {
/* 乐观锁插件 */
// configuration.addInterceptor(new OptimisticLockerInterceptor());
//非生产环境加载的插件
if (!CommonConstant.Env.PROD.equals(profile) ) {
if (!CommonConstant.Env.PRO.equals(profile) ) {
configuration.addInterceptor(new PerformanceInterceptor());
}
......
package com.secoo.mall.openfeign.config;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Configuration;
@Configuration
public class FeignConfig {
// TODO: 2019/5/8 实现enable package自动加载 实现灰度发布 client请求日志拦截
......
......@@ -8,6 +8,7 @@ import com.secoo.mall.common.util.response.ResponseUtil;
import com.secoo.mall.web.annotation.ApiController;
import com.secoo.mall.web.annotation.ApiIgnoreJson;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContext;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.core.MethodParameter;
import org.springframework.core.annotation.AnnotationUtils;
......@@ -20,6 +21,7 @@ import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
import javax.annotation.Resource;
import java.util.Locale;
@RestControllerAdvice(annotations = ApiController.class)
public class ControllerResponseAdvice implements ResponseBodyAdvice<Object> {
......@@ -34,8 +36,9 @@ public class ControllerResponseAdvice implements ResponseBodyAdvice<Object> {
@ExceptionHandler(ParameterException.class)
public Object parameterExceptionHandler(ParameterException e) {
LoggerUtil.info(getMsg(e));
return ResponseUtil.getFailResponse(e.getCode(), getMsg(e));
String msg=getMsg(e);
LoggerUtil.info(msg);
return ResponseUtil.getFailResponse(e.getCode(), msg);
}
@ExceptionHandler(Exception.class)
......
package com.secoo.mall.web.config;
import com.secoo.mall.web.resolver.DefaultLocaleResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver;
import org.springframework.web.servlet.i18n.CookieLocaleResolver;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import java.util.Locale;
@Configuration
public class WebConfig implements WebMvcConfigurer {
/**
* 自定义国际化文件存放路径
*
* @return
*/
/*@Bean
public org.springframework.validation.Validator getValidator() {
Validator validator = Validation.byDefaultProvider()
.configure()
.messageInterpolator(new ResourceBundleMessageInterpolator(new PlatformResourceBundleLocator("i18n/message")))
.buildValidatorFactory().getValidator();
return (org.springframework.validation.Validator) validator;
}*/
@Bean
public ResourceBundleMessageSource messageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
......@@ -34,19 +21,13 @@ public class WebConfig implements WebMvcConfigurer {
return messageSource;
}
@Bean
public LocaleResolver localeHeaderResolver() {
AcceptHeaderLocaleResolver localeResolver = new AcceptHeaderLocaleResolver();
localeResolver.setDefaultLocale(Locale.SIMPLIFIED_CHINESE);
return localeResolver;
}
@Bean
@Bean(name = "localeResolver")
public LocaleResolver localeCookieResolver() {
CookieLocaleResolver localeResolver = new CookieLocaleResolver();
DefaultLocaleResolver localeResolver = new DefaultLocaleResolver();
localeResolver.setCookieName("language");
//设置默认区域
localeResolver.setDefaultLocale(Locale.CANADA);
localeResolver.setDefaultLocale(Locale.SIMPLIFIED_CHINESE);
localeResolver.setCookieMaxAge(3600);//设置cookie有效期.
return localeResolver;
}
......
package com.secoo.mall.web.resolver;
import com.secoo.mall.common.util.string.StringUtil;
import org.springframework.lang.Nullable;
import org.springframework.web.servlet.i18n.CookieLocaleResolver;
import javax.servlet.http.HttpServletRequest;
import java.util.Locale;
public class DefaultLocaleResolver extends CookieLocaleResolver {
@Nullable
protected Locale determineDefaultLocale(HttpServletRequest request) {
if (!StringUtil.isEmpty(request.getHeader("Accept-Language"))) {
return Locale.forLanguageTag(request.getHeader("Accept-Language"));
}
String lang = request.getParameter("lang");
if (StringUtil.isNotEmpty(lang)) {
return parseLocaleValue(lang);
}
return super.determineDefaultLocale(request);
}
}
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