Commit 8f926a07 by 房斌

Merge branch 'master' into v1.3.2-lettucepatch

# Conflicts:
#	common-core/pom.xml
#	common-util/pom.xml
#	config-starter/pom.xml
#	elasticsearch-starter/pom.xml
#	logger-starter/pom.xml
#	matrix-bigdata/matrix-bigdata-hbase-starter/pom.xml
#	matrix-bigdata/matrix-bigdata-spark-starter/pom.xml
#	matrix-bigdata/pom.xml
#	matrix-bus/matrix-bus-canal-starter/pom.xml
#	matrix-bus/pom.xml
#	matrix-client/matrix-client-openfeign-starter/pom.xml
#	matrix-datahelper/matrix-datahelper-redis-core/pom.xml
#	matrix-datahelper/matrix-datahelper-redis-starter/pom.xml
#	matrix-datahelper/pom.xml
#	matrix-datasource/matrix-datasource-core/pom.xml
#	matrix-datasource/matrix-datasource-druid/pom.xml
#	matrix-datasource/pom.xml
#	matrix-job/matrix-job-core/pom.xml
#	matrix-job/matrix-job-xxl-core/pom.xml
#	matrix-job/matrix-job-xxl-starter/pom.xml
#	matrix-job/pom.xml
#	matrix-mq/matrix-mq-rocketmq-client/pom.xml
#	matrix-mq/matrix-mq-rocketmq-core/pom.xml
#	matrix-mq/matrix-mq-rocketmq-starter/pom.xml
#	matrix-mq/pom.xml
#	matrix-mybatis/matrix-mybatis-core/pom.xml
#	matrix-mybatis/matrix-mybatis-starter/pom.xml
#	matrix-mybatis/pom.xml
#	matrix-protocol/matrix-protocol-core/pom.xml
#	matrix-protocol/matrix-protocol-dubbo-core/pom.xml
#	matrix-protocol/matrix-protocol-dubbo-starter/pom.xml
#	matrix-protocol/matrix-protocol-web-core/pom.xml
#	matrix-protocol/matrix-protocol-web-starter/pom.xml
#	matrix-protocol/pom.xml
#	mongodb-starter/pom.xml
#	monitor-starter/pom.xml
#	pom.xml
#	redis-starter/pom.xml
#	redis-starter/src/main/java/com/secoo/mall/redis/config/MatrixeRedisAutoConfiguration.java
#	rocketmq-starter/pom.xml
parents f4287b2e 7d00ea38
......@@ -4,6 +4,25 @@ Matrix (矩阵)是一套组件增强套件,包括redis、rocketmq等中间件
# 组件
## V 2.x.x组件
主要进行组件进行
- 升级springboot到2.2.5
- 删除模块:<br>
elasticsearch-starter、 mongodb-starter、monitor-starter、matrix-rocketmq-core
- 新增模块<br>
1. matrix-bigdata封装hbase、spark等针对大数据相关技术组件
1. matrix-bus封装canal等涉及数据总线相关组件
- 调整模块:<br>
1. openfeign-starter->matrix-client-openfeign-starter
1. redis-starter->matrix-datahelper-redis-starter
1. rocketmq-starter->matrix-mq-rocketmq-starter
## V 1.x.x组件
- [common-core](common-core/README.md):提供通用bean,核心注解,定义通用业务异常。
- [common-util](common-util/README.md): 提供BeanUtil,BeanChecker、CollectionUtil等工具类。
- [config-starter](config-starter/README.md): 配置中心统一分装
......@@ -35,6 +54,8 @@ Matrix (矩阵)是一套组件增强套件,包括redis、rocketmq等中间件
# 示例
- [matrix-sample](http://gitlab.secoo.com:8090/mall/arch/matrix-sample.git)
# 期望
> 欢迎提出[issue](http://gitlab.secoo.com:8090/mall/arch/matrix/issues),帮助完善!
......
package com.secoo.mall.common.core.bean.gracefulshowtdownBean;
public class AbstractTransportData<T> {
T t;
public T getT() {
return t;
}
public void setT(T t) {
this.t = t;
}
}
package com.secoo.mall.common.core.bean.gracefulshowtdownBean;
import java.util.List;
public class ExecutorDetail {
String beginTime;
String endTime;
List<String> detail;
int code;
String serviceName;
public String getServiceName() {
return serviceName;
}
public void setServiceName(String serviceName) {
this.serviceName = serviceName;
}
public String getBeginTime() {
return beginTime;
}
public void setBeginTime(String beginTime) {
this.beginTime = beginTime;
}
public String getEndTime() {
return endTime;
}
public void setEndTime(String endTime) {
this.endTime = endTime;
}
public List<String> getDetail() {
return detail;
}
public void setDetail(List<String> detail) {
this.detail = detail;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
}
package com.secoo.mall.common.core.bean.gracefulshowtdownBean;
import java.util.List;
public class ExecutorDetails extends AbstractTransportData<List<ExecutorDetail>> {
int code;
String ip;
String name;
List<ExecutorDetail> details;
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.secoo.mall.common.core.service;
public interface StopService <T>{
public T stop();
}
package com.secoo.mall.common.core.service;
public interface UpDatas<T> {
public boolean upData(T t) throws Exception;
}
package com.secoo.mall.common.serializer;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.parser.ParserConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.util.IOUtils;
import com.secoo.mall.common.core.exception.SystemInternalException;
import com.secoo.mall.common.core.serializer.MatrixSerializer;
import lombok.Setter;
......@@ -15,42 +18,29 @@ public class FastJsonSerializer<T> implements MatrixSerializer<T> {
this.type = type;
}
private final static ParserConfig defaultRedisConfig = new ParserConfig();
@Override
public byte[] serialize(T t) {
if (t == null) {
static {
defaultRedisConfig.setAutoTypeSupport(true);
}
public byte[] serialize(T object) {
if (object == null) {
return new byte[0];
}
try {
return JSON.toJSONBytes(
fastJsonConfig.getCharset(),
t,
fastJsonConfig.getSerializeConfig(),
fastJsonConfig.getSerializeFilters(),
fastJsonConfig.getDateFormat(),
JSON.DEFAULT_GENERATE_FEATURE,
fastJsonConfig.getSerializerFeatures()
);
return JSON.toJSONBytes(object, SerializerFeature.WriteClassName);
} catch (Exception ex) {
throw new SystemInternalException();
}
}
@Override
public T deserialize(byte[] bytes) {
if (bytes == null || bytes.length == 0) {
return null;
}
try {
return (T) JSON.parseObject(
bytes,
fastJsonConfig.getCharset(),
type,
fastJsonConfig.getParserConfig(),
fastJsonConfig.getParseProcess(),
JSON.DEFAULT_PARSER_FEATURE,
fastJsonConfig.getFeatures()
);
return JSON.parseObject(new String(bytes, IOUtils.UTF8), type, defaultRedisConfig);
} catch (Exception ex) {
throw new SystemInternalException();
}
......
......@@ -77,6 +77,11 @@ public class StringUtil extends StringUtils {
}
public static String line(){
String lineSeparator = System.getProperty("line.separator", "\n");
return lineSeparator;
}
public static void main(String[] args) {
String str = "testJdate_order";
System.out.println(toCamelCase(str));
......
# 介绍
elasticsearch-starter。
# 特点
-
# 开始
- 添加依赖
- Maven:需要在自己项目的pom.xml增加,以下配置。
**注意:前置条件需要依赖项目中需要设置matrix的parent**
```xml
<dependency>
<groupId>com.secoo.mall</groupId>
<artifactId>elasticsearch-starter</artifactId>
</dependency>
```
# 示例
......@@ -5,16 +5,12 @@ import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfig;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
import com.ctrip.framework.apollo.spring.boot.ApolloAutoConfiguration;
import com.secoo.mall.logs.endpoint.LogEndpoint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnEnabledEndpoint;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.logging.LogLevel;
import org.springframework.boot.logging.LoggingSystem;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;
......@@ -64,10 +60,10 @@ public class MatrixLogListenerConfiguration implements InitializingBean {
refreshLoggingLevels();
}
@Bean
@ConditionalOnMissingBean
@ConditionalOnEnabledEndpoint
public LogEndpoint logEndpoint() {
return new LogEndpoint();
}
// @Bean
// @ConditionalOnMissingBean
// @ConditionalOnEnabledEndpoint
// public LogEndpoint logEndpoint() {
// return new LogEndpoint();
// }
}
......@@ -4,7 +4,7 @@ import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableFeignClients("com.secoo.mall")
@EnableFeignClients("com.secoo")
public class MatrixFeignAutoConfiguration {
}
<?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">
<parent>
<artifactId>matrix</artifactId>
<groupId>com.secoo.mall</groupId>
<version>2.0.17.RELEASE</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>matrix-client</artifactId>
<packaging>pom</packaging>
<modules>
<module>matrix-client-openfeign-starter</module>
</modules>
</project>
\ No newline at end of file
package com.secoo.mall.redis.helper;
import com.alibaba.fastjson.support.spring.FastJsonRedisSerializer;
import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer;
import com.secoo.mall.common.util.colletion.CollectionUtil;
import com.secoo.mall.common.util.string.StringUtil;
......@@ -12,7 +11,6 @@ import org.springframework.data.redis.connection.jedis.JedisClusterConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.types.Expiration;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
......@@ -26,6 +24,7 @@ import java.util.concurrent.TimeUnit;
*/
@Data
@Deprecated
public class RedisHelper {
private final String MUTEX_KEY = "mutex";
private final static RedisSerializer DEFAULT_STRING_SERIALIZER = new StringRedisSerializer();
......
......@@ -5,22 +5,25 @@ import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.*;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
/**
* @author QIANG
* @since 2.0.1
*/
public class MatrixRedisClusterUtils {
private static MatrixRedisClusterUtils cacheUtils;
@Resource
private RedisTemplate<String, String> redisTemplate;
public MatrixRedisClusterUtils(RedisTemplate<String, String> redisTemplate) {
this.redisTemplate = redisTemplate;
}
public static RedisTemplate<String, String> getRedisTemplate() {
return cacheUtils.redisTemplate;
}
......
package com.secoo.mall.redis.spring.boot.autoconfigure;
import com.secoo.mall.redis.helper.RedisHelper;
import com.secoo.mall.redis.utils.MatrixRedisClusterUtils;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
......@@ -11,8 +12,8 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import java.net.UnknownHostException;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
......@@ -27,7 +28,7 @@ public class MatrixeRedisAutoConfiguration {
@Bean
@ConditionalOnMissingBean(name = {"redisTemplate"})
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<Object, Object> template = new RedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
return template;
......@@ -38,4 +39,22 @@ public class MatrixeRedisAutoConfiguration {
public RedisHelper redisHelper(@Qualifier("redisTemplate") RedisTemplate redisTemplate) {
return new RedisHelper(redisTemplate);
}
/**
* @since 2.0.1 Deprecated,请使用RedisHelper
* @param redisTemplate
* @return
*/
@Bean
@ConditionalOnMissingBean(MatrixRedisClusterUtils.class)
public MatrixRedisClusterUtils jedisClusterUtils(RedisTemplate redisTemplate) {
RedisSerializer stringSerializer = new StringRedisSerializer();
redisTemplate.setKeySerializer(stringSerializer);
redisTemplate.setValueSerializer(stringSerializer);
redisTemplate.setHashKeySerializer(stringSerializer);
redisTemplate.setHashValueSerializer(stringSerializer);
return new MatrixRedisClusterUtils(redisTemplate);
}
}
......@@ -11,6 +11,7 @@ import java.util.List;
* 此数据源为支持@selectDataSource注解的多数据源使用
*/
@Slf4j
@Deprecated
public class MatrixDynamicDataSource extends AbsDataSource {
@Override
......
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>matrix</artifactId>
<groupId>com.secoo.mall</groupId>
<version>2.0.17.RELEASE</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>matrix-gracefulshutdown</artifactId>
<properties>
<secoo-dubbo.version>2.7.4.1-secoo1.4</secoo-dubbo.version>
<dubbo-starter.version>2.7.4.1</dubbo-starter.version>
<dubbo-zookper.version> 2.7.4.1</dubbo-zookper.version>
</properties>
<dependencies>
<dependency>
<groupId>com.secoo.mall</groupId>
<artifactId>matrix-protocol-core</artifactId>
</dependency>
<dependency>
<groupId>com.secoo.mall</groupId>
<artifactId>common-util</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>${secoo-dubbo.version}</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>${dubbo-starter.version}</version>
<exclusions>
<exclusion>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--zookeeper-->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper</artifactId>
<version>${dubbo-zookper.version}</version>
<type>pom</type>
<exclusions>
<exclusion>
<artifactId>log4j</artifactId>
<groupId>log4j</groupId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
</dependency>
<dependency>
<groupId>com.secoo.mall</groupId>
<artifactId>common-core</artifactId>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package com.secoo.mall.gracefulshutdown.autoconfigure;
import com.secoo.mall.common.core.service.UpDatas;
import com.secoo.mall.common.util.string.StringUtil;
import com.secoo.mall.gracefulshutdown.component.ShutDownDataReportAsyncHttp;
import com.secoo.mall.gracefulshutdown.monitor.config.ConfigCenter;
import com.secoo.mall.gracefulshutdown.monitor.dubbo.exception.ConfigurationException;
import com.secoo.mall.gracefulshutdown.monitor.dubbo.service.ProviderService;
import com.secoo.mall.gracefulshutdown.monitor.dubbo.service.RegistryServerSync;
import com.secoo.mall.gracefulshutdown.monitor.dubbo.service.impl.ProviderServiceImpl;
import com.secoo.mall.gracefulshutdown.monitor.utils.Tool;
import com.secoo.mall.gracefulshutdown.component.GracefulShutDown;
import com.secoo.mall.gracefulshutdown.component.ShutDownDataReport;
import com.secoo.mall.gracefulshutdown.component.TomcatGracefulShutDown;
import com.secoo.mall.gracefulshutdown.component.hook.DubboCustomerShutDownHook;
import com.secoo.mall.gracefulshutdown.component.hook.TomcatShutDownHook;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.extension.ExtensionLoader;
import org.apache.dubbo.registry.Registry;
import org.apache.dubbo.registry.RegistryFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.core.env.Environment;
@Configuration
public class MatrixGracefulShutDownAutoConfiguration {
@Bean
@ConditionalOnClass(RegistryServerSync.class)
@ConditionalOnExpression("!T(org.springframework.util.StringUtils).isEmpty('${dubbo.registry.address:}')")
ProviderService createProviderService( RegistryServerSync sync) {
return new ProviderServiceImpl(sync);
}
// @Bean
// SpringContextHolder createSpringContextHolder(){
// return new SpringContextHolder();
// }
// @Bean
// @ConditionalOnBean(SpringContextHolder.class)
// TomcatGracefulShutDownJudgment createJudgement(SpringContextHolder springContextHolder){
// return new TomcatGracefulShutDownJudgment(springContextHolder);
// }
@Bean
@ConditionalOnExpression("!T(org.springframework.util.StringUtils).isEmpty('${dubbo.registry.address:}')")
@ConditionalOnClass(ConfigCenter.class)
@DependsOn("configCenter")
UpDatas dubboUpdata(ConfigCenter configCenter) {
// ShutDownDataReport transport= new ShutDownDataReport();
// URL url =Tool.formUrl(configCenter.registryAddress, configCenter.registryGroup, configCenter.username, configCenter.password,configCenter.timeout);
// transport.setUrl(url);
// if(StringUtil.isEmpty(configCenter.patch)){
// configCenter.patch="/monitorZ";
// }
// transport.setPath(configCenter.patch);
ShutDownDataReportAsyncHttp report=new ShutDownDataReportAsyncHttp();
return report;
}
@Bean
@ConditionalOnClass({ProviderService.class,RegistryServerSync.class})
@ConditionalOnExpression("!T(org.springframework.util.StringUtils).isEmpty('${dubbo.registry.address:}')")
DubboCustomerShutDownHook createDubboConsumerDownHock(ProviderService providerService, RegistryServerSync registryServerSync) {
DubboCustomerShutDownHook hock= new DubboCustomerShutDownHook(providerService);
registryServerSync.addObserver(hock);
hock.setRegistryServerSync(registryServerSync);
return hock;
}
@Bean
@ConditionalOnExpression("!T(org.springframework.util.StringUtils).isEmpty('${dubbo.registry.address:}')")
public RegistryServerSync createSynObject(Registry registry) {
RegistryServerSync r= new RegistryServerSync(registry);
return r;
}
@Bean
@ConditionalOnExpression("!T(org.springframework.util.StringUtils).isEmpty('${dubbo.registry.address:}')")
ConfigCenter configCenter(Environment env){
ConfigCenter center= new ConfigCenter();
center.init(env);
return center;
}
@Bean
@ConditionalOnClass({TomcatGracefulShutDown.class,org.apache.catalina.connector.Connector.class})
@TomcatGracefulShutDownJudgmentInterface()
TomcatShutDownHook createServletConnectShoutDownHock() {
return new TomcatShutDownHook();
}
@Bean
@ConditionalOnClass(org.apache.catalina.connector.Connector.class)
@TomcatGracefulShutDownJudgmentInterface()
public TomcatGracefulShutDown createSpringbootTomcatInit(TomcatShutDownHook tomcatShutDownHook){
return new TomcatGracefulShutDown(tomcatShutDownHook);
}
@Bean
@ConditionalOnExpression("!T(org.springframework.util.StringUtils).isEmpty('${dubbo.registry.address:}')")
public GracefulShutDown createGraceObject(UpDatas dubboUpdata) {
return new GracefulShutDown(dubboUpdata);
}
@Bean
@ConditionalOnBean(ConfigCenter.class)
@ConditionalOnExpression("!T(org.springframework.util.StringUtils).isEmpty('${dubbo.registry.address:}')")
Registry getRegistry(ConfigCenter configCenter) {
Registry registry = null;
URL registryUrl = null;
if (registryUrl == null) {
registryUrl = Tool.formUrl(configCenter.getRegistryAddress(), configCenter.getRegistryGroup(), configCenter.getUsername(), configCenter.getPassword(), configCenter.getTimeout());
// registryUrl.setPath(patch);
}
RegistryFactory registryFactory = ExtensionLoader.getExtensionLoader(RegistryFactory.class).getAdaptiveExtension();
registry = registryFactory.getRegistry(registryUrl);
return registry;
}
}
\ No newline at end of file
package com.secoo.mall.gracefulshutdown.autoconfigure;
import com.secoo.mall.gracefulshutdown.condition.TomcatGracefulShutDownJudgment;
import org.springframework.context.annotation.Conditional;
import java.lang.annotation.*;
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(TomcatGracefulShutDownJudgment.class)
public @interface TomcatGracefulShutDownJudgmentInterface {
}
package com.secoo.mall.gracefulshutdown.component;
import com.secoo.mall.common.core.service.StopService;
import com.secoo.mall.common.core.bean.gracefulshowtdownBean.ExecutorDetail;
public abstract class AbstractShutDown implements StopService<ExecutorDetail>,Comparable {
//定义执行顺序
public abstract Integer getHandleTypeOrder();
@Override
public int compareTo(Object o) {
AbstractShutDown stop=(AbstractShutDown) o;
int result = (this.getHandleTypeOrder()>stop.getHandleTypeOrder()) ? 1 : -1;//升序
return result ;
}
}
package com.secoo.mall.gracefulshutdown.component;
public class ConfigConstant {
// public static String EXWARN="http://172.17.76.196:6080/gracefulshutdown/gracefulSave";
// public static String EXWARN="http://test-exwarn.secoo.com/gracefulshutdown/gracefulSave";
public static String EXWARN="http://exwarn.secoo.com/gracefulshutdown/gracefulSave";
public static final int CONNECT_TIMEOUT = 1000;//连接超时时间
public static final int SOCKET_TIMEOUT = 5000;//等待数据超时时间
public static final int REQUEST_TIMEOUT = 1000;//连接不够时等待超时时间,不设置将阻塞线程
}
package com.secoo.mall.gracefulshutdown.component;
import com.alibaba.fastjson.JSON;
import com.secoo.mall.common.core.bean.gracefulshowtdownBean.ExecutorDetail;
import com.secoo.mall.common.core.bean.gracefulshowtdownBean.ExecutorDetails;
import com.secoo.mall.common.core.service.StopService;
import com.secoo.mall.common.core.service.UpDatas;
import com.secoo.mall.common.util.date.DateUtil;
import com.secoo.mall.common.util.log.LoggerUtil;
import com.secoo.mall.gracefulshutdown.monitor.utils.Tool;
import com.secoo.mall.gracefulshutdown.component.hook.TomcatShutDownHook;
import org.apache.dubbo.common.utils.NetUtils;
import org.apache.dubbo.config.DubboShutdownHook;
import org.apache.dubbo.config.spring.extension.SpringExtensionFactory;
import org.apache.dubbo.rpc.model.ApplicationModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ApplicationEventMulticaster;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.core.Ordered;
import java.util.*;
import static org.springframework.context.support.AbstractApplicationContext.APPLICATION_EVENT_MULTICASTER_BEAN_NAME;
public class GracefulShutDown implements CommandLineRunner, ApplicationListener<ContextClosedEvent>, ApplicationContextAware, Ordered {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
private ApplicationContext context;
UpDatas dubboUpdata;
public GracefulShutDown(UpDatas dubboUpdata) {
this.dubboUpdata = dubboUpdata;
}
//容器初始化后执行
@Override
public void run(String... args) throws Exception {
if (DubboShutdownHook.getDubboShutdownHook() != null) {
//hock卸载
DubboShutdownHook.getDubboShutdownHook().unregister();
//listener 卸载
ApplicationEventMulticaster multicaster = context.getBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, ApplicationEventMulticaster.class);
Class clz = SpringExtensionFactory.class;
multicaster.removeApplicationListener((ApplicationListener) Tool.getPrivateConst("SHUTDOWN_HOOK_LISTENER"));
}
}
@Override
public void onApplicationEvent(ContextClosedEvent contextClosedEvent) {
ExecutorDetails result = new ExecutorDetails();
result.setCode(0);
List<ExecutorDetail> details = new ArrayList<ExecutorDetail>();
result.setT(details);
String ip = NetUtils.getIpByHost(NetUtils.getLocalAddress().getHostName());
String name = ApplicationModel.getApplication();
result.setIp(ip);
result.setName(name);
logger.info("gracefulshutdown execute:name:" + name + " ip:" + ip + " time:" + DateUtil.getDateTime());
try {
Map<String, StopService> map = contextClosedEvent.getApplicationContext().getBeansOfType(StopService.class);
if (map != null && map.size() > 0) {
Set<StopService> ts = new TreeSet<StopService>();
for (StopService service : map.values()) {
if (!(service instanceof TomcatGracefulShutDown)) {
ts.add(service);
}
}
//按定义顺序执行
Iterator<StopService> it = ts.iterator();
while (it.hasNext()) {
StopService service = (StopService) it.next();
ExecutorDetail one = (ExecutorDetail) service.stop();
if (one != null) {
details.add(one);
if (one.getCode() != 0) {
result.setCode(-1);
}
}
}
}
} catch (Exception e) {
LoggerUtil.error("matrix.gracefulshutdown.error", e);
}
try {
dubboUpdata.upData(result);
} catch (Exception e) {
LoggerUtil.error("matrix.gracefulshutdown update .error data:" + JSON.toJSONString(result), e);
}
logger.info("gracefulshutdown execute end :name:" + name + " ip:" + ip + " time:" + DateUtil.getDateTime());
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.context = applicationContext;
}
@Override
public int getOrder() {
return 2;
}
}
package com.secoo.mall.gracefulshutdown.component;
import com.alibaba.fastjson.JSON;
import com.secoo.mall.common.core.service.UpDatas;
import com.secoo.mall.common.util.log.LoggerUtil;
import com.secoo.mall.common.core.bean.gracefulshowtdownBean.ExecutorDetails;
import com.secoo.mall.common.util.string.StringUtil;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.extension.ExtensionLoader;
import org.apache.dubbo.remoting.zookeeper.ZookeeperClient;
import org.apache.dubbo.remoting.zookeeper.ZookeeperTransporter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ShutDownDataReport implements UpDatas<ExecutorDetails> {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
String path;
URL url;
public URL getUrl() {
return url;
}
public void setUrl(URL url) {
this.url = url;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
@Override
public boolean upData(ExecutorDetails obj) {
ZookeeperTransporter zookeeperTransporter = ExtensionLoader.getExtensionLoader(ZookeeperTransporter.class).getAdaptiveExtension();
if(StringUtil.isEmpty(path)){
path="/monitorZ";
}
String monitor=path+"/"+obj.getName()+"/"+obj.getIp();
try {
ZookeeperClient cliet = zookeeperTransporter.connect(url);
// String monitor = "/monitorZ/" + obj.getName() + "/" + obj.getIp();
logger.info("ShutDownDataReport sendData: path:"+monitor+" data:"+ JSON.toJSONString(obj));
cliet.create(monitor, JSON.toJSONString(obj), true);
String contest= cliet.getContent(monitor);
logger.info("result-------->"+contest);
} catch (Exception e) {
LoggerUtil.info("Exception e:" ,e);
}
return false;
}
}
package com.secoo.mall.gracefulshutdown.component;
import com.alibaba.fastjson.JSON;
import com.secoo.mall.common.core.bean.gracefulshowtdownBean.ExecutorDetails;
import com.secoo.mall.common.core.service.UpDatas;
import com.secoo.mall.common.util.http.HttpClientUtils;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.List;
public class ShutDownDataReportAsyncHttp implements UpDatas<ExecutorDetails> {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@Override
public boolean upData(ExecutorDetails obj) {
try {
String result=JSON.toJSONString(obj);
HttpPost httpPost = new HttpPost(ConfigConstant.EXWARN);
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("gracefulresult", result));
HttpClientUtils.setHttpParam(ConfigConstant.CONNECT_TIMEOUT,ConfigConstant.SOCKET_TIMEOUT,ConfigConstant.REQUEST_TIMEOUT);
httpPost.setEntity(new UrlEncodedFormEntity(nvps));
// logger.info("test in http start");
String back= HttpClientUtils.asynchronousPost(httpPost);
return true;
}catch (Exception e){
logger.error("matrix ShutDownDataReportAsyncHttp upData error",e);
}
return false;
}
}
package com.secoo.mall.gracefulshutdown.component;
import com.secoo.mall.gracefulshutdown.component.hook.TomcatShutDownHook;
import org.apache.catalina.connector.Connector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.core.Ordered;
public class TomcatGracefulShutDown implements TomcatConnectorCustomizer , ApplicationContextAware, Ordered , ApplicationListener<ContextClosedEvent> {
private ApplicationContext context;
TomcatShutDownHook tomcatShutDownHook;
private final Logger logger = LoggerFactory.getLogger(this.getClass());
public TomcatGracefulShutDown(TomcatShutDownHook tomcatShutDownHook) {
this.tomcatShutDownHook = tomcatShutDownHook;
}
@Override
public void customize(Connector connector) {
tomcatShutDownHook.setConnector(connector);
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.context = applicationContext;
}
public TomcatShutDownHook getTomcatShutDownHook() {
return tomcatShutDownHook;
}
public void setTomcatShutDownHook(TomcatShutDownHook tomcatShutDownHook) {
this.tomcatShutDownHook = tomcatShutDownHook;
}
@Override
public int getOrder() {
return 1;
}
@Override
public void onApplicationEvent(ContextClosedEvent contextClosedEvent) {
tomcatShutDownHook.stop();
}
}
package com.secoo.mall.gracefulshutdown.component.hook;
import com.secoo.mall.common.util.date.DateUtil;
import com.secoo.mall.common.util.string.StringUtil;
import com.secoo.mall.common.core.bean.gracefulshowtdownBean.ExecutorDetail;
import com.secoo.mall.gracefulshutdown.component.AbstractShutDown;
import org.apache.catalina.connector.Connector;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
//springboot 容器关闭
public class TomcatShutDownHook extends AbstractShutDown {
private volatile Connector connector;
public Connector getConnector() {
return connector;
}
public void setConnector(Connector connector) {
this.connector = connector;
}
@Override
public ExecutorDetail stop() {
ExecutorDetail detail = new ExecutorDetail();
detail.setBeginTime(DateUtil.getDateTime());
detail.setServiceName("ServletConnectShoutDownHock1");
List<String> str = new ArrayList<String>();
detail.setDetail(str);
detail.setCode(0);
if(connector!=null) {
this.connector.pause();
Executor executor = this.connector.getProtocolHandler().getExecutor();
if (executor!=null &&executor instanceof ThreadPoolExecutor) {
try {
ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) executor;
threadPoolExecutor.shutdown();
if (!threadPoolExecutor.awaitTermination(10, TimeUnit.SECONDS)) {
detail.setCode(-1);
str.add("warn:Tomcat thread pool did not shut down gracefully within "
+ "10" + " seconds. Proceeding with forceful shutdown" + StringUtil.line());
threadPoolExecutor.shutdownNow();
if (!threadPoolExecutor.awaitTermination(10, TimeUnit.SECONDS)) {
detail.setCode(-1);
str.add("warn:Tomcat thread pool did not terminate already wait 20s" + StringUtil.line());
}
} else {
str.add("warn:Tomcat thread pool gracefully shutdown already" + StringUtil.line());
}
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
detail.setCode(-1);
str.add("error:" + ex.getMessage() + StringUtil.line());
}
}else{
str.add("ThreadPoolExecutor is null");
}
}else{
str.add("connect is null");
}
detail.setEndTime(DateUtil.getDateTime());
return detail;
}
@Override
public Integer getHandleTypeOrder() {
return -1;
}
}
package com.secoo.mall.gracefulshutdown.condition;
import com.secoo.mall.gracefulshutdown.monitor.config.ConfigCenter;
import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.springframework.boot.SpringBootVersion;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
public class TomcatGracefulShutDownJudgment implements Condition {
public static final Logger logger = LoggerFactory.getLogger(ConfigCenter.class);
public static boolean flag = true;
@Override
public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
String version=SpringBootVersion.getVersion();
logger.info("springboot version:"+version);
if (!flag) {//非tomcat 环境 直接返回
return false;
}
try {
String[] str = version.split("\\.");
if (str != null && str.length >= 2) {
if (Integer.valueOf(str[0]).intValue() >= 2) {
if (Integer.valueOf(str[1]).intValue() >= 3) {
return false;
}
}
}
}catch (Exception e){
logger.error("matrix-gracefulshutdown version judgement error",e);
}
return true;
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.secoo.mall.gracefulshutdown.monitor.config;
import org.springframework.core.env.Environment;
public class ConfigCenter {
public String registryAddress;
public String registryGroup;
public String username;
public String password;
public String patch;
public String timeout;
public void init(Environment env) {
registryAddress = env.getProperty("dubbo.registry.address");
registryGroup = env.getProperty("dubbo.protocol.name");
username = env.getProperty("dubbo.registry.username");
password = env.getProperty("dubbo.registry.password");
timeout = env.getProperty("dubbo.application.timeout");
patch = env.getProperty("dubbo.monitorpatch");
}
public String getRegistryAddress() {
return registryAddress;
}
public void setRegistryAddress(String registryAddress) {
this.registryAddress = registryAddress;
}
public String getRegistryGroup() {
return registryGroup;
}
public void setRegistryGroup(String registryGroup) {
this.registryGroup = registryGroup;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPatch() {
return patch;
}
public void setPatch(String patch) {
this.patch = patch;
}
public String getTimeout() {
return timeout;
}
public void setTimeout(String timeout) {
this.timeout = timeout;
}
}
package com.secoo.mall.gracefulshutdown.monitor.dubbo.Constants;
import java.util.HashSet;
import java.util.Set;
public class Constants {
public static final String GROUP_KEY = "group";
public static final String CATEGORY_KEY = "category";
public static final String WEIGHT = "weight";
public static final String BALANCING = "balancing";
public static final String ANY_VALUE = "*";
public static final String EMPTY_PROTOCOL = "empty";
public static final String VERSION_KEY = "version";
public static final String PROVIDERS_CATEGORY = "providers";
public static final String PROVIDERS_CONSUMERS = "consumers";
public static final Set<String> CONFIGS = new HashSet<>();
static {
CONFIGS.add(WEIGHT);
CONFIGS.add(BALANCING);
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.secoo.mall.gracefulshutdown.monitor.dubbo.exception;
public class ConfigurationException extends RuntimeException {
public ConfigurationException(String message) {
super(message);
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.secoo.mall.gracefulshutdown.monitor.dubbo.service;
import java.util.List;
/**
* ProviderService
*/
public interface ProviderService {
/**
* 根据ip地址和应用名字查询服务
* @param address
* @param name
* @return
*/
public List<String> findServicesByAddressAndName(String address,String name);
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.secoo.mall.gracefulshutdown.monitor.dubbo.service.impl;
import com.secoo.mall.gracefulshutdown.monitor.dubbo.service.RegistryServerSync;
import org.apache.dubbo.common.URL;
import java.util.Map;
import java.util.concurrent.ConcurrentMap;
public class AbstractService {
private RegistryServerSync sync;
public AbstractService(RegistryServerSync sync) {
this.sync = sync;
}
public ConcurrentMap<String, ConcurrentMap<String, Map<String, URL>>> getRegistryCache() {
return sync.getRegistryCache();
}
}
package com.secoo.mall.gracefulshutdown.monitor.dubbo.service.impl;
import com.secoo.mall.common.util.string.StringUtil;
import com.secoo.mall.gracefulshutdown.monitor.dubbo.Constants.Constants;
import com.secoo.mall.gracefulshutdown.monitor.dubbo.service.ProviderService;
import com.secoo.mall.gracefulshutdown.monitor.dubbo.service.RegistryServerSync;
import org.apache.dubbo.common.URL;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentMap;
public class ProviderServiceImpl extends AbstractService implements ProviderService {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
public ProviderServiceImpl(RegistryServerSync sync) {
super(sync);
}
@Override
//根据ip 查询 所有的服务
public List<String> findServicesByAddressAndName(String address,String name) {
List<String> ret = new ArrayList<String>();
ConcurrentMap<String, Map<String, URL>> providerUrls = getRegistryCache().get(Constants.PROVIDERS_CATEGORY);
if (providerUrls == null || address == null || address.length() == 0 ||StringUtil.isEmpty(name)) {
return ret;
}
for (Entry<String, Map<String, URL>> e1 : providerUrls.entrySet()) {
Map<String, URL> value = e1.getValue();
for (Entry<String, URL> e2 : value.entrySet()) {
URL u = e2.getValue();
if (address.equals(u.getAddress())&&u.getParameter("application").equals(name)) {
logger.info("findServicesByAddressAndName is exist address:{},application:{},key:{}",address,name,e1.getKey());
ret.add(e1.getKey());
break;
}
}
}
return ret;
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.secoo.mall.gracefulshutdown.monitor.utils;
import org.apache.dubbo.common.io.Bytes;
import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerFactory;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class CoderUtil {
private static final Logger logger = LoggerFactory.getLogger(CoderUtil.class);
private static final char[] hexCode = "0123456789ABCDEF".toCharArray();
private static MessageDigest md;
static {
try {
md = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
logger.error(e.getMessage(), e);
}
}
public static String MD5_16bit(String input) {
String hash = MD5_32bit(input);
if (hash == null) {
return null;
}
return hash.substring(8, 24);
}
public static String MD5_32bit(String input) {
if (input == null || input.length() == 0) {
return null;
}
md.update(input.getBytes());
byte[] digest = md.digest();
String hash = convertToString(digest);
return hash;
}
public static String MD5_32bit(byte[] input) {
if (input == null || input.length == 0) {
return null;
}
md.update(input);
byte[] digest = md.digest();
String hash = convertToString(digest);
return hash;
}
private static String convertToString(byte[] data) {
StringBuilder r = new StringBuilder(data.length * 2);
for (byte b : data) {
r.append(hexCode[(b >> 4) & 0xF]);
r.append(hexCode[(b & 0xF)]);
}
return r.toString();
}
public static String decodeBase64(String source) {
return new String(Bytes.base642bytes(source));
}
}
package com.secoo.mall.gracefulshutdown.monitor.utils;
import com.secoo.mall.gracefulshutdown.condition.TomcatGracefulShutDownJudgment;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class SpringContextHolder implements ApplicationContextInitializer {
public ApplicationContext applicationContext;
private SpringContextHolder(ApplicationContext applicationContext1) {
this.applicationContext = applicationContext1;
}
public SpringContextHolder() {
}
public void setApplicationContext(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
@Override
public void initialize(ConfigurableApplicationContext configurableApplicationContext1) {
if (configurableApplicationContext1 instanceof AnnotationConfigApplicationContext) {
TomcatGracefulShutDownJudgment.flag = false;
}
}
}
package com.secoo.mall.gracefulshutdown.monitor.utils;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
public class Stack {
public static String errInfo(Exception e) {
StringWriter sw = null;
PrintWriter pw = null;
try {
sw = new StringWriter();
pw = new PrintWriter(sw);
// 将出错的栈信息输出到printWriter中
e.printStackTrace(pw);
pw.flush();
sw.flush();
} finally {
if (sw != null) {
try {
sw.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
if (pw != null) {
pw.close();
}
}
return sw.toString();
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.secoo.mall.gracefulshutdown.monitor.utils;
import com.secoo.mall.common.util.string.StringUtil;
import com.secoo.mall.gracefulshutdown.monitor.dubbo.Constants.Constants;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.config.spring.extension.SpringExtensionFactory;
import java.lang.reflect.Field;
/**
* Tool
*/
public class Tool {
public static String getInterface(String service) {
if (service != null && service.length() > 0) {
int i = service.indexOf('/');
if (i >= 0) {
service = service.substring(i + 1);
}
i = service.lastIndexOf(':');
if (i >= 0) {
service = service.substring(0, i);
}
}
return service;
}
public static String getGroup(String service) {
if (service != null && service.length() > 0) {
int i = service.indexOf('/');
if (i >= 0) {
return service.substring(0, i);
}
}
return null;
}
public static String getVersion(String service) {
if (service != null && service.length() > 0) {
int i = service.lastIndexOf(':');
if (i >= 0) {
return service.substring(i + 1);
}
}
return null;
}
public static Object getPrivateConst(String field) {
try {
Field f = SpringExtensionFactory.class.getDeclaredField(field);
f.setAccessible(true);
return f.get(null);
} catch (Exception e) {
}
return null;
}
public static URL formUrl(String config, String group, String username, String password, String timeout) {
URL url = URL.valueOf(config);
if (StringUtils.isNotEmpty(group)) {
url = url.addParameter(Constants.GROUP_KEY, group);
}
if(StringUtil.isEmpty(timeout)){
url=url.addParameter("timeout",20000);
}else{
url=url.addParameter("timeout", Integer.valueOf(timeout).intValue());
}
if (StringUtils.isNotEmpty(username)) {
url = url.setUsername(username);
}
if (StringUtils.isNotEmpty(password)) {
url = url.setPassword(password);
}
return url;
}
}
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.secoo.mall.gracefulshutdown.autoconfigure.MatrixGracefulShutDownAutoConfiguration
org.springframework.context.ApplicationContextInitializer=\
com.secoo.mall.gracefulshutdown.monitor.utils.SpringContextHolder
\ No newline at end of file
### 注意事项:
#### 1.本地host需要配置
```
172.17.105.72 NN01.yl.com
172.17.105.74 DN01.yl.com
172.17.105.77 DN02.yl.com
```
#### 1.本地host需要配置
```java
hbase:
zookeeper:
quorum: 172.17.105.72:2181
```
package com.secoo.mall.rocketmq.converter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.lang.Nullable;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.converter.ContentTypeResolver;
import org.springframework.messaging.converter.DefaultContentTypeResolver;
import org.springframework.messaging.converter.SmartMessageConverter;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.messaging.support.MessageHeaderAccessor;
import org.springframework.util.Assert;
import org.springframework.util.MimeType;
/**
* Abstract base class for {@link SmartMessageConverter} implementations including
* support for common properties and a partial implementation of the conversion methods,
* mainly to check if the converter supports the conversion based on the payload class
* and MIME type.
*
* @author Rossen Stoyanchev
* @author Sebastien Deleuze
* @author Juergen Hoeller
* @since 4.0
*/
public abstract class AbstractMessageConverter implements SmartMessageConverter {
protected final Log logger = LogFactory.getLog(getClass());
private final List<MimeType> supportedMimeTypes = new ArrayList<>(4);
@Nullable
private ContentTypeResolver contentTypeResolver = new DefaultContentTypeResolver();
private boolean strictContentTypeMatch = false;
private Class<?> serializedPayloadClass = byte[].class;
/**
* Constructor with a single MIME type.
* @param supportedMimeType the supported MIME type
*/
protected AbstractMessageConverter(MimeType supportedMimeType) {
this(Collections.singletonList(supportedMimeType));
}
/**
* Constructor with one or more MIME types via vararg.
* @param supportedMimeTypes the supported MIME types
* @since 5.2.2
*/
protected AbstractMessageConverter(MimeType... supportedMimeTypes) {
this(Arrays.asList(supportedMimeTypes));
}
/**
* Constructor with a Collection of MIME types.
* @param supportedMimeTypes the supported MIME types
*/
protected AbstractMessageConverter(Collection<MimeType> supportedMimeTypes) {
this.supportedMimeTypes.addAll(supportedMimeTypes);
}
/**
* Return the supported MIME types.
*/
public List<MimeType> getSupportedMimeTypes() {
return Collections.unmodifiableList(this.supportedMimeTypes);
}
/**
* Allows sub-classes to add more supported mime types.
* @since 5.2.2
*/
protected void addSupportedMimeTypes(MimeType... supportedMimeTypes) {
this.supportedMimeTypes.addAll(Arrays.asList(supportedMimeTypes));
}
/**
* Configure the {@link ContentTypeResolver} to use to resolve the content
* type of an input message.
* <p>Note that if no resolver is configured, then
* {@link #setStrictContentTypeMatch(boolean) strictContentTypeMatch} should
* be left as {@code false} (the default) or otherwise this converter will
* ignore all messages.
* <p>By default, a {@code DefaultContentTypeResolver} instance is used.
*/
public void setContentTypeResolver(@Nullable ContentTypeResolver resolver) {
this.contentTypeResolver = resolver;
}
/**
* Return the configured {@link ContentTypeResolver}.
*/
@Nullable
public ContentTypeResolver getContentTypeResolver() {
return this.contentTypeResolver;
}
/**
* Whether this converter should convert messages for which no content type
* could be resolved through the configured
* {@link org.springframework.messaging.converter.ContentTypeResolver}.
* <p>A converter can configured to be strict only when a
* {@link #setContentTypeResolver contentTypeResolver} is configured and the
* list of {@link #getSupportedMimeTypes() supportedMimeTypes} is not be empty.
* <p>When this flag is set to {@code true}, {@link #supportsMimeType(MessageHeaders)}
* will return {@code false} if the {@link #setContentTypeResolver contentTypeResolver}
* is not defined or if no content-type header is present.
*/
public void setStrictContentTypeMatch(boolean strictContentTypeMatch) {
if (strictContentTypeMatch) {
Assert.notEmpty(getSupportedMimeTypes(), "Strict match requires non-empty list of supported mime types");
Assert.notNull(getContentTypeResolver(), "Strict match requires ContentTypeResolver");
}
this.strictContentTypeMatch = strictContentTypeMatch;
}
/**
* Whether content type resolution must produce a value that matches one of
* the supported MIME types.
*/
public boolean isStrictContentTypeMatch() {
return this.strictContentTypeMatch;
}
/**
* Configure the preferred serialization class to use (byte[] or String) when
* converting an Object payload to a {@link Message}.
* <p>The default value is byte[].
* @param payloadClass either byte[] or String
*/
public void setSerializedPayloadClass(Class<?> payloadClass) {
Assert.isTrue(byte[].class == payloadClass || String.class == payloadClass,
() -> "Payload class must be byte[] or String: " + payloadClass);
this.serializedPayloadClass = payloadClass;
}
/**
* Return the configured preferred serialization payload class.
*/
public Class<?> getSerializedPayloadClass() {
return this.serializedPayloadClass;
}
/**
* Returns the default content type for the payload. Called when
* {@link #toMessage(Object, MessageHeaders)} is invoked without message headers or
* without a content type header.
* <p>By default, this returns the first element of the {@link #getSupportedMimeTypes()
* supportedMimeTypes}, if any. Can be overridden in sub-classes.
* @param payload the payload being converted to message
* @return the content type, or {@code null} if not known
*/
@Nullable
protected MimeType getDefaultContentType(Object payload) {
List<MimeType> mimeTypes = getSupportedMimeTypes();
return (!mimeTypes.isEmpty() ? mimeTypes.get(0) : null);
}
@Override
@Nullable
public final Object fromMessage(Message<?> message, Class<?> targetClass) {
return fromMessage(message, targetClass, null);
}
@Override
@Nullable
public final Object fromMessage(Message<?> message, Class<?> targetClass, @Nullable Object conversionHint) {
if (!canConvertFrom(message, targetClass)) {
return null;
}
return convertFromInternal(message, targetClass, conversionHint);
}
protected boolean canConvertFrom(Message<?> message, Class<?> targetClass) {
return (supports(targetClass) && supportsMimeType(message.getHeaders()));
}
@Override
@Nullable
public final Message<?> toMessage(Object payload, @Nullable MessageHeaders headers) {
return toMessage(payload, headers, null);
}
@Override
@Nullable
public final Message<?> toMessage(Object payload, @Nullable MessageHeaders headers, @Nullable Object conversionHint) {
if (!canConvertTo(payload, headers)) {
return null;
}
Object payloadToUse = convertToInternal(payload, headers, conversionHint);
if (payloadToUse == null) {
return null;
}
MimeType mimeType = getDefaultContentType(payloadToUse);
if (headers != null) {
MessageHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(headers, MessageHeaderAccessor.class);
if (accessor != null && accessor.isMutable()) {
if (mimeType != null) {
accessor.setHeaderIfAbsent(MessageHeaders.CONTENT_TYPE, mimeType);
}
return MessageBuilder.createMessage(payloadToUse, accessor.getMessageHeaders());
}
}
MessageBuilder<?> builder = MessageBuilder.withPayload(payloadToUse);
if (headers != null) {
builder.copyHeaders(headers);
}
if (mimeType != null) {
builder.setHeaderIfAbsent(MessageHeaders.CONTENT_TYPE, mimeType);
}
return builder.build();
}
protected boolean canConvertTo(Object payload, @Nullable MessageHeaders headers) {
return (supports(payload.getClass()) && supportsMimeType(headers));
}
protected boolean supportsMimeType(@Nullable MessageHeaders headers) {
if (getSupportedMimeTypes().isEmpty()) {
return true;
}
MimeType mimeType = getMimeType(headers);
if (mimeType == null) {
return !isStrictContentTypeMatch();
}
for (MimeType current : getSupportedMimeTypes()) {
if (current.getType().equals(mimeType.getType()) && current.getSubtype().equals(mimeType.getSubtype())) {
return true;
}
}
return false;
}
@Nullable
protected MimeType getMimeType(@Nullable MessageHeaders headers) {
return (headers != null && this.contentTypeResolver != null ? this.contentTypeResolver.resolve(headers) : null);
}
/**
* Whether the given class is supported by this converter.
* @param clazz the class to test for support
* @return {@code true} if supported; {@code false} otherwise
*/
protected abstract boolean supports(Class<?> clazz);
/**
* Convert the message payload from serialized form to an Object.
* @param message the input message
* @param targetClass the target class for the conversion
* @param conversionHint an extra object passed to the {@link org.springframework.messaging.converter.MessageConverter},
* e.g. the associated {@code MethodParameter} (may be {@code null}}
* @return the result of the conversion, or {@code null} if the converter cannot
* perform the conversion
* @since 4.2
*/
@Nullable
protected Object convertFromInternal(
Message<?> message, Class<?> targetClass, @Nullable Object conversionHint) {
return null;
}
/**
* cannot perform the conversion
* @since 4.2
*/
@Nullable
protected Object convertToInternal(
Object payload, @Nullable MessageHeaders headers, @Nullable Object conversionHint) {
return null;
}
}
\ No newline at end of file
package com.secoo.mall.rocketmq.converter;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.lang.reflect.Method;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import com.google.protobuf.ExtensionRegistry;
import com.google.protobuf.Message;
import com.google.protobuf.util.JsonFormat;
import org.springframework.lang.Nullable;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.converter.MessageConversionException;
import org.springframework.util.ClassUtils;
import org.springframework.util.ConcurrentReferenceHashMap;
import org.springframework.util.MimeType;
import static org.springframework.util.MimeTypeUtils.APPLICATION_JSON;
import static org.springframework.util.MimeTypeUtils.TEXT_PLAIN;
/**
* An {@code MessageConverter} that reads and writes
* {@link com.google.protobuf.Message com.google.protobuf.Messages} using
* <a href="https://developers.google.com/protocol-buffers/">Google Protocol Buffers</a>.
*
* <p>To generate {@code Message} Java classes, you need to install the {@code protoc} binary.
*
* <p>This converter supports by default {@code "application/x-protobuf"} with the official
* {@code "com.google.protobuf:protobuf-java"} library.
*
* <p>{@code "application/json"} can be supported with the official
* {@code "com.google.protobuf:protobuf-java-util"} 3.x, with 3.3 or higher recommended.
*
* @author Parviz Rozikov
* @author Rossen Stoyanchev
* @since 5.2.2
*/
public class ProtobufMessageConverter extends AbstractMessageConverter {
/**
* The default charset used by the converter.
*/
public static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;
/**
* The mime-type for protobuf {@code application/x-protobuf}.
*/
public static final MimeType PROTOBUF = new MimeType("application", "x-protobuf", DEFAULT_CHARSET);
private static final Map<Class<?>, Method> methodCache = new ConcurrentReferenceHashMap<>();
final ExtensionRegistry extensionRegistry;
@Nullable
private final ProtobufFormatSupport protobufFormatSupport;
/**
* Constructor with a default instance of {@link ExtensionRegistry}.
*/
public ProtobufMessageConverter() {
this(null, null);
}
/**
* Constructor with a given {@code ExtensionRegistry}.
*/
public ProtobufMessageConverter(ExtensionRegistry extensionRegistry) {
this(null, extensionRegistry);
}
ProtobufMessageConverter(@Nullable ProtobufFormatSupport formatSupport,
@Nullable ExtensionRegistry extensionRegistry) {
super(PROTOBUF, TEXT_PLAIN);
if (formatSupport != null) {
this.protobufFormatSupport = formatSupport;
} else if (ClassUtils.isPresent("com.google.protobuf.util.JsonFormat", getClass().getClassLoader())) {
this.protobufFormatSupport = new ProtobufJavaUtilSupport(null, null);
} else {
this.protobufFormatSupport = null;
}
if (this.protobufFormatSupport != null) {
addSupportedMimeTypes(this.protobufFormatSupport.supportedMediaTypes());
}
this.extensionRegistry = (extensionRegistry == null ? ExtensionRegistry.newInstance() : extensionRegistry);
}
@Override
protected boolean supports(Class<?> clazz) {
return Message.class.isAssignableFrom(clazz);
}
@Override
protected boolean canConvertTo(Object payload, @Nullable MessageHeaders headers) {
MimeType mimeType = getMimeType(headers);
return (super.canConvertTo(payload, headers) ||
this.protobufFormatSupport != null && this.protobufFormatSupport.supportsWriteOnly(mimeType));
}
@Override
protected Object convertFromInternal(org.springframework.messaging.Message<?> message,
Class<?> targetClass, @Nullable Object conversionHint) {
MimeType contentType = getMimeType(message.getHeaders());
final Object payload = message.getPayload();
if (contentType == null) {
contentType = PROTOBUF;
}
Charset charset = contentType.getCharset();
if (charset == null) {
charset = DEFAULT_CHARSET;
}
Message.Builder builder = getMessageBuilder(targetClass);
try {
if (PROTOBUF.isCompatibleWith(contentType)) {
builder.mergeFrom((byte[]) payload, this.extensionRegistry);
} else if (this.protobufFormatSupport != null) {
this.protobufFormatSupport.merge(message, charset, contentType, this.extensionRegistry, builder);
}
} catch (IOException ex) {
throw new MessageConversionException(message, "Could not read proto message" + ex.getMessage(), ex);
}
return builder.build();
}
@Override
protected Object convertToInternal(
Object payload, @Nullable MessageHeaders headers, @Nullable Object conversionHint) {
final Message message = (Message) payload;
MimeType contentType = getMimeType(headers);
if (contentType == null) {
contentType = PROTOBUF;
}
Charset charset = contentType.getCharset();
if (charset == null) {
charset = DEFAULT_CHARSET;
}
try {
if (PROTOBUF.isCompatibleWith(contentType)) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
message.writeTo(byteArrayOutputStream);
payload = byteArrayOutputStream.toByteArray();
} else if (this.protobufFormatSupport != null) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
this.protobufFormatSupport.print(message, outputStream, contentType, charset);
payload = outputStream.toString(charset.name());
}
} catch (IOException ex) {
throw new MessageConversionException("Failed to print Protobuf message: " + ex.getMessage(), ex);
}
return payload;
}
/**
* Create a new {@code Message.Builder} instance for the given class.
* <p>This method uses a ConcurrentReferenceHashMap for caching method lookups.
*/
private Message.Builder getMessageBuilder(Class<?> clazz) {
try {
Method method = methodCache.get(clazz);
if (method == null) {
method = clazz.getMethod("newBuilder");
methodCache.put(clazz, method);
}
return (Message.Builder) method.invoke(clazz);
} catch (Exception ex) {
throw new MessageConversionException(
"Invalid Protobuf Message type: no invocable newBuilder() method on " + clazz, ex);
}
}
/**
* Protobuf format support.
*/
interface ProtobufFormatSupport {
MimeType[] supportedMediaTypes();
boolean supportsWriteOnly(@Nullable MimeType mediaType);
void merge(org.springframework.messaging.Message<?> message,
Charset charset, MimeType contentType, ExtensionRegistry extensionRegistry,
Message.Builder builder) throws IOException, MessageConversionException;
void print(Message message, OutputStream output, MimeType contentType, Charset charset)
throws IOException, MessageConversionException;
}
/**
* {@link ProtobufFormatSupport} implementation used when
* {@code com.google.protobuf.util.JsonFormat} is available.
*/
static class ProtobufJavaUtilSupport implements ProtobufFormatSupport {
private final JsonFormat.Parser parser;
private final JsonFormat.Printer printer;
public ProtobufJavaUtilSupport(@Nullable JsonFormat.Parser parser, @Nullable JsonFormat.Printer printer) {
this.parser = (parser != null ? parser : JsonFormat.parser());
this.printer = (printer != null ? printer : JsonFormat.printer());
}
@Override
public MimeType[] supportedMediaTypes() {
return new MimeType[]{APPLICATION_JSON};
}
@Override
public boolean supportsWriteOnly(@Nullable MimeType mimeType) {
return false;
}
@Override
public void merge(org.springframework.messaging.Message<?> message, Charset charset,
MimeType contentType, ExtensionRegistry extensionRegistry, Message.Builder builder)
throws IOException, MessageConversionException {
if (contentType.isCompatibleWith(APPLICATION_JSON)) {
this.parser.merge(message.getPayload().toString(), builder);
} else {
throw new MessageConversionException(
"protobuf-java-util does not support parsing " + contentType);
}
}
@Override
public void print(Message message, OutputStream output, MimeType contentType, Charset charset)
throws IOException, MessageConversionException {
if (contentType.isCompatibleWith(APPLICATION_JSON)) {
OutputStreamWriter writer = new OutputStreamWriter(output, charset);
this.printer.appendTo(message, writer);
writer.flush();
} else {
throw new MessageConversionException(
"protobuf-java-util does not support printing " + contentType);
}
}
}
}
\ No newline at end of file
......@@ -25,7 +25,7 @@ public class MatrixConsumeHook implements ConsumeMessageHook {
@Override
public void consumeMessageAfter(ConsumeMessageContext context) {
try {
log.info("consumeMessageAfter,Succ:{} Topic:{},TraceContext:{},ConsumerGroup:{}", context.isSuccess(), context.getMq().getTopic(), context.getMqTraceContext(), context.getConsumerGroup());
log.debug("consumeMessageAfter,Succ:{} Topic:{},TraceContext:{},ConsumerGroup:{}", context.isSuccess(), context.getMq().getTopic(), context.getMqTraceContext(), context.getConsumerGroup());
} catch (Exception e) {//防灾冗余
}
}
......
......@@ -25,7 +25,7 @@ public class MatrixProducerHook implements SendMessageHook {
@Override
public void sendMessageAfter(SendMessageContext context) {
try {
log.info("sendMessageAfter,TraceContext:{},Mode:{},Topic:{},Targs:{},ProducerGroup:{},BrokerAddr:{},Namespace:{},Exception:{}", context.getMqTraceContext(), context.getCommunicationMode().name(), context.getMessage().getTopic(), context.getMessage().getTags(), context.getProducerGroup(), context.getBrokerAddr(), context.getNamespace(), context.getException() == null ? null : context.getException().getMessage());
log.debug("sendMessageAfter,TraceContext:{},Mode:{},Topic:{},Targs:{},ProducerGroup:{},BrokerAddr:{},Namespace:{},Exception:{}", context.getMqTraceContext(), context.getCommunicationMode().name(), context.getMessage().getTopic(), context.getMessage().getTags(), context.getProducerGroup(), context.getBrokerAddr(), context.getNamespace(), context.getException() == null ? null : context.getException().getMessage());
} catch (Exception e) {//防灾冗余}
}
}
......
package com.secoo.mall.mybatis.spring.boot.autoconfigure;
import com.secoo.mall.datasource.properties.MatrixDataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = MatrixDataSourceBootProperties.PREFIX)
public class MatrixDataSourceBootProperties extends MatrixDataSourceProperties {
public static final String PREFIX = MatrixDataSourceProperties.PREFIX + ".default";
}
......@@ -2,13 +2,8 @@ package com.secoo.mall.mybatis.spring.boot.autoconfigure;
import com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.ctrip.framework.apollo.spring.boot.ApolloAutoConfiguration;
import com.secoo.mall.datasource.bean.MatrixDataSource;
import com.secoo.mall.datasource.factory.DataSourceFactory;
import com.secoo.mall.datasource.factory.DruidDataSourceFactory;
import com.secoo.mall.datasource.provider.ApolloDruidDataSourceProvider;
import com.secoo.mall.datasource.provider.DataSourceProvider;
import com.secoo.mall.datasource.util.SysUtil;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
;
import com.secoo.mall.mybatis.bean.MatrixMybatisSqlSessionFactoryBean;
import com.secoo.mall.mybatis.config.MatrixMybatisConfiguration;
import com.secoo.mall.mybatis.config.MatrixMybatisGlobalConfig;
......@@ -29,51 +24,33 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import javax.annotation.Resource;
import javax.sql.DataSource;
import java.util.Optional;
@Configuration
@EnableConfigurationProperties({MatrixMybatisBootProperties.class})
@ConditionalOnClass(MatrixDataSource.class)
@AutoConfigureBefore({DataSourceAutoConfiguration.class, MybatisPlusAutoConfiguration.class})
@AutoConfigureAfter(ApolloAutoConfiguration.class)
@Slf4j
public class MatrixMybatisAutoConfiguration {
@Setter
private MatrixDataSourceBootProperties matrixDataSourceBootProperties;
@Autowired
@Resource
private MatrixMybatisBootProperties properties;
public MatrixMybatisAutoConfiguration() {
log.info("Init MatrixDataSouceAutoConfiguration");
}
/**
* 如果没有声明,则使用默认的
*
* @return
*/
@Bean
@ConditionalOnMissingBean(DataSource.class)
public DataSource dataSource() {
return new MatrixDataSource("default");
log.info("Init MatrixMybatisAutoConfiguration");
}
@Bean
@ConditionalOnMissingBean(SqlSessionFactory.class)
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
MybatisConfiguration mybatisConfiguration = Optional.ofNullable(properties.getConfiguration()).orElse(matrixMybatisConfig());
MatrixMybatisGlobalConfig globalConfig = Optional.ofNullable(properties.getGlobalConfig()).orElse(matrixMybatisGlobalConfig());
public SqlSessionFactory sqlSessionFactory(DataSource dataSource, GlobalConfig globalConfig, MybatisConfiguration mybatisConfiguration) throws Exception {
MatrixMybatisSqlSessionFactoryBean factory = new MatrixMybatisSqlSessionFactoryBean();
String aliasesPackage = Optional.ofNullable(this.properties.getTypeAliasesPackage()).orElse(MatrixMybatisProperties.DEFAULT_ALIAS_PACKAGES);
String[] mapperLocations = Optional.ofNullable(this.properties.getMapperLocations()).orElse(MatrixMybatisProperties.DEFAULT_MAPPER_LOCATIONS);
this.properties.setTypeAliasesPackage(aliasesPackage);
this.properties.setMapperLocations(mapperLocations);
if (this.properties.getConfigurationProperties() != null) {
factory.setConfigurationProperties(this.properties.getConfigurationProperties());
}
if (this.properties.getConfigurationProperties() != null) {
factory.setConfigurationProperties(this.properties.getConfigurationProperties());
......@@ -102,13 +79,16 @@ public class MatrixMybatisAutoConfiguration {
return factory.getObject();
}
public MatrixMybatisGlobalConfig matrixMybatisGlobalConfig() {
@Bean
@ConditionalOnMissingBean(GlobalConfig.class)
public MatrixMybatisGlobalConfig globalConfig() {
MatrixMybatisGlobalConfig globalConfig = new MatrixMybatisGlobalConfig();
return globalConfig;
}
public MatrixMybatisConfiguration matrixMybatisConfig() {
@Bean
@ConditionalOnMissingBean(MybatisConfiguration.class)
public MatrixMybatisConfiguration mybatisConfiguration() {
MatrixMybatisConfiguration config = new MatrixMybatisConfiguration();
return config;
}
......
package com.secoo.mall.mybatis.spring.boot.initializer;
import com.ctrip.framework.apollo.spring.boot.ApolloApplicationContextInitializer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.env.ConfigurableEnvironment;
public class MybatisApplicationContextInitializer extends ApolloApplicationContextInitializer {
private static Logger log = LoggerFactory.getLogger(MybatisApplicationContextInitializer.class);
public static String MYBATIS_ENCRYPT_PWD = "mybatis.encryptor.pwd";
public static String MYBATIS_ENCRYPT_SALT = "mybatis.encryptor.salt";
public static String ENCRYPT_FILE_NAME = "encryptor.properties";
@Override
protected void initialize(ConfigurableEnvironment environment) {
super.initialize(environment);
}
// public static Integer ORDER = ApolloApplicationContextInitializer.DEFAULT_ORDER + 100;
/* @Override
public void initialize(ConfigurableApplicationContext context) {
//log.info("initialize.....................");
ConfigurableEnvironment environment = context.getEnvironment();
if (!environment.containsProperty(MYBATIS_ENCRYPT_PWD)) {
Properties properties = new Properties();
//从-D读取
initDConfig(properties);
//从文件加载
initFileConfig(properties);
//解密配置
initApolloConfig(properties);
environment.getPropertySources();
environment.getPropertySources().addFirst((new PropertiesPropertySource("matrixProperties", properties)));
}
}
@Override
public int getOrder() {
return ORDER;
}
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
// log.info("postProcessEnvironment.......");
}
private void initDConfig(Properties properties) {
if (System.getProperty(MYBATIS_ENCRYPT_PWD) != null) {
properties.setProperty(MYBATIS_ENCRYPT_PWD, System.getProperty(MYBATIS_ENCRYPT_PWD));
properties.setProperty(MYBATIS_ENCRYPT_SALT, System.getProperty(MYBATIS_ENCRYPT_SALT));
}
}
private void initFileConfig(Properties properties) {
if(!properties.containsKey(MYBATIS_ENCRYPT_PWD)) {
try {
File file = ResourceUtils.getFile(SystemUtils.USER_HOME + File.separator + ENCRYPT_FILE_NAME);
if (!file.exists()) {
// log.warn(ENCRYPT_FILE_NAME + " not exist");
return;
}
properties.load(new FileInputStream(file));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
*//**
* 解密相关的配置
*//*
private void initApolloConfig(Properties properties){
Config appConfig = ConfigService.getConfig("db.config");
Set<String> propertyNames = appConfig.getPropertyNames();
// log.info(propertyNames.toString());
}*/
}
package com.secoo.mall.mybatis.spring.boot.listener;
import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.ConfigService;
import com.ctrip.framework.apollo.spring.boot.ApolloApplicationContextInitializer;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.SystemUtils;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringApplicationRunListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.Ordered;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.util.ResourceUtils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import java.util.Set;
@Slf4j
@NoArgsConstructor
public class MybatisApplicationRunListener implements SpringApplicationRunListener, Ordered {
public static String MYBATIS_ENCRYPT_PWD = "mybats.encryptor.pwd";
public static String MYBATIS_ENCRYPT_SALT = "mybats.encryptor.salt";
public static String ENCRYPT_FILE_NAME = "encryptor.properties";
public static Integer ORDER = ApolloApplicationContextInitializer.DEFAULT_ORDER + 1;
public MybatisApplicationRunListener(SpringApplication application, String[] args) {
}
@Override
public void starting() {
log.info("Appication Starting.....");
}
/**
* 配置加载的依次顺序:
* 1.-spring.config方式加载外部配置
* 2.-D获取
* 3.本地
*
* @param environment
*/
@Override
public void environmentPrepared(ConfigurableEnvironment environment) {
if (!environment.containsProperty(MYBATIS_ENCRYPT_PWD)) {
Properties properties = new Properties();
//从-D读取
initDConfig(properties);
//从文件加载
initFileConfig(properties);
//解密配置
// initApolloConfig(properties);
environment.getPropertySources();
environment.getPropertySources().addFirst((new PropertiesPropertySource("matrixProperties", properties)));
}
}
@Override
public void contextPrepared(ConfigurableApplicationContext context) {
ConfigurableEnvironment environment = context.getEnvironment();
log.info("contextPrepared......");
}
@Override
public void contextLoaded(ConfigurableApplicationContext context) {
}
@Override
public void started(ConfigurableApplicationContext context) {
}
@Override
public void running(ConfigurableApplicationContext context) {
}
@Override
public void failed(ConfigurableApplicationContext context, Throwable exception) {
}
@Override
public int getOrder() {
return ORDER;
}
private void initDConfig(Properties properties) {
if (System.getProperty(MYBATIS_ENCRYPT_PWD) != null) {
properties.setProperty(MYBATIS_ENCRYPT_PWD, System.getProperty(MYBATIS_ENCRYPT_PWD));
properties.setProperty(MYBATIS_ENCRYPT_SALT, System.getProperty(MYBATIS_ENCRYPT_SALT));
}
}
private void initFileConfig(Properties properties) {
if (!properties.containsKey(MYBATIS_ENCRYPT_PWD)) {
try {
File file = ResourceUtils.getFile(SystemUtils.USER_HOME + File.separator + ENCRYPT_FILE_NAME);
if (!file.exists()) {
log.warn(ENCRYPT_FILE_NAME + " not exist");
return;
}
properties.load(new FileInputStream(file));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 解密相关的配置
*/
private void initApolloConfig(Properties properties) {
Config appConfig = ConfigService.getConfig("db.config");
Set<String> propertyNames = appConfig.getPropertyNames();
log.info(propertyNames.toString());
}
}
......@@ -13,8 +13,9 @@ import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.web.bind.annotation.ExceptionHandler;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
public class ProtocolExceptionHandler {
public abstract class AbsProtocolExceptionHandler {
@Resource
private MessageSource messageSource;
......@@ -25,6 +26,8 @@ public class ProtocolExceptionHandler {
@ExceptionHandler({Exception.class})
public Object exceptionHandler(Exception e) {
try {
logReqParams();
if (exceptionProcessor == null) {
throw e;
}
......@@ -57,8 +60,13 @@ public class ProtocolExceptionHandler {
try {
return messageSource.getMessage(e.getMsg(), e.getArgs(), LocaleContextHolder.getLocale());
} catch (Exception ex) {
return String.format("[%s] not exsits.Please check config message_%s.properties.",e.getMsg(), LocaleContextHolder.getLocale());
return String.format("[%s] not exsits.Please check config message_%s.properties.", e.getMsg(), LocaleContextHolder.getLocale());
}
}
/**
* 打印请求参数包括args和reqbody
*/
protected abstract void logReqParams();
}
......@@ -3,7 +3,7 @@ package com.secoo.mall.dubbo.listener;
import com.secoo.mall.common.core.exception.BusinessException;
import com.secoo.mall.common.core.exception.ParameterException;
import com.secoo.mall.common.core.exception.SystemInternalException;
import com.secoo.mall.common.handler.ProtocolExceptionHandler;
import com.secoo.mall.common.handler.AbsProtocolExceptionHandler;
import com.secoo.mall.common.util.response.ResponseUtil;
import org.apache.dubbo.rpc.Invocation;
import org.apache.dubbo.rpc.Invoker;
......@@ -12,7 +12,7 @@ import org.springframework.web.method.annotation.ExceptionHandlerMethodResolver;
import java.lang.reflect.Method;
public class MatrixResponseExceptionListener extends ProtocolExceptionHandler implements ExceptionListener {
public class MatrixResponseExceptionListener extends AbsProtocolExceptionHandler implements ExceptionListener {
private ExceptionHandlerMethodResolver resolver;
public MatrixResponseExceptionListener() {
......@@ -52,4 +52,9 @@ public class MatrixResponseExceptionListener extends ProtocolExceptionHandler im
}
return exception;
}
@Override
protected void logReqParams() {
//unimpimplements
}
}
package com.secoo.mall.web.advice;
import com.secoo.mall.common.handler.ProtocolExceptionHandler;
import com.alibaba.fastjson.JSON;
import com.secoo.mall.common.handler.AbsProtocolExceptionHandler;
import com.secoo.mall.common.util.file.IOUtil;
import com.secoo.mall.common.util.response.ResponseUtil;
import com.secoo.mall.common.util.string.StringUtil;
import com.secoo.mall.common.util.web.WebUtil;
import com.secoo.mall.web.annotation.ApiController;
import com.secoo.mall.web.annotation.ApiIgnoreJson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.MethodParameter;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.Map;
import java.util.Optional;
@RestControllerAdvice(annotations = ApiController.class)
public class ControllerResponseAdvice extends ProtocolExceptionHandler implements ResponseBodyAdvice<Object> {
@Slf4j
public class ControllerResponseAdvice extends AbsProtocolExceptionHandler implements ResponseBodyAdvice<Object>, RequestBodyAdvice {
private final static String REQUEST_BODY = "requestBody";
@Override
public boolean supports(MethodParameter methodParameter, Class<? extends HttpMessageConverter<?>> aClass) {
......@@ -26,4 +47,66 @@ public class ControllerResponseAdvice extends ProtocolExceptionHandler implement
return ResponseUtil.getSuccessResponse(o);
}
@Override
protected void logReqParams() {
HttpServletRequest request = WebUtil.getRequest();
String uri = request.getRequestURI();
String queryString = request.getQueryString();
String params = getParams(request);
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("uri:").append(uri).append(",");
if (StringUtil.isNotEmpty(queryString)) {
stringBuilder.append("queryString:").append(queryString).append(",");
}
if (StringUtil.isNotEmpty(params)) {
stringBuilder.append("params:").append(params);
}
log.info("req:{}", stringBuilder);
}
private String getParams(HttpServletRequest request) {
StringBuilder stringBuilder = new StringBuilder();
// 获取内容格式
String contentType = request.getContentType();
if (StringUtil.isNotBlank(contentType)) {
contentType = contentType.split(";")[0];
}
// form表单格式 表单形式可以从 ParameterMap中获取
if (MediaType.APPLICATION_FORM_URLENCODED_VALUE.equalsIgnoreCase(contentType)) {
// 获取参数
Map<String, String[]> parameterMap = request.getParameterMap();
if (parameterMap != null) {
for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
stringBuilder.append(entry.getKey()).append(":").append(entry.getValue()[0]);
}
}
}
Optional.ofNullable(WebUtil.getAttribute(REQUEST_BODY)).ifPresent(param -> stringBuilder.append(param));
return stringBuilder.toString();
}
@Override
public boolean supports(MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {
return methodParameter.getParameterAnnotation(RequestBody.class) != null;
}
@Override
public HttpInputMessage beforeBodyRead(HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) throws IOException {
return httpInputMessage;
}
@Override
public Object afterBodyRead(Object o, HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {
WebUtil.setAttribute(REQUEST_BODY, JSON.toJSONString(o));
return o;
}
@Override
public Object handleEmptyBody(Object o, HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {
return o;
}
}
# 介绍
mongodb-starter
# 特点
-
# 开始
- 添加依赖
- Maven:需要在自己项目的pom.xml增加,以下配置。
**注意:前置条件需要依赖项目中需要设置matrix的parent**
```xml
<dependency>
<groupId>com.secoo.mall</groupId>
<artifactId>mongodb-starter</artifactId>
</dependency>
```
# 示例
# 介绍
monitor-starter
# 特点
-
# 开始
- 添加依赖
- Maven:需要在自己项目的pom.xml增加,以下配置。
**注意:前置条件需要依赖项目中需要设置matrix的parent**
```xml
<dependency>
<groupId>com.secoo.mall</groupId>
<artifactId>monitor-starter</artifactId>
</dependency>
```
# 示例
package com.secoo.matrix.monitor.config;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MonitorAutoConfiguration {
}
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.filter;
import com.secoo.matrix.monitor.utils.TraceIDUtils;
import javax.servlet.*;
import java.io.IOException;
/**
* @author qianglu
*/
public class HttpTraceFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
try {
TraceIDUtils.continueTraceID();
filterChain.doFilter(servletRequest, servletResponse);
} catch (Exception e) {
}
}
@Override
public void destroy() {
}
}
package com.secoo.matrix.monitor.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.http;
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.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("");
// }
}
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.secoo.matrix.monitor.config.MonitorAutoConfiguration
\ No newline at end of file
# 介绍
openfeign-starter
# 特点
-
# 开始
- 添加依赖
- Maven:需要在自己项目的pom.xml增加,以下配置。
**注意:前置条件需要依赖项目中需要设置matrix的parent**
```xml
<dependency>
<groupId>com.secoo.mall</groupId>
<artifactId>openfeign-starter</artifactId>
</dependency>
```
# 示例
# 介绍
redis-starter
# 特点
-
# 开始
- 添加依赖
- Maven:需要在自己项目的pom.xml增加,以下配置。
**注意:前置条件需要依赖项目中需要设置matrix的parent**
```xml
<dependency>
<groupId>com.secoo.mall</groupId>
<artifactId>redis-starter</artifactId>
</dependency>
```
# 示例
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.secoo.mall.redis.config.MatrixeRedisAutoConfiguration
\ No newline at end of file
package com.secoo.mall.rocketmq.config;
import com.secoo.mall.rocketmq.hook.MatrixConsumeHook;
import org.apache.rocketmq.spring.support.DefaultRocketMQListenerContainer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author qianglu
*/
@Configuration
public class MatrixListenerContainerConfiguration implements BeanPostProcessor {
private final static Logger log = LoggerFactory.getLogger(MatrixListenerContainerConfiguration.class);
@Value("${matrix.rocketmq.consumer.delayLevel:0}")
private int delayLevel;
@Bean
MatrixConsumeHook matrixConsumeHook() {
return new MatrixConsumeHook();
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof DefaultRocketMQListenerContainer) {
DefaultRocketMQListenerContainer container = (DefaultRocketMQListenerContainer) bean;
if (log.isDebugEnabled()) {
log.debug("MatrixListenerContainerConfiguration config setDelayLevelWhenNextConsume value:{}", delayLevel);
}
container.setDelayLevelWhenNextConsume(delayLevel);
container.getConsumer().getDefaultMQPushConsumerImpl().registerConsumeMessageHook(matrixConsumeHook());
}
return bean;
}
}
package com.secoo.mall.rocketmq.config;
import com.secoo.mall.rocketmq.hook.MatrixProducerHook;
import com.secoo.mall.rocketmq.producer.MartixProducer;
import org.apache.rocketmq.acl.common.AclClientRPCHook;
import org.apache.rocketmq.acl.common.SessionCredentials;
import org.apache.rocketmq.client.AccessChannel;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.spring.autoconfigure.RocketMQProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
* @author qianglu
*/
@Configuration
@Import(MatrixListenerContainerConfiguration.class)
public class MatrixRocketMQAutoConfiguration {
@Bean
public DefaultMQProducer defaultMQProducer(RocketMQProperties rocketMQProperties) {
RocketMQProperties.Producer producerConfig = rocketMQProperties.getProducer();
String nameServer = rocketMQProperties.getNameServer();
String groupName = producerConfig.getGroup();
Assert.hasText(nameServer, "[rocketmq.name-server] must not be null");
Assert.hasText(groupName, "[rocketmq.producer.group] must not be null");
String accessChannel = rocketMQProperties.getAccessChannel();
MartixProducer producer;
String ak = rocketMQProperties.getProducer().getAccessKey();
String sk = rocketMQProperties.getProducer().getSecretKey();
MatrixProducerHook matrixProducerHook = new MatrixProducerHook();
if (!StringUtils.isEmpty(ak) && !StringUtils.isEmpty(sk)) {
producer = new MartixProducer(groupName, new AclClientRPCHook(new SessionCredentials(ak, sk)),
rocketMQProperties.getProducer().isEnableMsgTrace(),
rocketMQProperties.getProducer().getCustomizedTraceTopic(),matrixProducerHook);
producer.setVipChannelEnabled(false);
} else {
producer = new MartixProducer(groupName, rocketMQProperties.getProducer().isEnableMsgTrace(),
rocketMQProperties.getProducer().getCustomizedTraceTopic(),matrixProducerHook);
}
producer.setNamesrvAddr(nameServer);
if (!StringUtils.isEmpty(accessChannel)) {
producer.setAccessChannel(AccessChannel.valueOf(accessChannel));
}
producer.setSendMsgTimeout(producerConfig.getSendMessageTimeout());
producer.setRetryTimesWhenSendFailed(producerConfig.getRetryTimesWhenSendFailed());
producer.setRetryTimesWhenSendAsyncFailed(producerConfig.getRetryTimesWhenSendAsyncFailed());
producer.setMaxMessageSize(producerConfig.getMaxMessageSize());
producer.setCompressMsgBodyOverHowmuch(producerConfig.getCompressMessageBodyThreshold());
producer.setRetryAnotherBrokerWhenNotStoreOK(producerConfig.isRetryNextServer());
return producer;
}
}
package com.secoo.mall.rocketmq.hook;
import org.apache.rocketmq.client.hook.ConsumeMessageContext;
import org.apache.rocketmq.client.hook.ConsumeMessageHook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author qianglu
*/
public class MatrixConsumeHook implements ConsumeMessageHook {
private final static Logger log = LoggerFactory.getLogger(MatrixConsumeHook.class);
@Override
public String hookName() {
return "MatrixConsumeHook";
}
@Override
public void consumeMessageBefore(ConsumeMessageContext context) {
}
@Override
public void consumeMessageAfter(ConsumeMessageContext context) {
try {
log.info("consumeMessageAfter,Succ:{} Topic:{},TraceContext:{},ConsumerGroup:{}", context.isSuccess(), context.getMq().getTopic(), context.getMqTraceContext(), context.getConsumerGroup());
} catch (Exception e) {//防灾冗余
}
}
}
package com.secoo.mall.rocketmq.hook;
import org.apache.rocketmq.client.hook.SendMessageContext;
import org.apache.rocketmq.client.hook.SendMessageHook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author qianglu
*/
public class MatrixProducerHook implements SendMessageHook {
private final static Logger log = LoggerFactory.getLogger(MatrixProducerHook.class);
@Override
public String hookName() {
return "MatrixProducerHook";
}
@Override
public void sendMessageBefore(SendMessageContext context) {
}
@Override
public void sendMessageAfter(SendMessageContext context) {
try {
log.info("sendMessageAfter,TraceContext:{},Mode:{},Topic:{},Targs:{},ProducerGroup:{},BrokerAddr:{},Namespace:{},Exception:{}", context.getMqTraceContext(), context.getCommunicationMode().name(), context.getMessage().getTopic(), context.getMessage().getTags(), context.getProducerGroup(), context.getBrokerAddr(), context.getNamespace(), context.getException() == null ? null : context.getException().getMessage());
} catch (Exception e) {//防灾冗余}
}
}
}
\ No newline at end of file
package com.secoo.mall.rocketmq.producer;
import org.apache.rocketmq.client.hook.SendMessageHook;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.remoting.RPCHook;
/**
* @author qianglu
*/
public class MartixProducer extends DefaultMQProducer {
public MartixProducer(final String producerGroup, RPCHook rpcHook, boolean enableMsgTrace,
final String customizedTraceTopic, SendMessageHook messageHook) {
super(producerGroup, rpcHook, enableMsgTrace, customizedTraceTopic);
this.defaultMQProducerImpl.registerSendMessageHook(messageHook);
}
public MartixProducer(final String producerGroup, boolean enableMsgTrace, final String customizedTraceTopic, SendMessageHook messageHook) {
super(producerGroup, enableMsgTrace, customizedTraceTopic);
this.defaultMQProducerImpl.registerSendMessageHook(messageHook);
}
}
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.secoo.mall.rocketmq.config.MatrixRocketMQAutoConfiguration
\ No newline at end of file
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