Overview
Spring Boot simplifies the development process of Spring applications and provides various out-of-the-box framework configurations following the principle of convention-first configuration. On the other hand, Spring Boot also has the ability to directly build code into executable jar packages, which are a deployment unit that can run independently. Based on the above features, it is now generally believed that Spring Boot provides a capability to quickly construct microservices (Micro-Service).
Among the most popular terms in web server development, microservices are definitely a place, and it has become one of the brightest technologies in the evolution of the 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 microservice cloud architectures and platforms in distributed components makes deployment, management, and service capabilities easier. Since the service has been divided and miniaturized, it is easy for us to think that if we combine it with docker, let docker carry one microservices to run, this will reduce the coupling between services, and the deployment will be simpler. At the same time, the system architecture will be clearer, which will facilitate long-term evolution. Based on this idea, we have the introductory practice of this article!
Create a maven-based spring bt project
Add dependencies in 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 just need to add a simple restful interface to the startup class. In order to verify the subsequent browser, access the /hello interface and return a Hello Docker to the browser! Say hello!
@RestControllerpublic class DockerDemoSpringApplication { public static void main(String[] args) { SpringApplication.run(DockerDemoSpringApplication.class, args); } @RequestMapping("/hello") public String hello(){ return "Hello! Docker!"; }} Writing Dockerfile
We create a Dockerfile in the root directory of the Spring Bt project and use it to complete the orchestration of Docker image construction:
FROM maven:3.3.3ADD pom.xml /tmp/build/RUN cd /tmp/build && mvn -q dependency:resolveADD src /tmp/build/src #Build application RUN cd /tmp/build && mvn -q -DskipTests=true package / #Copy the compilation results to the specified directory && mv target/*.jar /app.jar / #Cleaning the compilation traces && cd / && rm -rf /tmp/buildVOLUME /tmpEXPOSE 8080ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
Enter Docker's world
docker build -t springindocker .docker run -d -p 8080:8080 springindockerThis means that you have successfully Dockered an application based on Spring Boot.
Although this article is just a demo, a large web project is nothing more than a lot of such Rest services, plus various infrastructure, databases, communications, middleware and scheduling. The development of each child element still follows the basic process here.
Summarize
The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support to Wulin.com.