LuoliangDSGA’s blog blog address: Luoliangdsga.github. IO welcome to reprint, reprint please indicate the author and source, thank you!

Practice using Docker container SpringBoot+Dubbo application

Docker is becoming more and more popular in daily development. In the work, many back-end projects need to use Docker for containerization. SpringBoot+Docker is known as “native cloud application”, and it is very easy to combine SpringBoot application and Docker. However, the combination of Dubbo and Docker is not so smooth, because Dubbo official stopped maintenance for a long time, and Dubbo official began to actively support SpringBoot not long ago. After a lot of stumping, I use this blog to document how to use the Docker container Dubbo application, following up my last post on SpringBoot+Dubboblog.

Problems to be solved

  • Dubbo Provider registers services in Docker Container. The registered IP in ZooKeeper is the container IP, so external Dubbo Consumer cannot be invoked.

start

We need to have a SpringBoot+Dubbo project ready (which I used directly from my last blog) and Docker installed on the machine.

  • Create a new Dockerfile
vi Dockerfile
Copy the code
  • Write Dockerfile
FROM openjdk:8-jre-alpine

MAINTAINER luoliang

ADD target/dubbo-provider.jar app.jar

ENTRYPOINT ["java"."-Djava.security.egd=file:/dev/./urandom"."-jar"."/app.jar"]

EXPOSE 12345
Copy the code
  • Build the mirror
docker build -t dubbo-provider .
Copy the code
  • Running the Provider container
docker run -d -p 12345:12345 --name dubbo-provider dubbo-provider
Copy the code
  • View the startup log of the container

The registered IP address of the service is 172.17.0.2. At this time, my local IP address is 192.168.1.7. If the consumer tries to consume the service, it will not be successful.

  • Launch consumers will report errors directly

In this case, many online solutions are solved by fixed container IP, which is not very good portability. Dubbo has been re-maintained and two environment variables have been added in the new version to support Docker containers. Check out the official issue.

  • Modify the Dockerfile above and add env DUBBO_IP_TO_REGISTRY to the IP registered in ZooKeeper
FROM openJDK :8-jre-alpine MAINTAINER Luoliang ENV DUBBO_IP_TO_REGISTRY 192.168.1.7 ENV DUBBO_PORT_TO_REGISTRY 12345 ADD target/dubbo-provider.jar app.jar ENTRYPOINT ["java"."-Djava.security.egd=file:/dev/./urandom"."-jar"."/app.jar"]

EXPOSE 12345
Copy the code

Restart a container after a rebuild and you can see that the service registration address has changed to the specified IP address and the consumer can successfully make the call.

  • You can also pass in parameters using the run command without modifying the Dockerfile
docker run -d -p 12345:12345 -e DUBBO_IP_TO_REGISTRY=192.1681.7 - e DUBBO_PORT_TO_REGISTRY =12345 --name dubbo-provider dubbo-provider
Copy the code
  • The service provider’s log is as follows

The current host is 172.17.0.2, but the registered address of the service has changed to 192.168.1.7.

Build the image using the Maven plugin

All of the above images are manually constructed. In order to improve efficiency and enable projects like SpringBoot+Dubbo to better integrate into THE CI system like Jenkins to build automatic pipelines.

  • Add plug-in dependencies to the POM
<plugin>
    <groupId>com.spotify</groupId>
    <artifactId>dockerfile-maven-plugin</artifactId>
    <executions>
        <execution>
            <id>default</id>
            <goals>
                <goal>build</goal>
                <goal>push</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <repository>dubbo-provider</repository>
        <tag>latest</tag>
        <buildArgs>
            <JAR_FILE>${project.build.finalName}.jar</JAR_FILE>
        </buildArgs>
    </configuration>
</plugin>
Copy the code
  • Put dockerFile in the same directory as pom.xml
project/
  Dockerfile
  pom.xml
Copy the code
  • Use the Maven command to package
mvn packageOr MVN dockerfile: buildCopy the code
  • After executing the command, you can use Docker images to view the image. The remaining steps are the same as above. After testing, the container runs normally

End

This article is over here, I made some changes in the source code of the last blog, the Dockfile used in the article is among them, the source code is here, the article is a summary of personal learning practice, there will be imperfect place, if there is a better way, welcome to point out, thank you!