[Microservices] First project of Spring Boot and Docker


Share one very simple project, which I did as my first project for learning microservices and docker. Thanks.

Prerequisite

Following softwares must be installed, and they would better be the latest version

  • JDK
  • Maven
  • Docker

Spring boot project, helloworld

A very simple java web application built with spring boot. It only contains 2 files, pom and one java class.

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.b5wang.cloud</groupId>
    <artifactId>helloword</artifactId>
    <version>1.0.0</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
    </parent>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <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>


    <build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

Application.java

package com.b5wang.cloud.helloworld;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class Application {

    @RequestMapping("/")
    public String home() {
        return "Hello cloud World!";
    }

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

Now run maven build, you can get helloworld.jar

Build docker image, helloworld

Create a new folder at another place, named helloworld-image. Then copy helloworld.jar into it.
Create a empty file called Dockerfile.

Dockerfile

FROM openjdk:8-jdk-alpine
COPY helloword.jar /app/helloword.jar
ENTRYPOINT ["java","-jar","/app/helloword.jar"]

Build image

> cd helloworld-image
> docker build --tag=helloworld .

Sending build context to Docker daemon  16.79MB
Step 1/3 : FROM openjdk:8-jdk-alpine
 ---> a3562aa0b991
Step 2/3 : COPY helloword.jar /app/helloword.jar
 ---> 3f25eef0547b
Step 3/3 : ENTRYPOINT ["java","-jar","/app/helloword.jar"]
 ---> Running in 0307850d2c03
Removing intermediate container 0307850d2c03
 ---> a8aedecd490e
Successfully built a8aedecd490e
Successfully tagged helloworld:latest

Run image

> docker run -p 4000:8080 helloworld

Spring boot is using port 8080 by default.
Now open browser, access http://localhost:4000/. You should see “Hello cloud World!”

Reference

[1] https://spring.io/guides/gs/spring-boot-docker/
[2] https://docs.docker.com/get-started/part2/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值