Using disconf under Docker: Detailed demo development

Discof Full Text Link under Docker

  1. Docker Builds a disconf Environment, One of the Trilogy: Building a disconf at a Fast Speed
  2. Docker Builds a disconf Environment, Trilogy Two: Local Fast Building a disconf Mirror
  3. Docker Builds a disconf Environment, Trilogy 3: Detailed Construction Process
  4. Using disconf under Docker: Quick Experience
  5. Using disconf under Docker: Detailed demo Development

On demo Development

In the last chapter Using disconf under docker: Extreme Experience We have experienced the use of disconf services to obtain configuration and real-time perception of the configuration changes. Today we practice the demo development process and learn to use disconf services in projects.

This demo comes from the demo of disconf official website (git address: There are three projects. The demo of this article originates from disconf-standalone-demo. The official demo shows a large and complete function. As a beginner, I spent a lot of time looking at it, so I refined it based on the official code. Simplified, streamlined code is designed to quickly show beginners the most basic remote configuration and real-time awareness of configuration changes. If you want to learn the various services of disconf, you should also study the official documents and demo by yourself.

The source code of this demo is There are many projects in it after downloading. The project in the red box below is the demo of this article.

Next, let's look at the steps required to invoke the disconf service:

  1. Since this is a maven project, we first need to identify dependency packages. The main libraries we use are disconf libraries, spring libraries and log libraries.








maven's build plug-in is customized by the author. In addition to the basic functions, some scripting tools have been generated. We use them directly:

  1. The main method in the DisconfDemoMain class initializes the spring environment, starts a while dead cycle, and prints the host and port attributes of the JedisConfig instance to the terminal every five seconds.

  2. JedisConfig's host and port attributes are controlled by disconf's runtime environment, and can be set to the latest configuration values in real time. The corresponding remote configuration and specific property fields are accomplished through annotations:

As shown in the figure above:

The DisconfFile annotation in the red box binds this class to the configuration of the disconf server.
The DisconfFileItem annotation in the yellow box binds host and port attributes to the host and port attributes of configuration.
The DisconfUpdateService annotation in the purple box registers real-time broadcast listeners with configuration changes to the disconf runtime. When configuration changes, the reload method in the blue box will be called by the disconf runtime.

  1. The SimpleRedisService UpdateCallback class also registers broadcast listeners. When configuration changes, the reload method in the blue box will be called by disconf runtime.

  2. In addition to the code, focus on the file:

# Whether to use remote configuration files
# True (default) retrieves configuration false s remotely and gets local configuration directly

# Configure the server's HOST, separated by commas,

# Version, please use X_X_X format 

# APP in Product Line Service Name Format

# Environment disco

# Ignore which distributed configurations, separated by commas

# Get the number of remote configuration retries, default is 3
# Gets the sleep time for remote configuration retry by default of 5 seconds

# User-specified download folder, where remote files will be downloaded

# The downloaded files will be migrated to the classpath root path. It is strongly recommended that this option be set to true (default is true).

The value of conf_server_host is configured here as nginxhost, which corresponds to the link alias of nginx configured when the docker container starts, so that demo can access the disconf server.

  1. The running environment of disconf on demo is implemented by spring configuration, as follows:
<context:component-scan base-package="com.example"/>

    <aop:aspectj-autoproxy proxy-target-class="true"/>

    <!-- Use disconf The following configuration must be added -->
    <bean id="disconfMgrBean" class=""
        <property name="scanPackage" value="com.example.disconf.demo"/>
    <bean id="disconfMgrBean2" class=""
          init-method="init" destroy-method="destroy">

DiscofMgrBean replication core management, disconfMgrBean2 is responsible for starting timing scanning and achieving callbacks when destroyed.

  1. If you configure these, open the console and execute MVN clean package-U in the directory where the pom.xml file is located, then you can complete the packaging. When you enter the target directory, you can see the results as follows:

  1. Open the file in the starter-run folder in the figure above, and the bottom line is as follows:
nohup java $JAVA_OPTS -jar ${CUR_BUNDLE_NAME} >> log_`date +%s`.log 2>&1 &

This command will start the java process, and redirect the output information to the log file, so it is not convenient for us to observe the demo's performance. Modify this line as follows:


In this way, all the logs will be printed out in the console for us to observe the running state.

  1. Now that the package is finished, I can start to make the mirror. The basic mirror I choose here is tomcat:7.0.77-jre8. It's for the sake of convenience. It doesn't need to install jdk and configure the java environment. The dockerfile content is very simple. It creates a working directory and all the things under the starter-run folder generated in the previous package. Copy to this working directory:
# Docker image of disconf consumer
# VERSION 0.0.1
# Author: bolingcavalry

#Basic mirror using tomcat:7.0.77-jre8
FROM tomcat:7.0.77-jre8

MAINTAINER BolingCavalry <>

#Define working directories
ENV WORK_PATH /usr/local/work

#Create working folders
RUN mkdir -p $WORK_PATH

#Copy application packages to working folders
COPY ./starter-run $WORK_PATH/

Open the terminal and execute the following commands under the dockerfile directory to generate the image:

docker build -t local_disconf_standalone_demo:0.0.1 .
  1. Create a new docker-compose.yml file, which is as follows: (Here, we should pay attention to the link configuration of disconf_java. In addition to connecting disconf_nginx, we also need to connect disconf_zookeeper. Otherwise, when we start demo, we will prompt watcher to start failure, which will lead to the failure of demo to receive more configuration after modifying the configuration of disconf server. New broadcasting:
version: '2'
    restart: always
    restart: always
    image: zookeeper:3.3.6
    restart: always
    image: bolingcavalry/disconf_mysql:0.0.1
    restart: always
    image: bolingcavalry/disconf_tomcat:0.0.1
      - disconf_redis_1:redishost001 
      - disconf_redis_2:redishost002
      - disconf_zookeeper:zkhost
      - disconf_mysql:mysqlhost
    restart: always
    image: bolingcavalry/disconf_nginx:0.0.1
      - disconf_tomcat:tomcathost 
      - "80:80" 
    restart: always
    image: local_disconf_standalone_demo:0.0.1
      - disconf_nginx:nginxhost
      - disconf_zookeeper:zkhost
    restart: always  
  1. Execute the command docker-compose up-d in the directory where the docker-compose.yml file is located and start all containers as follows:

  1. Execute the command 08_disconf_java_1 to enter the container, then enter the / usr/local/work directory, and execute. / to start demo, you can see the start information of disconf local runtime and output configuration information every 5 seconds.

  2. Input localhost on browser, log in disconf, user name password are admin, modify the corresponding configuration items, docker container 08_disconf_java_1 console can see the corresponding output, the specific operation method refers to the previous chapter. Using disconf under docker: Extreme Experience

  3. The docker image material used in this chapter has also been uploaded to git at The directory is shown in the following red box:

So far, a demo using disconf has been developed and used, which only brings you a preliminary understanding. The configuration service of deisonf is very rich, you can get more detailed information in the official website documents and demo.

Welcome to my public number

Tags: Java Docker git Maven Spring

Posted on Wed, 11 Sep 2019 19:51:46 -0700 by ym_chaitu