Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
matrix
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
CI / CD
CI / CD
Pipelines
Schedules
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
mall
arch
matrix
Commits
23e97c41
Commit
23e97c41
authored
Feb 04, 2021
by
房斌
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
实现了根据springboot是否开启web容器进行tomcat组件的加载
parent
b1577e9d
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
80 additions
and
25 deletions
+80
-25
TomcatGracefulShutDownJudgment.java
matrix-gracefulshutdown/src/main/java/com/secoo/mall/dubbo/condition/TomcatGracefulShutDownJudgment.java
+11
-1
SpringContextHolder.java
matrix-gracefulshutdown/src/main/java/com/secoo/mall/dubbo/monitor/utils/SpringContextHolder.java
+29
-0
Tool.java
matrix-gracefulshutdown/src/main/java/com/secoo/mall/dubbo/monitor/utils/Tool.java
+18
-0
GracefulShutDown.java
matrix-gracefulshutdown/src/main/java/com/secoo/mall/dubbo/service/GracefulShutDown.java
+2
-11
DubboCustomerShutDownHook.java
matrix-gracefulshutdown/src/main/java/com/secoo/mall/dubbo/service/impl/DubboCustomerShutDownHook.java
+1
-4
MatrixGracefulShutDownAutoConfiguration.java
matrix-gracefulshutdown/src/main/java/com/secoo/mall/dubbo/spring/boot/autoconfigure/MatrixGracefulShutDownAutoConfiguration.java
+12
-5
TomcatGracefulShutDownJudgmentInterface.java
matrix-gracefulshutdown/src/main/java/com/secoo/mall/dubbo/spring/boot/autoconfigure/TomcatGracefulShutDownJudgmentInterface.java
+3
-2
spring.factories
matrix-gracefulshutdown/src/main/resources/META-INF/spring.factories
+4
-2
No files found.
matrix-gracefulshutdown/src/main/java/com/secoo/mall/dubbo/condition/
SpringBootVersio
nJudgment.java
→
matrix-gracefulshutdown/src/main/java/com/secoo/mall/dubbo/condition/
TomcatGracefulShutDow
nJudgment.java
View file @
23e97c41
...
...
@@ -8,12 +8,19 @@ import org.springframework.context.annotation.Condition;
import
org.springframework.context.annotation.ConditionContext
;
import
org.springframework.core.type.AnnotatedTypeMetadata
;
public
class
SpringBootVersionJudgment
implements
Condition
{
public
class
TomcatGracefulShutDownJudgment
implements
Condition
{
public
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
ConfigCenter
.
class
);
public
static
volatile
boolean
flag
=
true
;
@Override
public
boolean
matches
(
ConditionContext
conditionContext
,
AnnotatedTypeMetadata
annotatedTypeMetadata
)
{
String
version
=
SpringBootVersion
.
getVersion
();
logger
.
info
(
"springboot version:"
+
version
);
if
(!
flag
)
{
//非servlet 环境 直接返回
return
false
;
}
try
{
String
[]
str
=
version
.
split
(
"\\."
);
if
(
str
!=
null
&&
str
.
length
>=
2
)
{
...
...
@@ -26,6 +33,9 @@ public class SpringBootVersionJudgment implements Condition {
}
catch
(
Exception
e
){
logger
.
error
(
"matrix-gracefulshutdown version judgement error"
,
e
);
}
return
true
;
}
}
matrix-gracefulshutdown/src/main/java/com/secoo/mall/dubbo/monitor/utils/SpringContextHolder.java
0 → 100644
View file @
23e97c41
package
com
.
secoo
.
mall
.
dubbo
.
monitor
.
utils
;
import
com.secoo.mall.dubbo.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
;
}
}
}
matrix-gracefulshutdown/src/main/java/com/secoo/mall/dubbo/monitor/utils/Tool.java
View file @
23e97c41
...
...
@@ -16,6 +16,13 @@
*/
package
com
.
secoo
.
mall
.
dubbo
.
monitor
.
utils
;
import
org.apache.dubbo.config.spring.extension.SpringExtensionFactory
;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.WebApplicationType
;
import
org.springframework.boot.autoconfigure.SpringBootApplication
;
import
java.lang.reflect.Field
;
/**
* Tool
*/
...
...
@@ -54,4 +61,15 @@ public class Tool {
}
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
;
}
}
matrix-gracefulshutdown/src/main/java/com/secoo/mall/dubbo/service/GracefulShutDown.java
View file @
23e97c41
...
...
@@ -7,6 +7,7 @@ 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.dubbo.monitor.utils.Tool
;
import
com.secoo.mall.dubbo.service.impl.TomcatShutDownHook
;
import
org.apache.dubbo.common.utils.NetUtils
;
import
org.apache.dubbo.config.DubboShutdownHook
;
...
...
@@ -23,7 +24,6 @@ import org.springframework.context.event.ApplicationEventMulticaster;
import
org.springframework.context.event.ContextClosedEvent
;
import
org.springframework.core.Ordered
;
import
java.lang.reflect.Field
;
import
java.util.*
;
import
static
org
.
springframework
.
context
.
support
.
AbstractApplicationContext
.
APPLICATION_EVENT_MULTICASTER_BEAN_NAME
;
...
...
@@ -49,20 +49,11 @@ public class GracefulShutDown implements CommandLineRunner, ApplicationListener<
//listener 卸载
ApplicationEventMulticaster
multicaster
=
context
.
getBean
(
APPLICATION_EVENT_MULTICASTER_BEAN_NAME
,
ApplicationEventMulticaster
.
class
);
Class
clz
=
SpringExtensionFactory
.
class
;
multicaster
.
removeApplicationListener
((
ApplicationListener
)
getPrivateConst
(
"SHUTDOWN_HOOK_LISTENER"
));
multicaster
.
removeApplicationListener
((
ApplicationListener
)
Tool
.
getPrivateConst
(
"SHUTDOWN_HOOK_LISTENER"
));
}
}
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
void
test
()
{
logger
.
info
(
"222222222222 closeevent"
);
...
...
matrix-gracefulshutdown/src/main/java/com/secoo/mall/dubbo/service/impl/DubboCustomerShutDownHook.java
View file @
23e97c41
...
...
@@ -56,7 +56,7 @@ public class DubboCustomerShutDownHook extends AbstractShutDown implements Obser
public
ExecutorDetail
stop
()
{
ExecutorDetail
detail
=
new
ExecutorDetail
();
detail
.
setBeginTime
(
DateUtil
.
getDateTime
());
detail
.
setServiceName
(
"dubboDownHock10
46
"
);
detail
.
setServiceName
(
"dubboDownHock10
58
"
);
List
<
String
>
str
=
new
ArrayList
<
String
>();
detail
.
setDetail
(
str
);
detail
.
setCode
(
0
);
...
...
@@ -200,9 +200,6 @@ public class DubboCustomerShutDownHook extends AbstractShutDown implements Obser
}
catch
(
TimeoutException
e
)
{
logger
.
error
(
"matrix dubboshutdown check error"
,
e
);
}
finally
{
if
(
future
.
isDone
())
{
//TODO 结束线程,如何结束?
}
es
.
shutdownNow
();
}
return
detail
;
...
...
matrix-gracefulshutdown/src/main/java/com/secoo/mall/dubbo/spring/boot/autoconfigure/MatrixGracefulShutDownAutoConfiguration.java
View file @
23e97c41
...
...
@@ -12,11 +12,9 @@ import com.secoo.mall.dubbo.service.TomcatGracefulShutDown;
import
com.secoo.mall.dubbo.service.impl.DubboCustomerShutDownHook
;
import
com.secoo.mall.dubbo.service.impl.TomcatShutDownHook
;
import
org.apache.dubbo.common.URL
;
import
org.apache.dubbo.registry.Registry
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnClass
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnExpression
;
import
org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.context.annotation.DependsOn
;
...
...
@@ -32,11 +30,20 @@ public class MatrixGracefulShutDownAutoConfiguration {
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
)
{
UpDatas
dubboUpdata
(
ConfigCenter
configCenter
)
{
ShutDownDataReport
transport
=
new
ShutDownDataReport
();
URL
url
=
configCenter
.
formUrl
(
configCenter
.
registryAddress
,
configCenter
.
registryGroup
,
configCenter
.
username
,
configCenter
.
password
,
configCenter
.
timeout
);
transport
.
setUrl
(
url
);
...
...
@@ -78,13 +85,13 @@ public class MatrixGracefulShutDownAutoConfiguration {
@Bean
@ConditionalOnClass
({
TomcatGracefulShutDown
.
class
,
org
.
apache
.
catalina
.
connector
.
Connector
.
class
})
@
SpringBootVersionJudgment
()
@
TomcatGracefulShutDownJudgmentInterface
()
TomcatShutDownHook
createServletConnectShoutDownHock
()
{
return
new
TomcatShutDownHook
();
}
@Bean
@ConditionalOnClass
(
org
.
apache
.
catalina
.
connector
.
Connector
.
class
)
@
SpringBootVersionJudgment
()
@
TomcatGracefulShutDownJudgmentInterface
()
public
TomcatGracefulShutDown
createSpringbootTomcatInit
(
TomcatShutDownHook
tomcatShutDownHook
){
return
new
TomcatGracefulShutDown
(
tomcatShutDownHook
);
}
...
...
matrix-gracefulshutdown/src/main/java/com/secoo/mall/dubbo/spring/boot/autoconfigure/
SpringBootVersionJudgment
.java
→
matrix-gracefulshutdown/src/main/java/com/secoo/mall/dubbo/spring/boot/autoconfigure/
TomcatGracefulShutDownJudgmentInterface
.java
View file @
23e97c41
package
com
.
secoo
.
mall
.
dubbo
.
spring
.
boot
.
autoconfigure
;
import
com.secoo.mall.dubbo.condition.TomcatGracefulShutDownJudgment
;
import
org.springframework.context.annotation.Conditional
;
import
java.lang.annotation.*
;
...
...
@@ -7,6 +8,6 @@ import java.lang.annotation.*;
@Target
({
ElementType
.
TYPE
,
ElementType
.
METHOD
})
@Retention
(
RetentionPolicy
.
RUNTIME
)
@Documented
@Conditional
(
com
.
secoo
.
mall
.
dubbo
.
condition
.
SpringBootVersio
nJudgment
.
class
)
public
@interface
SpringBootVersionJudgment
{
@Conditional
(
TomcatGracefulShutDow
nJudgment
.
class
)
public
@interface
TomcatGracefulShutDownJudgmentInterface
{
}
matrix-gracefulshutdown/src/main/resources/META-INF/spring.factories
View file @
23e97c41
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.secoo.mall.dubbo.spring.boot.autoconfigure.MatrixGracefulShutDownAutoConfiguration,\
com.secoo.mall.dubbo.monitor.config.ConfigCenter
\ No newline at end of file
com.secoo.mall.dubbo.monitor.config.ConfigCenter
org.springframework.context.ApplicationContextInitializer=\
com.secoo.mall.dubbo.monitor.utils.SpringContextHolder
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment