1、需要一台服务器(阿里云,腾讯云、实体服务器都行,本次是以实体服务器为依照做的)
2、安装docker https://www.cnblogs.com/shijunjie/p/10436293.html
3、开始配置disconf
第一步:环境准备
要使用disconf,肯定是开发环境需求,因此需要 mysql 和 redis
上云的小伙伴可以直接购买云上的服务,比如阿里云的RDS和redis
没有上云的小伙伴可以在本地自己搭建,也可以使用docker搭建,但总之需要和disconf分开来。
这里就不介绍mysql和redis的搭建了,小伙伴可以自己上网查找
1、服务器
2、docker
3、mysql
4、redis
第二步:选择指定镜像版本centos 7.4 (版本不限制,看自己的喜好)
https://hub.docker.com/search/?q=&type=image docker 官方下载镜像的地址,主要是需要找到centos7.4在docker中的具体名称
docker pull centos:7.4.1708 #下载公共镜像 docker images #查看已有镜像
第三步:以centos:7.4.1708 镜像 为模板创建容器
docker run -dit \
-p 8085:8085 -p 2181:2181 \ -v /usr/local/centos_data:/usr/local/centos_data \ -v /etc/localtime:/etc/localtime:ro \
--restart=always \ --name centos01 \ centos:7.4.1708
解释:
-p 8085:8085 -p 2181:2181 主要是为了后面弄好之后可以直接测试访问
-v /usr/local/centos_data:/usr/local/centos_data 主要是需要有一个传输安装包的共享目录
-v /etc/localtime:/etc/localtime:ro 容器和宿主机的时间同步
--restart=always 容器随机自启动
第四步:进入容器centos,然后安装 nginx ,tomcat ,jdk、zookeeper、mysql客户端、redis客户端
共享目录:
/usr/local/centos_data
nginx安装:
wget http://nginx.org/download/nginx-1.14.2.tar.gz ## 下载nginx安装包的命令
yum -y install pcre-devel openssl openssl-devel gcc gcc-c++ #安装依赖包
./configure --prefix=/usr/local/nginx #编译安装,指定安装位置
make
make install
启动并测试,将监听的端口改为8085
curl http://127.0.0.1:8085
tomcat和jdk安装:
##下载jdk安装包
wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" https://download.oracle.com/otn-pub/java/jdk/8u201-b09/42970487e3af4f5aa5bca3f542482c60/jdk-8u201-linux-x64.tar.gz##下载tomcat安装包,链接可能会失效,因为版本更新
wget https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-8/v8.5.39/bin/apache-tomcat-8.5.39.tar.gz##安装jdk:
[root@a0a2568d002d centos_data]# mkdir /usr/java [root@a0a2568d002d centos_data]# tar -zxvf jdk-8u201-linux-x64.tar.gz -C /usr/java [root@a0a2568d002d centos_data]# vi /etc/profile export JAVA_HOME=/usr/java/jdk1.8.0_201 export JRE_HOME=${JAVA_HOME}/jre export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib export PATH=${JAVA_HOME}/bin:$PATH [root@a0a2568d002d centos_data]# vi /etc/profile [root@a0a2568d002d centos_data]# source /etc/profile [root@a0a2568d002d centos_data]# java -version java version "1.8.0_201" Java(TM) SE Runtime Environment (build 1.8.0_201-b09) Java HotSpot(TM) 64-Bit Server VM (build 25.201-b09, mixed mode)##安装tomcat
[root@a0a2568d002d centos_data]# mkdir /usr/local/tomcat [root@a0a2568d002d centos_data]# tar -xf apache-tomcat-8.5.39.tar.gz -C /usr/local/tomcat [root@a0a2568d002d centos_data]# vi /usr/local/tomcat/apache-tomcat-8.5.39/bin/setenv.shJAVA_OPTS="-Xms1024m -Xmx1024m -Xss1024K -XX:MetaspaceSize=64m -XX:MaxMetaspaceSize=128m"
[root@a0a2568d002d centos_data]# chmod +x /usr/local/tomcat/apache-tomcat-8.5.39/bin/setenv.sh
##修改监听端口为8015,然后启动测试[root@a0a2568d002d centos_data]# curl http://127.0.0.1:8015
zookeeper安装:
参考文档: https://www.cnblogs.com/leocook/p/zk_0.html
官网地址: http://zookeeper.apache.org/
镜像地址: http://mirror.bit.edu.cn/apache/zookeeper/
下载zookeeper:
wget https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/zookeeper-3.4.13/zookeeper-3.4.13.tar.gz ##链接会过期
单独的zookeeeper需要下载安装java,这里已经有jdk了,因此不需要。
[root@a0a2568d002d centos_data]# mkdir /usr/local/zookeeper [root@a0a2568d002d centos_data]# tar -xf zookeeper-3.4.13.tar.gz -C /usr/local/zookeeper解压后在zookeeper的conf目录下创建配置文件zoo.cfg,里面的配置信息可参考统计目录下的zoo_sample.cfg文件,我们这里配置为:
[root@a0a2568d002d conf]# cd /usr/local/zookeeper/zookeeper-3.4.13/conf/ [root@a0a2568d002d conf]# vi zoo.cfg tickTime=2000 initLimit=10 syncLimit=5 dataDir=/opt/zookeeper-data/ clientPort=2181tickTime:指定了ZooKeeper的基本时间单位(以毫秒为单位);
initLimit:指定了启动zookeeper时,zookeeper实例中的随从实例同步到领导实例的初始化连接时间限制,超出时间限制则连接失败(以tickTime为时间单位);
syncLimit:指定了zookeeper正常运行时,主从节点之间同步数据的时间限制,若超过这个时间限制,那么随从实例将会被丢弃;
dataDir:zookeeper存放数据的目录;
clientPort:用于连接客户端的端口。
启动:
[root@a0a2568d002d conf]# cd /usr/local/zookeeper/zookeeper-3.4.13/bin/ [root@a0a2568d002d bin]# ./zkServer.sh start ZooKeeper JMX enabled by default Using config: /usr/local/zookeeper/zookeeper-3.4.13/bin/../conf/zoo.cfg Starting zookeeper ... STARTED检查ZooKeeper是否正在运行
[root@a0a2568d002d bin]#yum -y install nc
[root@a0a2568d002d bin]# echo ruok | nc localhost 2181 ##若是正常运行的话会打印“imok”。
imok
mysql客户端安装:
下载安装包:
wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.24-1.el7.x86_64.rpm-bundle.tar解压:
[root@a0a2568d002d centos_data]# mkdir mysql [root@a0a2568d002d centos_data]# tar -xf mysql-5.7.24-1.el7.x86_64.rpm-bundle.tar -C mysql [root@a0a2568d002d centos_data]# ls mysql mysql-community-client-5.7.24-1.el7.x86_64.rpm mysql-community-embedded-compat-5.7.24-1.el7.x86_64.rpm mysql-community-minimal-debuginfo-5.7.24-1.el7.x86_64.rpm mysql-community-common-5.7.24-1.el7.x86_64.rpm mysql-community-embedded-devel-5.7.24-1.el7.x86_64.rpm mysql-community-server-5.7.24-1.el7.x86_64.rpm mysql-community-devel-5.7.24-1.el7.x86_64.rpm mysql-community-libs-5.7.24-1.el7.x86_64.rpm mysql-community-server-minimal-5.7.24-1.el7.x86_64.rpm mysql-community-embedded-5.7.24-1.el7.x86_64.rpm mysql-community-libs-compat-5.7.24-1.el7.x86_64.rpm mysql-community-test-5.7.24-1.el7.x86_64.rpm安装:
[root@a0a2568d002d mysql]# rpm -ivh mysql-community-common-5.7.24-1.el7.x86_64.rpm warning: mysql-community-common-5.7.24-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY Preparing... ################################# [100%] Updating / installing... 1:mysql-community-common-5.7.24-1.e################################# [100%] [root@a0a2568d002d mysql]# rpm -ivh mysql-community-libs-5.7.24-1.el7.x86_64.rpm warning: mysql-community-libs-5.7.24-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY Preparing... ################################# [100%] Updating / installing... 1:mysql-community-libs-5.7.24-1.el7################################# [100%] [root@a0a2568d002d mysql]# rpm -ivh mysql-community-client-5.7.24-1.el7.x86_64.rpm warning: mysql-community-client-5.7.24-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY Preparing... ################################# [100%] Updating / installing... 1:mysql-community-client-5.7.24-1.e################################# [100%]
测试:
用来测试连接已经准备好的mysql库:
redis客户端安装:
下载安装包:
wget http://download.redis.io/releases/redis-4.0.0.tar.gz安装:
[root@a0a2568d002d centos_data]# mkdir /usr/local/redis [root@a0a2568d002d centos_data]# tar -xf redis-4.0.0.tar.gz /usr/local/redis
[root@a0a2568d002d redis]# cd /usr/local/redis/redis-4.0.0/
[root@a0a2568d002d redis-4.0.0]# make
[root@a0a2568d002d redis-4.0.0]# make install链接远程redis进行测试:
[root@a0a2568d002d ~]# redis-cli -h 192.168.2.87 -p 6379 192.168.2.87:6379> AUTH "a123" OK 192.168.2.87:6379> ping PONG 192.168.2.87:6379>
第五步:最后创建disconf
https://blog.csdn.net/fengyao1995/article/details/66491226 参考文档
https://github.com/knightliao/disconf/tree/master/disconf-web/profile/rd git项目地址
构建:
Disconf进行构建时需要maven命令,所以需要安装Maven。
安装Maven的命令:
[root@a0a2568d002d ~]# yum -y install wget [root@a0a2568d002d ~]# wget http://repos.fedorapeople.org/repos/dchen/apache-maven/epel-apache-maven.repo -O /etc/yum.repos.d/epel-apache-maven.repo [root@a0a2568d002d ~]# yum -y install apache-maven配置文件所在路径: /usr/local/disconf/resource 配置源码所在路径:/usr/local/disconf/src 配置War包所在路径:/usr/local/disconf/war[root@a0a2568d002d ~]# mkdir -p /usr/local/disconf/{resource,src,war} [root@a0a2568d002d ~]# ls /usr/local/disconf/ resource src war
进入到disconf/src下,在GitHub上面直接下载源码,利用下面的命令:
[root@a0a2568d002d ~]# cd /usr/local/disconf/src/ [root@a0a2568d002d src]# yum -y install git
[root@a0a2568d002d src]# git clone https://github.com/knightliao/disconf.git
将/usr/local/disconf/src/disconf/disconf-web/profile/rd 路径下的配置文件拷贝到 /usr/local/disconf/resource(要将application-demo.properties文件改名为application.properties),并修改相应的配置文件。
[root@a0a2568d002d rd]# cp /usr/local/disconf/src/disconf/disconf-web/profile/rd/* /usr/local/disconf/resource/ [root@a0a2568d002d rd]# cd /usr/local/disconf/resource/ [root@a0a2568d002d resource]# mv application-demo.properties application.properties
修改相应的4个配置文件(application.properties 、jdbc-mysql.properties 、 redis-config.properties 、zoo.properties)
vi application.properties
![]()
vi jdbc-mysql.properties
![]()
vi redis-config.properties
![]()
修改相应的host和port,如果redis是集群,可配置相应的节点;如果redis是单机版,也必须配置两个redis-client(否则会 报错),两个redis-client配置相同的host和port即可。
vi zoo.properties
![]()
修改环境变量,将创建的配置文件的路径和war包的路径配置在环境变量中。
[root@a0a2568d002d resource]# vi /etc/profile
ONLINE_CONFIG_PATH=/usr/local/disconf/resource WAR_ROOT_PATH=/usr/local/disconf/war export ONLINE_CONFIG_PATH export WAR_ROOT_PATH[root@a0a2568d002d resource]# source /etc/profile
进入到disconf-web路径下,修改其pom文件,将以下内容加入到pom文件的profiles结点下,如果JDK的版本低于1.8,则此步骤不需要执行:
<profile> <id>doclint-java8-disable</id> <activation> <jdk>[1.8,)</jdk> </activation> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-javadoc-plugin</artifactId> <configuration> <additionalparam>-Xdoclint:none</additionalparam> </configuration> </plugin> </plugins> </build> </profile>
[root@a0a2568d002d resource]# cd /usr/local/disconf/src/disconf/disconf-web [root@a0a2568d002d disconf-web]# ls README.md bin deploy html pom.xml profile sql src [root@a0a2568d002d disconf-web]# vi pom.xml![]()
注意:在这个配置文件中会发现有乱码的情况,这是因为docker安装的centos系统不识别中文
解决方法:
https://www.cnblogs.com/shijunjie/p/10436575.html
开始构建:
利用deploy/deploy.sh进行构建,构建过程会download很多东西,可能会有些慢,虚拟机要一直保持能上网的状态:
[root@a0a2568d002d disconf-web]# cd /usr/local/disconf/src/disconf/disconf-web/ [root@a0a2568d002d disconf-web]# sh deploy/deploy.sh
构建完成后,在war目录下会生成相应的内容
修改相应的配置文件:
配置Tomcat 将构建时打成的war包部署到Tomcat中,需要修改Tomcat的server.xml文件,在Host节点下设定Context:
[root@544cefc71c42 disconf]# vi /usr/local/tomcat1/apache-tomcat-8.5.37/conf/server.xml<Context path="" docBase="/usr/local/disconf/war"></Context>
配置nginx
修改nginx.conf文件,将下面的内容添加进去:
http { 。。。 upstream disconf { server 127.0.0.1:8015;#Tomcat的IP和端口号,这里改成了8015 } server { listen 8085; #nginx监听的端口号 server_name localhost; #可自行定义,但必须与application.properties中的domain属性的值一样 access_log logs/disconf/access.log; error_log logs/disconf/error.log; location / { root /usr/local/disconf/war/html; #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; } } }
初始化数据库:
在/usr/local/disconf/src/disconf/disconf-web/sql目录下有sql文件,将这些sql文件初始化到数据库中。
这四个sql文件的执行是有顺序的,按照sql/readme.md文件中的顺序执行。
cd /usr/local/disconf/src/disconf/disconf-web/sql
mysql -h192.168.2.87 -uroot -p'Aa@123456' < 0-init_table.sql 创建disconf库和表
mysql -h192.168.2.87 -uroot -p'Aa@123456' disconf < 1-init_data.sql 创建数据
mysql -h192.168.2.87 -uroot -p'Aa@123456' disconf < 201512/20151225.sql 创建path
mysql -h192.168.2.87 -uroot -p'Aa@123456' disconf < 20160701/20160701.sql 创建path
里面默认有6个用户:
name pwd admin admin testUser1 MhxzKhl9209 testUser2 MhxzKhl167 testUser3 MhxzKhl783 testUser4 MhxzKhl8758 testUser5 MhxzKhl112
登录测试:
第六步,准备做成镜像并启动容器:
1、先重启容器
[root@xiangdong centos_data]# docker restart centos [root@xiangdong centos_data]# docker exec -it centos /bin/bash
2、查看环境变量是否生效
1、java
[root@a0a2568d002d /]# java -version openjdk version "1.8.0_201" OpenJDK Runtime Environment (build 1.8.0_201-b09) OpenJDK 64-Bit Server VM (build 25.201-b09, mixed mode)
2、中文识别问题是否解决![]()
3、做成镜像
[root@xiangdong centos_data]# docker commit centos disconf:v3.0
4、然后以此镜像创建新的容器,删除旧的容器,查看是否能访问disconf
docker run -dit \
-p 8085:8085 -p 2181:2181 \
-v /usr/local/centos_data:/usr/local/centos_data \
-v /etc/localtime:/etc/localtime:ro \
--restart=always \
--name disconf01 \
disconf:v3.0
第七步:设置程序随容器自启动(这一步也可以放在第六步之前)
经过试验得出结论:
需要删除旧的容器,然后启动新的容器时加上新的参数才能设置开启自启:
docker run -dit \ -p 8085:8085 -p 2181:2181 \ -v /usr/local/centos_data:/usr/local/centos_data \ -v /etc/localtime:/etc/localtime:ro \ --restart=always \ --privileged=true \ -e "container=docker" \ --name disconf01 \ disconf:v3.0 \ /usr/sbin/init
nginx自启动设置
进入容器配置nginx随机自启:
[root@ecaea7d3388c /]# cd /lib/systemd/system/ [root@ecaea7d3388c system]# vi nginx.service
[Unit]
Description=nginx service
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target[root@ecaea7d3388c system]# systemctl enable nginx
最后重启容器测试验证
学习:
[Unit]:服务的说明 Description:描述服务 After:描述服务类别 [Service]服务运行参数的设置 Type=forking是后台运行的形式 ExecStart为服务的具体运行命令 ExecReload为重启命令 ExecStop为停止命令 PrivateTmp=True表示给服务分配独立的临时空间 注意:[Service]的启动、重启、停止命令全部要求使用绝对路径 [Install]运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3# systemctl start nginx.service 启动nginx服务
# systemctl stop nginx.service 停止服务
# systemctl restart nginx.service 重新启动服务
# systemctl list-units --type=service 查看所有已启动的服务
# systemctl status nginx.service 查看服务当前状态
# systemctl enable nginx.service 设置开机自启动
# systemctl disable nginx.service 停止开机自启动
一个常见的错误
Warning: nginx.service changed on disk. Run 'systemctl daemon-reload' to reload units.
直接按照提示执行命令systemctl daemon-reload 即可。
tomcat自启动
[root@ecaea7d3388c /]# vi /lib/systemd/system/tomcat.service[Unit]
Description=tomcat
After=network.target[Service]
Type=oneshot
ExecStart=/usr/local/tomcat/apache-tomcat-8.5.39/bin/startup.sh
ExecStop=/usr/local/tomcat/apache-tomcat-8.5.39/bin/shutdown.sh
ExecReload=/bin/kill -s HUP $MAINPID
RemainAfterExit=yes[Install]
WantedBy=multi-user.target
[root@ecaea7d3388c /]# systemctl enable tomcat.service
[root@ecaea7d3388c /]# systemctl start tomcat.service
zookeeper自启动设置:
[root@ecaea7d3388c bin]# vi /lib/systemd/system/zookeeper.service[Unit]
Description=Zookeeper
After=network.target[Service]
Type=forking
User=root
Group=root
ExecStart=/usr/local/zookeeper/zookeeper-3.4.13/bin/zkServer.sh start
ExecStop=/usr/local/zookeeper/zookeeper-3.4.13/bin/zkServer.sh stop
PIDFile=/opt/zookeeper-data/zookeeper_server.pid
[Install]
WantedBy=multi-user.target
[root@ecaea7d3388c bin]# systemctl enable zookeeper.service
曾经遇到过的问题:
1、导入数据库的表有乱码的情况,原因:
可能 1、容器disconf内用了mariadb而不是mysql客户端,导致导入时出现了一些问题
可能 2、中文不识别,导致导入数据库数据时出现乱码情况
2、zk信息没有
主要配置文件 zoo.properties 需要修改和琢磨,并且需要真实的运行一个程序,需要开发配合导入一个
3、修改配置文件不生效
一共有3个目录下有这些配置文件:
4个配置文件: redis-config.properties、 zoo.properties、application.properties、 jdbc-mysql.properties
所在目录:
/usr/local/disconf/resource
/usr/local/disconf/war
/usr/local/disconf/war/WEB-INF/classes
比如说修改了/usr/local/disconf/war 目录下的zoo.properties文件,需要同时更新另外两个目录的zoo.properties文件
或者直接同步目录
cp /usr/local/disconf/war/{redis-config.properties,zoo.properties,application.properties,jdbc-mysql.properties} /usr/local/disconf/resource
cp /usr/local/disconf/war/{redis-config.properties,zoo.properties,application.properties,jdbc-mysql.properties} /usr/local/disconf/war/WEB-INF/classes
4、无法设置nginx开启自启
删除旧容器,重启容器时加上参数:
1、--privileged=true
2、-e "container=docker"
3、/usr/sbin/init