Toggle navigation
Toggle navigation
This project
Loading...
Sign in
yangyoupeng
/
microservice
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Authored by
yangyoupeng
2016-11-08 18:10:51 +0800
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
84b502038c9a0c65a7f9f8db99f9221706c32ad4
84b50203
1 parent
83d9bb33
对dubboService添加生存时间的控制。超过默认1小时就销dubboservice对象
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
39 additions
and
9 deletions
src/microservice-parent/support-gateway/src/main/java/com/zhaoonline/support/gateway/config/GateWayConfiguration.java
src/microservice-parent/support-gateway/src/main/java/com/zhaoonline/support/gateway/filter/TransforFilter.java
src/microservice-parent/support-gateway/src/main/java/com/zhaoonline/support/gateway/lifecycle/ServiceLifeCycleChecker.java
src/microservice-parent/support-gateway/src/main/java/com/zhaoonline/support/gateway/security/spring/BeanConfiguration.java
src/microservice-parent/support-gateway/src/main/java/com/zhaoonline/support/gateway/service/AbstractService.java
src/microservice-parent/support-gateway/src/main/java/com/zhaoonline/support/gateway/service/DubboService.java
src/microservice-parent/support-gateway/src/main/java/com/zhaoonline/support/gateway/service/HTTPService.java
src/microservice-parent/support-gateway/src/main/java/com/zhaoonline/support/gateway/service/ServiceManager.java
src/microservice-parent/support-gateway/src/main/resources/gateway.config
src/microservice-parent/support-gateway/src/main/java/com/zhaoonline/support/gateway/config/GateWayConfiguration.java
View file @
84b5020
...
...
@@ -50,7 +50,7 @@ public class GateWayConfiguration {
public
Integer
getInteger
(
String
key
,
int
defaultValue
)
{
Object
valObject
=
configMap
.
get
(
key
);
if
(
valObject
!=
null
){
Integer
.
valueOf
(
String
.
valueOf
(
valObject
));
return
Integer
.
valueOf
(
String
.
valueOf
(
valObject
));
}
return
defaultValue
;
}
...
...
src/microservice-parent/support-gateway/src/main/java/com/zhaoonline/support/gateway/filter/TransforFilter.java
View file @
84b5020
...
...
@@ -144,7 +144,7 @@ public class TransforFilter extends ZuulFilter implements InitializingBean,Dispo
}
private
HttpResponse
toDubboService
(
RequestContext
context
,
HttpServletRequest
request
,
ServiceInfo
serviceInfo
)
{
DubboService
service
=
ServiceManager
.
INSTANCE
.
buildDubboServiceFrom
(
serviceInfo
);
DubboService
service
=
ServiceManager
.
INSTANCE
.
buildDubboServiceFrom
(
serviceInfo
,
this
.
commonConfig
);
checkServiceBuilded
(
service
,
context
);
return
doForward
(
context
,
request
,
service
);
}
...
...
@@ -246,7 +246,7 @@ public class TransforFilter extends ZuulFilter implements InitializingBean,Dispo
public
void
afterPropertiesSet
()
throws
Exception
{
LOGGER
.
info
(
"begin to load gateway config file {}"
,
configFileName
);
commonConfig
=
GateWayConfiguration
.
loadFrom
(
configFileName
);
LOGGER
.
info
(
"done to load gateway config file {} with config item:"
,
configFileName
,
commonConfig
);
LOGGER
.
info
(
"done to load gateway config file {} with config item:
{}
"
,
configFileName
,
commonConfig
);
}
...
...
src/microservice-parent/support-gateway/src/main/java/com/zhaoonline/support/gateway/lifecycle/ServiceLifeCycleChecker.java
View file @
84b5020
...
...
@@ -2,11 +2,14 @@ package com.zhaoonline.support.gateway.lifecycle;
import
java.lang.ref.WeakReference
;
import
org.joda.time.DateTime
;
import
org.joda.time.Seconds
;
import
org.omg.PortableInterceptor.LOCATION_FORWARD
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
com.alibaba.dubbo.rpc.service.EchoService
;
import
com.zhaoonline.support.gateway.
filter.TransforFilter
;
import
com.zhaoonline.support.gateway.
config.GateWayConfiguration
;
import
com.zhaoonline.support.gateway.service.AbstractService
;
import
com.zhaoonline.support.gateway.service.DubboService
;
import
com.zhaoonline.support.gateway.service.ServiceInfo
;
...
...
@@ -15,18 +18,33 @@ public class ServiceLifeCycleChecker implements LifeCycleChecker {
private
static
Logger
LOGGER
=
LoggerFactory
.
getLogger
(
ServiceLifeCycleChecker
.
class
);
final
ServiceInfo
serviceInfo
;
final
WeakReference
<
AbstractService
>
serviceRef
;
public
static
Seconds
evictTime
=
null
;
EchoService
echoService
;
public
ServiceLifeCycleChecker
(
AbstractService
service
){
this
(
service
,
new
GateWayConfiguration
());
}
public
ServiceLifeCycleChecker
(
AbstractService
service
,
GateWayConfiguration
config
)
{
serviceInfo
=
service
.
getServicInfo
();
this
.
serviceRef
=
new
WeakReference
(
service
);
if
(
service
instanceof
DubboService
){
DubboService
dubboservice
=
(
DubboService
)
service
;
echoService
=(
EchoService
)
dubboservice
.
getAutomicReference
().
get
();
}
evictTime
=
Seconds
.
seconds
(
config
.
getInteger
(
"dubbo.service.evict.time"
,
3600
));
}
@Override
public
void
run
()
{
AbstractService
service
=
serviceRef
.
get
();
Long
startTime
=
service
.
getStartTime
();
DateTime
startDateTime
=
new
DateTime
(
startTime
);
Seconds
liveTime
=
Seconds
.
secondsBetween
(
startDateTime
,
DateTime
.
now
());
if
(
liveTime
.
isGreaterThan
(
evictTime
)){
LOGGER
.
info
(
"service [{}] live time is greatethan dubbo.service.evict.time [{}],just mark it closed"
,
serviceInfo
,
evictTime
.
getSeconds
());
service
.
setClosed
(
true
);
}
if
(
service
==
null
||
serviceRef
.
get
().
isClosed
()
){
LOGGER
.
info
(
"dubbo service is closed now"
);
throw
new
RuntimeException
(
String
.
format
(
"throw an exception to terminate the thread,the service %s is closed "
,
serviceInfo
));
...
...
src/microservice-parent/support-gateway/src/main/java/com/zhaoonline/support/gateway/security/spring/BeanConfiguration.java
View file @
84b5020
...
...
@@ -57,7 +57,7 @@ public class BeanConfiguration {
@Bean
public
AuthenticationProvider
zhaoAuthenticationProvider
(
UserAuthInfo
Service
userAuthInfoService
){
public
AuthenticationProvider
zhaoAuthenticationProvider
(
CachingUserDetails
Service
userAuthInfoService
){
AuthenticationProvider
provider
=
new
ZhaoUsernamePasswordAuthenticationProvider
(
userAuthInfoService
);
return
provider
;
}
...
...
src/microservice-parent/support-gateway/src/main/java/com/zhaoonline/support/gateway/service/AbstractService.java
View file @
84b5020
...
...
@@ -13,6 +13,7 @@ import com.netflix.zuul.context.RequestContext;
public
abstract
class
AbstractService
<
T
,
R
>
{
private
transient
boolean
closed
=
false
;
private
Long
startTime
=
null
;
protected
final
ServiceInfo
servicInfo
;
...
...
@@ -26,6 +27,14 @@ public abstract class AbstractService<T,R> {
public
abstract
void
init
()
throws
Exception
;
public
void
markStart
(){
startTime
=
System
.
currentTimeMillis
();
}
public
Long
getStartTime
(){
return
startTime
;
}
private
String
serviceType
;
...
...
src/microservice-parent/support-gateway/src/main/java/com/zhaoonline/support/gateway/service/DubboService.java
View file @
84b5020
...
...
@@ -414,6 +414,7 @@ public class DubboService extends AbstractService<HttpServletRequest, HttpRespon
//为了业务安全起见、我们不允许重试。
reference
.
setRetries
(
0
);
getServiceInstance
();
markStart
();
}
private
void
setAttachmentFromServiceInfo
()
{
...
...
src/microservice-parent/support-gateway/src/main/java/com/zhaoonline/support/gateway/service/HTTPService.java
View file @
84b5020
...
...
@@ -302,6 +302,7 @@ public class HTTPService extends AbstractService<HttpServletRequest, HttpRespons
public
void
init
()
{
SOCKET_TIMEOUT
=
this
.
config
.
getInteger
(
GWConstants
.
GATEWAY_HOST_SOCKET_TIMEOUT_MILLIS
,
10000
);
CONNECTION_TIMEOUT
=
this
.
config
.
getInteger
(
GWConstants
.
GATEWAY_HOST_CONNECT_TIMEOUT_MILLIS
,
2000
);
markStart
();
}
@Override
...
...
src/microservice-parent/support-gateway/src/main/java/com/zhaoonline/support/gateway/service/ServiceManager.java
View file @
84b5020
...
...
@@ -58,14 +58,14 @@ public enum ServiceManager{
* @param serviceInfo
* @return DubboService<BR>
*/
public
synchronized
DubboService
buildDubboServiceFrom
(
ServiceInfo
serviceInfo
)
{
public
synchronized
DubboService
buildDubboServiceFrom
(
ServiceInfo
serviceInfo
,
GateWayConfiguration
config
)
{
DubboService
cachedService
=
dubboServiceCache
.
get
(
serviceInfo
.
getServiceInterface
());
if
(
cachedService
==
null
){
LOG
.
info
(
"no cache for dubbo service {},create new instance for service"
,
serviceInfo
.
getServiceInterface
());
DubboService
dubboSerivce
=
new
DubboService
(
serviceInfo
);
try
{
dubboSerivce
.
init
();
monitorStatus
(
dubboSerivce
);
monitorStatus
(
dubboSerivce
,
config
);
}
catch
(
Exception
e
)
{
LOG
.
info
(
"fail to init the dubbo service interface {},cause by {}"
,
serviceInfo
.
getServiceInterface
(),
e
.
getMessage
());
e
.
printStackTrace
();
...
...
@@ -83,9 +83,9 @@ public enum ServiceManager{
return
cachedService
;
}
private
void
monitorStatus
(
DubboService
dubboSerivce
)
{
private
void
monitorStatus
(
DubboService
dubboSerivce
,
GateWayConfiguration
config
)
{
LOG
.
info
(
"assign the dubboservice {} to be status monitored by {}"
,
dubboSerivce
.
getServicInfo
(),
LifeCycleCheckerManager
.
class
.
getName
());
lifeCycleCheckerManager
.
submit
(
new
ServiceLifeCycleChecker
(
dubboSerivce
));
lifeCycleCheckerManager
.
submit
(
new
ServiceLifeCycleChecker
(
dubboSerivce
,
config
));
}
...
...
src/microservice-parent/support-gateway/src/main/resources/gateway.config
View file @
84b5020
dubbo
.
service
.
evict
.
time
=
3600
\ No newline at end of file
...
...
Please
register
or
login
to post a comment