先说官方文档: http://disconf.readthedocs.io/zh_CN/latest/index.html
不管是否要根据官方文档来搭建 disconf, 都应该看一下这一份文档. 精炼清晰地介绍了 disconf 的功能和其他一些包含的东西. 看完至少对 disconf 已经有了大概的了解
我搭建的页面:
开始步骤吧:
其中安装 redis 和 nginx 稍微麻烦一些, 参考:
开始配置:
将你的配置文件放到此地址目录下(以下地址可自行设定):
- /home/work/dsp/disconf-rd/online-resources
我的路径与这个不同, 为了避免引起不必要的贴图错误, 按照官方的来
拷贝 / disconf-web/profile/rd / 目录下的文件,拷贝过去后修改即可。
配置文件包括
- - jdbc-mysql.properties (数据库配置)
- - redis-config.properties (Redis配置,主要用于web登录使用)
- - zoo.properties (Zookeeper配置)
- - application.properties (应用配置)
- cp application-demo.properties application.properties
注意,即使只有一个 redis,也应该配置两个 redis client,否则将造成内部错误。*
设置 War 包将要被部署的地址(以下地址可自行设定):
- /home/work/dsp/disconf-rd/war
- ONLINE_CONFIG_PATH=/home/work/dsp/disconf-rd/online-resources
- WAR_ROOT_PATH=/home/work/dsp/disconf-rd/war
- export ONLINE_CONFIG_PATH
- export WAR_ROOT_PATH
- cd disconf-web
- sh deploy/deploy.sh
这样会在 /home/work/dsp/disconf-rd/war 生成以下结果:
- -disconf-web.war
- -html
- -META-INF
- -WEB-INF
还有其他的配置文件也都会在这个目录下
初始化数据库:
可以参考 sql/readme.md 来进行数据库的初始化。注意顺序执行 0-init_table.sql 1-init_data.sql 201512/20151225.sql 20160701/20160701.sql里面默认有 6 个用户(请注意线上环境删除这些用户以避免潜在的安全问题)
name | pwd |
---|---|
admin | admin |
testUser1 | MhxzKhl9209 |
testUser2 | MhxzKhl167 |
testUser3 | MhxzKhl783 |
testUser4 | MhxzKhl8758 |
testUser5 | MhxzKhl112 |
如果想自己设置初始化的用户名信息,可以参考代码来自己生成用户:
- src/main/java/com/baidu/disconf/web/tools/UserCreateTools.java
修改 server.xml 文件,在 Host 结点下设定 Context:
- <Context path="" docBase="/home/work/dsp/disconf-rd/war">
- </Context>
并设置端口为 8015
启动 Tomcat,即可。
修改 nginx.conf
- upstream disconf {
- server 127.0.0.1 : 8015;
- }
- server {
- listen 8081;
- server_name disconf.com;
- access_log / home / work /
- var / logs / disconf / access.log;
- error_log / home / work /
- var / logs / disconf / error.log;
- location / {
- root / home / work / dsp / disconf - rd / war / html;
- if ($query_string) {
- expires max;
- }
- }
- location~ ^ /(api|export) {
- proxy_pass_header Server;
- proxy_set_header Host $http_host;
- proxy_redirect off;
- proxy_set_header X-Real-IP $remote_addr;
- proxy_set_header X-Scheme $scheme;
- proxy_pass http:/ / disconf;
- }
- }
这里的配置没有贴全, 有些人可能会配错. 我贴一份全的 (因为对 nginx 不熟悉, 我也遇到了一些小麻烦)
- user root;
- worker_processes 1;
- #error_log logs/error.log;
- #error_log logs/error.log notice;
- #error_log logs/error.log info;
- #pid logs/nginx.pid;
- events {
- worker_connections 1024;
- }
- http {
- include mime.types;
- default_type application/octet-stream;
- #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
- # '$status $body_bytes_sent "$http_referer" '
- # '"$http_user_agent" "$http_x_forwarded_for"';
- #access_log logs/access.log main;
- sendfile on;
- #tcp_nopush on;
- #keepalive_timeout 0;
- keepalive_timeout 65;
- gzip on;
- gzip_http_version 1.0;
- gzip_disable "MSIE [1-6].";
- gzip_types text/plain application/x-javascript text/CSS text/javascript application/x-httpd-php image/jpeg image/gif image/png;
- upstream disconf {
- server 10.10.10.10:8015;
- }
- server {
- listen 8081;
- server_name disconftest.com;
- access_log /home/work/dsp/access.log;
- error_log /home/work/dsp/error.log;
- location / {
- root /home/docker/garfield/war/html;
- if ($query_string) {
- expires max;
- }
- }
- location ~ ^/(api|export) {
- proxy_pass_header Server;
- proxy_set_header Host $http_host;
- proxy_redirect off;
- proxy_set_header X-Real-IP $remote_addr;
- proxy_set_header X-Scheme $scheme;
- proxy_pass http://disconf;
- }
- }
- }
这里我把 server name 改成了 disconftest.com, 跟 application.properties 里面的配置保持一样即可,
注意:
1. 须保证日志文件的写入有权限哦, 日志文件夹提前建好, 否则会报错
2. 开头是运行权限, 这边因为没有用 root 运行会导致页面持续被拦截, 报 403, 有遇到的人注意下
服务管理端的部署到这里就结束了, 启动 zookeeper,redis,tomcat,mysql 和 nginx 就可以访问页面了, 输入
- http: //10.10.10.10:8081/
看到如下页面就成功了:
输入 admin/admin 发现里面已经有一些配置, 但是没有机器在运行, 接下来进行客户端应用
其实 git 下载下来的 demo 直接运行就可以看到配置文件的下载了, 当然要配置访问的服务端地址. 这方面就不赘述了, 记录一下我的两个实践
1. 将 xml 下载到指定路径 -- 将 mybatis.xml 文件从平台上下载, 统一管理
先写一个简单的 mybatis 应用程序 (打印出数据库所有用户表名称), 主要类:
- @Service
- @Transactional
- public class UserService {
- private static Logger logger = LogManager.getLogger(UserService.class);
- @Autowired
- private UserDao userMapper;
- public List searchAll(){
- List list = userMapper.findAll();
- for (User user : list) {
- System.out.println(user.getUsername());
- }
- return list;
- }
- }
主函数:
- /**
- * @author garfield
- * @version 2017-2-17
- */
- public class DisconfDemoMain {
- private static String[] fn = null;
- // 初始化spring文档
- private static void contextInitialized() {
- fn = new String[] {
- "applicationContext.xml"
- };
- }
- /**
- * @param args
- *
- * @throws Exception
- */
- public static void main(String[] args) throws Exception {
- contextInitialized();
- ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(fn);
- UserService userService = ctx.getBean("userService", UserService.class);
- while (true) {
- userService.searchAll();
- Thread.sleep(10000);
- }
- }
- }
配置类:
- import com.baidu.disconf.client.common.annotations.DisconfFile;
- import com.baidu.disconf.client.common.annotations.DisconfUpdateService;
- import com.baidu.disconf.client.common.update.IDisconfUpdate;
- import org.springframework.context.annotation.Scope;
- import org.springframework.stereotype.Service;
- /**
- * Created by garfield on 2017/2/23.
- */
- @Service
- @Scope("singleton")
- @DisconfFile(filename = "UserMapper.xml",targetDirPath = "cloud/simple/service/dao")
- @DisconfUpdateService(classes = {XMLConfig.class})
- public class XMLConfig implements IDisconfUpdate {
- public void reload() throws Exception {
- System.out.println("=====================================================================");
- }
- }
targetDirPath 是配置下载到指定的路径中, 这种情况下, profile 里面的 disconf.enable_local_download_dir_in_class_path 配置项须置为 true, 运行后效果如下:
输出:
- 16:40:47.484 [main] DEBUG o.m.s.t.SpringManagedTransaction - JDBC Connection [ProxyConnection[PooledConnection[com.mysql.jdbc.JDBC4Connection@1f6f7075]]] will not be managed by Spring
- 16:40:47.489 [main] DEBUG c.simple.service.dao.UserDao.findAll - ==> Preparing: select * from user
- 16:40:47.513 [main] DEBUG c.simple.service.dao.UserDao.findAll - ==> Parameters:
- 16:40:47.532 [main] DEBUG c.simple.service.dao.UserDao.findAll - <== Total: 2
- 16:40:47.533 [main] DEBUG org.mybatis.spring.SqlSessionUtils - Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@7eadb544]
- username = 1
- username = 2
2. 第二个实例, 将 log4j2.xml 日志配置文件统一管理
这个例子其实关键在于手动加载日志, 本身实际上也是一个下载 xml 文件的过程
配置类:
- import com.baidu.disconf.client.common.annotations.DisconfFile;
- import com.baidu.disconf.client.common.annotations.DisconfUpdateService;
- import com.baidu.disconf.client.common.update.IDisconfUpdate;
- import org.springframework.context.annotation.Scope;
- import org.springframework.stereotype.Service;
- /**
- * Created by garfield on 2017/2/23.
- */
- @Service
- @Scope("singleton")
- @DisconfFile(filename = "log4j2.xml")
- @DisconfUpdateService(classes = {XMLConfig.class})
- public class LogConfig implements IDisconfUpdate {
- public void reload() throws Exception {
- System.out.println("=====================================================================");
- }
- }
下载下来之后 log4j.xml 文件的位置:(与工程同级)
在主函数做一个加载动作:
运行后查看一下日志文件是否生效:
- import cloud.simple.service.domain.UserService;
- import org.apache.logging.log4j.LogManager;
- import org.apache.logging.log4j.Logger;
- import org.apache.logging.log4j.core.config.Configurator;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import org.apache.logging.log4j.core.config.ConfigurationSource;
- import java.io.File;
- import java.io.FileInputStream;
- import java.net.URL;
- /**
- * @author garfield
- * @version 2017-2-17
- */
- public class DisconfDemoMain {
- private static String[] fn = null;
- private static Logger logger;
- // 初始化spring文档
- private static void contextInitialized() {
- fn = new String[] {
- "applicationContext.xml"
- };
- }
- /**
- * @param args
- *
- * @throws Exception
- */
- public static void main(String[] args) throws Exception {
- contextInitialized();
- ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(fn);
- //此处加载日志文件
- URL url = DisconfDemoMain.class.getResource("/log4j2.xml");
- System.out.println(url.getPath());
- ConfigurationSource source = new ConfigurationSource(new FileInputStream(new File(url.getPath())), url);
- Configurator.initialize(null, source);
- logger = LogManager.getLogger(DisconfDemoMain.class);
- UserService userService = ctx.getBean("userService", UserService.class);
- logger.info("begin to search...");
- while (true) {
- userService.searchAll();
- Thread.sleep(10000);
- }
- }
- }
- 2017 - 02 - 24 16 : 46 : 01,
- 467 INFO main(cloud.simple.service.DisconfDemoMain) - begin to search...2017 - 02 - 24 16 : 46 : 01,
- 820 INFO main(cloud.simple.service.domain.UserService) - =========================2017 - 02 - 24 16 : 46 : 01,
- 820 INFO main(cloud.simple.service.domain.UserService) - username = 1 2017 - 02 - 24 16 : 46 : 01,
- 820 INFO main(cloud.simple.service.domain.UserService) - =========================2017 - 02 - 24 16 : 46 : 01,
- 820 INFO main(cloud.simple.service.domain.UserService) - username = 2
demo 中还有一些其他的例子, 不过看文档大概也能明白, 就不说明了.
done!
来源: http://www.cnblogs.com/garfieldcgf/p/6439221.html