Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
A
apollo-patch
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
0
Merge Requests
0
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
apollo-patch
Commits
05351d4a
Commit
05351d4a
authored
Aug 14, 2019
by
xiaoguang.xu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
first commit
parents
Changes
9
Show whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
257 additions
and
0 deletions
+257
-0
.gitignore
.gitignore
+4
-0
pom.xml
pom.xml
+46
-0
QGApolloInjector.java
...main/java/cn/quantgroup/apollopatch/QGApolloInjector.java
+64
-0
QGConfigManager.java
src/main/java/cn/quantgroup/apollopatch/QGConfigManager.java
+54
-0
QGMetaServerProvider.java
.../java/cn/quantgroup/apollopatch/QGMetaServerProvider.java
+24
-0
QGSpringApplicationRunListener.java
...up/apollopatch/spring/QGSpringApplicationRunListener.java
+58
-0
com.ctrip.framework.apollo.core.spi.MetaServerProvider
...es/com.ctrip.framework.apollo.core.spi.MetaServerProvider
+2
-0
com.ctrip.framework.apollo.internals.Injector
...NF/services/com.ctrip.framework.apollo.internals.Injector
+2
-0
spring.factories
src/main/resources/META-INF/spring.factories
+3
-0
No files found.
.gitignore
0 → 100644
View file @
05351d4a
.idea/*
*.iml
target/*
\ No newline at end of file
pom.xml
0 → 100644
View file @
05351d4a
<?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"
>
<modelVersion>
4.0.0
</modelVersion>
<groupId>
cn.quantgroup
</groupId>
<artifactId>
apollo-patch
</artifactId>
<version>
0.0.1
</version>
<build>
<plugins>
<plugin>
<groupId>
org.apache.maven.plugins
</groupId>
<artifactId>
maven-compiler-plugin
</artifactId>
<configuration>
<source>
8
</source>
<target>
8
</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>
org.projectlombok
</groupId>
<artifactId>
lombok
</artifactId>
<version>
1.18.8
</version>
<scope>
provided
</scope>
</dependency>
<dependency>
<groupId>
com.ctrip.framework.apollo
</groupId>
<artifactId>
apollo-client
</artifactId>
<version>
1.4.0
</version>
</dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-autoconfigure
</artifactId>
<version>
2.1.7.RELEASE
</version>
<optional>
true
</optional>
</dependency>
</dependencies>
</project>
\ No newline at end of file
src/main/java/cn/quantgroup/apollopatch/QGApolloInjector.java
0 → 100644
View file @
05351d4a
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
);
}
}
}
src/main/java/cn/quantgroup/apollopatch/QGConfigManager.java
0 → 100644
View file @
05351d4a
package
cn
.
quantgroup
.
apollopatch
;
import
com.ctrip.framework.apollo.build.ApolloInjector
;
import
com.ctrip.framework.apollo.core.enums.Env
;
import
com.ctrip.framework.apollo.internals.DefaultConfigManager
;
import
com.ctrip.framework.apollo.util.http.HttpRequest
;
import
com.ctrip.framework.apollo.util.http.HttpResponse
;
import
com.ctrip.framework.apollo.util.http.HttpUtil
;
import
com.ctrip.framework.foundation.Foundation
;
import
lombok.ToString
;
import
lombok.extern.slf4j.Slf4j
;
import
java.util.Map
;
import
java.util.Properties
;
@Slf4j
public
class
QGConfigManager
extends
DefaultConfigManager
{
public
QGConfigManager
()
{
super
();
boolean
isPro
=
Env
.
PRO
.
name
().
equalsIgnoreCase
(
Foundation
.
server
().
getEnvType
());
//如果是生产环境, 我也啥都不干. 太吓人了.
if
(
isPro
)
{
log
.
info
(
"哇, 生产环境. 配置中心静悄悄. 什么都不敢做."
);
return
;
}
String
namespace
=
System
.
getProperty
(
"NAMESPACE"
);
if
(
namespace
==
null
)
{
log
.
info
(
"你好像没有配置 NAMESPACE 哦?你不打算连接到 kubernetes 内部么?"
);
return
;
}
String
eosHost
=
System
.
getProperty
(
"eos_server_host"
,
"http://eos.quantgroups.com/"
);
HttpUtil
httpUtil
=
ApolloInjector
.
getInstance
(
HttpUtil
.
class
);
HttpRequest
httpRequest
=
new
HttpRequest
(
eosHost
+
"api/apollo/env_vars?namespace="
+
namespace
);
HttpResponse
<
KubeEnvInfo
>
mapHttpResponse
=
httpUtil
.
doGet
(
httpRequest
,
KubeEnvInfo
.
class
);
KubeEnvInfo
body
=
mapHttpResponse
.
getBody
();
if
(
body
!=
null
&&
body
.
success
)
{
log
.
info
(
"客官请放心, kubernetes 的环境变量已经注入, 您可以放心的在 kubernetes 之外启动你的服务了"
);
Properties
properties
=
System
.
getProperties
();
properties
.
putAll
(
body
.
details
);
return
;
}
log
.
error
(
"额... 看起来 kubernetes server 有点问题, 返回了false, serverInfo:{} ,body:{}"
,
eosHost
,
body
);
}
@ToString
private
static
class
KubeEnvInfo
{
boolean
success
;
Map
<
String
,
String
>
details
;
}
}
src/main/java/cn/quantgroup/apollopatch/QGMetaServerProvider.java
0 → 100644
View file @
05351d4a
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
;
}
}
src/main/java/cn/quantgroup/apollopatch/spring/QGSpringApplicationRunListener.java
0 → 100644
View file @
05351d4a
package
cn
.
quantgroup
.
apollopatch
.
spring
;
import
com.ctrip.framework.foundation.Foundation
;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.SpringApplicationRunListener
;
import
org.springframework.context.ConfigurableApplicationContext
;
import
org.springframework.core.env.ConfigurableEnvironment
;
public
class
QGSpringApplicationRunListener
implements
SpringApplicationRunListener
{
private
final
SpringApplication
application
;
private
final
String
[]
args
;
public
QGSpringApplicationRunListener
(
SpringApplication
application
,
String
[]
args
)
{
this
.
application
=
application
;
this
.
args
=
args
;
System
.
setProperty
(
"apollo.bootstrap.enabled"
,
"true"
);
String
property
=
Foundation
.
app
().
getProperty
(
"namespace"
,
"application"
);
System
.
setProperty
(
"apollo.bootstrap.namespaces"
,
property
);
System
.
setProperty
(
"apollo.bootstrap.eagerLoad.enabled"
,
"true"
);
}
@Override
public
void
starting
()
{
}
@Override
public
void
environmentPrepared
(
ConfigurableEnvironment
environment
)
{
}
@Override
public
void
contextPrepared
(
ConfigurableApplicationContext
context
)
{
}
@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
)
{
}
}
src/main/resources/META-INF/services/com.ctrip.framework.apollo.core.spi.MetaServerProvider
0 → 100644
View file @
05351d4a
cn.quantgroup.apollopatch.QGMetaServerProvider
\ No newline at end of file
src/main/resources/META-INF/services/com.ctrip.framework.apollo.internals.Injector
0 → 100644
View file @
05351d4a
cn.quantgroup.apollopatch.QGApolloInjector
\ No newline at end of file
src/main/resources/META-INF/spring.factories
0 → 100644
View file @
05351d4a
org.springframework.boot.SpringApplicationRunListener=\
cn.quantgroup.apollopatch.spring.QGSpringApplicationRunListener
\ 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