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: git@github.com:knightliao/disconf-demos-java.git). 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 git@github.com:zq2599/blog_demos.git. 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.
<dependencies>

        <dependency>
            <groupId>com.baidu.disconf</groupId>
            <artifactId>disconf-client</artifactId>
            <version>2.6.36</version>
        </dependency>

        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>1.4</version>
        </dependency>

        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.4</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.6</version>
        </dependency>

        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
            <version>1.0.9</version>
        </dependency>

        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.0.9</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.1.7.RELEASE</version>
        </dependency>
    </dependencies>

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:

<plugin>
                <groupId>com.github.knightliao.plugin</groupId>
                <artifactId>starter-shade-maven-plugin</artifactId>
                <version>1.0.0</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <finalName>${project.build.finalName}</finalName>
                            <transformers>
                                <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>com.example.disconf.demo.DisconfDemoMain</mainClass>
                                </transformer>
                                <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.DontIncludeResourceTransformer">
                                    <resources>
                                        <resource>env</resource>
                                        <resource>disconf.properties</resource>
                                        <resource>logback.xml</resource>
                                    </resources>
                                </transformer>
                                <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                    <resource>META-INF/spring.handlers</resource>
                                </transformer>
                                <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                    <resource>META-INF/spring.schemas</resource>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
  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 redis.properties configuration of the disconf server.
The DisconfFileItem annotation in the yellow box binds host and port attributes to the host and port attributes of redis.properties 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 disconf.properties file:

# Whether to use remote configuration files
# True (default) retrieves configuration false s remotely and gets local configuration directly
disconf.enable.remote.conf=true

#
# Configure the server's HOST, separated by commas 127.0.0.1:8004, 127.0.0.1:8004
#
disconf.conf_server_host=nginxhost:80
#disconf.conf_server_host=127.0.0.1:80

# Version, please use X_X_X format 
disconf.version=1_0_0_0

# APP in Product Line Service Name Format 
disconf.app=disconf_demo

# Environment disco
disconf.env=rd

# Ignore which distributed configurations, separated by commas
disconf.ignore=

# Get the number of remote configuration retries, default is 3
disconf.conf_server_url_retry_times=1
# Gets the sleep time for remote configuration retry by default of 5 seconds
disconf.conf_server_url_retry_sleep_seconds=1

# User-specified download folder, where remote files will be downloaded
disconf.user_define_download_dir=./disconf/download2

# 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).
disconf.enable_local_download_dir_in_class_path=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="com.baidu.disconf.client.DisconfMgrBean"
          destroy-method="destroy">
        <property name="scanPackage" value="com.example.disconf.demo"/>
    </bean>
    <bean id="disconfMgrBean2" class="com.baidu.disconf.client.DisconfMgrBeanSecond"
          init-method="init" destroy-method="destroy">
    </bean>

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 start.sh 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:

java $JAVA_OPTS -jar ${CUR_BUNDLE_NAME}

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

#author
MAINTAINER BolingCavalry <zq2599@gmail.com>

#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'
services:
  disconf_redis_1: 
    image: daocloud.io/library/redis
    restart: always
  disconf_redis_2: 
    image: daocloud.io/library/redis
    restart: always
  disconf_zookeeper: 
    image: zookeeper:3.3.6
    restart: always
  disconf_mysql: 
    image: bolingcavalry/disconf_mysql:0.0.1
    environment:
      MYSQL_ROOT_PASSWORD: 123456
    restart: always
  disconf_tomcat: 
    image: bolingcavalry/disconf_tomcat:0.0.1
    links: 
      - disconf_redis_1:redishost001 
      - disconf_redis_2:redishost002
      - disconf_zookeeper:zkhost
      - disconf_mysql:mysqlhost
    restart: always
  disconf_nginx: 
    image: bolingcavalry/disconf_nginx:0.0.1
    links: 
      - disconf_tomcat:tomcathost 
    ports: 
      - "80:80" 
    restart: always
  disconf_java: 
    image: local_disconf_standalone_demo:0.0.1
    links: 
      - 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. / start.sh 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 git@github.com:zq2599/docker_disconf.git. 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