An overview of the

Definitely one of the hottest terms in Web server development right now
Micro serviceIt has also become one of the most shining technologies in the evolution of the 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!


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/HelloHello 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 Pop.xml/TMP /build/ RUN CD/TMP /build && MVN -q dependency:resolve ADD SRC/TMP /build/ SRC CD/TMP /build && MVN -q -dskiptests =true package \ # Copy the compiled result to the specified directory && mv target/*.jar /app.jar \ # Clean up the compiled 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

    1. In the Spring project root directory according to the Dockerfile to generate the docker image

          `docker build -t springindocker .`
      Copy the code
    1. Start the container from the image you just created

         `docker run -d -p 8080:8080 springindocker`
      Copy the code
    1. Open your browser or use curl to access the urlhttp://127.0.0.1:8080You can see what’s returned from the Web serverHello Docker!!!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.

  • Author of more original articles in the cloud habitat community

The author has more SpringBt practice articles here:

  • ElasticSearch in SpringBoot
  • A preliminary study on Kotlin+SpringBoot joint programming
  • Spring Boot Logging framework practices
  • SpringBoot elegant coding: Lombok plus