An overview of the
Microservice is definitely one of the hottest terms in web server development, and it has become one of the brightest technologies in the evolution of Internet back-end service architecture. The basic idea behind microservices is to consider creating applications around business domain components that can be developed, managed, and accelerated independently. Using microservices cloud architectures and platforms in decentralized components makes deployment, management, and service functionality delivery easier. Since services are divided and miniaturization, it’s easy to think about if it is combined with docker, let the docker carrying a micro service operation, thus will make reduce coupling between services, deployment is concise, at the same time, the architecture of the system is more clear, facilitate long-term evolution, based on the idea of this article introduction to practice!
Note: This article was published on My public account CodeSheep. You can subscribe by holding down or scanning the heart below ↓ ↓ ↓
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>
Copy the code
- All we need to do is add a simple restful interface to the startup class, so that the browser can verify it later, and return one to the browser when accessing/Hello
Hello Docker!
Say hello!
@RestController
public class DockerDemoSpringApplication {
public static void main(String[] args) {
SpringApplication.run(DockerDemoSpringApplication.class, args);
}
@RequestMapping("/hello")
public String hello() {return "Hello! Docker!" ; }}Copy the code
Write Dockerfile
We create a Dockerfile in the root directory of the Spring Bt project, which is used to complete the choreography of the Docker image construction:
Maven :3.3.3 ADD pom.xml/TMP /build/ RUNcd /tmp/build && mvn -q dependency:resolve
ADD src /tmp/build/src
# Build the app
RUN cd /tmp/build && mvn -q -DskipTests=true package \
Copy the compiled result to the specified directory
&& mv target/*.jar /app.jar \
# Clean up the compile trace
&& cd / && rm -rf /tmp/build
VOLUME /tmp
EXPOSE 8080
ENTRYPOINT ["java"."-Djava.security.egd=file:/dev/./urandom"."-jar"."/ app. Jar"]Copy the code
Enter the Docker world
-
0x01. Generate the Docker image from the Dockerfile in the root directory of the Spring project
`docker build -t springindocker .` Copy the code
-
Start the container docker run -d -p 8080:8080 springinDocker from the newly created image
-
0x03. Open your browser or use curl to visit http://127.0.0.1:8080 to see Hello Docker returned from your web server. Hello to the character
This means that you have successfully converted a Spring Boot-based application Docker.
This article is just a Demo, but a large Web project consists of many of these Rest services, plus a variety of infrastructure, databases, communications, middleware, and scheduling, and the development of each child element still follows the basic process here.
Afterword.
-
The author’s more original articles are here, welcome to watch
-
My Personal Blog
The author has more SpringBt practice articles here:
- Spring Boot application monitoring actual combat
- The SpringBoot application is deployed in an external Tomcat container
- ElasticSearch in SpringBt practice
- A preliminary study on Kotlin+SpringBoot joint programming
- Spring Boot Logging framework practices
- SpringBoot elegant coding: Lombok plus
If you are interested, take some time to read some of the author’s articles on containerization and microservitization:
- Use K8S technology stack to create personal private cloud serial articles
- Nginx server configuration from a detailed configuration list
- Docker container visual monitoring center was built
- Use ELK to build Docker containerized application log center
- RPC framework practice: Apache Thrift
- RPC framework practice: Google gRPC
- Establishment of microservice call chain tracking center
- Docker containers communicate across hosts
- Preliminary study on Docker Swarm cluster
- Several guidelines for writing dockerFiles efficiently