Overview
Microservices definitely have a place among the hottest terms in web server development today , and they have also become one of the brightest technologies in the evolution of the current Internet back-end service architecture. The basic idea of microservices is to consider creating applications around business domain components that can be independently developed, managed, and accelerated. Using a microservice cloud architecture and platform in disparate components makes deployment, management, and service function delivery simpler. Now that the services are divided and miniaturized, it is easy to think that if we combine them with docker and let docker run microservices one by one, the coupling between services will be reduced, the deployment will be simple, and the architecture of the system will be reduced. It is also clearer and easier for long-term evolution. Based on this idea, there is an entry practice for this article!
Create a maven-based spring bt project
- Add dependencies to pom.xml:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
- We only need to add a simple restful interface to the startup class, so that the browser can verify it later, access the /hello interface, and return
Hello Docker!
a !
@RestController
public class DockerDemoSpringApplication {
public static void main(String[] args) {
SpringApplication.run(DockerDemoSpringApplication.class, args);
}
@RequestMapping("/hello")
public String hello(){
return "Hello! Docker!”;
}
}
Write Dockerfile
We create a Dockerfile in the root directory of the Spring Bt project, and use it to complete the orchestration of the Docker image build:
FROM maven:3.3.3
ADD pom.xml /tmp/build/
RUN cd /tmp/build && mvn -q dependency:resolve
ADD src /tmp/build/src
#构建应用
RUN cd /tmp/build && mvn -q -DskipTests=true package \
#拷贝编译结果到指定目录
&& mv target/*.jar /app.jar \
#清理编译痕迹
&& cd / && rm -rf /tmp/build
VOLUME /tmp
EXPOSE 8080
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar”]
Enter the world of Docker
-
-
Generate a docker image according to the Dockerfile in the root directory of the Spring project
docker build -t springindocker .
-
-
-
Start the container from the image you just created
docker run -d -p 8080:8080 springindocker
-
-
- Open the browser, or use curl to access
http://127.0.0.1:8080
, you can see theHello Docker!!!
hello characters returned in the web server
- Open the browser, or use curl to access
So far, you have successfully Dockerized a Spring Boot-based application.
Although this article is just a Demo, a large-scale web project is nothing more than composed of many such Rest services, plus various infrastructures, databases, communications, middleware and scheduling. The development of each sub-element still follows the rules here. Basic process.
postscript
The author's more SpringBt practice articles are here: