Rumah >Java >javaTutorial >Membina dan Meletakkan Apl Java Pertama Anda dengan Docker dalam Just inutes
Mari buat apl java ringkas yang mengembalikan teks dan tersedia pada port 1800 persekitaran setempat anda menggunakan bekas Docker dalam masa 5 minit (bergantung pada kelajuan sambungan internet anda).
Anda sentiasa boleh mengambil kod sumber penuh daripada repositori awam saya:
https://github.com/alexander-uspenskiy/simple-service
# Install Homebrew if not present /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" # Install Java 8 brew tap homebrew/cask-versions brew install --cask temurin8 # Install Maven brew install maven # Install Docker Desktop brew install --cask docker # Install VS Code brew install --cask visual-studio-code # Install VS Code Extensions code --install-extension vscjava.vscode-java-pack code --install-extension ms-azuretools.vscode-docker
# Install Chocolatey if not present Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1')) # Install Java 8 choco install temurin8 # Install Maven choco install maven # Install Docker Desktop choco install docker-desktop # Install VS Code choco install vscode # Install VS Code Extensions code --install-extension vscjava.vscode-java-pack code --install-extension ms-azuretools.vscode-docker
# Create project structure mkdir -p simple-service cd simple-service
{ "java.configuration.runtimes": [ { "name": "JavaSE-1.8", "path": "/Library/Java/JavaVirtualMachines/temurin-8.jdk/Contents/Home", "default": true } ], "java.configuration.updateBuildConfiguration": "automatic", "java.compile.nullAnalysis.mode": "automatic", "maven.executable.path": "/usr/local/bin/mvn" }
# Verify Java java -version # Verify Maven mvn -version # Verify Docker docker --version
# Create Maven project mvn archetype:generate \ -DgroupId=com.example \ -DartifactId=simple-service \ -DarchetypeArtifactId=maven-archetype-quickstart \ -DarchetypeVersion=1.4 \ -DinteractiveMode=false
Selepas langkah terakhir anda harus mempunyai direktori perkhidmatan ringkas dengan struktur yang telah dibina sebelumnya.
Langkah 1
<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 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>simple-service</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>simple-service</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.httpcomponents.client5</groupId> <artifactId>httpclient5</artifactId> <version>5.4</version> </dependency> </dependencies> <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.11.0</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>com.example.App</mainClass> </transformer> </transformers> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
Langkah 2
Tambah logik pada App.java
package com.example; import com.sun.net.httpserver.HttpServer; import java.net.InetSocketAddress; import java.io.IOException; import java.io.OutputStream; public class App { public static void main(String[] args) throws IOException { HttpServer server = HttpServer.create(new InetSocketAddress(1800), 0); server.createContext("/", (exchange -> { String response = "Hello from Java!"; exchange.sendResponseHeaders(200, response.length()); try (OutputStream os = exchange.getResponseBody()) { os.write(response.getBytes()); } })); server.setExecutor(null); server.start(); System.out.println("Server started on port 1800"); } }
Penjelasan pantas:
Import & Persediaan
Konfigurasi Pelayan
HttpServer.create()
InetSocketAddress(1800)
createContext("/")
Aliran Respons
Permulaan Pelayan
setExecutor(null)
server.start()
Langkah 3
Buat Dockerfile dalam akar projek:
FROM amazoncorretto:8 WORKDIR /app COPY target/simple-service-1.0-SNAPSHOT.jar app.jar EXPOSE 1800 CMD ["java", "-jar", "app.jar"]
Langkah 4
Cipta docker-compose.yml untuk membina dan memetakan bekas ke port 1800
services: app: build: . ports: - "1800:1800" restart: unless-stopped
Langkah 5
Cipta build.sh
#!/bin/bash mvn clean package docker compose build docker compose up
Dan benarkan kebenaran exec untuk fail ini dalam terminal:
# Install Homebrew if not present /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" # Install Java 8 brew tap homebrew/cask-versions brew install --cask temurin8 # Install Maven brew install maven # Install Docker Desktop brew install --cask docker # Install VS Code brew install --cask visual-studio-code # Install VS Code Extensions code --install-extension vscjava.vscode-java-pack code --install-extension ms-azuretools.vscode-docker
Lari sahaja
# Install Chocolatey if not present Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1')) # Install Java 8 choco install temurin8 # Install Maven choco install maven # Install Docker Desktop choco install docker-desktop # Install VS Code choco install vscode # Install VS Code Extensions code --install-extension vscjava.vscode-java-pack code --install-extension ms-azuretools.vscode-docker
Anda sepatutnya telah membina projek, imej dibuat dan bekas dilaksanakan.
Untuk menguji apl hanya buka penyemak imbas di alamat http://localhost:1800/
Selamat Pengekodan!
Atas ialah kandungan terperinci Membina dan Meletakkan Apl Java Pertama Anda dengan Docker dalam Just inutes. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!