分布式配置管理--百度disconf搭建过程和详细使用
先说官方文档:http://disconf.readthedocs.io/zh_CN/latest/index.html
不管是否要根据官方文档来搭建disconf,都应该看一下这一份文档.精炼清晰地介绍了disconf的功能和其他一些包含的东西.看完至少对disconf已经有了大概的了解
- disconf: https://github.com/knightliao/disconf(管理端)
- demos: https://github.com/knightliao/disconf-demos-java(客户端)
- wiki: https://github.com/knightliao/disconf/wiki(wiki)
我搭建的页面:
开始步骤吧:
- 安装Mysql(Ver 14.12 Distrib 5.0.45, for unknown-linux-gnu (x86_64) using EditLine wrapper)
- 安装Tomcat(apache-tomcat-7.0.50)
- 安装Nginx(nginx/1.5.3)
- 安装 zookeeeper (zookeeper-3.3.0)
- 安装 Redis (2.4.5)
其中安装redis和nginx稍微麻烦一些,参考:
http://www.cnblogs.com/garfieldcgf/p/6438898.html
http://www.cnblogs.com/garfieldcgf/p/6438814.html
开始配置:
将你的配置文件放到此地址目录下(以下地址可自行设定):
/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
还有其他的配置文件也都会在这个目录下
上线前的初始化工作
初始化数据库:
里面默认有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
部署War
修改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<User> searchAll(){ List<User> 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!