Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
wrr-cat
apollo
提交
fa1c4fe3
apollo
项目概览
wrr-cat
/
apollo
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
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,发现更多精彩内容 >>
提交
fa1c4fe3
编写于
3月 14, 2016
作者:
Y
Yiming Liu
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #11 from nobodyiam/client
init apollo client
上级
9009c1c0
934e4f54
变更
16
显示空白变更内容
内联
并排
Showing
16 changed file
with
490 addition
and
19 deletion
+490
-19
apollo-client/pom.xml
apollo-client/pom.xml
+37
-16
apollo-client/src/main/java/com/ctrip/apollo/client/ApolloConfig.java
...t/src/main/java/com/ctrip/apollo/client/ApolloConfig.java
+72
-0
apollo-client/src/main/java/com/ctrip/apollo/client/loader/ConfigLoader.java
...ain/java/com/ctrip/apollo/client/loader/ConfigLoader.java
+14
-0
apollo-client/src/main/java/com/ctrip/apollo/client/loader/ConfigLoaderFactory.java
...a/com/ctrip/apollo/client/loader/ConfigLoaderFactory.java
+21
-0
apollo-client/src/main/java/com/ctrip/apollo/client/loader/impl/LocalConfigLoader.java
...om/ctrip/apollo/client/loader/impl/LocalConfigLoader.java
+17
-0
apollo-client/src/main/java/com/ctrip/apollo/client/loader/impl/MockConfigLoader.java
...com/ctrip/apollo/client/loader/impl/MockConfigLoader.java
+31
-0
apollo-client/src/main/java/com/ctrip/apollo/client/loader/impl/RemoteConfigLoader.java
...m/ctrip/apollo/client/loader/impl/RemoteConfigLoader.java
+17
-0
apollo-client/src/test/java/com/ctrip/apollo/client/ApolloConfigTest.java
...c/test/java/com/ctrip/apollo/client/ApolloConfigTest.java
+70
-0
apollo-demo/pom.xml
apollo-demo/pom.xml
+40
-0
apollo-demo/src/main/java/com/ctrip/apollo/demo/AppConfig.java
...o-demo/src/main/java/com/ctrip/apollo/demo/AppConfig.java
+24
-0
apollo-demo/src/main/java/com/ctrip/apollo/demo/DemoController.java
...o/src/main/java/com/ctrip/apollo/demo/DemoController.java
+27
-0
apollo-demo/src/main/java/com/ctrip/apollo/demo/WebConfig.java
...o-demo/src/main/java/com/ctrip/apollo/demo/WebConfig.java
+38
-0
apollo-demo/src/main/webapp/WEB-INF/urlrewrite.xml
apollo-demo/src/main/webapp/WEB-INF/urlrewrite.xml
+36
-0
apollo-demo/src/main/webapp/WEB-INF/views/welcome.jsp
apollo-demo/src/main/webapp/WEB-INF/views/welcome.jsp
+5
-0
apollo-demo/src/main/webapp/WEB-INF/web.xml
apollo-demo/src/main/webapp/WEB-INF/web.xml
+37
-0
pom.xml
pom.xml
+4
-3
未找到文件。
apollo-client/pom.xml
浏览文件 @
fa1c4fe3
...
...
@@ -11,9 +11,30 @@
<artifactId>
apollo-client
</artifactId>
<name>
Apollo Client
</name>
<dependencies>
<!-- apollo -->
<dependency>
<groupId>
com.ctrip.apollo
</groupId>
<artifactId>
apollo-core
</artifactId>
</dependency>
<!-- end of apollo -->
<!-- spring -->
<dependency>
<groupId>
org.springframework
</groupId>
<artifactId>
spring-context
</artifactId>
</dependency>
<!-- end of spring -->
<!-- util -->
<dependency>
<groupId>
com.google.guava
</groupId>
<artifactId>
guava
</artifactId>
</dependency>
<!-- end of util -->
<!-- test -->
<dependency>
<groupId>
commons-logging
</groupId>
<artifactId>
commons-logging
</artifactId>
<scope>
test
</scope>
</dependency>
<!-- end of test -->
</dependencies>
</project>
apollo-client/src/main/java/com/ctrip/apollo/client/ApolloConfig.java
0 → 100644
浏览文件 @
fa1c4fe3
package
com.ctrip.apollo.client
;
import
com.ctrip.apollo.client.loader.ConfigLoader
;
import
com.ctrip.apollo.client.loader.ConfigLoaderFactory
;
import
org.springframework.beans.BeansException
;
import
org.springframework.beans.factory.config.ConfigurableListableBeanFactory
;
import
org.springframework.beans.factory.support.BeanDefinitionRegistry
;
import
org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor
;
import
org.springframework.context.ApplicationContext
;
import
org.springframework.context.ApplicationContextAware
;
import
org.springframework.context.ConfigurableApplicationContext
;
import
org.springframework.core.Ordered
;
import
org.springframework.core.PriorityOrdered
;
import
org.springframework.core.env.CompositePropertySource
;
import
org.springframework.core.env.MutablePropertySources
;
/**
* Client side config
*
* @author Jason Song(song_s@ctrip.com)
*/
public
class
ApolloConfig
implements
BeanDefinitionRegistryPostProcessor
,
PriorityOrdered
,
ApplicationContextAware
{
public
static
final
String
APOLLO_PROPERTY_SOURCE_NAME
=
"ApolloConfigProperties"
;
private
ConfigLoader
configLoader
;
private
ConfigurableApplicationContext
applicationContext
;
public
ApolloConfig
()
{
this
.
configLoader
=
ConfigLoaderFactory
.
getInstance
().
getMockConfigLoader
();
}
@Override
public
void
setApplicationContext
(
ApplicationContext
applicationContext
)
throws
BeansException
{
if
(!(
applicationContext
instanceof
ConfigurableApplicationContext
))
{
throw
new
RuntimeException
(
String
.
format
(
"ApplicationContext must implement ConfigurableApplicationContext, but found: %s"
,
applicationContext
.
getClass
().
getName
()));
}
this
.
applicationContext
=
(
ConfigurableApplicationContext
)
applicationContext
;
}
/**
* This is the first method invoked, so we could prepare the property sources here
*/
@Override
public
void
postProcessBeanDefinitionRegistry
(
BeanDefinitionRegistry
registry
)
throws
BeansException
{
preparePropertySource
();
}
/**
* This is executed after postProcessBeanDefinitionRegistry
*/
@Override
public
void
postProcessBeanFactory
(
ConfigurableListableBeanFactory
beanFactory
)
throws
BeansException
{
}
@Override
public
int
getOrder
()
{
return
Ordered
.
HIGHEST_PRECEDENCE
+
1
;
}
void
preparePropertySource
()
{
MutablePropertySources
currentPropertySources
=
applicationContext
.
getEnvironment
().
getPropertySources
();
if
(
currentPropertySources
.
contains
(
APOLLO_PROPERTY_SOURCE_NAME
))
{
currentPropertySources
.
remove
(
APOLLO_PROPERTY_SOURCE_NAME
);
}
CompositePropertySource
composite
=
new
CompositePropertySource
(
APOLLO_PROPERTY_SOURCE_NAME
);
composite
.
addPropertySource
(
configLoader
.
loadPropertySource
());
currentPropertySources
.
addFirst
(
composite
);
}
}
apollo-client/src/main/java/com/ctrip/apollo/client/loader/ConfigLoader.java
0 → 100644
浏览文件 @
fa1c4fe3
package
com.ctrip.apollo.client.loader
;
import
org.springframework.core.env.CompositePropertySource
;
/**
* @author Jason Song(songs_ctrip.com)
*/
public
interface
ConfigLoader
{
/**
* Load property source for client use
* @return property source
*/
CompositePropertySource
loadPropertySource
();
}
apollo-client/src/main/java/com/ctrip/apollo/client/loader/ConfigLoaderFactory.java
0 → 100644
浏览文件 @
fa1c4fe3
package
com.ctrip.apollo.client.loader
;
import
com.ctrip.apollo.client.loader.impl.MockConfigLoader
;
/**
* @author Jason Song(song_s@ctrip.com)
*/
public
class
ConfigLoaderFactory
{
private
static
ConfigLoaderFactory
configLoaderFactory
=
new
ConfigLoaderFactory
();
private
ConfigLoaderFactory
()
{
}
public
static
ConfigLoaderFactory
getInstance
()
{
return
configLoaderFactory
;
}
public
ConfigLoader
getMockConfigLoader
()
{
return
new
MockConfigLoader
();
}
}
apollo-client/src/main/java/com/ctrip/apollo/client/loader/impl/LocalConfigLoader.java
0 → 100644
浏览文件 @
fa1c4fe3
package
com.ctrip.apollo.client.loader.impl
;
import
com.ctrip.apollo.client.loader.ConfigLoader
;
import
org.springframework.core.env.CompositePropertySource
;
/**
* Load config from local backup file
* @author Jason Song(song_s@ctrip.com)
*/
public
class
LocalConfigLoader
implements
ConfigLoader
{
private
static
final
String
PROPERTY_SOURCE_NAME
=
"ApolloLocalConfigProperties"
;
@Override
public
CompositePropertySource
loadPropertySource
()
{
return
null
;
}
}
apollo-client/src/main/java/com/ctrip/apollo/client/loader/impl/MockConfigLoader.java
0 → 100644
浏览文件 @
fa1c4fe3
package
com.ctrip.apollo.client.loader.impl
;
import
com.ctrip.apollo.client.loader.ConfigLoader
;
import
com.google.common.collect.Maps
;
import
org.springframework.core.env.CompositePropertySource
;
import
org.springframework.core.env.MapPropertySource
;
import
java.util.Map
;
/**
* Mock config
* @author Jason Song(song_s@ctrip.com)
*/
public
class
MockConfigLoader
implements
ConfigLoader
{
private
static
final
String
PROPERTY_SOURCE_NAME
=
"ApolloMockConfigProperties"
;
@Override
public
CompositePropertySource
loadPropertySource
()
{
CompositePropertySource
composite
=
new
CompositePropertySource
(
PROPERTY_SOURCE_NAME
);
composite
.
addPropertySource
(
mock
());
return
composite
;
}
private
MapPropertySource
mock
()
{
Map
<
String
,
Object
>
source
=
Maps
.
newHashMap
();
source
.
put
(
"apollo.foo"
,
"bar"
);
MapPropertySource
propertySource
=
new
MapPropertySource
(
"mock source"
,
source
);
return
propertySource
;
}
}
apollo-client/src/main/java/com/ctrip/apollo/client/loader/impl/RemoteConfigLoader.java
0 → 100644
浏览文件 @
fa1c4fe3
package
com.ctrip.apollo.client.loader.impl
;
import
com.ctrip.apollo.client.loader.ConfigLoader
;
import
org.springframework.core.env.CompositePropertySource
;
/**
* Load config from remote config server
* @author Jason Song(song_s@ctrip.com)
*/
public
class
RemoteConfigLoader
implements
ConfigLoader
{
private
static
final
String
PROPERTY_SOURCE_NAME
=
"ApolloRemoteConfigProperties"
;
@Override
public
CompositePropertySource
loadPropertySource
()
{
return
null
;
}
}
apollo-client/src/test/java/com/ctrip/apollo/client/ApolloConfigTest.java
0 → 100644
浏览文件 @
fa1c4fe3
package
com.ctrip.apollo.client
;
import
com.ctrip.apollo.client.loader.ConfigLoader
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.mockito.ArgumentCaptor
;
import
org.mockito.Mock
;
import
org.mockito.runners.MockitoJUnitRunner
;
import
org.springframework.context.ApplicationContext
;
import
org.springframework.context.ConfigurableApplicationContext
;
import
org.springframework.core.env.CompositePropertySource
;
import
org.springframework.core.env.ConfigurableEnvironment
;
import
org.springframework.core.env.MutablePropertySources
;
import
org.springframework.test.util.ReflectionTestUtils
;
import
static
org
.
mockito
.
Mockito
.*;
import
static
org
.
junit
.
Assert
.*;
/**
* @author Jason Song(song_s@ctrip.com)
*/
@RunWith
(
MockitoJUnitRunner
.
class
)
public
class
ApolloConfigTest
{
private
ApolloConfig
apolloConfig
;
@Mock
private
ConfigLoader
configLoader
;
@Mock
private
ConfigurableApplicationContext
applicationContext
;
@Mock
private
ConfigurableEnvironment
env
;
@Mock
private
MutablePropertySources
mutablePropertySources
;
@Before
public
void
setUp
()
{
apolloConfig
=
new
ApolloConfig
();
when
(
applicationContext
.
getEnvironment
()).
thenReturn
(
env
);
when
(
env
.
getPropertySources
()).
thenReturn
(
mutablePropertySources
);
apolloConfig
.
setApplicationContext
(
applicationContext
);
ReflectionTestUtils
.
setField
(
apolloConfig
,
"configLoader"
,
configLoader
);
}
@Test
(
expected
=
RuntimeException
.
class
)
public
void
testInvalidApplicationContext
()
{
ApplicationContext
someInvalidApplication
=
mock
(
ApplicationContext
.
class
);
apolloConfig
.
setApplicationContext
(
someInvalidApplication
);
}
@Test
public
void
testPreparePropertySourceSuccessful
()
{
CompositePropertySource
somePropertySource
=
mock
(
CompositePropertySource
.
class
);
final
ArgumentCaptor
<
CompositePropertySource
>
captor
=
ArgumentCaptor
.
forClass
(
CompositePropertySource
.
class
);
when
(
configLoader
.
loadPropertySource
()).
thenReturn
(
somePropertySource
);
apolloConfig
.
preparePropertySource
();
verify
(
configLoader
,
times
(
1
)).
loadPropertySource
();
verify
(
mutablePropertySources
,
times
(
1
)).
addFirst
(
captor
.
capture
());
final
CompositePropertySource
insertedPropertySource
=
captor
.
getValue
();
assertEquals
(
ApolloConfig
.
APOLLO_PROPERTY_SOURCE_NAME
,
insertedPropertySource
.
getName
());
assertTrue
(
insertedPropertySource
.
getPropertySources
().
contains
(
somePropertySource
));
}
}
apollo-demo/pom.xml
0 → 100644
浏览文件 @
fa1c4fe3
<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/maven-v4_0_0.xsd"
>
<parent>
<artifactId>
apollo
</artifactId>
<groupId>
com.ctrip.apollo
</groupId>
<version>
0.0.1
</version>
</parent>
<modelVersion>
4.0.0
</modelVersion>
<artifactId>
apollo-demo
</artifactId>
<packaging>
war
</packaging>
<name>
Apollo Demo
</name>
<url>
http://maven.apache.org
</url>
<dependencies>
<dependency>
<groupId>
com.ctrip.apollo
</groupId>
<artifactId>
apollo-client
</artifactId>
<version>
${project.version}
</version>
</dependency>
<dependency>
<groupId>
org.springframework
</groupId>
<artifactId>
spring-webmvc
</artifactId>
</dependency>
<dependency>
<groupId>
org.tuckey
</groupId>
<artifactId>
urlrewritefilter
</artifactId>
<version>
4.0.4
</version>
</dependency>
<dependency>
<groupId>
javax.servlet
</groupId>
<artifactId>
jstl
</artifactId>
</dependency>
<dependency>
<groupId>
commons-logging
</groupId>
<artifactId>
commons-logging
</artifactId>
</dependency>
</dependencies>
<build>
<finalName>
apollo-demo
</finalName>
</build>
</project>
apollo-demo/src/main/java/com/ctrip/apollo/demo/AppConfig.java
0 → 100644
浏览文件 @
fa1c4fe3
package
com.ctrip.apollo.demo
;
import
com.ctrip.apollo.client.ApolloConfig
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.ComponentScan
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.context.support.PropertySourcesPlaceholderConfigurer
;
/**
* @author Jason Song(song_s@ctrip.com)
*/
@Configuration
@ComponentScan
(
value
=
"com.ctrip.apollo.demo"
)
public
class
AppConfig
{
@Bean
public
ApolloConfig
apolloConfig
()
{
return
new
ApolloConfig
();
}
@Bean
public
PropertySourcesPlaceholderConfigurer
propertySourcesPlaceholderConfigurer
()
{
return
new
PropertySourcesPlaceholderConfigurer
();
}
}
apollo-demo/src/main/java/com/ctrip/apollo/demo/DemoController.java
0 → 100644
浏览文件 @
fa1c4fe3
package
com.ctrip.apollo.demo
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.core.env.Environment
;
import
org.springframework.web.bind.annotation.PathVariable
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RequestMethod
;
import
org.springframework.web.bind.annotation.RestController
;
/**
* @author Jason Song(song_s@ctrip.com)
*/
@RestController
@RequestMapping
(
"/demo"
)
public
class
DemoController
{
@Autowired
private
Environment
env
;
@Value
(
"${apollo.foo}"
)
private
String
foo
;
@RequestMapping
(
value
=
"/config/{configName:.*}"
,
method
=
RequestMethod
.
GET
)
public
String
queryConfig
(
@PathVariable
String
configName
)
{
return
env
.
getProperty
(
configName
,
"undefined"
);
}
}
apollo-demo/src/main/java/com/ctrip/apollo/demo/WebConfig.java
0 → 100644
浏览文件 @
fa1c4fe3
package
com.ctrip.apollo.demo
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.web.servlet.config.annotation.EnableWebMvc
;
import
org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry
;
import
org.springframework.web.servlet.config.annotation.ViewControllerRegistry
;
import
org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter
;
import
org.springframework.web.servlet.view.InternalResourceViewResolver
;
import
org.springframework.web.servlet.view.JstlView
;
/**
* @author Jason Song(song_s@ctrip.com)
*/
@Configuration
@EnableWebMvc
public
class
WebConfig
extends
WebMvcConfigurerAdapter
{
@Override
public
void
addViewControllers
(
ViewControllerRegistry
registry
)
{
registry
.
addViewController
(
"/"
).
setViewName
(
"welcome"
);
registry
.
addViewController
(
"/index"
).
setViewName
(
"welcome"
);
}
@Bean
public
InternalResourceViewResolver
viewResolver
()
{
InternalResourceViewResolver
viewResolver
=
new
InternalResourceViewResolver
();
viewResolver
.
setViewClass
(
JstlView
.
class
);
viewResolver
.
setPrefix
(
"/WEB-INF/views/"
);
viewResolver
.
setSuffix
(
".jsp"
);
return
viewResolver
;
}
@Override
public
void
addResourceHandlers
(
ResourceHandlerRegistry
registry
)
{
registry
.
addResourceHandler
(
"/**"
).
addResourceLocations
(
"/"
);
}
}
apollo-demo/src/main/webapp/WEB-INF/urlrewrite.xml
0 → 100644
浏览文件 @
fa1c4fe3
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 3.0//EN" "http://tuckey.org/res/dtds/urlrewrite3.0.dtd">
<urlrewrite
default-match-type=
"wildcard"
>
<rule>
<from>
/favicon.ico
</from>
<to>
/s/favicon.ico
</to>
</rule>
<rule>
<from>
/images/**
</from>
<to>
/s/images/$1
</to>
</rule>
<rule>
<from>
/scripts/**
</from>
<to>
/s/scripts/$1
</to>
</rule>
<rule>
<from>
/styles/**
</from>
<to>
/s/styles/$1
</to>
</rule>
<rule>
<from>
/templates/**
</from>
<to>
/s/templates/$1
</to>
</rule>
<rule
match-type=
"regex"
>
<from>
^\/(index\..*)?
</from>
<to>
/app/
</to>
</rule>
<rule>
<from>
/**
</from>
<to>
/app/$1
</to>
</rule>
<outbound-rule>
<from>
/app/**
</from>
<to>
/$1
</to>
</outbound-rule>
</urlrewrite>
apollo-demo/src/main/webapp/WEB-INF/views/welcome.jsp
0 → 100644
浏览文件 @
fa1c4fe3
<html>
<body>
<h2>
Hello World!
</h2>
</body>
</html>
apollo-demo/src/main/webapp/WEB-INF/web.xml
0 → 100644
浏览文件 @
fa1c4fe3
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>
Apollo demo
</display-name>
<!-- Enables clean URLs with JSP views e.g. /welcome instead of /app/welcome -->
<filter>
<filter-name>
UrlRewriteFilter
</filter-name>
<filter-class>
org.tuckey.web.filters.urlrewrite.UrlRewriteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>
UrlRewriteFilter
</filter-name>
<url-pattern>
/*
</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>
Spring MVC Dispatcher Servlet
</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>
contextClass
</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</init-param>
<init-param>
<param-name>
contextConfigLocation
</param-name>
<param-value>
com.ctrip.apollo.demo.AppConfig
</param-value>
</init-param>
<load-on-startup>
1
</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>
Spring MVC Dispatcher Servlet
</servlet-name>
<url-pattern>
/app/*
</url-pattern>
</servlet-mapping>
</web-app>
pom.xml
浏览文件 @
fa1c4fe3
...
...
@@ -70,6 +70,7 @@
<module>
apollo-configserver
</module>
<module>
apollo-portal
</module>
<module>
apollo-assembly
</module>
<module>
apollo-demo
</module>
</modules>
<dependencyManagement>
<dependencies>
...
...
@@ -97,9 +98,9 @@
</dependency>
<!-- declare Spring BOMs in order -->
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-parent
</artifactId>
<version>
1.3
.3.RELEASE
</version>
<groupId>
io.spring.platform
</groupId>
<artifactId>
platform-bom
</artifactId>
<version>
2.0
.3.RELEASE
</version>
<type>
pom
</type>
<scope>
import
</scope>
</dependency>
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录