├── .gitignore
├── .travis.yml
├── README.md
├── disconf-spring-boot-demo
├── .gitignore
├── README.md
├── disconf-spring-boot-core
│ ├── .gitignore
│ ├── pom.xml
│ └── src
│ │ └── main
│ │ └── java
│ │ └── com
│ │ └── example
│ │ └── disconf
│ │ ├── callbacks
│ │ └── SimpleDemoServiceUpdateCallback.java
│ │ ├── config
│ │ ├── AutoService.java
│ │ └── SimpleConfig.java
│ │ ├── service
│ │ └── SimpleDemoService.java
│ │ └── task
│ │ └── DisconfDemoTask.java
├── disconf-spring-boot-web
│ ├── .gitignore
│ ├── pom.xml
│ └── src
│ │ └── main
│ │ ├── java
│ │ ├── AppContextMain
│ │ ├── Application.java
│ │ └── com
│ │ │ └── example
│ │ │ └── disconf
│ │ │ ├── controller
│ │ │ └── HelloController.java
│ │ │ └── init
│ │ │ └── InstantiationTracingBeanPostProcessor.java
│ │ └── resources
│ │ ├── application.properties
│ │ ├── autoconfig.properties
│ │ ├── disconf.properties
│ │ ├── disconf.xml
│ │ ├── logback.xml
│ │ └── simple.properties
└── pom.xml
├── disconf-standalone-demo
├── .gitignore
├── README.md
├── conffiles
│ ├── autoconfig.properties
│ ├── autoconfig2.properties
│ ├── code.properties
│ ├── coefficients.properties
│ ├── empty.properties
│ ├── myserver.properties
│ ├── myserver_slave.properties
│ ├── redis.properties
│ ├── remote.properties
│ ├── static.properties
│ ├── testJson.json
│ ├── testXml.xml
│ └── testXml2.xml
├── pom.xml
├── profile
│ └── rd
│ │ ├── disconf.properties
│ │ ├── env
│ │ └── logback.xml
└── src
│ └── main
│ ├── java
│ └── com
│ │ └── example
│ │ └── disconf
│ │ └── demo
│ │ ├── DisconfDemoMain.java
│ │ ├── config
│ │ ├── AutoConfig.java
│ │ ├── CodeConfig.java
│ │ ├── Coefficients.java
│ │ ├── EmptyConf.java
│ │ ├── JedisConfig.java
│ │ ├── RemoteServerConfig.java
│ │ ├── StaticConfig.java
│ │ └── TestXmlConfig.java
│ │ ├── service
│ │ ├── AutoService.java
│ │ ├── AutoService2.java
│ │ ├── BaoBaoService.java
│ │ ├── RemoteService.java
│ │ ├── SimpleRedisService.java
│ │ ├── SimpleStaticService.java
│ │ └── callbacks
│ │ │ ├── AutoServiceCallback.java
│ │ │ ├── RemoteServiceUpdateCallback.java
│ │ │ ├── SimpleRedisServiceUpdateCallback.java
│ │ │ ├── TestJsonConfigCallback.java
│ │ │ ├── TestXmlConfigCallback.java
│ │ │ └── UpdatePipelineCallback.java
│ │ ├── task
│ │ └── DisconfDemoTask.java
│ │ └── utils
│ │ └── JedisUtil.java
│ └── resources
│ └── applicationContext.xml
├── disconf-standalone-dubbo-demo
├── .gitignore
├── README.md
├── pom.xml
├── profile
│ └── rd
│ │ ├── disconf.properties
│ │ ├── env
│ │ └── logback.xml
└── src
│ └── main
│ ├── java
│ └── com
│ │ └── example
│ │ └── disconf
│ │ └── demo
│ │ ├── DisconfDemoMain.java
│ │ ├── config
│ │ ├── Coefficients.java
│ │ ├── EmptyConf.java
│ │ ├── JedisConfig.java
│ │ ├── RemoteServerConfig.java
│ │ └── StaticConfig.java
│ │ ├── dubbo
│ │ ├── consumer
│ │ │ └── Consumer.java
│ │ └── service
│ │ │ ├── DubboService.java
│ │ │ └── impl
│ │ │ └── DubboServiceImpl.java
│ │ ├── service
│ │ ├── BaoBaoService.java
│ │ ├── RemoteService.java
│ │ ├── RemoteServiceUpdateCallback.java
│ │ ├── SimpleRedisService.java
│ │ ├── SimpleRedisServiceUpdateCallback.java
│ │ └── SimpleStaticService.java
│ │ ├── task
│ │ └── DisconfDemoTask.java
│ │ └── utils
│ │ └── JedisUtil.java
│ └── resources
│ ├── applicationContext.xml
│ ├── dubbo-consumer.xml
│ └── dubbo-provider.xml
└── pom.xml
/.gitignore:
--------------------------------------------------------------------------------
1 | /.project
2 | /.settings
3 | /target
4 | /pom.xml.releaseBackup
5 | /*.iml
6 | /.idea
7 | /.DS_Store
8 | .idea
9 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: java
2 |
3 | after_success:
4 | - mvn clean cobertura:cobertura coveralls:report
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | disconf-demos-java [](https://travis-ci.org/knightliao/disconf-demos-java) [](https://coveralls.io/github/knightliao/disconf-demos-java?branch=master)
2 | =======
3 |
4 | several demos for disconf java client.
5 |
6 | ### demos
7 |
8 | - [disconf-standalone-demo](https://github.com/knightliao/disconf-demos-java/tree/master/disconf-standalone-demo): 使用disconf的基于Spring的standalone demo程序
9 | - [disconf-standalone-dubbo-demo](https://github.com/knightliao/disconf-demos-java/tree/master/disconf-standalone-dubbo-demo): 集成了disconf和dubbo的基于Spring的standalone demo程序
10 | - [disconf-spring-boot-demo](https://github.com/knightliao/disconf-demos-java/tree/master/disconf-spring-boot-demo): 使用disconf的spring-boot demo程序,更少的配置
11 |
12 | ### disconf home page
13 |
14 | - src: https://github.com/knightliao/disconf
15 | - doc: http://disconf.readthedocs.io/
--------------------------------------------------------------------------------
/disconf-spring-boot-demo/.gitignore:
--------------------------------------------------------------------------------
1 | /.settings
2 | /target
3 | /.classpath
4 | /.project
5 | /.DS_Store
6 | /disconf
7 | /log
8 | /bin
9 | /*.iml
10 | .idea
11 |
--------------------------------------------------------------------------------
/disconf-spring-boot-demo/README.md:
--------------------------------------------------------------------------------
1 | disconf-spring-boot-demo
2 | =======
3 |
4 | 基于微服务架构的流行,disconf作为微服务的标配,目标 是实现无配置化的jar启动微服务项目。
5 |
6 | 本demo 使用disconf的spring-boot demo程序,更少的配置.
7 |
8 | ## Quick start
9 |
10 | ### Main
11 |
12 | 项目启动运行Application main方法
13 |
14 | @Configuration
15 | @EnableAutoConfiguration
16 | @ComponentScan(basePackages = {"com.baidu","com.example"})
17 | @PropertySource({"classpath:application.properties"})
18 | @ImportResource({"classpath:disconf.xml"})//引入disconf
19 | public class Application extends SpringBootServletInitializer {
20 |
21 | public static void main(String[] args) {
22 | SpringApplication.run(Application.class, args);
23 | }
24 |
25 | @Override
26 | protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
27 | return application.sources(Application.class);
28 | }
29 | }
30 |
31 | ### xml配置
32 |
33 | disconf.xml
34 |
35 |
36 |
38 |
39 |
40 |
42 |
43 |
44 |
45 |
47 |
48 |
49 | classpath*:autoconfig.properties
50 |
51 |
52 |
53 |
54 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 | ### 使用
71 |
72 | #### 注解式 分布式配置
73 |
74 | @Service
75 | @Scope("singleton")
76 | @DisconfFile(filename = "simple.properties")
77 | public class SimpleConfig {
78 | // 代表连接地址
79 | private String host;
80 |
81 | // 代表连接port
82 | private int port;
83 |
84 | /**
85 | * 地址
86 | *
87 | * @return
88 | */
89 | @DisconfFileItem(name = "host")
90 | public String getHost() {
91 | return host;
92 | }
93 |
94 | public void setHost(String host) {
95 | this.host = host;
96 | }
97 |
98 | /**
99 | * 端口
100 | *
101 | * @return
102 | */
103 | @DisconfFileItem(name = "port")
104 | public int getPort() {
105 | return port;
106 | }
107 |
108 | public void setPort(int port) {
109 | this.port = port;
110 | }
111 | }
112 |
113 | #### 回调函数
114 |
115 | /**
116 | * 更新配置时的回调函数
117 | */
118 | @Service
119 | @DisconfUpdateService(classes = { SimpleConfig.class})
120 | public class SimpleDemoServiceUpdateCallback implements IDisconfUpdate {
121 |
122 | protected static final Logger LOGGER = LoggerFactory
123 | .getLogger(SimpleDemoServiceUpdateCallback.class);
124 |
125 | @Autowired
126 | private SimpleDemoService simpleDemoService;
127 |
128 | public void reload() throws Exception {
129 | simpleDemoService.changeConfig();
130 | }
131 |
132 | }
133 |
134 | #### XML 分布式配置
135 |
136 | classpath*:autoconfig.properties
137 |
138 | ## Run 打包、部署、运行
139 |
140 | ### war 包式
141 |
142 | 只要修改下pom.xml,使用war进行打包,然后放到tomcat下运行即可。
143 |
144 | ### Jar 包式
145 |
146 | 微服务的一个重要特征就是不需要tomcat了,一个jar就可以运行。这可能是未来的趋势。因此demo演示了 Jar包运行方式。
147 |
148 | #### 打包
149 |
150 | mvn package
151 |
152 | 结果是
153 |
154 | ➜ disconf-spring-boot-web git:(master) ✗ ls target
155 | classes disconf-spring-boot-web-1.0.0.jar.original maven-archiver
156 | disconf-spring-boot-web-1.0.0.jar generated-sources maven-status
157 | ➜ disconf-spring-boot-web git:(master) ✗
158 |
159 | #### 运行
160 |
161 | cd target
162 | java -jar disconf-spring-boot-web-1.0.0.jar
163 |
164 | 结果
165 |
166 | ➜ target git:(master) ✗ ls
167 | autoconfig.properties disconf log simple.properties.lock
168 | autoconfig.properties.lock disconf-spring-boot-web-1.0.0.jar maven-archiver
169 | classes disconf-spring-boot-web-1.0.0.jar.original maven-status
170 | config generated-sources simple.properties
171 | ➜ target git:(master) ✗
172 |
173 | spring-boot 采用jar包的运行方式,classpath是jar包内。因此,disconf如果发现是jar包启动方式,便会把当前执行路径当成classpath来读写配置文件。
174 | 所以配置文件均会被下载到执行路径。
--------------------------------------------------------------------------------
/disconf-spring-boot-demo/disconf-spring-boot-core/.gitignore:
--------------------------------------------------------------------------------
1 | /.settings
2 | /target
3 | /.classpath
4 | /.project
5 | /.DS_Store
6 | /disconf
7 | /log
8 | /bin
9 | /*.iml
10 | .idea
11 |
--------------------------------------------------------------------------------
/disconf-spring-boot-demo/disconf-spring-boot-core/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
7 | disconf-spring-boot-demo
8 | com.baidu.disconf
9 | 1.0.0
10 |
11 | 4.0.0
12 |
13 | disconf-spring-boot-core
14 |
15 |
16 |
17 |
18 | com.baidu.disconf
19 | disconf-client
20 | 2.6.36
21 |
22 |
23 |
24 | org.springframework.boot
25 | spring-boot-starter
26 |
27 |
28 |
29 | org.springframework.boot
30 | spring-boot-starter-aop
31 |
32 |
33 |
34 | redis.clients
35 | jedis
36 | 2.7.3
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 | org.apache.maven.plugins
45 | maven-source-plugin
46 |
47 |
48 | attach-sources
49 |
50 | jar
51 |
52 |
53 |
54 |
55 |
56 |
57 |
--------------------------------------------------------------------------------
/disconf-spring-boot-demo/disconf-spring-boot-core/src/main/java/com/example/disconf/callbacks/SimpleDemoServiceUpdateCallback.java:
--------------------------------------------------------------------------------
1 | package com.example.disconf.callbacks;
2 |
3 | import com.example.disconf.config.SimpleConfig;
4 | import com.example.disconf.service.SimpleDemoService;
5 | import org.slf4j.Logger;
6 | import org.slf4j.LoggerFactory;
7 | import org.springframework.beans.factory.annotation.Autowired;
8 | import org.springframework.stereotype.Service;
9 |
10 | import com.baidu.disconf.client.common.annotations.DisconfUpdateService;
11 | import com.baidu.disconf.client.common.update.IDisconfUpdate;
12 |
13 |
14 | /**
15 | * 更新配置时的回调函数
16 | */
17 | @Service
18 | @DisconfUpdateService(classes = { SimpleConfig.class})
19 | public class SimpleDemoServiceUpdateCallback implements IDisconfUpdate {
20 |
21 | protected static final Logger LOGGER = LoggerFactory
22 | .getLogger(SimpleDemoServiceUpdateCallback.class);
23 |
24 | @Autowired
25 | private SimpleDemoService simpleDemoService;
26 |
27 | public void reload() throws Exception {
28 | simpleDemoService.changeConfig();
29 | }
30 |
31 | }
--------------------------------------------------------------------------------
/disconf-spring-boot-demo/disconf-spring-boot-core/src/main/java/com/example/disconf/config/AutoService.java:
--------------------------------------------------------------------------------
1 | package com.example.disconf.config;
2 |
3 | /**
4 | * Created by knightliao on 15/3/19.
5 | */
6 | public class AutoService {
7 |
8 | private String auto;
9 |
10 | public String getAuto() {
11 | return auto;
12 | }
13 |
14 | public void setAuto(String auto) {
15 | this.auto = auto;
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/disconf-spring-boot-demo/disconf-spring-boot-core/src/main/java/com/example/disconf/config/SimpleConfig.java:
--------------------------------------------------------------------------------
1 | package com.example.disconf.config;
2 |
3 | import com.baidu.disconf.client.common.annotations.DisconfFile;
4 | import com.baidu.disconf.client.common.annotations.DisconfFileItem;
5 | import org.springframework.context.annotation.Scope;
6 | import org.springframework.stereotype.Service;
7 |
8 | @Service
9 | @Scope("singleton")
10 | @DisconfFile(filename = "simple.properties")
11 | public class SimpleConfig {
12 | // 代表连接地址
13 | private String host;
14 |
15 | // 代表连接port
16 | private int port;
17 |
18 | /**
19 | * 地址
20 | *
21 | * @return
22 | */
23 | @DisconfFileItem(name = "host")
24 | public String getHost() {
25 | return host;
26 | }
27 |
28 | public void setHost(String host) {
29 | this.host = host;
30 | }
31 |
32 | /**
33 | * 端口
34 | *
35 | * @return
36 | */
37 | @DisconfFileItem(name = "port")
38 | public int getPort() {
39 | return port;
40 | }
41 |
42 | public void setPort(int port) {
43 | this.port = port;
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/disconf-spring-boot-demo/disconf-spring-boot-core/src/main/java/com/example/disconf/service/SimpleDemoService.java:
--------------------------------------------------------------------------------
1 | package com.example.disconf.service;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 | import org.springframework.beans.factory.DisposableBean;
6 | import org.springframework.beans.factory.InitializingBean;
7 | import org.springframework.beans.factory.annotation.Autowired;
8 | import org.springframework.context.annotation.Scope;
9 | import org.springframework.stereotype.Service;
10 |
11 | import com.example.disconf.config.SimpleConfig;
12 |
13 | @Service
14 | @Scope("singleton")
15 | public class SimpleDemoService implements InitializingBean, DisposableBean {
16 |
17 | protected static final Logger LOGGER = LoggerFactory
18 | .getLogger(SimpleDemoService.class);
19 |
20 | /**
21 | * 分布式配置
22 | */
23 | @Autowired
24 | private SimpleConfig simpleConfig;
25 |
26 | /**
27 | * 关闭
28 | */
29 | public void destroy() throws Exception {
30 |
31 | LOGGER.info("destroy ==> ");
32 | }
33 |
34 | /**
35 | * 进行连接
36 | */
37 | public void afterPropertiesSet() throws Exception {
38 |
39 | LOGGER.info("connect ==> ");
40 | }
41 |
42 | /**
43 | * 后台更改值
44 | */
45 | public void changeConfig() {
46 |
47 | LOGGER.info("start to change hosts to: {} : {}", simpleConfig.getHost(), simpleConfig.getPort());
48 |
49 | LOGGER.info("change ok.");
50 | }
51 | }
--------------------------------------------------------------------------------
/disconf-spring-boot-demo/disconf-spring-boot-core/src/main/java/com/example/disconf/task/DisconfDemoTask.java:
--------------------------------------------------------------------------------
1 | package com.example.disconf.task;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 |
6 | import com.example.disconf.config.SimpleConfig;
7 |
8 | /**
9 | * 定时输出配置修改
10 | */
11 | public class DisconfDemoTask implements Runnable {
12 |
13 | protected static final Logger LOGGER = LoggerFactory
14 | .getLogger(DisconfDemoTask.class);
15 |
16 | private SimpleConfig simpleConfig;
17 |
18 | public DisconfDemoTask(SimpleConfig simpleConfig) {
19 | this.simpleConfig = simpleConfig;
20 | }
21 |
22 | public void run() {
23 | try {
24 |
25 | while (true) {
26 | Thread.sleep(5000);
27 | LOGGER.info("simple config ==> host:{}, port:{} ", simpleConfig.getHost(), simpleConfig.getPort());
28 | }
29 |
30 | } catch (Exception e) {
31 | LOGGER.error(e.toString(), e);
32 | }
33 | }
34 | }
--------------------------------------------------------------------------------
/disconf-spring-boot-demo/disconf-spring-boot-web/.gitignore:
--------------------------------------------------------------------------------
1 | /.settings
2 | /target
3 | /.classpath
4 | /.project
5 | /.DS_Store
6 | /disconf
7 | /log
8 | /bin
9 | /*.iml
10 | .idea
11 | dependency-reduced-pom.xml
--------------------------------------------------------------------------------
/disconf-spring-boot-demo/disconf-spring-boot-web/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
7 | disconf-spring-boot-demo
8 | com.baidu.disconf
9 | 1.0.0
10 |
11 |
12 | 4.0.0
13 | jar
14 | disconf-spring-boot-web
15 |
16 |
17 |
18 | com.baidu.disconf
19 | disconf-spring-boot-core
20 | ${parent.version}
21 |
22 |
23 | org.springframework.boot
24 | spring-boot-starter-web
25 |
26 |
27 |
28 | org.springframework.boot
29 | spring-boot-starter-test
30 | test
31 |
32 |
33 |
34 |
35 |
36 |
37 | org.springframework.boot
38 | spring-boot-maven-plugin
39 |
40 |
41 | org.springframework
42 | springloaded
43 | 1.2.4.RELEASE
44 |
45 |
46 |
47 |
48 | org.apache.maven.plugins
49 | maven-deploy-plugin
50 |
51 | true
52 |
53 |
54 |
55 | org.apache.maven.plugins
56 | maven-war-plugin
57 |
58 | false
59 |
60 |
61 |
62 |
63 |
64 |
65 |
--------------------------------------------------------------------------------
/disconf-spring-boot-demo/disconf-spring-boot-web/src/main/java/AppContextMain:
--------------------------------------------------------------------------------
1 |
2 | import org.springframework.context.ApplicationContext;
3 | import org.springframework.context.support.ClassPathXmlApplicationContext;
4 | import com.example.disconf.config.SimpleConfig;
5 | /**
6 | * Created by Colonel.Hou on 2016/8/26.
7 | * Main函数运行获取bean值
8 | */
9 | public class AppContextMain {
10 | public static void main(String[] args) {
11 | ApplicationContext factory = new ClassPathXmlApplicationContext("classpath:disconf.xml");
12 | SimpleConfig redis = (SimpleConfig )factory.getBean("simpleConfig");
13 | System.out.println(redis.getHost() + "\t================>\t" + redis.getPort());
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/disconf-spring-boot-demo/disconf-spring-boot-web/src/main/java/Application.java:
--------------------------------------------------------------------------------
1 | import org.springframework.boot.SpringApplication;
2 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
3 | import org.springframework.boot.builder.SpringApplicationBuilder;
4 | import org.springframework.boot.context.web.SpringBootServletInitializer;
5 | import org.springframework.context.annotation.ComponentScan;
6 | import org.springframework.context.annotation.Configuration;
7 | import org.springframework.context.annotation.ImportResource;
8 | import org.springframework.context.annotation.PropertySource;
9 |
10 | /**
11 | * Created by wenjl on 2015/8/5.
12 | */
13 | @Configuration
14 | @EnableAutoConfiguration
15 | @ComponentScan(basePackages = {"com.example"})
16 | @PropertySource({"classpath:application.properties"})
17 | @ImportResource({"classpath:disconf.xml"})//引入disconf
18 | public class Application extends SpringBootServletInitializer {
19 |
20 | public static void main(String[] args) {
21 | SpringApplication.run(Application.class, args);
22 | }
23 |
24 | @Override
25 | protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
26 |
27 | return application.sources(Application.class);
28 | }
29 |
30 | }
31 |
--------------------------------------------------------------------------------
/disconf-spring-boot-demo/disconf-spring-boot-web/src/main/java/com/example/disconf/controller/HelloController.java:
--------------------------------------------------------------------------------
1 | package com.example.disconf.controller;
2 |
3 | import org.springframework.stereotype.Controller;
4 | import org.springframework.web.bind.annotation.RequestMapping;
5 | import org.springframework.web.bind.annotation.ResponseBody;
6 |
7 | /**
8 | * Created by wenjl on 2015/8/5.
9 | */
10 | @Controller
11 | public class HelloController {
12 |
13 | @RequestMapping("/")
14 | @ResponseBody
15 | String home() {
16 | return "Hello World!";
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/disconf-spring-boot-demo/disconf-spring-boot-web/src/main/java/com/example/disconf/init/InstantiationTracingBeanPostProcessor.java:
--------------------------------------------------------------------------------
1 | package com.example.disconf.init;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 | import org.springframework.beans.factory.annotation.Autowired;
6 | import org.springframework.context.ApplicationListener;
7 | import org.springframework.context.annotation.Configuration;
8 | import org.springframework.context.event.ContextRefreshedEvent;
9 |
10 | import com.example.disconf.config.AutoService;
11 | import com.example.disconf.config.SimpleConfig;
12 | import com.example.disconf.task.DisconfDemoTask;
13 |
14 | /**
15 | * spring初始化结束后,执行
16 | *
17 | * @author wenjl
18 | */
19 | @Configuration
20 | public class InstantiationTracingBeanPostProcessor implements ApplicationListener {
21 | protected static final Logger LOGGER = LoggerFactory
22 | .getLogger(InstantiationTracingBeanPostProcessor.class);
23 |
24 | @Override
25 | public void onApplicationEvent(ContextRefreshedEvent event) {
26 | if (event.getApplicationContext().getParent() == null)//root applicationContext没有parent,保证是统一的context
27 | {
28 | LOGGER.info("init thread...");
29 | new Thread(new DisconfDemoTask(simpleConfig)).start();
30 |
31 | while (true) {
32 |
33 | LOGGER.info("auto service : {} ", autoService.getAuto());
34 |
35 | try {
36 | Thread.sleep(5000);
37 | } catch (InterruptedException e) {
38 | }
39 | }
40 |
41 | }
42 | }
43 |
44 | @Autowired
45 | private SimpleConfig simpleConfig;
46 |
47 | @Autowired
48 | private AutoService autoService;
49 | }
50 |
--------------------------------------------------------------------------------
/disconf-spring-boot-demo/disconf-spring-boot-web/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | spring.config.name=application
2 | spring.main.show-banner=false
3 | server.port=8081
4 | server.session-timeout=1800
5 | server.servlet-path=/
6 | server.tomcat.basedir=/tmp
7 | server.tomcat.uri-encoding = UTF-8
8 |
9 | http.client.socketTimeout=5
10 | http.client.connectionRequestTimeout=5
11 | http.client.connectionTimeout=5
12 | http.client.closeIdleConnections=30
13 |
14 | # MVC
15 | spring.view.prefix=/WEB-INF/views/
16 | spring.view.suffix=.jsp
17 |
18 | #jmx
19 | spring.jmx.enabled=false
20 |
21 | #multipart
22 | spring.multipart.enabled=true
23 | # File size limit
24 | multipart.maxFileSize = 20Mb
25 | # Total request size for a multipart/form-data
26 | multipart.maxRequestSize = 20Mb
27 |
--------------------------------------------------------------------------------
/disconf-spring-boot-demo/disconf-spring-boot-web/src/main/resources/autoconfig.properties:
--------------------------------------------------------------------------------
1 | auto=\u4F60\u597D\u54C7 bbd
2 |
3 | cc=03343444456
--------------------------------------------------------------------------------
/disconf-spring-boot-demo/disconf-spring-boot-web/src/main/resources/disconf.properties:
--------------------------------------------------------------------------------
1 | # \u662F\u5426\u4F7F\u7528\u8FDC\u7A0B\u914D\u7F6E\u6587\u4EF6
2 | # true(\u9ED8\u8BA4)\u4F1A\u4ECE\u8FDC\u7A0B\u83B7\u53D6\u914D\u7F6E false\u5219\u76F4\u63A5\u83B7\u53D6\u672C\u5730\u914D\u7F6E
3 | disconf.enable.remote.conf=true
4 |
5 | #
6 | # \u914D\u7F6E\u670D\u52A1\u5668\u7684 HOST,\u7528\u9017\u53F7\u5206\u9694 127.0.0.1:8000,127.0.0.1:8000
7 | #
8 | disconf.conf_server_host=127.0.0.1:8084
9 |
10 | # \u7248\u672C, \u8BF7\u91C7\u7528 X_X_X_X \u683C\u5F0F
11 | disconf.version=1_0_0_0
12 |
13 | # APP \u8BF7\u91C7\u7528 \u4EA7\u54C1\u7EBF_\u670D\u52A1\u540D \u683C\u5F0F
14 | disconf.app=disconf_demo
15 |
16 | # \u73AF\u5883
17 | disconf.env=rd
18 |
19 | # \u5FFD\u7565\u54EA\u4E9B\u5206\u5E03\u5F0F\u914D\u7F6E\uFF0C\u7528\u9017\u53F7\u5206\u9694
20 | disconf.ignore=
21 |
22 | # \u83B7\u53D6\u8FDC\u7A0B\u914D\u7F6E \u91CD\u8BD5\u6B21\u6570\uFF0C\u9ED8\u8BA4\u662F3\u6B21
23 | disconf.conf_server_url_retry_times=1
24 | # \u83B7\u53D6\u8FDC\u7A0B\u914D\u7F6E \u91CD\u8BD5\u65F6\u4F11\u7720\u65F6\u95F4\uFF0C\u9ED8\u8BA4\u662F5\u79D2
25 | disconf.conf_server_url_retry_sleep_seconds=1
26 |
27 | # \u81EA\u5B9A\u4E49\u7684\u4E0B\u8F7D\u8DEF\u5F84
28 | disconf.user_define_download_dir=./config
29 |
--------------------------------------------------------------------------------
/disconf-spring-boot-demo/disconf-spring-boot-web/src/main/resources/disconf.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
9 |
10 |
11 |
12 |
13 |
15 |
16 |
17 |
19 |
20 |
21 |
22 |
24 |
25 |
26 | classpath*:autoconfig.properties
27 |
28 |
29 |
30 |
31 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
--------------------------------------------------------------------------------
/disconf-spring-boot-demo/disconf-spring-boot-web/src/main/resources/logback.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 | %date [%thread] %-5level %logger{80} - %msg%n
12 |
13 |
14 |
15 |
16 |
18 | ${log.base}.log
19 |
21 | ${log.base}.%d{yyyy-MM-dd}.log
22 |
23 |
24 | %date [%thread] %-5level %logger{80} - %msg%n
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
--------------------------------------------------------------------------------
/disconf-spring-boot-demo/disconf-spring-boot-web/src/main/resources/simple.properties:
--------------------------------------------------------------------------------
1 | port=31
2 | host=127.0.0.1
--------------------------------------------------------------------------------
/disconf-spring-boot-demo/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | com.baidu.disconf
8 | disconf-spring-boot-demo
9 | pom
10 | 1.0.0
11 |
12 | disconf-spring-boot-core
13 | disconf-spring-boot-web
14 |
15 |
16 |
17 | UTF-8
18 |
19 |
20 |
21 | org.springframework.boot
22 | spring-boot-starter-parent
23 | 1.4.0.RELEASE
24 |
25 |
26 |
--------------------------------------------------------------------------------
/disconf-standalone-demo/.gitignore:
--------------------------------------------------------------------------------
1 | /target
2 | /.settings
3 | /bin
4 | /.classpath
5 | /.project
6 | /target
7 | /disconf
8 | /log
9 | /pom.xml.releaseBackup
10 | /*.iml
11 | /.DS_Store
12 | .idea
13 | dependency-reduced-pom.xml
14 |
--------------------------------------------------------------------------------
/disconf-standalone-demo/README.md:
--------------------------------------------------------------------------------
1 | disconf-standalone-demo
2 | =======
3 |
4 | 使用disconf的standalone demo程序(基于spring)
5 |
6 | ## Quick Start
7 |
8 | ### 基于注解式的分布式配置
9 |
10 | #### 第一步:撰写配置类
11 |
12 | 目录 com.example.disconf.demo.config 下
13 |
14 | - code.properties: 分布式配置文件
15 | - coefficients.properties: 分布式配置文件
16 | - redis.properties: 分布式配置文件
17 | - remote.properties: 分布式配置文件
18 | - empty.properties: 空的分布式配置文件
19 | - static.properties: 静态配置文件示例
20 | - testXml.xml: xml配置文件示例
21 |
22 | 以 `code.properties` 举例:
23 |
24 | @Service
25 | @DisconfFile(filename = "code.properties", copy2TargetDirPath = "disconf")
26 | public class CodeConfig {
27 |
28 | private String codeError = "";
29 |
30 | @DisconfFileItem(name = "syserror.paramtype", associateField = "codeError")
31 | public String getCodeError() {
32 | return codeError;
33 | }
34 |
35 | public void setCodeError(String codeError) {
36 | this.codeError = codeError;
37 | }
38 | }
39 |
40 | #### 第一步附: 配置项示例:
41 |
42 | com.example.disconf.demo.config.Coefficients.discount
43 |
44 | /**
45 | * 金融系数文件
46 | */
47 | @Service
48 | @DisconfFile(filename = "coefficients.properties")
49 | public class Coefficients {
50 |
51 | public static final String key = "discountRate";
52 |
53 | @Value(value = "2.0d")
54 | private Double discount;
55 | }
56 |
57 | #### 第二步:撰写回调类
58 |
59 | - com.example.disconf.demo.config.JedisConfig: 配置类与回调类 是同一个类的示例
60 | - com.example.disconf.demo.service.callbacks.*: 配置类与配置项 的回调类示例
61 | - RemoteServiceUpdateCallback:
62 | - SimpleRedisServiceUpdateCallback
63 | - TestXmlConfigCallback: XML的回调函数
64 |
65 | 示例:
66 |
67 | /**
68 | * 更新Redis配置时的回调函数
69 | *
70 | * @author liaoqiqi
71 | * @version 2014-6-17
72 | */
73 | @Service
74 | @Scope("singleton")
75 | @DisconfUpdateService(classes = {JedisConfig.class}, itemKeys = {Coefficients.key})
76 | public class SimpleRedisServiceUpdateCallback implements IDisconfUpdate {
77 |
78 | protected static final Logger LOGGER = LoggerFactory.getLogger(SimpleRedisServiceUpdateCallback.class);
79 |
80 | @Autowired
81 | private SimpleRedisService simpleRedisService;
82 |
83 | /**
84 | *
85 | */
86 | public void reload() throws Exception {
87 |
88 | simpleRedisService.changeJedis();
89 | }
90 |
91 | }
92 |
93 |
94 | #### 第三步:使用起来
95 |
96 | 看这里 com.example.disconf.demo.task.DisconfDemoTask
97 |
98 | - 支持spring注解式(bean)
99 | - 支持静态类(非bean)
100 |
101 | ### XML配置方式
102 |
103 | #### 第一步:增加XML配置
104 |
105 | 需要自动reload的
106 |
107 | - autoconfig.properties
108 | - autoconfig2.properties
109 | - myserver_slave.properties
110 | - testJson.json
111 | - testXml2.xml
112 |
113 | 不需要自动reload的:
114 |
115 | - myserver.properties
116 |
117 |
118 |
120 |
121 |
122 | classpath:/autoconfig.properties
123 | classpath:/autoconfig2.properties
124 | classpath:/myserver_slave.properties
125 | classpath:/testJson.json
126 | testXml2.xml
127 |
128 |
129 |
130 |
131 |
133 |
134 |
135 |
136 |
137 |
138 |
139 |
140 |
141 |
142 |
143 |
145 |
146 |
147 | myserver.properties
148 |
149 |
150 |
151 |
152 |
154 |
155 |
156 |
157 |
158 |
159 |
160 |
161 |
162 |
163 |
164 |
165 |
166 |
167 |
168 |
169 |
170 |
171 | #### 第二步:撰写相应的bean类
172 |
173 | - com.example.disconf.demo.service.AutoService
174 | - com.example.disconf.demo.service.AutoService2
175 |
176 | #### 第三步:回调类
177 |
178 | - com.example.disconf.demo.service.callbacks.*:
179 | - AutoServiceCallback:
180 | - TestJsonConfigCallback
181 |
182 | 示例:
183 |
184 | /**
185 | * 这是 autoconfig.properties 的回调函数类
186 | *
187 | * Created by knightliao on 15/3/21.
188 | */
189 | @Service
190 | @DisconfUpdateService(confFileKeys = {"autoconfig.properties", "autoconfig2.properties"})
191 | public class AutoServiceCallback implements IDisconfUpdate {
192 |
193 | protected static final Logger LOGGER = LoggerFactory.getLogger(AutoServiceCallback.class);
194 |
195 | @Autowired
196 | private AutoService autoService;
197 |
198 | @Override
199 | public void reload() throws Exception {
200 |
201 | LOGGER.info("reload callback " + "autoconfig.properties or autoconfig2.properties" + autoService.getAuto());
202 |
203 | }
204 | }
205 |
206 | ## Run 打包、部署、运行
207 |
208 | ### 打包
209 |
210 | mvn clean package
211 |
212 | ### 运行
213 |
214 | ➜ disconf-standalone-demo git:(dev) ✗ cd target/starter-run
215 | ➜ starter-run git:(dev) ✗ ll
216 | total 42680
217 | -rw-r--r-- 1 knightliao staff 21831639 6 8 14:36 disconf-standalone-demo.jar
218 | -rw-r--r-- 1 knightliao staff 1431 6 8 14:36 disconf.properties
219 | -rw-r--r-- 1 knightliao staff 417 6 8 14:36 env
220 | -rw-r--r-- 1 knightliao staff 1493 6 8 14:36 logback.xml
221 | -rw-r--r-- 1 knightliao staff 1037 6 8 14:36 start.sh
222 | -rw-r--r-- 1 knightliao staff 532 6 8 14:36 stop.sh
223 |
224 | ➜ starter-run git:(dev) ✗ sh start.sh
225 | nohup java -server -Xms1024m -Xmx1024m -Xmn448m -Xss256K -XX:MaxPermSize=128m -XX:ReservedCodeCacheSize=64m -XX:+UseParallelGC -XX:+UseParallelOldGC -XX:ParallelGCThreads=2 -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -Dlogback.configurationFile=file:logback.xml -jar disconf-standalone-demo.jar >> log_1465367799.log 2>&1 &
226 | ➜ starter-run git:(dev) ✗ tail -f log_1465367799.log
227 | disConfCommonModel=DisConfCommonModel [app=disconf_demo, version=1_0_0_0, env=rd]
228 | disconfCommonCallbackModel=DisconfCommonCallbackModel{disconfConfUpdates=[com.example.disconf.demo.service.callbacks.SimpleRedisServiceUpdateCallback@50b7ae59], disconfUpdatesActiveBackups=[]}]]
229 |
230 |
--------------------------------------------------------------------------------
/disconf-standalone-demo/conffiles/autoconfig.properties:
--------------------------------------------------------------------------------
1 | auto=bbd中国
2 |
--------------------------------------------------------------------------------
/disconf-standalone-demo/conffiles/autoconfig2.properties:
--------------------------------------------------------------------------------
1 | auto2=cc\u4E2D\u56FD
--------------------------------------------------------------------------------
/disconf-standalone-demo/conffiles/code.properties:
--------------------------------------------------------------------------------
1 | syserror.paramtype=\u8BF7\u6C42\u53C2\u6570\u89E3\u6790\u9519" + "\u8BEF
2 | proxy.routerRateEnable=false
--------------------------------------------------------------------------------
/disconf-standalone-demo/conffiles/coefficients.properties:
--------------------------------------------------------------------------------
1 | coe.baiFaCoe=1.3
2 | coe.yuErBaoCoe=1.3
3 |
--------------------------------------------------------------------------------
/disconf-standalone-demo/conffiles/empty.properties:
--------------------------------------------------------------------------------
1 | redis.host=127.0.0.1
2 | redis.port=8310
--------------------------------------------------------------------------------
/disconf-standalone-demo/conffiles/myserver.properties:
--------------------------------------------------------------------------------
1 | server=127.0.0.1:16600,127.0.0.1:16602,127.0.0.1:16603
2 | retry=5
3 |
--------------------------------------------------------------------------------
/disconf-standalone-demo/conffiles/myserver_slave.properties:
--------------------------------------------------------------------------------
1 | #online
2 | server=127.0.0.1:16700,127.0.0.1:16700,127.0.0.1:16700,127.0.0.1:16700
3 | retry=3
--------------------------------------------------------------------------------
/disconf-standalone-demo/conffiles/redis.properties:
--------------------------------------------------------------------------------
1 | redis.host=127.0.0.1
2 | redis.port=6379
--------------------------------------------------------------------------------
/disconf-standalone-demo/conffiles/remote.properties:
--------------------------------------------------------------------------------
1 | remoteHost=127.0.0.1
2 | remotePort=8081
--------------------------------------------------------------------------------
/disconf-standalone-demo/conffiles/static.properties:
--------------------------------------------------------------------------------
1 | staticVar=147
--------------------------------------------------------------------------------
/disconf-standalone-demo/conffiles/testJson.json:
--------------------------------------------------------------------------------
1 | {"message": {}, "success": "true"}
--------------------------------------------------------------------------------
/disconf-standalone-demo/conffiles/testXml.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
5 |
6 | /tradeMap
7 | /tradeMap
8 |
9 |
10 |
--------------------------------------------------------------------------------
/disconf-standalone-demo/conffiles/testXml2.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
5 |
6 | /tradeMap
7 | /tradeMap
8 |
9 |
10 |
--------------------------------------------------------------------------------
/disconf-standalone-demo/pom.xml:
--------------------------------------------------------------------------------
1 |
3 |
4 | 4.0.0
5 | com.baidu.disconf
6 | disconf-standalone-demo
7 | 2.0.0
8 |
9 |
10 |
11 |
12 | com.baidu.disconf
13 | disconf-client
14 | 2.6.36
15 |
16 |
17 |
18 | commons-io
19 | commons-io
20 | 2.7
21 |
22 |
23 |
24 | commons-lang
25 | commons-lang
26 | 2.4
27 |
28 |
29 |
30 | org.slf4j
31 | slf4j-api
32 | 1.7.25
33 |
34 |
35 |
36 | ch.qos.logback
37 | logback-classic
38 | 1.2.5
39 |
40 |
41 |
42 | junit
43 | junit
44 | 4.13.1
45 | test
46 |
47 |
48 |
49 | redis.clients
50 | jedis
51 | 2.1.0
52 |
53 |
54 |
55 | org.springframework
56 | spring-context
57 | 4.1.7.RELEASE
58 |
59 |
60 |
61 |
62 |
63 | package
64 | ${project.artifactId}
65 | target/classes
66 |
67 |
68 |
69 | ${project.basedir}/src/main/java
70 |
71 | **/*.java
72 |
73 |
74 |
75 | ${project.basedir}/src/main/resources
76 |
77 | *.*
78 |
79 |
80 |
81 |
82 |
83 |
84 | com.github.knightliao.plugin
85 | starter-shade-maven-plugin
86 | 1.0.0
87 |
88 |
89 | package
90 |
91 | shade
92 |
93 |
94 | ${project.build.finalName}
95 |
96 |
98 | com.example.disconf.demo.DisconfDemoMain
99 |
100 |
102 |
103 | env
104 | disconf.properties
105 | logback.xml
106 |
107 |
108 |
110 | META-INF/spring.handlers
111 |
112 |
114 | META-INF/spring.schemas
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 |
125 |
126 |
127 |
128 | rd
129 |
130 | true
131 |
132 |
133 |
134 |
135 |
136 | ${basedir}/profile/rd
137 |
138 |
139 |
140 |
141 |
142 |
143 |
--------------------------------------------------------------------------------
/disconf-standalone-demo/profile/rd/disconf.properties:
--------------------------------------------------------------------------------
1 |
2 |
3 | # \u662F\u5426\u4F7F\u7528\u8FDC\u7A0B\u914D\u7F6E\u6587\u4EF6
4 | # true(\u9ED8\u8BA4)\u4F1A\u4ECE\u8FDC\u7A0B\u83B7\u53D6\u914D\u7F6E false\u5219\u76F4\u63A5\u83B7\u53D6\u672C\u5730\u914D\u7F6E
5 | disconf.enable.remote.conf=true
6 |
7 | #
8 | # \u914D\u7F6E\u670D\u52A1\u5668\u7684 HOST,\u7528\u9017\u53F7\u5206\u9694 127.0.0.1:8004,127.0.0.1:8004
9 | #
10 | disconf.conf_server_host=127.0.0.1:8084
11 |
12 | # \u7248\u672C, \u8BF7\u91C7\u7528 X_X_X_X \u683C\u5F0F
13 | disconf.version=1_0_0_0
14 |
15 | # APP \u8BF7\u91C7\u7528 \u4EA7\u54C1\u7EBF_\u670D\u52A1\u540D \u683C\u5F0F
16 | disconf.app=disconf_demo
17 |
18 | # \u73AF\u5883
19 | disconf.env=rd
20 |
21 | # \u5FFD\u7565\u54EA\u4E9B\u5206\u5E03\u5F0F\u914D\u7F6E\uFF0C\u7528\u9017\u53F7\u5206\u9694
22 | disconf.ignore=
23 |
24 | # \u83B7\u53D6\u8FDC\u7A0B\u914D\u7F6E \u91CD\u8BD5\u6B21\u6570\uFF0C\u9ED8\u8BA4\u662F3\u6B21
25 | disconf.conf_server_url_retry_times=1
26 | # \u83B7\u53D6\u8FDC\u7A0B\u914D\u7F6E \u91CD\u8BD5\u65F6\u4F11\u7720\u65F6\u95F4\uFF0C\u9ED8\u8BA4\u662F5\u79D2
27 | disconf.conf_server_url_retry_sleep_seconds=1
28 |
29 | # \u7528\u6237\u6307\u5B9A\u7684\u4E0B\u8F7D\u6587\u4EF6\u5939, \u8FDC\u7A0B\u6587\u4EF6\u4E0B\u8F7D\u540E\u4F1A\u653E\u5728\u8FD9\u91CC
30 | disconf.user_define_download_dir=./disconf/download2
31 |
32 | # \u4E0B\u8F7D\u7684\u6587\u4EF6\u4F1A\u88AB\u8FC1\u79FB\u5230classpath\u6839\u8DEF\u5F84\u4E0B\uFF0C\u5F3A\u70C8\u5EFA\u8BAE\u5C06\u6B64\u9009\u9879\u7F6E\u4E3A true(\u9ED8\u8BA4\u662Ftrue)
33 | disconf.enable_local_download_dir_in_class_path=true
--------------------------------------------------------------------------------
/disconf-standalone-demo/profile/rd/env:
--------------------------------------------------------------------------------
1 |
2 | BUNDLE_JAR_NAME=disconf-standalone-demo.jar
3 |
4 | export JAVA_OPTS="$JAVA_OPTS -server -Xms1024m -Xmx1024m -Xmn448m -Xss256K -XX:MaxPermSize=128m -XX:ReservedCodeCacheSize=64m"
5 | export JAVA_OPTS="$JAVA_OPTS -XX:+UseParallelGC -XX:+UseParallelOldGC -XX:ParallelGCThreads=2"
6 | export JAVA_OPTS="$JAVA_OPTS -XX:+PrintGCDetails -XX:+PrintGCTimeStamps"
7 | export JAVA_OPTS="$JAVA_OPTS -Dlogback.configurationFile=file:logback.xml"
8 |
9 |
--------------------------------------------------------------------------------
/disconf-standalone-demo/profile/rd/logback.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 | %date [%thread] %-5level %logger{80} - %msg%n
12 |
13 |
14 |
15 |
16 |
18 | ${log.base}.log
19 |
21 | ${log.base}.%d{yyyy-MM-dd}.log
22 |
23 |
24 |
25 | %date [%thread] %-5level %logger{80} - %msg%n
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
--------------------------------------------------------------------------------
/disconf-standalone-demo/src/main/java/com/example/disconf/demo/DisconfDemoMain.java:
--------------------------------------------------------------------------------
1 | package com.example.disconf.demo;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 | import org.springframework.context.support.ClassPathXmlApplicationContext;
6 |
7 | import com.example.disconf.demo.task.DisconfDemoTask;
8 |
9 | /**
10 | * @author liaoqiqi
11 | * @version 2014-6-17
12 | */
13 | public class DisconfDemoMain {
14 |
15 | protected static final Logger LOGGER = LoggerFactory.getLogger(DisconfDemoMain.class);
16 |
17 | private static String[] fn = null;
18 |
19 | // 初始化spring文档
20 | private static void contextInitialized() {
21 | fn = new String[] {"applicationContext.xml"};
22 | }
23 |
24 | /**
25 | * @param args
26 | *
27 | * @throws Exception
28 | */
29 | public static void main(String[] args) throws Exception {
30 |
31 | contextInitialized();
32 | ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(fn);
33 |
34 | DisconfDemoTask task = ctx.getBean("disconfDemoTask", DisconfDemoTask.class);
35 |
36 | int ret = task.run();
37 |
38 | System.exit(ret);
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/disconf-standalone-demo/src/main/java/com/example/disconf/demo/config/AutoConfig.java:
--------------------------------------------------------------------------------
1 | package com.example.disconf.demo.config;
2 |
3 | import org.springframework.beans.factory.annotation.Value;
4 | import org.springframework.stereotype.Service;
5 |
6 | /**
7 | * Created by knightliao on 16/6/24.
8 | */
9 | @Service
10 | public class AutoConfig {
11 |
12 | @Value("${auto}")
13 | private String auto;
14 |
15 | public String getAuto() {
16 | return auto;
17 | }
18 |
19 | public void setAuto(String auto) {
20 | this.auto = auto;
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/disconf-standalone-demo/src/main/java/com/example/disconf/demo/config/CodeConfig.java:
--------------------------------------------------------------------------------
1 | package com.example.disconf.demo.config;
2 |
3 | import org.springframework.stereotype.Service;
4 |
5 | import com.baidu.disconf.client.common.annotations.DisconfFile;
6 | import com.baidu.disconf.client.common.annotations.DisconfFileItem;
7 |
8 | /**
9 | * Created by knightliao on 15/1/7.
10 | */
11 | @Service
12 | @DisconfFile(filename = "code.properties", targetDirPath = "disconf")
13 | public class CodeConfig {
14 |
15 | private String codeError = "";
16 |
17 | private boolean routerRateLimitEnable;
18 |
19 | @DisconfFileItem(name = "syserror.paramtype", associateField = "codeError")
20 | public String getCodeError() {
21 | return codeError;
22 | }
23 |
24 | @DisconfFileItem(name = "proxy.routerRateEnable", associateField = "routerRateLimitEnable")
25 | public boolean isRouterRateLimitEnable() {
26 | return routerRateLimitEnable;
27 | }
28 |
29 | public void setRouterRateLimitEnable(boolean routerRateLimitEnable) {
30 | this.routerRateLimitEnable = routerRateLimitEnable;
31 | }
32 |
33 | public void setCodeError(String codeError) {
34 | this.codeError = codeError;
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/disconf-standalone-demo/src/main/java/com/example/disconf/demo/config/Coefficients.java:
--------------------------------------------------------------------------------
1 | package com.example.disconf.demo.config;
2 |
3 | import org.springframework.beans.factory.annotation.Value;
4 | import org.springframework.stereotype.Service;
5 |
6 | import com.baidu.disconf.client.common.annotations.DisconfFile;
7 | import com.baidu.disconf.client.common.annotations.DisconfFileItem;
8 | import com.baidu.disconf.client.common.annotations.DisconfItem;
9 |
10 | /**
11 | * 金融系数文件
12 | */
13 | @Service
14 | @DisconfFile(filename = "coefficients.properties")
15 | public class Coefficients {
16 |
17 | public static final String key = "discountRate";
18 |
19 | @Value(value = "2.0d")
20 | private Double discount;
21 |
22 | private double baiFaCoe;
23 |
24 | private double yuErBaoCoe;
25 |
26 | /**
27 | * 阿里余额宝的系数, 分布式文件配置
28 | *
29 | * @return
30 | */
31 | @DisconfFileItem(name = "coe.baiFaCoe")
32 | public double getBaiFaCoe() {
33 | return baiFaCoe;
34 | }
35 |
36 | public void setBaiFaCoe(double baiFaCoe) {
37 | this.baiFaCoe = baiFaCoe;
38 | }
39 |
40 | /**
41 | * 百发的系数, 分布式文件配置
42 | *
43 | * @return
44 | */
45 | @DisconfFileItem(name = "coe.yuErBaoCoe")
46 | public double getYuErBaoCoe() {
47 | return yuErBaoCoe;
48 | }
49 |
50 | public void setYuErBaoCoe(double yuErBaoCoe) {
51 | this.yuErBaoCoe = yuErBaoCoe;
52 | }
53 |
54 | /**
55 | * 折扣率,分布式配置
56 | *
57 | * @return
58 | */
59 | @DisconfItem(key = key)
60 | public Double getDiscount() {
61 | return discount;
62 | }
63 |
64 | public void setDiscount(Double discount) {
65 | this.discount = discount;
66 | }
67 |
68 | }
69 |
--------------------------------------------------------------------------------
/disconf-standalone-demo/src/main/java/com/example/disconf/demo/config/EmptyConf.java:
--------------------------------------------------------------------------------
1 | package com.example.disconf.demo.config;
2 |
3 | import org.springframework.stereotype.Service;
4 |
5 | import com.baidu.disconf.client.common.annotations.DisconfFile;
6 |
7 | /**
8 | * 空的分布式配置文件,用途有两种:
9 | * 1. 对配置文件里的内容不感兴趣,只是单纯的下载
10 | * 2. 当配置文件更新时,可以自动下载到本地
11 | */
12 | @Service
13 | @DisconfFile(filename = "empty.properties")
14 | public class EmptyConf {
15 |
16 | }
17 |
--------------------------------------------------------------------------------
/disconf-standalone-demo/src/main/java/com/example/disconf/demo/config/JedisConfig.java:
--------------------------------------------------------------------------------
1 | package com.example.disconf.demo.config;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 | import org.springframework.context.annotation.Scope;
6 | import org.springframework.stereotype.Service;
7 |
8 | import com.baidu.disconf.client.common.annotations.DisconfFile;
9 | import com.baidu.disconf.client.common.annotations.DisconfFileItem;
10 | import com.baidu.disconf.client.common.annotations.DisconfUpdateService;
11 | import com.baidu.disconf.client.common.update.IDisconfUpdate;
12 |
13 | /**
14 | * Redis配置文件
15 | *
16 | * @author liaoqiqi
17 | * @version 2014-6-17
18 | */
19 | @Service
20 | @Scope("singleton")
21 | @DisconfFile(filename = "redis.properties")
22 | @DisconfUpdateService(classes = {JedisConfig.class})
23 | public class JedisConfig implements IDisconfUpdate {
24 |
25 | protected static final Logger LOGGER = LoggerFactory.getLogger(JedisConfig.class);
26 |
27 | // 代表连接地址
28 | private String host;
29 |
30 | // 代表连接port
31 | private int port;
32 |
33 | /**
34 | * 地址, 分布式文件配置
35 | *
36 | * @return
37 | */
38 | @DisconfFileItem(name = "redis.host", associateField = "host")
39 | public String getHost() {
40 | return host;
41 | }
42 |
43 | public void setHost(String host) {
44 | this.host = host;
45 | }
46 |
47 | /**
48 | * 端口, 分布式文件配置
49 | *
50 | * @return
51 | */
52 | @DisconfFileItem(name = "redis.port", associateField = "port")
53 | public int getPort() {
54 | return port;
55 | }
56 |
57 | public void setPort(int port) {
58 | this.port = port;
59 | LOGGER.info("i' m here: setting redis port");
60 | }
61 |
62 | public void reload() throws Exception {
63 | LOGGER.info("host: " + host);
64 | }
65 | }
66 |
--------------------------------------------------------------------------------
/disconf-standalone-demo/src/main/java/com/example/disconf/demo/config/RemoteServerConfig.java:
--------------------------------------------------------------------------------
1 | package com.example.disconf.demo.config;
2 |
3 | import org.springframework.stereotype.Service;
4 |
5 | import com.baidu.disconf.client.common.annotations.DisconfFile;
6 | import com.baidu.disconf.client.common.annotations.DisconfFileItem;
7 |
8 | /**
9 | * @author liaoqiqi
10 | * @version 2014-6-17
11 | */
12 | @Service
13 | @DisconfFile(filename = "remote.properties")
14 | public class RemoteServerConfig {
15 |
16 | // 代表连接地址
17 | private String remoteHost;
18 |
19 | // 代表连接port
20 | private int remotePort = 8080;
21 |
22 | /**
23 | * 地址, 分布式文件配置
24 | *
25 | * @return
26 | */
27 | @DisconfFileItem(name = "remoteHost")
28 | public String getRemoteHost() {
29 | return remoteHost;
30 | }
31 |
32 | public void setRemoteHost(String remoteHost) {
33 | this.remoteHost = remoteHost;
34 | }
35 |
36 | /**
37 | * 端口, 分布式文件配置
38 | *
39 | * @return
40 | */
41 | @DisconfFileItem(name = "remotePort")
42 | public int getRemotePort() {
43 | return remotePort;
44 | }
45 |
46 | public void setRemotePort(int remotePort) {
47 | this.remotePort = remotePort;
48 | }
49 |
50 | }
51 |
--------------------------------------------------------------------------------
/disconf-standalone-demo/src/main/java/com/example/disconf/demo/config/StaticConfig.java:
--------------------------------------------------------------------------------
1 | package com.example.disconf.demo.config;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 |
6 | import com.baidu.disconf.client.common.annotations.DisconfFile;
7 | import com.baidu.disconf.client.common.annotations.DisconfFileItem;
8 |
9 | /**
10 | * 静态 配置文件 示例
11 | *
12 | * @author liaoqiqi
13 | * @version 2014-6-17
14 | */
15 | @DisconfFile(filename = "static.properties")
16 | public class StaticConfig {
17 |
18 | protected static final Logger LOGGER = LoggerFactory.getLogger(StaticConfig.class);
19 |
20 | private static int staticVar;
21 |
22 | @DisconfFileItem(name = "staticVar", associateField = "staticVar")
23 | public static int getStaticVar() {
24 | return staticVar;
25 | }
26 |
27 | public static void setStaticVar(int staticVar) {
28 | StaticConfig.staticVar = staticVar;
29 | LOGGER.info("i' m here: setting static class variable");
30 | }
31 |
32 | }
33 |
--------------------------------------------------------------------------------
/disconf-standalone-demo/src/main/java/com/example/disconf/demo/config/TestXmlConfig.java:
--------------------------------------------------------------------------------
1 | package com.example.disconf.demo.config;
2 |
3 | import org.springframework.stereotype.Service;
4 |
5 | import com.baidu.disconf.client.common.annotations.DisconfFile;
6 |
7 | /**
8 | * 空的分布式配置文件,用途有两种:
9 | * 1. 对配置文件里的内容不感兴趣,只是单纯的下载
10 | * 2. 当配置文件更新时,可以自动下载到本地
11 | */
12 | @Service
13 | @DisconfFile(filename = "testXml.xml")
14 | public class TestXmlConfig {
15 |
16 | }
17 |
--------------------------------------------------------------------------------
/disconf-standalone-demo/src/main/java/com/example/disconf/demo/service/AutoService.java:
--------------------------------------------------------------------------------
1 | package com.example.disconf.demo.service;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 |
6 | /**
7 | * Created by knightliao on 15/3/19.
8 | */
9 | public class AutoService {
10 |
11 | protected static final Logger LOGGER = LoggerFactory.getLogger(AutoService.class);
12 |
13 | private String auto;
14 |
15 | public String getAuto() {
16 | return auto;
17 | }
18 |
19 | public void setAuto(String auto) {
20 | this.auto = auto;
21 | LOGGER.info("i' m here: setting auto");
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/disconf-standalone-demo/src/main/java/com/example/disconf/demo/service/AutoService2.java:
--------------------------------------------------------------------------------
1 | package com.example.disconf.demo.service;
2 |
3 | /**
4 | * Created by knightliao on 15/3/19.
5 | */
6 | public class AutoService2 {
7 |
8 | private String auto2;
9 |
10 | public String getAuto2() {
11 | return auto2;
12 | }
13 |
14 | public void setAuto2(String auto2) {
15 | this.auto2 = auto2;
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/disconf-standalone-demo/src/main/java/com/example/disconf/demo/service/BaoBaoService.java:
--------------------------------------------------------------------------------
1 | package com.example.disconf.demo.service;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 | import org.springframework.beans.factory.annotation.Autowired;
6 | import org.springframework.beans.factory.annotation.Value;
7 | import org.springframework.stereotype.Service;
8 |
9 | import com.baidu.disconf.client.common.annotations.DisconfItem;
10 | import com.example.disconf.demo.config.Coefficients;
11 |
12 | /**
13 | * 金融宝服务,计算一天赚多少钱
14 | *
15 | * @author liaoqiqi
16 | * @version 2014-5-16
17 | */
18 | @Service
19 | public class BaoBaoService {
20 |
21 | protected static final Logger LOGGER = LoggerFactory.getLogger(BaoBaoService.class);
22 |
23 | public static final String key = "moneyInvest";
24 |
25 | @Value(value = "2000d")
26 | private Double moneyInvest;
27 |
28 | @Autowired
29 | private Coefficients coefficients;
30 |
31 | /**
32 | * 计算百发一天赚多少钱
33 | *
34 | * @return
35 | */
36 | public double calcBaiFa() {
37 | return coefficients.getBaiFaCoe() * coefficients.getDiscount() * getMoneyInvest();
38 | }
39 |
40 | /**
41 | * k 计算余额宝一天赚多少钱
42 | *
43 | * @return
44 | */
45 | public double calcYuErBao() {
46 | return coefficients.getYuErBaoCoe() * coefficients.getDiscount() * getMoneyInvest();
47 | }
48 |
49 | /**
50 | * 投资的钱,分布式配置
51 | *
52 | * 这里切面无法生效,因为SpringAOP不支持。
53 | * 但是这里还是正确的,因为我们会将值注入到Bean的值里.
54 | *
55 | * @return
56 | */
57 | @DisconfItem(key = key)
58 | public Double getMoneyInvest() {
59 | return moneyInvest;
60 | }
61 |
62 | public void setMoneyInvest(Double moneyInvest) {
63 | this.moneyInvest = moneyInvest;
64 | LOGGER.info("i' m here: setting moneyInvest");
65 | }
66 |
67 | }
68 |
--------------------------------------------------------------------------------
/disconf-standalone-demo/src/main/java/com/example/disconf/demo/service/RemoteService.java:
--------------------------------------------------------------------------------
1 | package com.example.disconf.demo.service;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 |
6 | import org.slf4j.Logger;
7 | import org.slf4j.LoggerFactory;
8 | import org.springframework.beans.factory.DisposableBean;
9 | import org.springframework.beans.factory.InitializingBean;
10 | import org.springframework.beans.factory.annotation.Autowired;
11 | import org.springframework.stereotype.Service;
12 |
13 | import com.example.disconf.demo.config.RemoteServerConfig;
14 |
15 | /**
16 | * 一个未知远程服务, 这里也不使用注解的 @Service
17 | *
18 | * @author liaoqiqi
19 | * @version 2014-6-17
20 | */
21 | @Service
22 | public class RemoteService implements InitializingBean, DisposableBean {
23 |
24 | protected static final Logger LOGGER = LoggerFactory.getLogger(RemoteService.class);
25 |
26 | private List list = new ArrayList();
27 |
28 | @Autowired
29 | private RemoteServerConfig remoteServerConfig;
30 |
31 | public void destroy() throws Exception {
32 |
33 | }
34 |
35 | public void afterPropertiesSet() throws Exception {
36 |
37 | list.add(remoteServerConfig.getRemoteHost());
38 | list.add(String.valueOf(remoteServerConfig.getRemoteHost()));
39 | }
40 |
41 | /**
42 | * 更改Jedis
43 | */
44 | public void reload() {
45 |
46 | LOGGER.info("start to reload remote service to: " + remoteServerConfig.getRemoteHost() + " : " +
47 | remoteServerConfig.getRemoteHost());
48 |
49 | list.add(remoteServerConfig.getRemoteHost());
50 | list.add(String.valueOf(remoteServerConfig.getRemoteHost()));
51 |
52 | LOGGER.info("reload ok.");
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/disconf-standalone-demo/src/main/java/com/example/disconf/demo/service/SimpleRedisService.java:
--------------------------------------------------------------------------------
1 | package com.example.disconf.demo.service;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 | import org.springframework.beans.factory.DisposableBean;
6 | import org.springframework.beans.factory.InitializingBean;
7 | import org.springframework.beans.factory.annotation.Autowired;
8 | import org.springframework.context.annotation.Scope;
9 | import org.springframework.stereotype.Service;
10 |
11 | import com.example.disconf.demo.config.JedisConfig;
12 | import com.example.disconf.demo.utils.JedisUtil;
13 |
14 | import redis.clients.jedis.Jedis;
15 |
16 | /**
17 | * 一个简单的Redis服务
18 | *
19 | * @author liaoqiqi
20 | * @version 2014-6-17
21 | */
22 | @Service
23 | @Scope("singleton")
24 | public class SimpleRedisService implements InitializingBean, DisposableBean {
25 |
26 | protected static final Logger LOGGER = LoggerFactory.getLogger(SimpleRedisService.class);
27 |
28 | // jedis 实例
29 | private Jedis jedis = null;
30 |
31 | /**
32 | * 分布式配置
33 | */
34 | @Autowired
35 | private JedisConfig jedisConfig;
36 |
37 | /**
38 | * 关闭
39 | */
40 | public void destroy() throws Exception {
41 |
42 | if (jedis != null) {
43 | jedis.disconnect();
44 | }
45 | }
46 |
47 | /**
48 | * 进行连接
49 | */
50 | public void afterPropertiesSet() throws Exception {
51 |
52 | jedis = JedisUtil.createJedis(jedisConfig.getHost(), jedisConfig.getPort());
53 | }
54 |
55 | /**
56 | * 获取一个值
57 | *
58 | * @param key
59 | *
60 | * @return
61 | */
62 | public String getKey(String key) {
63 | if (jedis != null) {
64 | return jedis.get(key);
65 | }
66 |
67 | return null;
68 | }
69 |
70 | /**
71 | * 更改Jedis
72 | */
73 | public void changeJedis() {
74 |
75 | LOGGER.info("start to change jedis hosts to: " + jedisConfig.getHost() + " : " + jedisConfig.getPort());
76 |
77 | jedis = JedisUtil.createJedis(jedisConfig.getHost(), jedisConfig.getPort());
78 |
79 | LOGGER.info("change ok.");
80 | }
81 | }
82 |
--------------------------------------------------------------------------------
/disconf-standalone-demo/src/main/java/com/example/disconf/demo/service/SimpleStaticService.java:
--------------------------------------------------------------------------------
1 | package com.example.disconf.demo.service;
2 |
3 | import com.baidu.disconf.client.common.annotations.DisconfItem;
4 | import com.example.disconf.demo.config.StaticConfig;
5 |
6 | /**
7 | * 使用静态配置文件的示例
8 | * Plus
9 | * 静态配置项 使用示例
10 | *
11 | * @author liaoqiqi
12 | * @version 2014-8-14
13 | */
14 | public class SimpleStaticService {
15 |
16 | private static int staticItem = 56;
17 |
18 | /**
19 | * @return
20 | */
21 | public static int getStaticFileData() {
22 |
23 | return StaticConfig.getStaticVar();
24 | }
25 |
26 | @DisconfItem(key = "staticItem")
27 | public static int getStaticItem() {
28 | return staticItem;
29 | }
30 |
31 | public static void setStaticItem(int staticItem) {
32 | SimpleStaticService.staticItem = staticItem;
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/disconf-standalone-demo/src/main/java/com/example/disconf/demo/service/callbacks/AutoServiceCallback.java:
--------------------------------------------------------------------------------
1 | package com.example.disconf.demo.service.callbacks;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 | import org.springframework.beans.factory.annotation.Autowired;
6 | import org.springframework.stereotype.Service;
7 |
8 | import com.baidu.disconf.client.common.annotations.DisconfUpdateService;
9 | import com.baidu.disconf.client.common.update.IDisconfUpdate;
10 | import com.example.disconf.demo.service.AutoService;
11 |
12 | /**
13 | * 这是 autoconfig.properties 的回调函数类
14 | *
15 | * Created by knightliao on 15/3/21.
16 | */
17 | @Service
18 | @DisconfUpdateService(confFileKeys = {"autoconfig.properties", "autoconfig2.properties"})
19 | public class AutoServiceCallback implements IDisconfUpdate {
20 |
21 | protected static final Logger LOGGER = LoggerFactory.getLogger(AutoServiceCallback.class);
22 |
23 | @Autowired
24 | private AutoService autoService;
25 |
26 | public void reload() throws Exception {
27 |
28 | LOGGER.info("reload callback " + "autoconfig.properties or autoconfig2.properties" + autoService.getAuto());
29 |
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/disconf-standalone-demo/src/main/java/com/example/disconf/demo/service/callbacks/RemoteServiceUpdateCallback.java:
--------------------------------------------------------------------------------
1 | package com.example.disconf.demo.service.callbacks;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 | import org.springframework.beans.factory.annotation.Autowired;
6 | import org.springframework.stereotype.Service;
7 |
8 | import com.baidu.disconf.client.common.annotations.DisconfUpdateService;
9 | import com.baidu.disconf.client.common.update.IDisconfUpdate;
10 | import com.example.disconf.demo.config.RemoteServerConfig;
11 | import com.example.disconf.demo.service.RemoteService;
12 |
13 | /**
14 | * 这是RemoteService的回调函数类
15 | *
16 | * @author liaoqiqi
17 | * @version 2014-6-17
18 | */
19 | @Service
20 | @DisconfUpdateService(classes = {RemoteServerConfig.class})
21 | public class RemoteServiceUpdateCallback implements IDisconfUpdate {
22 |
23 | protected static final Logger LOGGER = LoggerFactory.getLogger(RemoteServiceUpdateCallback.class);
24 |
25 | @Autowired
26 | private RemoteService remoteService;
27 |
28 | /**
29 | *
30 | */
31 | public void reload() throws Exception {
32 |
33 | remoteService.reload();
34 | }
35 |
36 | }
37 |
--------------------------------------------------------------------------------
/disconf-standalone-demo/src/main/java/com/example/disconf/demo/service/callbacks/SimpleRedisServiceUpdateCallback.java:
--------------------------------------------------------------------------------
1 | package com.example.disconf.demo.service.callbacks;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 | import org.springframework.beans.factory.annotation.Autowired;
6 | import org.springframework.context.annotation.Scope;
7 | import org.springframework.stereotype.Service;
8 |
9 | import com.baidu.disconf.client.common.annotations.DisconfUpdateService;
10 | import com.baidu.disconf.client.common.update.IDisconfUpdate;
11 | import com.example.disconf.demo.config.Coefficients;
12 | import com.example.disconf.demo.config.JedisConfig;
13 | import com.example.disconf.demo.service.SimpleRedisService;
14 |
15 | /**
16 | * 更新Redis配置时的回调函数
17 | *
18 | * @author liaoqiqi
19 | * @version 2014-6-17
20 | */
21 | @Service
22 | @Scope("singleton")
23 | @DisconfUpdateService(classes = {JedisConfig.class}, itemKeys = {Coefficients.key})
24 | public class SimpleRedisServiceUpdateCallback implements IDisconfUpdate {
25 |
26 | protected static final Logger LOGGER = LoggerFactory.getLogger(SimpleRedisServiceUpdateCallback.class);
27 |
28 | @Autowired
29 | private SimpleRedisService simpleRedisService;
30 |
31 | /**
32 | *
33 | */
34 | public void reload() throws Exception {
35 |
36 | simpleRedisService.changeJedis();
37 | }
38 |
39 | }
40 |
--------------------------------------------------------------------------------
/disconf-standalone-demo/src/main/java/com/example/disconf/demo/service/callbacks/TestJsonConfigCallback.java:
--------------------------------------------------------------------------------
1 | package com.example.disconf.demo.service.callbacks;
2 |
3 | import org.springframework.stereotype.Service;
4 |
5 | import com.baidu.disconf.client.common.annotations.DisconfUpdateService;
6 | import com.baidu.disconf.client.common.update.IDisconfUpdate;
7 |
8 | @Service
9 | @DisconfUpdateService(confFileKeys = {"testJson.json"})
10 | public class TestJsonConfigCallback implements IDisconfUpdate {
11 |
12 | public void reload() throws Exception {
13 |
14 | System.out.println("now i'm at xml update callback ");
15 | }
16 |
17 | }
18 |
--------------------------------------------------------------------------------
/disconf-standalone-demo/src/main/java/com/example/disconf/demo/service/callbacks/TestXmlConfigCallback.java:
--------------------------------------------------------------------------------
1 | package com.example.disconf.demo.service.callbacks;
2 |
3 | import com.baidu.disconf.client.common.annotations.DisconfUpdateService;
4 | import com.baidu.disconf.client.common.update.IDisconfUpdate;
5 | import com.example.disconf.demo.config.TestXmlConfig;
6 |
7 | /**
8 | * 如果你的回调类里没有 进行autowired ,那么你的回调类可以是非 spring托管的
9 | */
10 | @DisconfUpdateService(classes = {TestXmlConfig.class}, confFileKeys = {"testJson.json"})
11 | public class TestXmlConfigCallback implements IDisconfUpdate {
12 |
13 | public void reload() throws Exception {
14 |
15 | System.out.println("now i'm at xml update callback ");
16 | }
17 |
18 | }
19 |
--------------------------------------------------------------------------------
/disconf-standalone-demo/src/main/java/com/example/disconf/demo/service/callbacks/UpdatePipelineCallback.java:
--------------------------------------------------------------------------------
1 | package com.example.disconf.demo.service.callbacks;
2 |
3 | import java.io.IOException;
4 | import java.nio.charset.Charset;
5 | import java.nio.file.Files;
6 | import java.nio.file.Paths;
7 |
8 | import org.springframework.stereotype.Service;
9 |
10 | import com.baidu.disconf.client.common.update.IDisconfUpdatePipeline;
11 |
12 | /**
13 | * Created by knightliao on 16/3/22.
14 | */
15 | @Service
16 | public class UpdatePipelineCallback implements IDisconfUpdatePipeline {
17 |
18 | static String readFile(String path, Charset encoding)
19 | throws IOException {
20 | byte[] encoded = Files.readAllBytes(Paths.get(path));
21 | return new String(encoded, encoding);
22 | }
23 |
24 | public void reloadDisconfFile(String key, String filePath) throws Exception {
25 |
26 | System.out.println(key + " : " + filePath);
27 |
28 | System.out.println(readFile(filePath, Charset.defaultCharset()));
29 | }
30 |
31 | public void reloadDisconfItem(String key, Object content) throws Exception {
32 | System.out.println(key + " : " + content);
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/disconf-standalone-demo/src/main/java/com/example/disconf/demo/task/DisconfDemoTask.java:
--------------------------------------------------------------------------------
1 | package com.example.disconf.demo.task;
2 |
3 | import java.text.MessageFormat;
4 |
5 | import org.slf4j.Logger;
6 | import org.slf4j.LoggerFactory;
7 | import org.springframework.beans.factory.annotation.Autowired;
8 | import org.springframework.stereotype.Service;
9 |
10 | import com.baidu.disconf.client.usertools.DisconfDataGetter;
11 | import com.example.disconf.demo.config.AutoConfig;
12 | import com.example.disconf.demo.config.CodeConfig;
13 | import com.example.disconf.demo.config.JedisConfig;
14 | import com.example.disconf.demo.service.AutoService;
15 | import com.example.disconf.demo.service.AutoService2;
16 | import com.example.disconf.demo.service.BaoBaoService;
17 | import com.example.disconf.demo.service.SimpleRedisService;
18 | import com.example.disconf.demo.service.SimpleStaticService;
19 |
20 | /**
21 | * 演示分布式配置文件、分布式配置的更新Demo
22 | *
23 | * @author liaoqiqi
24 | * @version 2014-6-17
25 | */
26 | @Service
27 | public class DisconfDemoTask {
28 |
29 | protected static final Logger LOGGER = LoggerFactory.getLogger(DisconfDemoTask.class);
30 |
31 | @Autowired
32 | private BaoBaoService baoBaoService;
33 |
34 | @Autowired
35 | private SimpleRedisService simpleRedisService;
36 |
37 | @Autowired
38 | private JedisConfig jedisConfig;
39 |
40 | @Autowired
41 | private AutoService autoService;
42 |
43 | @Autowired
44 | private AutoService2 autoService2;
45 |
46 | @Autowired
47 | private CodeConfig codeConfig;
48 |
49 | private static final String REDIS_KEY = "disconf_key";
50 |
51 | @Autowired
52 | private AutoConfig autoConfig;
53 |
54 | /**
55 | *
56 | */
57 | public int run() {
58 |
59 | try {
60 |
61 | while (true) {
62 |
63 | //
64 | // service demo
65 | //
66 |
67 | LOGGER.info("baobao--baifa: {}", baoBaoService.calcBaiFa());
68 | LOGGER.info("baobao--yuerbao: {}", baoBaoService.calcYuErBao());
69 |
70 | Thread.sleep(5000);
71 |
72 | LOGGER.info(MessageFormat.format("redis( {0} , {1,number,#} ) get key : {2}", jedisConfig.getHost(),
73 | jedisConfig.getPort(), REDIS_KEY));
74 |
75 | LOGGER.info("redis( {} , {} )", jedisConfig.getHost(), jedisConfig.getPort());
76 |
77 | LOGGER.info("code config: {}", codeConfig.getCodeError());
78 |
79 | //
80 | // xml demo
81 | //
82 |
83 | LOGGER.info("autoservice: {}", autoService.getAuto());
84 |
85 | LOGGER.info("autoservice2: {}", autoService2.getAuto2());
86 |
87 | //
88 | // static config demo
89 | //
90 | LOGGER.info("static file data: {}", SimpleStaticService.getStaticFileData());
91 |
92 | LOGGER.info("static item data: {}", SimpleStaticService.getStaticItem());
93 |
94 | //
95 | // 动态的写法
96 | //
97 | LOGGER.info(DisconfDataGetter.getByFile("redis.properties").toString());
98 | LOGGER.info(DisconfDataGetter.getByFile("autoconfig.properties").toString());
99 | if (DisconfDataGetter.getByFile("autoconfig.properties").containsKey("auto")) {
100 | LOGGER.info(DisconfDataGetter.getByFile("autoconfig.properties").get("auto").toString());
101 | }
102 | if (DisconfDataGetter.getByFileItem("autoconfig.properties", "auto") != null) {
103 | LOGGER.info(DisconfDataGetter.getByFileItem("autoconfig.properties", "auto").toString());
104 | }
105 | LOGGER.info(DisconfDataGetter.getByItem("moneyInvest").toString());
106 |
107 | LOGGER.info("get bean @value : {}", autoConfig.getAuto());
108 | }
109 |
110 | } catch (Exception e) {
111 |
112 | LOGGER.error(e.toString(), e);
113 | }
114 |
115 | return 0;
116 | }
117 | }
118 |
--------------------------------------------------------------------------------
/disconf-standalone-demo/src/main/java/com/example/disconf/demo/utils/JedisUtil.java:
--------------------------------------------------------------------------------
1 | package com.example.disconf.demo.utils;
2 |
3 | import org.apache.commons.lang.StringUtils;
4 |
5 | import redis.clients.jedis.Jedis;
6 |
7 | /**
8 | * @author liaoqiqi
9 | * @version 2014-6-17
10 | */
11 | public class JedisUtil {
12 |
13 | public static Jedis createJedis() {
14 | Jedis jedis = new Jedis("127.0.0.1");
15 | return jedis;
16 | }
17 |
18 | public static Jedis createJedis(String host, int port) {
19 | Jedis jedis = new Jedis(host, port);
20 |
21 | return jedis;
22 | }
23 |
24 | public static Jedis createJedis(String host, int port, String password) {
25 | Jedis jedis = new Jedis(host, port);
26 |
27 | if (!StringUtils.isNotBlank(password)) {
28 | jedis.auth(password);
29 | }
30 |
31 | return jedis;
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/disconf-standalone-demo/src/main/resources/applicationContext.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
20 |
21 |
22 |
24 |
25 |
26 |
27 |
29 |
30 |
31 | classpath:/autoconfig.properties
32 | classpath:/autoconfig2.properties
33 | classpath:/myserver_slave.properties
34 | classpath:/testJson.json
35 | testXml2.xml
36 |
37 |
38 |
39 |
40 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
54 |
55 |
56 | myserver.properties
57 |
58 |
59 |
60 |
61 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
--------------------------------------------------------------------------------
/disconf-standalone-dubbo-demo/.gitignore:
--------------------------------------------------------------------------------
1 | /.settings
2 | /target
3 | /.classpath
4 | /.project
5 | /.DS_Store
6 | /disconf
7 | /log
8 | /bin
9 | /disconf-standalone-dubbo-demo.iml
10 | .idea
11 | dependency-reduced-pom.xml
--------------------------------------------------------------------------------
/disconf-standalone-dubbo-demo/README.md:
--------------------------------------------------------------------------------
1 |
2 | 鉴于现在 SOA服务治理 dubbo 的流行化,此demo演示和 disconf 和 dubbo 的集成 demo.
3 |
4 | - using dubbo 2.5.3
5 |
6 | 可以参见:
7 |
8 | https://github.com/knightliao/disconf-demos-java/tree/master/disconf-standalone-demo
--------------------------------------------------------------------------------
/disconf-standalone-dubbo-demo/pom.xml:
--------------------------------------------------------------------------------
1 |
3 |
4 | 4.0.0
5 | com.baidu.disconf
6 | disconf-standalone-dubbo-demo
7 | 2.0.0
8 |
9 |
10 |
11 |
12 | com.baidu.disconf
13 | disconf-client
14 | 2.6.36
15 |
16 |
17 |
18 | commons-io
19 | commons-io
20 | 2.7
21 |
22 |
23 |
24 | commons-lang
25 | commons-lang
26 | 2.4
27 |
28 |
29 |
30 | org.slf4j
31 | slf4j-api
32 | 1.7.25
33 |
34 |
35 |
36 | ch.qos.logback
37 | logback-classic
38 | 1.2.5
39 |
40 |
41 |
42 | junit
43 | junit
44 | 4.13.1
45 | test
46 |
47 |
48 |
49 | redis.clients
50 | jedis
51 | 2.1.0
52 |
53 |
54 |
55 | org.springframework
56 | spring-context
57 | 4.1.7.RELEASE
58 |
59 |
60 |
61 | com.alibaba
62 | dubbo
63 | 2.5.3
64 |
65 |
66 |
67 | com.101tec
68 | zkclient
69 | 0.4
70 |
71 |
72 |
73 |
74 |
75 |
76 | package
77 | ${project.artifactId}
78 | target/classes
79 |
80 |
81 |
82 | ${project.basedir}/src/main/java
83 |
84 | **/*.java
85 |
86 |
87 |
88 | ${project.basedir}/src/main/resources
89 |
90 | *.*
91 |
92 |
93 |
94 |
95 |
96 |
97 | com.github.knightliao.plugin
98 | starter-shade-maven-plugin
99 | 1.0.0
100 |
101 |
102 | package
103 |
104 | shade
105 |
106 |
107 | ${project.build.finalName}
108 |
109 |
111 | com.example.disconf.demo.DisconfDemoMain
112 |
113 |
115 |
116 | env
117 | disconf.properties
118 | logback.xml
119 |
120 |
121 |
123 | META-INF/spring.handlers
124 |
125 |
127 | META-INF/spring.schemas
128 |
129 |
130 |
131 |
132 |
133 |
134 |
135 |
136 |
137 |
138 |
139 |
140 |
141 | rd
142 |
143 | true
144 |
145 |
146 |
147 |
148 |
149 | ${basedir}/profile/rd
150 |
151 |
152 |
153 |
154 |
155 |
156 |
--------------------------------------------------------------------------------
/disconf-standalone-dubbo-demo/profile/rd/disconf.properties:
--------------------------------------------------------------------------------
1 |
2 |
3 | # \u662F\u5426\u4F7F\u7528\u8FDC\u7A0B\u914D\u7F6E\u6587\u4EF6
4 | # true(\u9ED8\u8BA4)\u4F1A\u4ECE\u8FDC\u7A0B\u83B7\u53D6\u914D\u7F6E false\u5219\u76F4\u63A5\u83B7\u53D6\u672C\u5730\u914D\u7F6E
5 | disconf.enable.remote.conf=true
6 |
7 | #
8 | # \u914D\u7F6E\u670D\u52A1\u5668\u7684 HOST,\u7528\u9017\u53F7\u5206\u9694 127.0.0.1:8000,127.0.0.1:8000
9 | #
10 | disconf.conf_server_host=127.0.0.1:8080
11 |
12 | # \u7248\u672C, \u8BF7\u91C7\u7528 X_X_X_X \u683C\u5F0F
13 | disconf.version=1_0_0_0
14 |
15 | # APP \u8BF7\u91C7\u7528 \u4EA7\u54C1\u7EBF_\u670D\u52A1\u540D \u683C\u5F0F
16 | disconf.app=disconf_demo
17 |
18 | # \u73AF\u5883
19 | disconf.env=rd
20 |
21 | # \u5FFD\u7565\u54EA\u4E9B\u5206\u5E03\u5F0F\u914D\u7F6E\uFF0C\u7528\u9017\u53F7\u5206\u9694
22 | disconf.ignore=
23 |
24 | # \u83B7\u53D6\u8FDC\u7A0B\u914D\u7F6E \u91CD\u8BD5\u6B21\u6570\uFF0C\u9ED8\u8BA4\u662F3\u6B21
25 | disconf.conf_server_url_retry_times=1
26 | # \u83B7\u53D6\u8FDC\u7A0B\u914D\u7F6E \u91CD\u8BD5\u65F6\u4F11\u7720\u65F6\u95F4\uFF0C\u9ED8\u8BA4\u662F5\u79D2
27 | disconf.conf_server_url_retry_sleep_seconds=1
28 |
29 | # \u7528\u6237\u6307\u5B9A\u7684\u4E0B\u8F7D\u6587\u4EF6\u5939, \u8FDC\u7A0B\u6587\u4EF6\u4E0B\u8F7D\u540E\u4F1A\u653E\u5728\u8FD9\u91CC
30 | disconf.user_define_download_dir=./disconf/download2
--------------------------------------------------------------------------------
/disconf-standalone-dubbo-demo/profile/rd/env:
--------------------------------------------------------------------------------
1 |
2 | BUNDLE_JAR_NAME=disconf-standalone-dubbo-demo.jar
3 |
4 | export JAVA_OPTS="$JAVA_OPTS -server -Xms1024m -Xmx1024m -Xmn448m -Xss256K -XX:MaxPermSize=128m -XX:ReservedCodeCacheSize=64m"
5 | export JAVA_OPTS="$JAVA_OPTS -XX:+UseParallelGC -XX:+UseParallelOldGC -XX:ParallelGCThreads=2"
6 | export JAVA_OPTS="$JAVA_OPTS -XX:+PrintGCDetails -XX:+PrintGCTimeStamps"
7 | export JAVA_OPTS="$JAVA_OPTS -Dlogback.configurationFile=file:logback.xml"
8 |
9 |
--------------------------------------------------------------------------------
/disconf-standalone-dubbo-demo/profile/rd/logback.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 | %date [%thread] %-5level %logger{80} - %msg%n
12 |
13 |
14 |
15 |
16 |
18 | ${log.base}.log
19 |
21 | ${log.base}.%d{yyyy-MM-dd}.log
22 |
23 |
24 | %date [%thread] %-5level %logger{80} - %msg%n
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
--------------------------------------------------------------------------------
/disconf-standalone-dubbo-demo/src/main/java/com/example/disconf/demo/DisconfDemoMain.java:
--------------------------------------------------------------------------------
1 | package com.example.disconf.demo;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 | import org.springframework.context.support.ClassPathXmlApplicationContext;
6 |
7 | import com.example.disconf.demo.task.DisconfDemoTask;
8 |
9 | /**
10 | *
11 | * @author liaoqiqi
12 | * @version 2014-6-17
13 | */
14 | public class DisconfDemoMain {
15 |
16 | protected static final Logger LOGGER = LoggerFactory.getLogger(DisconfDemoMain.class);
17 |
18 | private static String[] fn = null;
19 |
20 | // 初始化spring文档
21 | private static void contextInitialized() {
22 | fn = new String[] { "applicationContext.xml" };
23 | }
24 |
25 | /***
26 | *
27 | * @param args
28 | * @throws Exception
29 | */
30 | public static void main(String[] args) throws Exception {
31 |
32 | contextInitialized();
33 | ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(fn);
34 |
35 | DisconfDemoTask task = (DisconfDemoTask) ctx.getBean("disconfDemoTask", DisconfDemoTask.class);
36 |
37 | int ret = task.run();
38 |
39 | System.exit(ret);
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/disconf-standalone-dubbo-demo/src/main/java/com/example/disconf/demo/config/Coefficients.java:
--------------------------------------------------------------------------------
1 | package com.example.disconf.demo.config;
2 |
3 | import org.springframework.beans.factory.annotation.Value;
4 | import org.springframework.stereotype.Service;
5 |
6 | import com.baidu.disconf.client.common.annotations.DisconfFile;
7 | import com.baidu.disconf.client.common.annotations.DisconfFileItem;
8 | import com.baidu.disconf.client.common.annotations.DisconfItem;
9 |
10 | /**
11 | * 金融系数文件
12 | *
13 | **/
14 | @Service
15 | @DisconfFile(filename = "coefficients.properties")
16 | public class Coefficients {
17 |
18 | public static final String key = "discountRate";
19 |
20 | @Value(value = "2.0d")
21 | private Double discount;
22 |
23 | private double baiFaCoe;
24 |
25 | private double yuErBaoCoe;
26 |
27 | /**
28 | * 阿里余额宝的系数, 分布式文件配置
29 | *
30 | * @return
31 | */
32 | @DisconfFileItem(name = "coe.baiFaCoe")
33 | public double getBaiFaCoe() {
34 | return baiFaCoe;
35 | }
36 |
37 | public void setBaiFaCoe(double baiFaCoe) {
38 | this.baiFaCoe = baiFaCoe;
39 | }
40 |
41 | /**
42 | * 百发的系数, 分布式文件配置
43 | *
44 | * @return
45 | */
46 | @DisconfFileItem(name = "coe.yuErBaoCoe")
47 | public double getYuErBaoCoe() {
48 | return yuErBaoCoe;
49 | }
50 |
51 | public void setYuErBaoCoe(double yuErBaoCoe) {
52 | this.yuErBaoCoe = yuErBaoCoe;
53 | }
54 |
55 | /**
56 | * 折扣率,分布式配置
57 | *
58 | * @return
59 | */
60 | @DisconfItem(key = key)
61 | public Double getDiscount() {
62 | return discount;
63 | }
64 |
65 | public void setDiscount(Double discount) {
66 | this.discount = discount;
67 | }
68 | }
69 |
--------------------------------------------------------------------------------
/disconf-standalone-dubbo-demo/src/main/java/com/example/disconf/demo/config/EmptyConf.java:
--------------------------------------------------------------------------------
1 | package com.example.disconf.demo.config;
2 |
3 | import org.springframework.stereotype.Service;
4 |
5 | import com.baidu.disconf.client.common.annotations.DisconfFile;
6 |
7 | /**
8 | * 空的分布式配置文件,用途有两种:
9 | * 1. 对配置文件里的内容不感兴趣,只是单纯的下载
10 | * 2. 当配置文件更新时,可以自动下载到本地
11 | *
12 | **/
13 | @Service
14 | @DisconfFile(filename = "empty.properties")
15 | public class EmptyConf {
16 |
17 | }
18 |
--------------------------------------------------------------------------------
/disconf-standalone-dubbo-demo/src/main/java/com/example/disconf/demo/config/JedisConfig.java:
--------------------------------------------------------------------------------
1 | package com.example.disconf.demo.config;
2 |
3 | import org.springframework.context.annotation.Scope;
4 | import org.springframework.stereotype.Service;
5 |
6 | import com.baidu.disconf.client.common.annotations.DisconfFile;
7 | import com.baidu.disconf.client.common.annotations.DisconfFileItem;
8 |
9 | /**
10 | * Redis配置文件
11 | *
12 | * @author liaoqiqi
13 | * @version 2014-6-17
14 | */
15 | @Service
16 | @Scope("singleton")
17 | @DisconfFile(filename = "redis.properties")
18 | public class JedisConfig {
19 |
20 | // 代表连接地址
21 | private String host;
22 |
23 | // 代表连接port
24 | private int port;
25 |
26 | /**
27 | * 地址, 分布式文件配置
28 | *
29 | * @return
30 | */
31 | @DisconfFileItem(name = "redis.host", associateField = "host")
32 | public String getHost() {
33 | return host;
34 | }
35 |
36 | public void setHost(String host) {
37 | this.host = host;
38 | }
39 |
40 | /**
41 | * 端口, 分布式文件配置
42 | *
43 | * @return
44 | */
45 | @DisconfFileItem(name = "redis.port", associateField = "port")
46 | public int getPort() {
47 | return port;
48 | }
49 |
50 | public void setPort(int port) {
51 | this.port = port;
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/disconf-standalone-dubbo-demo/src/main/java/com/example/disconf/demo/config/RemoteServerConfig.java:
--------------------------------------------------------------------------------
1 | package com.example.disconf.demo.config;
2 |
3 | import org.springframework.stereotype.Service;
4 |
5 | import com.baidu.disconf.client.common.annotations.DisconfFile;
6 | import com.baidu.disconf.client.common.annotations.DisconfFileItem;
7 |
8 | /**
9 | *
10 | * @author liaoqiqi
11 | * @version 2014-6-17
12 | */
13 | @Service
14 | @DisconfFile(filename = "remote.properties")
15 | public class RemoteServerConfig {
16 |
17 | // 代表连接地址
18 | private String remoteHost;
19 |
20 | // 代表连接port
21 | private int remotePort = 8080;
22 |
23 | /**
24 | * 地址, 分布式文件配置
25 | *
26 | * @return
27 | */
28 | @DisconfFileItem(name = "remoteHost")
29 | public String getRemoteHost() {
30 | return remoteHost;
31 | }
32 |
33 | public void setRemoteHost(String remoteHost) {
34 | this.remoteHost = remoteHost;
35 | }
36 |
37 | /**
38 | * 端口, 分布式文件配置
39 | *
40 | * @return
41 | */
42 | @DisconfFileItem(name = "remotePort")
43 | public int getRemotePort() {
44 | return remotePort;
45 | }
46 |
47 | public void setRemotePort(int remotePort) {
48 | this.remotePort = remotePort;
49 | }
50 |
51 | }
52 |
--------------------------------------------------------------------------------
/disconf-standalone-dubbo-demo/src/main/java/com/example/disconf/demo/config/StaticConfig.java:
--------------------------------------------------------------------------------
1 | package com.example.disconf.demo.config;
2 |
3 | import com.baidu.disconf.client.common.annotations.DisconfFile;
4 | import com.baidu.disconf.client.common.annotations.DisconfFileItem;
5 |
6 | /**
7 | * 静态 配置文件 示例
8 | *
9 | * @author liaoqiqi
10 | * @version 2014-6-17
11 | */
12 | @DisconfFile(filename = "static.properties")
13 | public class StaticConfig {
14 |
15 | private static int staticVar;
16 |
17 | @DisconfFileItem(name = "staticVar", associateField = "staticVar")
18 | public static int getStaticVar() {
19 | return staticVar;
20 | }
21 |
22 | public static void setStaticVar(int staticVar) {
23 | StaticConfig.staticVar = staticVar;
24 | }
25 |
26 | }
27 |
--------------------------------------------------------------------------------
/disconf-standalone-dubbo-demo/src/main/java/com/example/disconf/demo/dubbo/consumer/Consumer.java:
--------------------------------------------------------------------------------
1 | package com.example.disconf.demo.dubbo.consumer;
2 |
3 | import org.springframework.context.support.ClassPathXmlApplicationContext;
4 |
5 | import com.example.disconf.demo.dubbo.service.DubboService;
6 |
7 | public class Consumer {
8 |
9 | public static void main(String[] args) throws Exception {
10 |
11 | ClassPathXmlApplicationContext context =
12 | new ClassPathXmlApplicationContext(new String[] { "dubbo-consumer.xml" });
13 |
14 | context.start();
15 |
16 | DubboService demoService = (DubboService) context.getBean("dubboService"); // 获取远程服务代理
17 |
18 | for (int i = 0; i < 100; ++i) {
19 |
20 | String hello = demoService.printWord("Dubbo Client: " + i); // 执行远程方法
21 |
22 | System.out.println(hello); // 显示调用结果
23 |
24 | Thread.sleep(1000);
25 | }
26 | }
27 |
28 | }
29 |
--------------------------------------------------------------------------------
/disconf-standalone-dubbo-demo/src/main/java/com/example/disconf/demo/dubbo/service/DubboService.java:
--------------------------------------------------------------------------------
1 | package com.example.disconf.demo.dubbo.service;
2 |
3 | public interface DubboService {
4 |
5 | public String printWord(String word);
6 |
7 | }
--------------------------------------------------------------------------------
/disconf-standalone-dubbo-demo/src/main/java/com/example/disconf/demo/dubbo/service/impl/DubboServiceImpl.java:
--------------------------------------------------------------------------------
1 | package com.example.disconf.demo.dubbo.service.impl;
2 |
3 | import java.text.SimpleDateFormat;
4 | import java.util.Date;
5 |
6 | import com.example.disconf.demo.dubbo.service.DubboService;
7 |
8 | public class DubboServiceImpl implements DubboService {
9 |
10 | public String printWord(String word) {
11 |
12 | String outWord = new SimpleDateFormat("[yyyy-MM-dd HH:mm:ss ]").format(new Date()) + word;
13 | System.out.println("Dubbo Server: " + outWord);
14 | return outWord;
15 | }
16 |
17 | }
--------------------------------------------------------------------------------
/disconf-standalone-dubbo-demo/src/main/java/com/example/disconf/demo/service/BaoBaoService.java:
--------------------------------------------------------------------------------
1 | package com.example.disconf.demo.service;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 | import org.springframework.beans.factory.annotation.Autowired;
6 | import org.springframework.beans.factory.annotation.Value;
7 | import org.springframework.stereotype.Service;
8 |
9 | import com.baidu.disconf.client.common.annotations.DisconfItem;
10 | import com.example.disconf.demo.config.Coefficients;
11 |
12 | /**
13 | * 金融宝服务,计算一天赚多少钱
14 | *
15 | * @author liaoqiqi
16 | * @version 2014-5-16
17 | */
18 | @Service
19 | public class BaoBaoService {
20 |
21 | protected static final Logger LOGGER = LoggerFactory.getLogger(BaoBaoService.class);
22 |
23 | public static final String key = "moneyInvest";
24 |
25 | @Value(value = "2000d")
26 | private Double moneyInvest;
27 |
28 | @Autowired
29 | private Coefficients coefficients;
30 |
31 | /**
32 | * 计算百发一天赚多少钱
33 | *
34 | * @return
35 | */
36 | public double calcBaiFa() {
37 | return coefficients.getBaiFaCoe() * coefficients.getDiscount() * getMoneyInvest();
38 | }
39 |
40 | /**
41 | * k 计算余额宝一天赚多少钱
42 | *
43 | * @return
44 | */
45 | public double calcYuErBao() {
46 | return coefficients.getYuErBaoCoe() * coefficients.getDiscount() * getMoneyInvest();
47 | }
48 |
49 | /**
50 | * 投资的钱,分布式配置
51 | *
52 | * 这里切面无法生效,因为SpringAOP不支持。
53 | * 但是这里还是正确的,因为我们会将值注入到Bean的值里.
54 | *
55 | * @return
56 | */
57 | @DisconfItem(key = key)
58 | public Double getMoneyInvest() {
59 | return moneyInvest;
60 | }
61 |
62 | public void setMoneyInvest(Double moneyInvest) {
63 | this.moneyInvest = moneyInvest;
64 | }
65 |
66 | }
67 |
--------------------------------------------------------------------------------
/disconf-standalone-dubbo-demo/src/main/java/com/example/disconf/demo/service/RemoteService.java:
--------------------------------------------------------------------------------
1 | package com.example.disconf.demo.service;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 |
6 | import org.slf4j.Logger;
7 | import org.slf4j.LoggerFactory;
8 | import org.springframework.beans.factory.DisposableBean;
9 | import org.springframework.beans.factory.InitializingBean;
10 | import org.springframework.beans.factory.annotation.Autowired;
11 | import org.springframework.stereotype.Service;
12 |
13 | import com.example.disconf.demo.config.RemoteServerConfig;
14 |
15 | /**
16 | * 一个未知远程服务, 这里也不使用注解的 @Service
17 | *
18 | * @author liaoqiqi
19 | * @version 2014-6-17
20 | */
21 | @Service
22 | public class RemoteService implements InitializingBean, DisposableBean {
23 |
24 | protected static final Logger LOGGER = LoggerFactory.getLogger(RemoteService.class);
25 |
26 | private List list = new ArrayList();
27 |
28 | @Autowired
29 | private RemoteServerConfig remoteServerConfig;
30 |
31 | public void destroy() throws Exception {
32 |
33 | }
34 |
35 | public void afterPropertiesSet() throws Exception {
36 |
37 | list.add(remoteServerConfig.getRemoteHost());
38 | list.add(String.valueOf(remoteServerConfig.getRemoteHost()));
39 | }
40 |
41 | /**
42 | * 更改Jedis
43 | */
44 | public void reload() {
45 |
46 | LOGGER.info("start to reload remote service to: " + remoteServerConfig.getRemoteHost() + " : "
47 | + remoteServerConfig.getRemoteHost());
48 |
49 | list.add(remoteServerConfig.getRemoteHost());
50 | list.add(String.valueOf(remoteServerConfig.getRemoteHost()));
51 |
52 | LOGGER.info("reload ok.");
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/disconf-standalone-dubbo-demo/src/main/java/com/example/disconf/demo/service/RemoteServiceUpdateCallback.java:
--------------------------------------------------------------------------------
1 | package com.example.disconf.demo.service;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 | import org.springframework.beans.factory.annotation.Autowired;
6 | import org.springframework.stereotype.Service;
7 |
8 | import com.baidu.disconf.client.common.annotations.DisconfUpdateService;
9 | import com.baidu.disconf.client.common.update.IDisconfUpdate;
10 | import com.example.disconf.demo.config.RemoteServerConfig;
11 |
12 | /**
13 | * 这是RemoteService的回调函数类,这里不使用 @Service 进行注解
14 | *
15 | * @author liaoqiqi
16 | * @version 2014-6-17
17 | */
18 | @Service
19 | @DisconfUpdateService(classes = { RemoteServerConfig.class })
20 | public class RemoteServiceUpdateCallback implements IDisconfUpdate {
21 |
22 | protected static final Logger LOGGER = LoggerFactory.getLogger(RemoteServiceUpdateCallback.class);
23 |
24 | @Autowired
25 | private RemoteService remoteService;
26 |
27 | /**
28 | *
29 | */
30 | public void reload() throws Exception {
31 |
32 | remoteService.reload();
33 | }
34 |
35 | }
36 |
--------------------------------------------------------------------------------
/disconf-standalone-dubbo-demo/src/main/java/com/example/disconf/demo/service/SimpleRedisService.java:
--------------------------------------------------------------------------------
1 | package com.example.disconf.demo.service;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 | import org.springframework.beans.factory.DisposableBean;
6 | import org.springframework.beans.factory.InitializingBean;
7 | import org.springframework.beans.factory.annotation.Autowired;
8 | import org.springframework.context.annotation.Scope;
9 | import org.springframework.stereotype.Service;
10 |
11 | import redis.clients.jedis.Jedis;
12 |
13 | import com.example.disconf.demo.config.JedisConfig;
14 | import com.example.disconf.demo.utils.JedisUtil;
15 |
16 | /**
17 | * 一个简单的Redis服务
18 | *
19 | * @author liaoqiqi
20 | * @version 2014-6-17
21 | */
22 | @Service
23 | @Scope("singleton")
24 | public class SimpleRedisService implements InitializingBean, DisposableBean {
25 |
26 | protected static final Logger LOGGER = LoggerFactory.getLogger(SimpleRedisService.class);
27 |
28 | // jedis 实例
29 | private Jedis jedis = null;
30 |
31 | /**
32 | * 分布式配置
33 | */
34 | @Autowired
35 | private JedisConfig jedisConfig;
36 |
37 | /**
38 | * 关闭
39 | */
40 | public void destroy() throws Exception {
41 |
42 | if (jedis != null) {
43 | jedis.disconnect();
44 | }
45 | }
46 |
47 | /**
48 | * 进行连接
49 | */
50 | public void afterPropertiesSet() throws Exception {
51 |
52 | jedis = JedisUtil.createJedis(jedisConfig.getHost(), jedisConfig.getPort());
53 | }
54 |
55 | /**
56 | * 获取一个值
57 | *
58 | * @param key
59 | * @return
60 | */
61 | public String getKey(String key) {
62 | if (jedis != null) {
63 | return jedis.get(key);
64 | }
65 |
66 | return null;
67 | }
68 |
69 | /**
70 | * 更改Jedis
71 | */
72 | public void changeJedis() {
73 |
74 | LOGGER.info("start to change jedis hosts to: " + jedisConfig.getHost() + " : " + jedisConfig.getPort());
75 |
76 | jedis = JedisUtil.createJedis(jedisConfig.getHost(), jedisConfig.getPort());
77 |
78 | LOGGER.info("change ok.");
79 | }
80 | }
81 |
--------------------------------------------------------------------------------
/disconf-standalone-dubbo-demo/src/main/java/com/example/disconf/demo/service/SimpleRedisServiceUpdateCallback.java:
--------------------------------------------------------------------------------
1 | package com.example.disconf.demo.service;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 | import org.springframework.beans.factory.annotation.Autowired;
6 | import org.springframework.context.annotation.Scope;
7 | import org.springframework.stereotype.Service;
8 |
9 | import com.baidu.disconf.client.common.annotations.DisconfUpdateService;
10 | import com.baidu.disconf.client.common.update.IDisconfUpdate;
11 | import com.example.disconf.demo.config.Coefficients;
12 | import com.example.disconf.demo.config.JedisConfig;
13 |
14 | /**
15 | * 更新Redis配置时的回调函数
16 | *
17 | * @author liaoqiqi
18 | * @version 2014-6-17
19 | */
20 | @Service
21 | @Scope("singleton")
22 | @DisconfUpdateService(classes = { JedisConfig.class }, itemKeys = { Coefficients.key })
23 | public class SimpleRedisServiceUpdateCallback implements IDisconfUpdate {
24 |
25 | protected static final Logger LOGGER = LoggerFactory.getLogger(SimpleRedisServiceUpdateCallback.class);
26 |
27 | @Autowired
28 | private SimpleRedisService simpleRedisService;
29 |
30 | /**
31 | *
32 | */
33 | public void reload() throws Exception {
34 |
35 | simpleRedisService.changeJedis();
36 | }
37 |
38 | }
39 |
--------------------------------------------------------------------------------
/disconf-standalone-dubbo-demo/src/main/java/com/example/disconf/demo/service/SimpleStaticService.java:
--------------------------------------------------------------------------------
1 | package com.example.disconf.demo.service;
2 |
3 | import com.baidu.disconf.client.common.annotations.DisconfItem;
4 | import com.example.disconf.demo.config.StaticConfig;
5 |
6 | /**
7 | * 使用静态配置文件的示例
8 | * Plus
9 | * 静态配置项 使用示例
10 | *
11 | * @author liaoqiqi
12 | * @version 2014-8-14
13 | */
14 | public class SimpleStaticService {
15 |
16 | private static int staticItem = 56;
17 |
18 | /**
19 | *
20 | * @return
21 | */
22 | public static int getStaticFileData() {
23 |
24 | return StaticConfig.getStaticVar();
25 | }
26 |
27 | @DisconfItem(key = "staticItem")
28 | public static int getStaticItem() {
29 | return staticItem;
30 | }
31 |
32 | public static void setStaticItem(int staticItem) {
33 | SimpleStaticService.staticItem = staticItem;
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/disconf-standalone-dubbo-demo/src/main/java/com/example/disconf/demo/task/DisconfDemoTask.java:
--------------------------------------------------------------------------------
1 | package com.example.disconf.demo.task;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 | import org.springframework.beans.factory.annotation.Autowired;
6 | import org.springframework.stereotype.Service;
7 |
8 | import com.example.disconf.demo.config.JedisConfig;
9 | import com.example.disconf.demo.service.BaoBaoService;
10 | import com.example.disconf.demo.service.SimpleRedisService;
11 | import com.example.disconf.demo.service.SimpleStaticService;
12 |
13 | /**
14 | * 演示分布式配置文件、分布式配置的更新Demo
15 | *
16 | * @author liaoqiqi
17 | * @version 2014-6-17
18 | */
19 | @Service
20 | public class DisconfDemoTask {
21 |
22 | protected static final Logger LOGGER = LoggerFactory.getLogger(DisconfDemoTask.class);
23 |
24 | @Autowired
25 | private BaoBaoService baoBaoService;
26 |
27 | @Autowired
28 | private SimpleRedisService simpleRedisService;
29 |
30 | @Autowired
31 | private JedisConfig jedisConfig;
32 |
33 | private static final String REDIS_KEY = "disconf_key";
34 |
35 | /**
36 | *
37 | */
38 | public int run() {
39 |
40 | try {
41 |
42 | while (true) {
43 |
44 | LOGGER.info("baobao--baifa: {}", baoBaoService.calcBaiFa());
45 | LOGGER.info("baobao--yuerbao: {}", baoBaoService.calcYuErBao());
46 |
47 | Thread.sleep(5000);
48 |
49 | // LOGGER.info("redis( " + jedisConfig.getHost() + ","
50 | // + jedisConfig.getPort() + ") get key: " + REDIS_KEY
51 | // + " , " + simpleRedisService.getKey(REDIS_KEY));
52 | LOGGER.info("redis( {} , {} )", jedisConfig.getHost(), jedisConfig.getPort());
53 |
54 | LOGGER.info("static file data: {}", SimpleStaticService.getStaticFileData());
55 |
56 | LOGGER.info("static item data: {} ", SimpleStaticService.getStaticItem());
57 |
58 | }
59 |
60 | } catch (Exception e) {
61 |
62 | LOGGER.error(e.toString(), e);
63 | }
64 |
65 | return 0;
66 | }
67 | }
68 |
--------------------------------------------------------------------------------
/disconf-standalone-dubbo-demo/src/main/java/com/example/disconf/demo/utils/JedisUtil.java:
--------------------------------------------------------------------------------
1 | package com.example.disconf.demo.utils;
2 |
3 | import org.apache.commons.lang.StringUtils;
4 |
5 | import redis.clients.jedis.Jedis;
6 |
7 | /**
8 | *
9 | * @author liaoqiqi
10 | * @version 2014-6-17
11 | */
12 | public class JedisUtil {
13 |
14 | public static Jedis createJedis() {
15 | Jedis jedis = new Jedis("127.0.0.1");
16 | return jedis;
17 | }
18 |
19 | public static Jedis createJedis(String host, int port) {
20 | Jedis jedis = new Jedis(host, port);
21 |
22 | return jedis;
23 | }
24 |
25 | public static Jedis createJedis(String host, int port, String passwrod) {
26 | Jedis jedis = new Jedis(host, port);
27 |
28 | if (!StringUtils.isNotBlank(passwrod))
29 | jedis.auth(passwrod);
30 |
31 | return jedis;
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/disconf-standalone-dubbo-demo/src/main/resources/applicationContext.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
12 |
13 |
14 |
15 |
16 |
17 |
19 |
20 |
21 |
23 |
24 |
25 |
27 |
28 |
29 |
30 |
31 |
32 |
33 | classpath*:/redis.properties
34 | classpath*:/coefficients.properties
35 | classpath*:/remote.properties
36 | classpath*:/static.properties
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
49 |
50 |
51 | myserver.properties
52 |
53 |
54 |
55 |
56 |
57 |
59 |
60 |
61 | myserver_slave.properties
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
--------------------------------------------------------------------------------
/disconf-standalone-dubbo-demo/src/main/resources/dubbo-consumer.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/disconf-standalone-dubbo-demo/src/main/resources/dubbo-provider.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
21 |
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
5 | 4.0.0
6 |
7 | com.baidu.disconf
8 | disconf-demos-java
9 | 2.0.0
10 | pom
11 |
12 |
13 |
14 | disconf-spring-boot-demo
15 | disconf-standalone-demo
16 | disconf-standalone-dubbo-demo
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 | org.apache.maven.plugins
26 | maven-checkstyle-plugin
27 | 2.9.1
28 |
29 |
30 |
31 |
32 | org.apache.maven.plugins
33 | maven-pmd-plugin
34 | 3.0.1
35 |
36 | true
37 | 1.6
38 |
39 |
40 |
41 |
42 |
43 | org.apache.maven.plugins
44 | maven-project-info-reports-plugin
45 | 2.7
46 |
47 |
48 |
49 | index
50 | summary
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 | org.apache.maven.plugins
59 | maven-surefire-report-plugin
60 | 2.5
61 |
62 | true
63 | -Dfile.encoding=UTF-8
64 |
65 |
66 |
67 |
68 |
69 | report-only
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 | org.codehaus.mojo
78 | cobertura-maven-plugin
79 | 2.6
80 |
81 | UTF-8
82 | true
83 |
84 | xml
85 | html
86 |
87 |
88 |
89 | **/*Proto.class
90 | **/*Proto$*.class
91 | **/*Test.class
92 | **/*Controller.class
93 | **/*Vo.class
94 | **/*Form.class
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 | org.codehaus.mojo
104 | findbugs-maven-plugin
105 | 2.5.2
106 |
107 | true
108 |
109 |
110 |
111 |
112 |
113 | org.apache.maven.plugins
114 | maven-release-plugin
115 | 2.5
116 |
117 |
118 |
119 |
120 |
121 |
--------------------------------------------------------------------------------