java文件端点续传效果图_Java单依赖性Dockerized HTTP端点
java文件端點(diǎn)續(xù)傳效果圖
在本文中,我們將創(chuàng)建一個(gè)基于Java的HTTP端點(diǎn),使用它創(chuàng)建一個(gè)可執(zhí)行jar,將其打包在Docker中并立即在本地運(yùn)行。
本文面向初學(xué)者,他們想要尋找一個(gè)簡(jiǎn)單的演練來(lái)在Docker中運(yùn)行Java應(yīng)用程序。
描述Dockerized環(huán)境中Java應(yīng)用程序的絕大多數(shù)示例都包括使用Spring Boot之類的沉重框架。 我們想在這里表明,您不需要太多就可以在Docker中使用Java運(yùn)行端點(diǎn)。
實(shí)際上,我們僅將單個(gè)庫(kù)用作依賴項(xiàng): HttpMate core 。 對(duì)于此示例,我們將使用具有單個(gè)HTTP處理程序的HttpMate的LowLevel構(gòu)建器 。
本示例使用的環(huán)境
- Java 11+
- Maven 3.5+
- Java友好的IDE
- Docker版本18+
- 對(duì)HTTP / bash / Java的基本了解
最終結(jié)果在此git repo中可用。
組織項(xiàng)目
讓我們創(chuàng)建我們的初始項(xiàng)目結(jié)構(gòu):
mkdir -p simple-java-http-docker/src/main/java/com/envimate/examples/http讓我們從我們?cè)谶@里稱為simple-java-http-docker的根目錄中的項(xiàng)目的pom文件開(kāi)始:
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"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.envimate.examples</groupId><artifactId>simple-java-http-docker</artifactId><version>0.0.1</version><dependencies><dependency><groupId>com.envimate.httpmate</groupId><artifactId>core</artifactId><version>1.0.21</version></dependency></dependencies> </project>這里我們有:
- 我們項(xiàng)目的標(biāo)準(zhǔn)groupId / artifactId / version定義
- 對(duì)HttpMate核心庫(kù)的單一依賴關(guān)系
這足以在所選的IDE中開(kāi)始開(kāi)發(fā)我們的端點(diǎn)。 其中大多數(shù)都支持基于Maven的Java項(xiàng)目。
應(yīng)用入口
要啟動(dòng)我們的小服務(wù)器,我們將使用一個(gè)簡(jiǎn)單的main方法。 讓我們?cè)谀夸泂rc/main/java/com/envimate/examples/http中將它作為Application.java文件創(chuàng)建到應(yīng)用程序的條目,該文件現(xiàn)在僅將時(shí)間輸出到控制臺(tái)。
package com.envimate.examples.http;import java.time.LocalDateTime; import java.time.format.DateTimeFormatter;public final class Application {public static void main(String[] args) {final LocalDateTime time = LocalDateTime.now();final String dateFormatted = time.format(DateTimeFormatter.ISO_TIME);System.out.println("current time is " + dateFormatted);} }嘗試運(yùn)行此類,您將看到當(dāng)前時(shí)間。
讓我們使其更具功能,并將打印時(shí)間的部分分離為不帶參數(shù)的lambda函數(shù),即Supplier 。
package com.envimate.examples.http;import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.function.Supplier;public final class Application {public static void main(String[] args) {Supplier handler = () -> {final LocalDateTime time = LocalDateTime.now();final String dateFormatted = time.format(DateTimeFormatter.ISO_TIME);return "current time is " + dateFormatted;};System.out.println(handler.get());} }低級(jí)HttpMate提供的便捷接口看起來(lái)沒(méi)有什么不同,除了返回一個(gè)String ,沒(méi)有設(shè)置String ,而是將String設(shè)置為響應(yīng),以及表示一切正常的指示(aka響應(yīng)代碼200)。
final HttpHandler httpHandler = (request, response) -> {final LocalDateTime time = LocalDateTime.now();final String dateFormatted = time.format(DateTimeFormatter.ISO_TIME);response.setStatus(200);response.setBody("current time is " + dateFormatted); };HttpMate還提供了一個(gè)簡(jiǎn)單的Java HttpServer包裝器– PureJavaEndpoint ,該包裝PureJavaEndpoint您可以啟動(dòng)端點(diǎn)而無(wú)需任何進(jìn)一步的依賴。
我們要做的就是為它提供HttpMate的實(shí)例:
package com.envimate.examples.http;import com.envimate.httpmate.HttpMate; import com.envimate.httpmate.convenience.endpoints.PureJavaEndpoint; import com.envimate.httpmate.convenience.handler.HttpHandler;import java.time.LocalDateTime; import java.time.format.DateTimeFormatter;import static com.envimate.httpmate.HttpMate.anHttpMateConfiguredAs; import static com.envimate.httpmate.LowLevelBuilder.LOW_LEVEL;public final class Application {public static void main(String[] args) {final HttpHandler httpHandler = (request, response) -> {final LocalDateTime time = LocalDateTime.now();final String dateFormatted = time.format(DateTimeFormatter.ISO_TIME);response.setStatus(200);response.setBody("current time is " + dateFormatted);};final HttpMate httpMate = anHttpMateConfiguredAs(LOW_LEVEL).get("/time", httpHandler).build();PureJavaEndpoint.pureJavaEndpointFor(httpMate).listeningOnThePort(1337);} }注意,當(dāng)使用方法GET調(diào)用時(shí),我們已將httpHandler配置為提供路徑/time 。
現(xiàn)在是啟動(dòng)我們的應(yīng)用程序并提出一些要求的時(shí)候了:
curl http://localhost:1337/time current time is 15:09:34.458756在將所有內(nèi)容放入Dockerfile之前,我們需要將其打包為舊的jar。
建立罐子
為此,我們需要兩個(gè)maven插件: maven-compiler-plugin和maven-assembly-plugin來(lái)構(gòu)建可執(zhí)行jar。
<?xml version="1.0" encoding="UTF-8"?> <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"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.envimate.examples</groupId><artifactId>simple-java-http-docker</artifactId><version>0.0.1</version><dependencies><dependency><groupId>com.envimate.httpmate</groupId><artifactId>core</artifactId><version>1.0.21</version></dependency></dependencies><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.8.1</version><configuration><release>${java.version}</release><source>${java.version}</source><target>${java.version}</target></configuration></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-assembly-plugin</artifactId><executions><execution><phase>package</phase><goals><goal>single</goal></goals><configuration><archive><manifest><mainClass>com.envimate.examples.http.Application</mainClass></manifest></archive><descriptorRefs><descriptorRef>jar-with-dependencies</descriptorRef></descriptorRefs></configuration></execution></executions></plugin></plugins></build> </project>一旦有了這些,就讓我們來(lái)構(gòu)建我們的jar:
mvn clean verify并運(yùn)行生成的jar:
java -jar target/simple-java-http-docker-0.0.1-jar-with-dependencies.jar相同的卷曲度:
curl http://localhost:1337/time current time is 15:14:42.992563Docker化罐子
Dockerfile看起來(lái)非常簡(jiǎn)單:
FROM openjdk:12ADD target/simple-java-http-docker-0.0.1-jar-with-dependencies.jar /opt/application.jarEXPOSE 1337ENTRYPOINT exec java -jar /opt/application.jar它指定
- FROM :用作基礎(chǔ)的圖像。 我們將使用openjdk圖像。
- ADD :我們想要的罐子到我們想要的目錄
- EXPOSE :我們正在監(jiān)聽(tīng)的端口
- ENTRYPOINT :對(duì)于命令,我們要執(zhí)行
要構(gòu)建并標(biāo)記我們的Docker映像,我們從目錄的根目錄運(yùn)行以下命令:
docker build --tag simple-java-http-docker .這將產(chǎn)生一個(gè)我們可以運(yùn)行的docker映像:
docker run --publish 1337:1337 simple-java-http-docker請(qǐng)注意,我們正在傳遞--publish參數(shù),該參數(shù)指示裸露的1337端口在計(jì)算機(jī)的1337端口下可用。
相同的卷曲度:
curl http://localhost:1337/time current time is 15:23:04.275515就是這樣:我們對(duì)簡(jiǎn)單的HTTP端點(diǎn)進(jìn)行了docker化!
結(jié)冰
當(dāng)然,這是一個(gè)簡(jiǎn)化的示例,我們編寫的端點(diǎn)并不完全有用。 它表明盡管您不需要大量的庫(kù)就可以擁有一個(gè)正在運(yùn)行的HTTP端點(diǎn),打包可運(yùn)行的jar,在您的Java應(yīng)用程序中使用docker以及低級(jí)HttpMate的基本用法是多么容易。
當(dāng)您需要快速旋轉(zhuǎn)測(cè)試HTTP服務(wù)器時(shí),這種兩分鐘的設(shè)置很方便。 前幾天,我在開(kāi)發(fā)一個(gè)Twitter機(jī)器人(敬請(qǐng)關(guān)注有關(guān)該文章的文章),我不得不調(diào)試接收方真正的請(qǐng)求。 顯然,我無(wú)法要求Twitter將請(qǐng)求轉(zhuǎn)儲(chǔ)給我,因此我需要一個(gè)簡(jiǎn)單的終結(jié)點(diǎn),該終結(jié)點(diǎn)將輸出有關(guān)請(qǐng)求的所有信息。
HttpMate的處理程序提供對(duì)名為MetaData的對(duì)象的訪問(wèn),該對(duì)象幾乎就是所謂的–請(qǐng)求的元數(shù)據(jù),意味著有關(guān)請(qǐng)求的所有可用信息。
使用該對(duì)象,我們可以打印請(qǐng)求的所有內(nèi)容。
public final class FakeTwitter {public static void main(String[] args) {final HttpMate httpMate = HttpMate.aLowLevelHttpMate().callingTheHandler(metaData -> {System.out.println(metaData);}).forRequestPath("/*").andRequestMethods(GET, POST, PUT).build();PureJavaEndpoint.pureJavaEndpointFor(httpMate).listeningOnThePort(1337);} }現(xiàn)在,請(qǐng)求路徑/time已替換為一種模式,捕獲了所有路徑,并且我們可以添加所有我們感興趣的HTTP方法。
運(yùn)行我們的FakeTwitter服務(wù)器并發(fā)出請(qǐng)求:
curl -XGET http://localhost:1337/some/path/with?someParameter=someValue我們將在控制臺(tái)中看到以下輸出(為便于閱讀而格式化的輸出:它是下面的地圖,因此,如果您愿意,可以很好地設(shè)置其格式)
{PATH=Path(path=/some/path/with),BODY_STRING=,RAW_QUERY_PARAMETERS={someParameter=someValue},QUERY_PARAMETERS=QueryParameters(queryParameters={QueryParameterKey(key=someParameter)=QueryParameterValue(value=someValue)}),RESPONSE_STATUS=200,RAW_HEADERS={Accept=*/*,Host=localhost:1337,User-agent=curl/7.61.0},RAW_METHOD=GET,IS_HTTP_REQUEST=true,PATH_PARAMETERS=PathParameters(pathParameters={}),BODY_STREAM=sun.net.httpserver.FixedLengthInputStream@6053cef4,RESPONSE_HEADERS={},HEADERS=Headers(headers={HeaderKey(value=user-agent)=HeaderValue(value=curl/7.61.0),HeaderKey(value=host)=HeaderValue(value=localhost:1337),HeaderKey(value=accept)=HeaderValue(value=*/*)}),CONTENT_TYPE=ContentType(value=null),RAW_PATH=/some/path/with,METHOD=GET,LOGGER=com.envimate.httpmate.logger.Loggers$$Lambda$17/0x000000080118f040@5106c12f,HANDLER=com.envimate.examples.http.FakeTwitter$$Lambda$18/0x000000080118f440@68157191 }最后的話
HttpMate本身提供了更多功能。 但是,它還很年輕,尚未用于生產(chǎn),需要您的支持! 如果您喜歡閱讀的內(nèi)容,請(qǐng)給我們發(fā)送電子郵件至opensource@envimate.com,或者僅嘗試HttpMate并在反饋問(wèn)題中發(fā)表評(píng)論,讓我們知道。
翻譯自: https://www.javacodegeeks.com/2019/08/java-single-dependency-dockerized-http-endpoint.html
java文件端點(diǎn)續(xù)傳效果圖
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的java文件端点续传效果图_Java单依赖性Dockerized HTTP端点的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: tf.metrics._将指标标签与Mi
- 下一篇: 金鱼身上发黑能恢复吗(金鱼身上的黑斑怎么