Docker-based dubbo container for simple and elastic cloud expansion
This article is modified based on this example:
git clone https://github.com/binblee/dubbo-docker.git
cd dubbo-docker
ls -l
drwxr-xr-x 4 root root 43 3月 16 16:16 service-api
drwxr-xr-x 4 root root 60 3月 16 16:16 service-consumer
drwxr-xr-x 4 root root 60 3月 16 16:16 service-producer
We use a zookeeper cluster with three nodes
Revise
service-consumer/src/main/resources/services.xml
service-producer/src/main/resources/services.xml
Modify the zookeeper configuration, configure a total of three nodes, and use system environment variables ( one of the recommended practices applied by [12 factor]( https://12factor.net/ ))
<dubbo:registry protocol="zookeeper" address="${ZOOKEEPER_NODE_01}:2181,${ZOOKEEPER_NODE_02}:2181,${ZOOKEEPER_NODE_03}:2181" />
Compile and package in the top-level directory of maven and install it to the local maven repository (mainly install the api package)
mvn clean compile package install -Dmaven.test.skip=true
The first Docker image
build producer image (using the openjdk base image)
cd service-producer
vi Dockerfile
FROM openjdk:8-jre
VOLUME /tmp
COPY target/*.jar /app.jar
RUN sh -c 'touch /app.jar'
CMD ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
docker build -t dubbo-producer .
Second Docker image
build consumer image (using the openjdk base image)
cd service-consumer
vi Dockerfile
FROM openjdk:8-jre
VOLUME /tmp
COPY target/*.jar /app.jar
RUN sh -c 'touch /app.jar'
CMD ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
docker build -t dubbo-consumer .
Third Docker image
build dubbo-admin image (use openjdk base image)
This article uses the dubbo source code to create the war package of dubbo-admin
git clone https://github.com/alibaba/dubbo.git
Configure the zookeeper cluster address of the dubbo-admin management console
cd dubbo/dubbo-admin/src/main/webapp/WEB-INF
vi dubbo.properties
dubbo.registry.address=zookeeper://${ZOOKEEPER_NODE_01}:2181?backup=${ZOOKEEPER_NODE_02}:2181,${ZOOKEEPER_NODE_03}:2181
package, skip tests
mvn clean compile package install -Dmaven.test.skip=true
cd dubbo/dubbo-admin
vi Dockerfile
FROM openjdk:8-jre
ADD apache-tomcat-8.0.33.tar.gz /opt
RUN rm -rf /opt/apache-tomcat-8.0.33/webapps/ROOT
COPY target/dubbo-admin-2.5.4-SNAPSHOT.war /opt/apache-tomcat-8.0.33/webapps/ROOT.war
CMD ["/opt/apache-tomcat-8.0.33/bin/catalina.sh", "run"]
This example uses apache-tomcat-8.0.33, please download it yourself
docker build -t dubbo-admin .
Edit docker-compose to prepare for automatic orchestration
vi docker-compose.yml
version: '2'
services:
zookeeper-node-01:
image: zookeeper
restart: always
container_name: dubbo-zookeeper-node-01
ports:
- "2181:2181"
environment:
ZOO_MY_ID: 1
ZOO_SERVERS: server.1=zookeeper-node-01:2888:3888 server.2=zookeeper-node-02:2888:3888 server.3=zookeeper-node-03:2888:3888
zookeeper-node-02:
image: zookeeper
restart: always
container_name: dubbo-zookeeper-node-02
ports:
- "2182:2181"
environment:
ZOO_MY_ID: 2
ZOO_SERVERS: server.1=zookeeper-node-01:2888:3888 server.2=zookeeper-node-02:2888:3888 server.3=zookeeper-node-03:2888:3888
zookeeper-node-03:
image: zookeeper
restart: always
container_name: dubbo-zookeeper-node-03
ports:
- "2183:2181"
environment:
ZOO_MY_ID: 3
ZOO_SERVERS: server.1=zookeeper-node-01:2888:3888 server.2=zookeeper-node-02:2888:3888 server.3=zookeeper-node-03:2888:3888
producer:
image: 'producer:latest'
container_name: dubbo-producer
environment:
- ZOOKEEPER_NODE_01=zookeeper-node-01
- ZOOKEEPER_NODE_02=zookeeper-node-02
- ZOOKEEPER_NODE_03=zookeeper-node-03
consumer:
image: 'consumer:latest'
container_name: dubbo-consumer
environment:
- ZOOKEEPER_NODE_01=zookeeper-node-01
- ZOOKEEPER_NODE_02=zookeeper-node-02
- ZOOKEEPER_NODE_03=zookeeper-node-03
- SERVER_PORT=8899
ports:
- 8899
dubbo-admin:
image: 'dubbo-admin:latest'
container_name: dubbo-admin
environment:
- ZOOKEEPER_NODE_01=zookeeper-node-01
- ZOOKEEPER_NODE_02=zookeeper-node-02
- ZOOKEEPER_NODE_03=zookeeper-node-03
ports:
- 8003:8080
start in the background
docker-compose up -d
stop
docker-compose stop
Forced deletion (dynamically expanded containers can also be deleted together)
docker-compose rm -f
First configure the zookeeper cluster, a total of three nodes
Export the 8899 port of the consumer, and the port will be randomly mapped on the host, which can be viewed through docker ps
The dubbo-admin console uses port 8003
http://yourip: Random port for application
You can see Greetings from Dubbo Docker
http://yourip:8003
Enter the dubbo-admin management console
Account password can be modified in dubbo.properties, we use root/root
If you have installed management tools such as shipyard (refer to: http://crabdave.iteye.com/blog/2362149 ), you can dynamically scale horizontally to a specified number of containers for producers and consumers to achieve simple elastic expansion
After the extension, you can observe which producer the current consumer is calling
You can modify the java files in the consumer and producer to record the ip address, and then observe when calling
Which producer is the consumer calling
Modify Application.java in consumer
Modify GreetingsImpl.java in producer
Please refer to the attachment for the modified file
The page call shows:
Greetings from (Producer IP: ip:172.21.0.2 ip:127.0.0.1 )Dubbo Docker (Consumer IP: ip:172.21.0.5 ip:127.0.0.1 )
The address of the called Producer keeps changing (scale two Producers and two consumers)
/** * Get local IP address * @return * @throws SocketException */ private static String getLocalIP() throws SocketException { Enumeration allNetInterfaces = NetworkInterface.getNetworkInterfaces(); InetAddress ip = null; StringBuffer str=new StringBuffer(); while (allNetInterfaces.hasMoreElements()) { NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement (); System.out.println(netInterface.getName()); Enumeration addresses = netInterface.getInetAddresses (); while (addresses.hasMoreElements()) { ip = (InetAddress) addresses.nextElement(); if (ip != null && ip instanceof Inet4Address) { System.out.println("本机的IP = " + ip.getHostAddress()); str.append(" ip:").append(ip.getHostAddress()).append(" "); } } } return str.toString(); }