mirror of
https://gitee.com/nutz/nutzboot.git
synced 2024-11-30 02:38:28 +08:00
Merge pull request #105 from nutzam/add_apollo_client
add: 添加apollo配置中心的对接
This commit is contained in:
commit
9644029760
@ -4,6 +4,7 @@
|
||||
|
||||
* 变更:
|
||||
* add: starter-tio by [蛋蛋](https://github.com/TopCoderMyDream)
|
||||
* add: starter-apollo-client 对接apollo配置中心. apollo是携程框架部门研发的分布式配置中心
|
||||
* update: 更新到HikariCP 2.7.5
|
||||
|
||||
## 2.1.0 "Start Of Something New"
|
||||
|
@ -143,6 +143,12 @@ public class MainLauncher {
|
||||
- [x] [hystrix](https://github.com/Netflix/Hystrix) 熔断器,集成在feign中
|
||||
- [x] starter-[eureka-server](https://github.com/Netflix/eureka) 服务治理的服务器端
|
||||
- [x] starter-[eureka-client](https://github.com/Netflix/eureka) 服务治理的客户端
|
||||
- 配置中心
|
||||
- [ ] NB Config Server/Client
|
||||
- [x] starter-[apollo-client](https://github.com/ctripcorp/apollo) 携程框架部门研发的分布式配置中心
|
||||
- API网关
|
||||
- [ ] NB API网关
|
||||
- [ ] zuul
|
||||
- 数据库类相关
|
||||
- 关系型数据库
|
||||
- 数据源
|
||||
|
@ -0,0 +1,61 @@
|
||||
<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>
|
||||
<parent>
|
||||
<groupId>org.nutz</groupId>
|
||||
<artifactId>nutzboot-demo-simple</artifactId>
|
||||
<version>2.2-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>nutzboot-demo-simple-apollo-client</artifactId>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.nutz</groupId>
|
||||
<artifactId>nutzboot-starter-nutz-mvc</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.nutz</groupId>
|
||||
<artifactId>nutzboot-starter-apollo-client</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.nutz</groupId>
|
||||
<artifactId>nutzboot-starter-jetty</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-log4j12</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.ctrip.framework.apollo</groupId>
|
||||
<artifactId>apollo-client-pure</artifactId>
|
||||
<version>0.9.1</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-shade-plugin</artifactId>
|
||||
<version>3.0.0</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>shade</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<transformers>
|
||||
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
|
||||
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
|
||||
<resource>META-INF/nutz/org.nutz.boot.starter.NbStarter</resource>
|
||||
</transformer>
|
||||
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
|
||||
<mainClass>io.nutz.demo.simple.MainLauncher</mainClass>
|
||||
</transformer>
|
||||
</transformers>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
@ -0,0 +1,39 @@
|
||||
package io.nutz.demo.simple;
|
||||
|
||||
import org.nutz.boot.NbApp;
|
||||
import org.nutz.ioc.impl.PropertiesProxy;
|
||||
import org.nutz.ioc.loader.annotation.Inject;
|
||||
import org.nutz.ioc.loader.annotation.IocBean;
|
||||
import org.nutz.mvc.annotation.At;
|
||||
import org.nutz.mvc.annotation.Ok;
|
||||
|
||||
/**
|
||||
* 注意, 本demo需要配合 apollo 配置中心的服务端使用
|
||||
* @author wendal
|
||||
*
|
||||
*/
|
||||
@IocBean
|
||||
public class MainLauncher {
|
||||
/*
|
||||
本demo的一些说明:
|
||||
1. 请先查阅apollo官网的文档,把服务端跑起来,并为SimpleApp项目添加配置 server.port=9080
|
||||
2. 本项目依赖的是apollo-client-pure,里面没有带apollo-env.properties,主要是演示用途,当然,你需要的使用的话我也不会介意...
|
||||
3. 生产项目应安装apollo的规范,自行编译apollo-core/apollo-client到私库,并依赖apollo-client
|
||||
4. main方法中的`System.setProperty("env", "dev")`是为了演示方便,生产环境不应该这样用的.
|
||||
5. 实现apollo的ConfigChangeListener可实现配置更新的通知,本demo尚未展示.
|
||||
*/
|
||||
@Inject
|
||||
protected PropertiesProxy conf;
|
||||
|
||||
@Ok("json:full")
|
||||
@At("/config/getall")
|
||||
public PropertiesProxy getAll() {
|
||||
return conf;
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
System.setProperty("env", "dev");
|
||||
new NbApp().setPrintProcDoc(true).run();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1 @@
|
||||
app.id=SampleApp
|
@ -0,0 +1,6 @@
|
||||
local.meta=http://127.0.0.1:8080
|
||||
dev.meta=http://127.0.0.1:8080
|
||||
fat.meta=http://127.0.0.1:8080
|
||||
uat.meta=http://127.0.0.1:8080
|
||||
lpt.meta=http://127.0.0.1:8080
|
||||
pro.meta=http://127.0.0.1:8080
|
@ -0,0 +1,7 @@
|
||||
log4j.rootLogger=debug,Console
|
||||
|
||||
log4j.logger.org.eclipse.jetty=info
|
||||
|
||||
log4j.appender.Console=org.apache.log4j.ConsoleAppender
|
||||
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
|
||||
log4j.appender.Console.layout.ConversionPattern=[%-5p] %d{HH:mm:ss.SSS} %l - %m%n
|
@ -0,0 +1,10 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Hello, So NB!</title>
|
||||
</head>
|
||||
<body>
|
||||
Hello, So NB!
|
||||
</body>
|
||||
</html>
|
24
nutzboot-starter/nutzboot-starter-apollo-client/pom.xml
Normal file
24
nutzboot-starter/nutzboot-starter-apollo-client/pom.xml
Normal file
@ -0,0 +1,24 @@
|
||||
<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>
|
||||
<parent>
|
||||
<groupId>org.nutz</groupId>
|
||||
<artifactId>nutzboot-starter</artifactId>
|
||||
<version>2.2-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>nutzboot-starter-apollo-client</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<name>nutzboot-starter-apollo-client</name>
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
<description>NutzBoot, micoservice base on Nutz</description>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.ctrip.framework.apollo</groupId>
|
||||
<artifactId>apollo-client-pure</artifactId>
|
||||
<version>0.9.1</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
@ -0,0 +1,23 @@
|
||||
package org.nutz.boot.starter.zkclient;
|
||||
|
||||
import org.nutz.boot.AppContext;
|
||||
import org.nutz.boot.starter.ServerFace;
|
||||
import org.nutz.ioc.loader.annotation.Inject;
|
||||
import org.nutz.ioc.loader.annotation.IocBean;
|
||||
|
||||
import com.ctrip.framework.apollo.Config;
|
||||
import com.ctrip.framework.apollo.ConfigChangeListener;
|
||||
import com.ctrip.framework.apollo.ConfigService;
|
||||
|
||||
@IocBean
|
||||
public class ApolloConfigureChangeStarter implements ServerFace {
|
||||
|
||||
@Inject
|
||||
protected AppContext appContext;
|
||||
|
||||
public void start() throws Exception {
|
||||
Config config = ConfigService.getAppConfig();
|
||||
appContext.getBeans(ConfigChangeListener.class).forEach((listener)->config.addChangeListener(listener));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,67 @@
|
||||
package org.nutz.boot.starter.zkclient;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.nutz.boot.config.impl.AbstractConfigureLoader;
|
||||
import org.nutz.ioc.impl.PropertiesProxy;
|
||||
import org.nutz.lang.Lang;
|
||||
|
||||
import com.ctrip.framework.apollo.Config;
|
||||
import com.ctrip.framework.apollo.ConfigService;
|
||||
|
||||
public class ApolloConfigureLoader extends AbstractConfigureLoader {
|
||||
|
||||
public void init() throws Exception {
|
||||
Config config = ConfigService.getAppConfig();
|
||||
conf = new PropertiesProxy() {
|
||||
public String get(String key) {
|
||||
return maps.getOrDefault(key, config.getProperty(key, null));
|
||||
}
|
||||
|
||||
public List<String> keys() {
|
||||
ArrayList<String> _keys = new ArrayList<>();
|
||||
_keys.addAll(maps.keySet());
|
||||
_keys.addAll(config.getPropertyNames());
|
||||
return _keys;
|
||||
}
|
||||
|
||||
public Collection<String> values() {
|
||||
ArrayList<String> values = new ArrayList<>();
|
||||
keys().forEach((key) -> values.add(get(key)));
|
||||
return values;
|
||||
}
|
||||
|
||||
public List<String> getKeys() {
|
||||
return keys();
|
||||
}
|
||||
|
||||
public boolean containsKey(Object key) {
|
||||
return maps.containsKey(key) || config.getPropertyNames().contains(key);
|
||||
}
|
||||
|
||||
public boolean containsValue(Object value) {
|
||||
throw Lang.noImplement();
|
||||
}
|
||||
|
||||
public Map<String, String> toMap() {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
keys().forEach((key) -> map.put(key, get(key)));
|
||||
return map;
|
||||
}
|
||||
|
||||
public Set<Entry<String, String>> entrySet() {
|
||||
return toMap().entrySet();
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return keys().size() == 0;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1 @@
|
||||
org.nutz.boot.starter.zkclient.ApolloConfigureLoader
|
@ -0,0 +1 @@
|
||||
org.nutz.boot.starter.zkclient.ApolloConfigureChangeStarter
|
@ -52,7 +52,8 @@
|
||||
<module>nutzboot-starter-eureka-server</module>
|
||||
<module>nutzboot-starter-rabbitmq</module>
|
||||
<module>nutzboot-starter-tio</module>
|
||||
</modules>
|
||||
<module>nutzboot-starter-apollo-client</module>
|
||||
</modules>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.nutz</groupId>
|
||||
|
5
pom.xml
5
pom.xml
@ -644,6 +644,11 @@
|
||||
<artifactId>nutzboot-starter-activiti</artifactId>
|
||||
<version>${nutzboot.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.nutz</groupId>
|
||||
<artifactId>nutzboot-starter-apollo-client</artifactId>
|
||||
<version>${nutzboot.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javassist</groupId>
|
||||
<artifactId>javassist</artifactId>
|
||||
|
Loading…
Reference in New Issue
Block a user