日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

java文件端点续传效果图_Java单依赖性Dockerized HTTP端点

發布時間:2023/12/3 java 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java文件端点续传效果图_Java单依赖性Dockerized HTTP端点 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

java文件端點續傳效果圖

在本文中,我們將創建一個基于Java的HTTP端點,使用它創建一個可執行jar,將其打包在Docker中并立即在本地運行。

本文面向初學者,他們想要尋找一個簡單的演練來在Docker中運行Java應用程序。

描述Dockerized環境中Java應用程序的絕大多數示例都包括使用Spring Boot之類的沉重框架。 我們想在這里表明,您不需要太多就可以在Docker中使用Java運行端點。

實際上,我們僅將單個庫用作依賴項: HttpMate core 。 對于此示例,我們將使用具有單個HTTP處理程序的HttpMate的LowLevel構建器 。

本示例使用的環境

  • Java 11+
  • Maven 3.5+
  • Java友好的IDE
  • Docker版本18+
  • 對HTTP / bash / Java的基本了解

最終結果在此git repo中可用。

組織項目

讓我們創建我們的初始項目結構:

mkdir -p simple-java-http-docker/src/main/java/com/envimate/examples/http

讓我們從我們在這里稱為simple-java-http-docker的根目錄中的項目的pom文件開始:

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

這里我們有:

  • 我們項目的標準groupId / artifactId / version定義
  • 對HttpMate核心庫的單一依賴關系

這足以在所選的IDE中開始開發我們的端點。 其中大多數都支持基于Maven的Java項目。

應用入口

要啟動我們的小服務器,我們將使用一個簡單的main方法。 讓我們在目錄src/main/java/com/envimate/examples/http中將它作為Application.java文件創建到應用程序的條目,該文件現在僅將時間輸出到控制臺。

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);} }

嘗試運行此類,您將看到當前時間。

讓我們使其更具功能,并將打印時間的部分分離為不帶參數的lambda函數,即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());} }

低級HttpMate提供的便捷接口看起來沒有什么不同,除了返回一個String ,沒有設置String ,而是將String設置為響應,以及表示一切正常的指示(aka響應代碼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還提供了一個簡單的Java HttpServer包裝器– PureJavaEndpoint ,該包裝PureJavaEndpoint您可以啟動端點而無需任何進一步的依賴。

我們要做的就是為它提供HttpMate的實例:

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);} }

注意,當使用方法GET調用時,我們已將httpHandler配置為提供路徑/time 。

現在是啟動我們的應用程序并提出一些要求的時候了:

curl http://localhost:1337/time current time is 15:09:34.458756

在將所有內容放入Dockerfile之前,我們需要將其打包為舊的jar。

建立罐子

為此,我們需要兩個maven插件: maven-compiler-plugin和maven-assembly-plugin來構建可執行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>

一旦有了這些,就讓我們來構建我們的jar:

mvn clean verify

并運行生成的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.992563

Docker化罐子

Dockerfile看起來非常簡單:

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 :用作基礎的圖像。 我們將使用openjdk圖像。
  • ADD :我們想要的罐子到我們想要的目錄
  • EXPOSE :我們正在監聽的端口
  • ENTRYPOINT :對于命令,我們要執行

要構建并標記我們的Docker映像,我們從目錄的根目錄運行以下命令:

docker build --tag simple-java-http-docker .

這將產生一個我們可以運行的docker映像:

docker run --publish 1337:1337 simple-java-http-docker

請注意,我們正在傳遞--publish參數,該參數指示裸露的1337端口在計算機的1337端口下可用。

相同的卷曲度:

curl http://localhost:1337/time current time is 15:23:04.275515

就是這樣:我們對簡單的HTTP端點進行了docker化!

結冰

當然,這是一個簡化的示例,我們編寫的端點并不完全有用。 它表明盡管您不需要大量的庫就可以擁有一個正在運行的HTTP端點,打包可運行的jar,在您的Java應用程序中使用docker以及低級HttpMate的基本用法是多么容易。

當您需要快速旋轉測試HTTP服務器時,這種兩分鐘的設置很方便。 前幾天,我在開發一個Twitter機器人(敬請關注有關該文章的文章),我不得不調試接收方真正的請求。 顯然,我無法要求Twitter將請求轉儲給我,因此我需要一個簡單的終結點,該終結點將輸出有關請求的所有信息。

HttpMate的處理程序提供對名為MetaData的對象的訪問,該對象幾乎就是所謂的–請求的元數據,意味著有關請求的所有可用信息。

使用該對象,我們可以打印請求的所有內容。

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);} }

現在,請求路徑/time已替換為一種模式,捕獲了所有路徑,并且我們可以添加所有我們感興趣的HTTP方法。

運行我們的FakeTwitter服務器并發出請求:

curl -XGET http://localhost:1337/some/path/with?someParameter=someValue

我們將在控制臺中看到以下輸出(為便于閱讀而格式化的輸出:它是下面的地圖,因此,如果您愿意,可以很好地設置其格式)

{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本身提供了更多功能。 但是,它還很年輕,尚未用于生產,需要您的支持! 如果您喜歡閱讀的內容,請給我們發送電子郵件至opensource@envimate.com,或者僅嘗試HttpMate并在反饋問題中發表評論,讓我們知道。

翻譯自: https://www.javacodegeeks.com/2019/08/java-single-dependency-dockerized-http-endpoint.html

java文件端點續傳效果圖

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的java文件端点续传效果图_Java单依赖性Dockerized HTTP端点的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。