Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
commons
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
3
Merge Requests
3
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DevOps
commons
Commits
d6cf818c
Commit
d6cf818c
authored
Mar 18, 2022
by
孙 楠
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
使用 ApolloInjectorCustomizer 扩展
parent
76f8a595
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
53 additions
and
104 deletions
+53
-104
QGApolloInjector.java
...main/java/cn/quantgroup/apollopatch/QGApolloInjector.java
+0
-64
QGConfigManager.java
.../main/java/cn/quantgroup/apollopatch/QGConfigManager.java
+14
-11
QGInjectorCustomizer.java
.../java/cn/quantgroup/apollopatch/QGInjectorCustomizer.java
+29
-0
QGMetaServerProvider.java
.../java/cn/quantgroup/apollopatch/QGMetaServerProvider.java
+0
-24
QGSpringApplicationRunListener.java
...up/apollopatch/spring/QGSpringApplicationRunListener.java
+9
-3
com.ctrip.framework.apollo.core.spi.MetaServerProvider
...es/com.ctrip.framework.apollo.core.spi.MetaServerProvider
+0
-1
com.ctrip.framework.apollo.internals.Injector
...NF/services/com.ctrip.framework.apollo.internals.Injector
+0
-1
com.ctrip.framework.apollo.spi.ApolloInjectorCustomizer
...s/com.ctrip.framework.apollo.spi.ApolloInjectorCustomizer
+1
-0
No files found.
qg-apollo-starter/src/main/java/cn/quantgroup/apollopatch/QGApolloInjector.java
deleted
100644 → 0
View file @
76f8a595
package
cn
.
quantgroup
.
apollopatch
;
import
com.ctrip.framework.apollo.exceptions.ApolloConfigException
;
import
com.ctrip.framework.apollo.internals.ConfigManager
;
import
com.ctrip.framework.apollo.internals.ConfigServiceLocator
;
import
com.ctrip.framework.apollo.internals.Injector
;
import
com.ctrip.framework.apollo.internals.RemoteConfigLongPollService
;
import
com.ctrip.framework.apollo.spi.*
;
import
com.ctrip.framework.apollo.tracer.Tracer
;
import
com.ctrip.framework.apollo.util.ConfigUtil
;
import
com.ctrip.framework.apollo.util.http.HttpUtil
;
import
com.ctrip.framework.apollo.util.yaml.YamlParser
;
import
com.google.inject.AbstractModule
;
import
com.google.inject.Guice
;
import
com.google.inject.Singleton
;
import
lombok.extern.slf4j.Slf4j
;
@Slf4j
public
class
QGApolloInjector
implements
Injector
{
private
com
.
google
.
inject
.
Injector
m_injector
;
public
QGApolloInjector
()
{
try
{
m_injector
=
Guice
.
createInjector
(
new
QGApolloInjector
.
ApolloModule
());
}
catch
(
Throwable
ex
)
{
ApolloConfigException
exception
=
new
ApolloConfigException
(
"Unable to initialize Guice Injector!"
,
ex
);
Tracer
.
logError
(
exception
);
throw
exception
;
}
}
@Override
public
<
T
>
T
getInstance
(
Class
<
T
>
clazz
)
{
try
{
return
m_injector
.
getInstance
(
clazz
);
}
catch
(
Throwable
ex
)
{
Tracer
.
logError
(
ex
);
throw
new
ApolloConfigException
(
String
.
format
(
"Unable to load instance for %s!"
,
clazz
.
getName
()),
ex
);
}
}
@Override
public
<
T
>
T
getInstance
(
Class
<
T
>
clazz
,
String
name
)
{
//Guice does not support get instance by type and name
return
null
;
}
private
static
class
ApolloModule
extends
AbstractModule
{
@Override
protected
void
configure
()
{
bind
(
ConfigManager
.
class
).
to
(
QGConfigManager
.
class
).
in
(
Singleton
.
class
);
bind
(
ConfigFactoryManager
.
class
).
to
(
DefaultConfigFactoryManager
.
class
).
in
(
Singleton
.
class
);
bind
(
ConfigRegistry
.
class
).
to
(
DefaultConfigRegistry
.
class
).
in
(
Singleton
.
class
);
bind
(
ConfigFactory
.
class
).
to
(
DefaultConfigFactory
.
class
).
in
(
Singleton
.
class
);
bind
(
ConfigUtil
.
class
).
in
(
Singleton
.
class
);
bind
(
HttpUtil
.
class
).
in
(
Singleton
.
class
);
bind
(
ConfigServiceLocator
.
class
).
in
(
Singleton
.
class
);
bind
(
RemoteConfigLongPollService
.
class
).
in
(
Singleton
.
class
);
bind
(
YamlParser
.
class
).
in
(
Singleton
.
class
);
}
}
}
qg-apollo-starter/src/main/java/cn/quantgroup/apollopatch/QGConfigManager.java
View file @
d6cf818c
...
@@ -2,47 +2,50 @@ package cn.quantgroup.apollopatch;
...
@@ -2,47 +2,50 @@ package cn.quantgroup.apollopatch;
import
com.ctrip.framework.apollo.build.ApolloInjector
;
import
com.ctrip.framework.apollo.build.ApolloInjector
;
import
com.ctrip.framework.apollo.core.enums.Env
;
import
com.ctrip.framework.apollo.core.enums.Env
;
import
com.ctrip.framework.apollo.core.utils.DeferredLoggerFactory
;
import
com.ctrip.framework.apollo.internals.DefaultConfigManager
;
import
com.ctrip.framework.apollo.internals.DefaultConfigManager
;
import
com.ctrip.framework.apollo.util.http.DefaultHttpClient
;
import
com.ctrip.framework.apollo.util.http.HttpRequest
;
import
com.ctrip.framework.apollo.util.http.HttpRequest
;
import
com.ctrip.framework.apollo.util.http.HttpResponse
;
import
com.ctrip.framework.apollo.util.http.HttpResponse
;
import
com.ctrip.framework.apollo.util.http.HttpUtil
;
import
com.ctrip.framework.foundation.Foundation
;
import
com.ctrip.framework.foundation.Foundation
;
import
lombok.ToString
;
import
lombok.ToString
;
import
lombok.extern.slf4j.Slf4j
;
import
org.slf4j.Logger
;
import
java.util.Map
;
import
java.util.Map
;
import
java.util.Properties
;
import
java.util.Properties
;
@Slf4j
public
class
QGConfigManager
extends
DefaultConfigManager
{
public
class
QGConfigManager
extends
DefaultConfigManager
{
private
static
final
Logger
log
=
DeferredLoggerFactory
.
getLogger
(
QGConfigManager
.
class
);
public
QGConfigManager
()
{
public
QGConfigManager
()
{
super
();
super
();
boolean
isPro
=
Env
.
PRO
.
name
().
equalsIgnoreCase
(
Foundation
.
server
().
getEnvType
());
String
env
=
Foundation
.
server
().
getEnvType
();
boolean
isPro
=
Env
.
PRO
.
name
().
equalsIgnoreCase
(
env
);
//如果是生产环境, 我也啥都不干. 太吓人了.
//如果是生产环境, 我也啥都不干. 太吓人了.
if
(
isPro
)
{
if
(
isPro
)
{
log
.
info
(
"哇, 生产环境. 配置中心静悄悄. 什么都不敢做."
);
log
.
info
(
"哇, 生产环境. 配置中心静悄悄. 什么都不敢做."
);
return
;
return
;
}
}
String
namespace
=
System
.
getProperty
(
"NAMESPACE"
);
String
namespace
=
System
.
getProperty
(
"NAMESPACE"
);
if
(
namespace
==
null
)
{
if
(
namespace
==
null
)
{
log
.
info
(
"你好像没有配置 NAMESPACE 哦?你不打算连接到 kubernetes 内部么?"
);
log
.
info
(
"你好像没有配置 NAMESPACE 哦?你不打算连接到 kubernetes 内部么?"
);
return
;
return
;
}
}
String
eosHost
=
System
.
getProperty
(
"eos_server_host"
,
"http://eos.quantgroups.com/"
);
String
eosHost
=
System
.
getProperty
(
"eos_server_host"
,
"http://eos.quantgroups.com/"
);
HttpUtil
httpUtil
=
ApolloInjector
.
getInstance
(
HttpUtil
.
class
);
DefaultHttpClient
httpClient
=
ApolloInjector
.
getInstance
(
DefaultHttpClient
.
class
);
HttpRequest
httpRequest
=
new
HttpRequest
(
eosHost
+
"api/apollo/env_vars?namespace="
+
namespace
);
HttpRequest
httpRequest
=
new
HttpRequest
(
eosHost
+
"api/apollo/env_vars?namespace="
+
namespace
);
HttpResponse
<
KubeEnvInfo
>
mapHttpResponse
=
httpClient
.
doGet
(
httpRequest
,
KubeEnvInfo
.
class
);
HttpResponse
<
KubeEnvInfo
>
mapHttpResponse
=
httpUtil
.
doGet
(
httpRequest
,
KubeEnvInfo
.
class
);
KubeEnvInfo
body
=
mapHttpResponse
.
getBody
();
KubeEnvInfo
body
=
mapHttpResponse
.
getBody
();
if
(
body
!=
null
&&
body
.
success
)
{
if
(
body
!=
null
&&
body
.
success
)
{
log
.
info
(
"客官请放心, kubernetes 的环境变量已经注入, 您可以放心的在 kubernetes 之外启动你的服务了"
);
log
.
info
(
"Env={} Namespace={} Kubernetes 的环境变量已经注入, "
+
"您可以放心的在 Kubernetes 之外启动你的服务了"
,
env
,
namespace
);
Properties
properties
=
System
.
getProperties
();
Properties
properties
=
System
.
getProperties
();
properties
.
putAll
(
body
.
details
);
properties
.
putAll
(
body
.
details
);
return
;
return
;
}
}
log
.
error
(
"
额... 看起来 eos server 有点问题, 返回了false, serverInfo:{} ,body:{}"
,
eosHost
,
body
);
log
.
error
(
"
Env={} Namespace={} eos server 返回异常, serverInfo:{} ,body:{}"
,
env
,
namespace
,
eosHost
,
body
);
}
}
...
...
qg-apollo-starter/src/main/java/cn/quantgroup/apollopatch/QGInjectorCustomizer.java
0 → 100644
View file @
d6cf818c
package
cn
.
quantgroup
.
apollopatch
;
import
com.ctrip.framework.apollo.internals.ConfigManager
;
import
com.ctrip.framework.apollo.spi.ApolloInjectorCustomizer
;
/**
* @author sunnan
*/
public
class
QGInjectorCustomizer
implements
ApolloInjectorCustomizer
{
@Override
public
int
getOrder
()
{
return
0
;
}
@Override
public
<
T
>
T
getInstance
(
Class
<
T
>
clazz
)
{
if
(
clazz
==
ConfigManager
.
class
)
{
return
(
T
)
new
QGConfigManager
();
}
return
null
;
}
@Override
public
<
T
>
T
getInstance
(
Class
<
T
>
clazz
,
String
name
)
{
return
null
;
}
}
qg-apollo-starter/src/main/java/cn/quantgroup/apollopatch/QGMetaServerProvider.java
deleted
100644 → 0
View file @
76f8a595
package
cn
.
quantgroup
.
apollopatch
;
import
com.ctrip.framework.apollo.core.enums.Env
;
import
com.ctrip.framework.apollo.internals.DefaultMetaServerProvider
;
import
java.util.HashMap
;
import
java.util.Map
;
public
class
QGMetaServerProvider
extends
DefaultMetaServerProvider
{
private
Map
<
Env
,
String
>
envMeta
=
new
HashMap
<>();
{
envMeta
.
put
(
Env
.
DEV
,
"http://apollo-dev.quantgroups.com"
);
envMeta
.
put
(
Env
.
PRO
,
"http://apollo-pro.quantgroups.com"
);
}
@Override
public
String
getMetaServerAddress
(
Env
targetEnv
)
{
String
metaServerAddress
=
super
.
getMetaServerAddress
(
targetEnv
);
return
metaServerAddress
==
null
?
envMeta
.
get
(
targetEnv
)
:
metaServerAddress
;
}
}
qg-apollo-starter/src/main/java/cn/quantgroup/apollopatch/spring/QGSpringApplicationRunListener.java
View file @
d6cf818c
...
@@ -3,20 +3,26 @@ package cn.quantgroup.apollopatch.spring;
...
@@ -3,20 +3,26 @@ package cn.quantgroup.apollopatch.spring;
import
com.ctrip.framework.foundation.Foundation
;
import
com.ctrip.framework.foundation.Foundation
;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.SpringApplicationRunListener
;
import
org.springframework.boot.SpringApplicationRunListener
;
import
org.springframework.core.Ordered
;
public
class
QGSpringApplicationRunListener
implements
SpringApplicationRunListener
{
public
class
QGSpringApplicationRunListener
implements
SpringApplicationRunListener
,
Ordered
{
private
final
SpringApplication
application
;
private
final
SpringApplication
application
;
private
final
String
[]
args
;
private
final
String
[]
args
;
public
QGSpringApplicationRunListener
(
SpringApplication
application
,
String
[]
args
)
{
public
QGSpringApplicationRunListener
(
SpringApplication
application
,
String
[]
args
)
{
this
.
application
=
application
;
this
.
application
=
application
;
this
.
args
=
args
;
this
.
args
=
args
;
System
.
setProperty
(
"apollo.bootstrap.enabled"
,
"true"
);
System
.
setProperty
(
"apollo.bootstrap.enabled"
,
"true"
);
String
property
=
Foundation
.
app
().
getProperty
(
"namespace"
,
"application"
);
String
property
=
Foundation
.
app
().
getProperty
(
"namespace"
,
"application"
);
System
.
setProperty
(
"apollo.bootstrap.namespaces"
,
property
);
System
.
setProperty
(
"apollo.bootstrap.namespaces"
,
property
);
System
.
setProperty
(
"apollo.bootstrap.eagerLoad.enabled"
,
"true"
);
System
.
setProperty
(
"apollo.bootstrap.eagerLoad.enabled"
,
"true"
);
}
}
@Override
public
int
getOrder
()
{
return
0
;
}
}
}
qg-apollo-starter/src/main/resources/META-INF/services/com.ctrip.framework.apollo.core.spi.MetaServerProvider
deleted
100644 → 0
View file @
76f8a595
cn.quantgroup.apollopatch.QGMetaServerProvider
\ No newline at end of file
qg-apollo-starter/src/main/resources/META-INF/services/com.ctrip.framework.apollo.internals.Injector
deleted
100644 → 0
View file @
76f8a595
cn.quantgroup.apollopatch.QGApolloInjector
\ No newline at end of file
qg-apollo-starter/src/main/resources/META-INF/services/com.ctrip.framework.apollo.spi.ApolloInjectorCustomizer
0 → 100644
View file @
d6cf818c
cn.quantgroup.apollopatch.QGInjectorCustomizer
\ 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