2 Springboot Project integration using DISCONF, based on Docker environment

Source: Internet
Author: User
Tags aop xmlns zookeeper docker ps docker run

In the previous article we completed the disconf service side of the environment to build, this one to see how the client springboot inherit disconf, eventually run under Docker.

Assume that you have built a disconf web-side environment on this machine, and have been able to use localhost to access the DISCONF Web interface.

See below how the client uses disconf.

Create a new Springboot project, tick web and AOP. As for why it is necessary to check AOP, use the disconf callback later, do not care about it first.

Then add the disconf dependency in the Pom. The final pom.xml are as follows

<?xml version= "1.0" encoding= "UTF-8"?> <project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http ://www.w3.org/2001/XMLSchema-instance "xsi:schemalocation=" http://maven.apache.org/POM/4.0.0 Http://maven.apache . org/xsd/maven-4.0.0.xsd "> <modelVersion>4.0.0</modelVersion> <groupid>com.tianyalei</gr Oupid> <artifactId>test_disconf</artifactId> <version>0.0.1-SNAPSHOT</version> < Packaging>jar</packaging> <name>test_disconf</name> <description>demo Project for Spri ng boot</description> <parent> <groupId>org.springframework.boot</groupId> &L
        T;artifactid>spring-boot-starter-parent</artifactid> <version>1.5.7.RELEASE</version> <relativePath/> <!--lookup parent from repository to </parent> <properties> &L T;project.build.sourceencoding>Utf-8</project.build.sourceencoding> <project.reporting.outputencoding>utf-8</ project.reporting.outputencoding> <java.version>1.8</java.version> </properties> &lt
            ;d ependencies> <dependency> <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency>
            <groupId>com.baidu.disconf</groupId> <artifactId>disconf-client</artifactId> <version>2.6.36</version> </dependency> <dependency> <g Roupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-aop</artifactid&gt
        ;
            </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactid>spring-boot-starter-test</artifactid> <scope>test</scope> </dependency> </dependenc ies> <build> <plugins> <plugin> <groupid>org.springfra Mework.boot</groupid> <artifactId>spring-boot-maven-plugin</artifactId> <
 /plugin> </plugins> </build> </project>

Disconf's Official document address: http://disconf.readthedocs.io/zh_CN/latest/tutorial-client/ Index.html, can refer to, it is based on the spring configuration, all XML, more trouble, we use the springboot, it is much simpler to configure.

We create a new configuration class, Disconfig

Package com.tianyalei.disconf.config;

Import Com.baidu.disconf.client.DisconfMgrBean;
Import Com.baidu.disconf.client.DisconfMgrBeanSecond;
Import Org.springframework.context.annotation.Bean;
Import org.springframework.context.annotation.Configuration;

/**
 * @author Wuweifeng wrote on 2017/10/16.
 *
/@Configuration public
class Disconfig {
    @Bean (Destroymethod = "destroy") public
    Disconfmgrbean Getdisconfmgrbean () {
        Disconfmgrbean Disconfmgrbean = new Disconfmgrbean ();
        Disconfmgrbean.setscanpackage ("com.tianyalei.disconf");
        return disconfmgrbean;
    }

    @Bean (Destroymethod = "Destroy", Initmethod = "init") public
    Disconfmgrbeansecond getDisconfMgrBean2 () {
        return new Disconfmgrbeansecond ();
    }
}
Note Set the Scanpackage settings package name. Nothing else, configuration classes in this category, very simple.


Then create a disconf.properties under the resources

# Whether to use remote profile
# True (the default) will get the configuration from remote, false then directly get the local configuration
enable.remote.conf=true
#
# configuration Server HOST, separated  by commas 127.0.0.1:8000,127.0.0.1:8000
#
conf_server_host=localhost:80
# version, please use the x_x_x_x format
version=1_0_0_0
# APP Please use product line _ Service name format
app=test_disconf
# environment
env=local
# debug
debug=true
# Ignore which distributed configurations, comma-delimited
ignore=
# Get remote configuration retries, default is 3 times
Conf_server_url_retry_times=1
# Gets the remote configuration to retry when the sleep time, The default is 5 seconds
Conf_server_url_retry_sleep_seconds=1
Here to note that there is a server_host address, like here I was running in this machine, and the previous article docker-compose.xml configured Nginx port 80 and mapped the native 80, so here directly write localhost:80. Represents the address of the disconf server, and if it is deployed in Docker, it needs to write a mapped container alias, such as nginxhost:80. Version, app, env all need to be consistent with adding on server side.


Then we create a class that uses the Disconf configuration feature.

Package com.tianyalei.disconf.service;

Import Com.baidu.disconf.client.common.annotations.DisconfItem;
Import Org.springframework.stereotype.Service;

/**
 * @author Wuweifeng wrote on 2017/10/16.
 */
@Service public
class Priceservice

    {private double money = +;

    private static final String KEY = "Money";

    /**
     * Individual configuration item
     *
    /@DisconfItem (key = key) public
    double Getmoney () {
        return money;
    }

    public void Setmoney (double money) {
        This.money = money;
    }
}
Here is a disconfitem annotation, indicating a key, then the item will be a dynamically configurable item, on the service side by modifying key to Money items, you can dynamically modify the value.

Let's write a controller to get the value of the price and see the effect.

/**
 * @author Wuweifeng wrote on 2017/10/16.
 */
@RestController public
class Indexcontroller
    {@Autowired
    private priceservice priceservice;
    
    @RequestMapping ("/index") public
    Object GetPrice () {
        return Priceservice.getmoney ();
    }
}
Start Project



can see the following error message, that is, not even on zookeeper, find Zkhost.

So what does that mean, disconf is the configuration information that was pushed with zookeeper, the server changed from zookeeper to the client, and in the previous article we completed the configuration of the server. If you remember, when you configure Tomcat, there is a zoo.properties, which is the address of the configuration zookeeper.

Error message in the Zkhost unknown is from here, the client from the server to get zookeeper address, got such an address zkhost:2181, and then the client is not connected zookeeper, so the error.

Since we are deploying zookeeper Docker without opening its external ports, which means that we cannot access zookeeper directly from the outside, only through the Docker link. So if you want to run this client demo, it has to be in the Docker environment.

We create Dockerfile in the engineering root directory

From hub.c.163.com/wuxukun/maven-aliyun:3-jdk-8

add pom.xml/tmp/build/

add src/tmp/build/src
        #构建应用
RUN cd/tmp/build && mvn clean package \
        #拷贝编译结果到指定目录
        && mv target/*.jar/app.jar \
        #清理编 Traces
        && CD/&& rm-rf/tmp/build

volume/tmp
EXPOSE 8080
entrypoint ["Java", "-jar", "/ App.jar "]
Then notice that the host address in the Disconf.properties is modified to
Conf_server_host=nginxhost:80
LocalHost under Docker is not working, but also through link to get the IP address of the Nginx container.

Build the image and execute it under the project root directory

Docker build-t test_disconf.
When the build is complete, you can get a mirror called test_disconf.

Let's take a look at the previous container that has been started: Docker PS


To start the image, it's important to note that we've already started a docker-compose.xml, and now we're going to start a new container, and we want to link the container inside. Let's take a look at the normal startup usage:

Docker run--link=wuwf_disconf_zookeeper_1:zkhost--link=wuwf_disconf_nginx_1:nginxhost  -d-p 8080:8080  Test_disconf

Found an error, said the belong to the default network, there is a solution, https://stackoverflow.com/questions/36489696/ Cannot-link-to-a-running-container-started-by-docker-compose

That means Docker-compose started Docker in a network, and you're now starting one, and in another network, there's no link between the different network.

Let's look at the Docker network LS by order


Can see the previous network as Wuwf_default, below we can be the following way to link

Docker run--link=wuwf_disconf_zookeeper_1:zkhost--link=wuwf_disconf_nginx_1:nginxhost--net wuwf_default  -d-p 8080:8080  test_disconf
Adding--net is OK.

Execute again, start successfully.

After successful launch, we can see in the background web interface


Let's Test it:

Access
Localhost:8080/index

You can see that the value of the price here is already configured from the disconf background, no longer the code is written in 100.

Let's change the value of price.


You can see the background changes immediately after the effect.

This is the use of the configuration center, followed by a complex configuration, not just the individual configuration of this single property.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.