Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
wrr-cat
apollo
提交
79f31784
apollo
项目概览
wrr-cat
/
apollo
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
apollo
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
79f31784
编写于
2月 21, 2017
作者:
J
Jason Song
提交者:
GitHub
2月 21, 2017
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #547 from lepdou/hermes
send msg to mq when namespace published
上级
d0fdd728
1abe61e7
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
326 addition
and
47 deletion
+326
-47
apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/component/config/PortalConfig.java
...ramework/apollo/portal/component/config/PortalConfig.java
+3
-0
apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/listener/ConfigPublishListener.java
...amework/apollo/portal/listener/ConfigPublishListener.java
+86
-47
apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/spi/MQService.java
...java/com/ctrip/framework/apollo/portal/spi/MQService.java
+10
-0
apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/spi/configuration/MQConfiguration.java
...work/apollo/portal/spi/configuration/MQConfiguration.java
+36
-0
apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/spi/ctrip/CtripMQService.java
...rip/framework/apollo/portal/spi/ctrip/CtripMQService.java
+177
-0
apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/spi/defaultimpl/DefaultMQService.java
...ework/apollo/portal/spi/defaultimpl/DefaultMQService.java
+14
-0
未找到文件。
apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/component/config/PortalConfig.java
浏览文件 @
79f31784
...
...
@@ -215,5 +215,8 @@ public class PortalConfig extends RefreshableConfig {
return
getValue
(
"clogging.server.port"
);
}
public
String
hermesServerAddress
()
{
return
getValue
(
"hermes.server.address"
);
}
}
apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/listener/ConfigPublishListener.java
浏览文件 @
79f31784
...
...
@@ -2,6 +2,7 @@ package com.ctrip.framework.apollo.portal.listener;
import
com.ctrip.framework.apollo.common.constants.ReleaseOperation
;
import
com.ctrip.framework.apollo.core.enums.Env
;
import
com.ctrip.framework.apollo.core.utils.ApolloThreadFactory
;
import
com.ctrip.framework.apollo.portal.component.config.PortalConfig
;
import
com.ctrip.framework.apollo.portal.component.emailbuilder.GrayPublishEmailBuilder
;
import
com.ctrip.framework.apollo.portal.component.emailbuilder.MergeEmailBuilder
;
...
...
@@ -11,12 +12,18 @@ import com.ctrip.framework.apollo.portal.entity.bo.Email;
import
com.ctrip.framework.apollo.portal.entity.bo.ReleaseHistoryBO
;
import
com.ctrip.framework.apollo.portal.service.ReleaseHistoryService
;
import
com.ctrip.framework.apollo.portal.spi.EmailService
;
import
com.ctrip.framework.apollo.portal.spi.MQService
;
import
com.ctrip.framework.apollo.tracer.Tracer
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.context.event.EventListener
;
import
org.springframework.stereotype.Component
;
import
java.util.concurrent.ExecutorService
;
import
java.util.concurrent.Executors
;
import
javax.annotation.PostConstruct
;
@Component
public
class
ConfigPublishListener
{
...
...
@@ -34,75 +41,107 @@ public class ConfigPublishListener {
private
MergeEmailBuilder
mergeEmailBuilder
;
@Autowired
private
PortalConfig
portalConfig
;
@Autowired
private
MQService
mqService
;
private
ExecutorService
executorService
;
@PostConstruct
public
void
init
()
{
executorService
=
Executors
.
newSingleThreadExecutor
(
ApolloThreadFactory
.
create
(
"ConfigPublishNotify"
,
false
));
}
@EventListener
public
void
onConfigPublish
(
ConfigPublishEvent
event
)
{
Env
env
=
event
.
getConfigPublishInfo
().
getEnv
();
if
(!
portalConfig
.
emailSupportedEnvs
().
contains
(
env
))
{
return
;
}
executorService
.
submit
(
new
ConfigPublishNotifyTask
(
event
.
getConfigPublishInfo
()));
}
ReleaseHistoryBO
releaseHistory
=
getReleaseHistory
(
event
);
if
(
releaseHistory
==
null
)
{
Tracer
.
logError
(
"Will not send email, because load release history error"
,
null
);
return
;
}
private
class
ConfigPublishNotifyTask
implements
Runnable
{
int
realOperation
=
releaseHistory
.
getOperation
()
;
private
ConfigPublishEvent
.
ConfigPublishInfo
publishInfo
;
Email
email
=
null
;
try
{
email
=
buildEmail
(
env
,
releaseHistory
,
realOperation
);
}
catch
(
Throwable
e
)
{
Tracer
.
logError
(
"build email failed."
,
e
);
ConfigPublishNotifyTask
(
ConfigPublishEvent
.
ConfigPublishInfo
publishInfo
)
{
this
.
publishInfo
=
publishInfo
;
}
if
(
email
!=
null
)
{
emailService
.
send
(
email
);
@Override
public
void
run
()
{
ReleaseHistoryBO
releaseHistory
=
getReleaseHistory
();
if
(
releaseHistory
==
null
)
{
Tracer
.
logError
(
"Load release history failed"
,
null
);
return
;
}
sendPublishEmail
(
releaseHistory
);
sendPublishMsg
(
releaseHistory
);
}
}
private
ReleaseHistoryBO
getReleaseHistory
(
ConfigPublishEvent
event
)
{
ConfigPublishEvent
.
ConfigPublishInfo
info
=
event
.
getConfigPublishInfo
();
Env
env
=
info
.
getEnv
();
private
ReleaseHistoryBO
getReleaseHistory
()
{
Env
env
=
publishInfo
.
getEnv
();
int
operation
=
i
nfo
.
isMergeEvent
()
?
ReleaseOperation
.
GRAY_RELEASE_MERGE_TO_MASTER
:
i
nfo
.
isRollbackEvent
()
?
ReleaseOperation
.
ROLLBACK
:
i
nfo
.
isNormalPublishEvent
()
?
ReleaseOperation
.
NORMAL_RELEASE
:
i
nfo
.
isGrayPublishEvent
()
?
ReleaseOperation
.
GRAY_RELEASE
:
-
1
;
int
operation
=
publishI
nfo
.
isMergeEvent
()
?
ReleaseOperation
.
GRAY_RELEASE_MERGE_TO_MASTER
:
publishI
nfo
.
isRollbackEvent
()
?
ReleaseOperation
.
ROLLBACK
:
publishI
nfo
.
isNormalPublishEvent
()
?
ReleaseOperation
.
NORMAL_RELEASE
:
publishI
nfo
.
isGrayPublishEvent
()
?
ReleaseOperation
.
GRAY_RELEASE
:
-
1
;
if
(
operation
==
-
1
)
{
return
null
;
}
if
(
operation
==
-
1
)
{
return
null
;
}
if
(
i
nfo
.
isRollbackEvent
())
{
return
releaseHistoryService
.
findLatestByPreviousReleaseIdAndOperation
(
env
,
i
nfo
.
getPreviousReleaseId
(),
operation
);
}
else
{
return
releaseHistoryService
.
findLatestByReleaseIdAndOperation
(
env
,
i
nfo
.
getReleaseId
(),
operation
);
}
if
(
publishI
nfo
.
isRollbackEvent
())
{
return
releaseHistoryService
.
findLatestByPreviousReleaseIdAndOperation
(
env
,
publishI
nfo
.
getPreviousReleaseId
(),
operation
);
}
else
{
return
releaseHistoryService
.
findLatestByReleaseIdAndOperation
(
env
,
publishI
nfo
.
getReleaseId
(),
operation
);
}
}
}
private
void
sendPublishEmail
(
ReleaseHistoryBO
releaseHistory
)
{
Env
env
=
publishInfo
.
getEnv
();
private
Email
buildEmail
(
Env
env
,
ReleaseHistoryBO
releaseHistory
,
int
operation
)
{
switch
(
operation
)
{
case
ReleaseOperation
.
GRAY_RELEASE
:
{
return
grayPublishEmailBuilder
.
build
(
env
,
releaseHistory
);
if
(!
portalConfig
.
emailSupportedEnvs
().
contains
(
env
))
{
return
;
}
case
ReleaseOperation
.
NORMAL_RELEASE
:
{
return
normalPublishEmailBuilder
.
build
(
env
,
releaseHistory
);
int
realOperation
=
releaseHistory
.
getOperation
();
Email
email
=
null
;
try
{
email
=
buildEmail
(
env
,
releaseHistory
,
realOperation
);
}
catch
(
Throwable
e
)
{
Tracer
.
logError
(
"build email failed."
,
e
);
}
case
ReleaseOperation
.
ROLLBACK
:
{
return
rollbackEmailBuilder
.
build
(
env
,
releaseHistory
);
if
(
email
!=
null
)
{
emailService
.
send
(
email
);
}
case
ReleaseOperation
.
GRAY_RELEASE_MERGE_TO_MASTER
:
{
return
mergeEmailBuilder
.
build
(
env
,
releaseHistory
);
}
private
void
sendPublishMsg
(
ReleaseHistoryBO
releaseHistory
)
{
mqService
.
sendPublishMsg
(
publishInfo
.
getEnv
(),
releaseHistory
);
}
private
Email
buildEmail
(
Env
env
,
ReleaseHistoryBO
releaseHistory
,
int
operation
)
{
switch
(
operation
)
{
case
ReleaseOperation
.
GRAY_RELEASE
:
{
return
grayPublishEmailBuilder
.
build
(
env
,
releaseHistory
);
}
case
ReleaseOperation
.
NORMAL_RELEASE
:
{
return
normalPublishEmailBuilder
.
build
(
env
,
releaseHistory
);
}
case
ReleaseOperation
.
ROLLBACK
:
{
return
rollbackEmailBuilder
.
build
(
env
,
releaseHistory
);
}
case
ReleaseOperation
.
GRAY_RELEASE_MERGE_TO_MASTER
:
{
return
mergeEmailBuilder
.
build
(
env
,
releaseHistory
);
}
default
:
return
null
;
}
default
:
return
null
;
}
}
}
apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/spi/MQService.java
0 → 100644
浏览文件 @
79f31784
package
com.ctrip.framework.apollo.portal.spi
;
import
com.ctrip.framework.apollo.core.enums.Env
;
import
com.ctrip.framework.apollo.portal.entity.bo.ReleaseHistoryBO
;
public
interface
MQService
{
void
sendPublishMsg
(
Env
env
,
ReleaseHistoryBO
releaseHistory
);
}
apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/spi/configuration/MQConfiguration.java
0 → 100644
浏览文件 @
79f31784
package
com.ctrip.framework.apollo.portal.spi.configuration
;
import
com.ctrip.framework.apollo.portal.spi.ctrip.CtripMQService
;
import
com.ctrip.framework.apollo.portal.spi.defaultimpl.DefaultMQService
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.context.annotation.Profile
;
@Configuration
public
class
MQConfiguration
{
@Configuration
@Profile
(
"ctrip"
)
public
static
class
CtripMQConfiguration
{
@Bean
public
CtripMQService
mqService
()
{
return
new
CtripMQService
();
}
}
/**
* spring.profiles.active != ctrip
*/
@Configuration
@Profile
({
"!ctrip"
})
public
static
class
DefaultMQConfiguration
{
@Bean
public
DefaultMQService
mqService
()
{
return
new
DefaultMQService
();
}
}
}
apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/spi/ctrip/CtripMQService.java
0 → 100644
浏览文件 @
79f31784
package
com.ctrip.framework.apollo.portal.spi.ctrip
;
import
com.google.gson.Gson
;
import
com.ctrip.framework.apollo.common.dto.ReleaseDTO
;
import
com.ctrip.framework.apollo.common.entity.App
;
import
com.ctrip.framework.apollo.core.enums.Env
;
import
com.ctrip.framework.apollo.portal.component.config.PortalConfig
;
import
com.ctrip.framework.apollo.portal.entity.bo.ReleaseHistoryBO
;
import
com.ctrip.framework.apollo.portal.service.AppService
;
import
com.ctrip.framework.apollo.portal.service.ReleaseService
;
import
com.ctrip.framework.apollo.portal.spi.MQService
;
import
com.ctrip.framework.apollo.tracer.Tracer
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.http.HttpEntity
;
import
org.springframework.http.HttpHeaders
;
import
org.springframework.http.MediaType
;
import
org.springframework.http.client.SimpleClientHttpRequestFactory
;
import
org.springframework.http.converter.FormHttpMessageConverter
;
import
org.springframework.http.converter.json.MappingJackson2HttpMessageConverter
;
import
org.springframework.web.client.RestTemplate
;
import
java.util.Arrays
;
import
javax.annotation.PostConstruct
;
public
class
CtripMQService
implements
MQService
{
private
static
final
org
.
apache
.
commons
.
lang
.
time
.
FastDateFormat
TIMESTAMP_FORMAT
=
org
.
apache
.
commons
.
lang
.
time
.
FastDateFormat
.
getInstance
(
"yyyy-MM-dd hh:mm:ss"
);
private
static
final
String
CONFIG_PUBLISH_NOTIFY_TO_NOC_TOPIC
=
"ops.noc.record.created"
;
private
Gson
gson
=
new
Gson
();
@Autowired
private
AppService
appService
;
@Autowired
private
ReleaseService
releaseService
;
@Autowired
private
PortalConfig
portalConfig
;
private
RestTemplate
restTemplate
;
@PostConstruct
public
void
init
()
{
restTemplate
=
new
RestTemplate
();
SimpleClientHttpRequestFactory
rf
=
(
SimpleClientHttpRequestFactory
)
restTemplate
.
getRequestFactory
();
rf
.
setReadTimeout
(
portalConfig
.
readTimeout
());
rf
.
setConnectTimeout
(
portalConfig
.
connectTimeout
());
MappingJackson2HttpMessageConverter
converter
=
new
MappingJackson2HttpMessageConverter
();
converter
.
setSupportedMediaTypes
(
Arrays
.
asList
(
MediaType
.
APPLICATION_JSON
,
MediaType
.
APPLICATION_OCTET_STREAM
));
restTemplate
.
setMessageConverters
(
Arrays
.
asList
(
converter
,
new
FormHttpMessageConverter
()));
}
@Override
public
void
sendPublishMsg
(
Env
env
,
ReleaseHistoryBO
releaseHistory
)
{
if
(
releaseHistory
==
null
)
{
return
;
}
PublishMsg
msg
=
buildPublishMsg
(
env
,
releaseHistory
);
sendMsg
(
portalConfig
.
hermesServerAddress
(),
CONFIG_PUBLISH_NOTIFY_TO_NOC_TOPIC
,
msg
);
}
private
PublishMsg
buildPublishMsg
(
Env
env
,
ReleaseHistoryBO
releaseHistory
)
{
PublishMsg
msg
=
new
PublishMsg
();
msg
.
setPriority
(
"中"
);
msg
.
setTool_origin
(
"Apollo"
);
String
appId
=
releaseHistory
.
getAppId
();
App
app
=
appService
.
load
(
appId
);
msg
.
setInfluence_bu
(
app
.
getOrgId
());
msg
.
setAppid
(
appId
);
ReleaseDTO
release
=
releaseService
.
findReleaseById
(
env
,
releaseHistory
.
getReleaseId
());
msg
.
setAssginee
(
release
.
getDataChangeCreatedBy
());
msg
.
setDesc
(
gson
.
toJson
(
releaseService
.
compare
(
env
,
releaseHistory
.
getPreviousReleaseId
(),
releaseHistory
.
getReleaseId
())));
msg
.
setOperation_time
(
TIMESTAMP_FORMAT
.
format
(
release
.
getDataChangeCreatedTime
()));
return
msg
;
}
private
void
sendMsg
(
String
serverAddress
,
String
topic
,
Object
msg
)
{
HttpHeaders
headers
=
new
HttpHeaders
();
headers
.
setContentType
(
MediaType
.
parseMediaType
(
MediaType
.
APPLICATION_OCTET_STREAM
+
";charset=UTF-8"
));
HttpEntity
<
Object
>
request
=
new
HttpEntity
<>(
msg
,
headers
);
try
{
//send msg by hermes RestAPI
restTemplate
.
postForObject
(
serverAddress
+
"/topics/"
+
topic
,
request
,
Object
.
class
);
}
catch
(
Exception
e
)
{
Tracer
.
logError
(
"Send publish msg to hermes failed"
,
e
);
}
}
private
class
PublishMsg
{
private
String
assginee
;
private
String
desc
;
private
String
operation_time
;
private
String
tool_origin
;
private
String
priority
;
private
String
influence_bu
;
private
String
appid
;
public
String
getAssginee
()
{
return
assginee
;
}
public
void
setAssginee
(
String
assginee
)
{
this
.
assginee
=
assginee
;
}
public
String
getDesc
()
{
return
desc
;
}
public
void
setDesc
(
String
desc
)
{
this
.
desc
=
desc
;
}
public
String
getOperation_time
()
{
return
operation_time
;
}
public
void
setOperation_time
(
String
operation_time
)
{
this
.
operation_time
=
operation_time
;
}
public
String
getTool_origin
()
{
return
tool_origin
;
}
public
void
setTool_origin
(
String
tool_origin
)
{
this
.
tool_origin
=
tool_origin
;
}
public
String
getPriority
()
{
return
priority
;
}
public
void
setPriority
(
String
priority
)
{
this
.
priority
=
priority
;
}
public
String
getInfluence_bu
()
{
return
influence_bu
;
}
public
void
setInfluence_bu
(
String
influence_bu
)
{
this
.
influence_bu
=
influence_bu
;
}
public
String
getAppid
()
{
return
appid
;
}
public
void
setAppid
(
String
appid
)
{
this
.
appid
=
appid
;
}
}
}
apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/spi/defaultimpl/DefaultMQService.java
0 → 100644
浏览文件 @
79f31784
package
com.ctrip.framework.apollo.portal.spi.defaultimpl
;
import
com.ctrip.framework.apollo.core.enums.Env
;
import
com.ctrip.framework.apollo.portal.entity.bo.ReleaseHistoryBO
;
import
com.ctrip.framework.apollo.portal.spi.MQService
;
public
class
DefaultMQService
implements
MQService
{
@Override
public
void
sendPublishMsg
(
Env
env
,
ReleaseHistoryBO
releaseHistory
)
{
//do nothing
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录