Java微服務應用測試,走起

dbasdk發表於2018-04-24

Java微服務應用測試,走起

從近幾年的技術發展來看,行業裡提到微服務應用場景越來越多,是時候能夠提升目前的業務擴充套件能力了。相比於傳統的EJB,SOA的方案,微服務是一種相對於更加輕量的方案,在Java領域有很多種實現,spring cloud算是一種,還有比如dubbo等等。

我們就簡單演示下如何通過idea的工具來建立一個簡單的微服務應用,以此為切入點,我們會牽扯出整個微服務的額整體設計,來儘可能達到融會貫通的的。

我們建立一個全新的工程。

Java微服務應用測試,走起

注意下如下的配置。

Java微服務應用測試,走起

這裡我們選擇的sping boot 2.0+的版本。

Java微服務應用測試,走起

輸入專案名

Java微服務應用測試,走起

這是一個建立成功後初步的狀態。

Java微服務應用測試,走起

我們把如下的檔案刪掉,準備加入模組,這樣專案變成一個父專案,我們在此基礎上建立一個子專案

Java微服務應用測試,走起

這個時候是 新建一個模組(module)

Java微服務應用測試,走起

這個地方選擇web和資料來源為MySQL,當然你也可以選擇其他的,先按照這個配置吧。

Java微服務應用測試,走起

建立成功後的狀態如下:

Java微服務應用測試,走起

我們把如下的幾個檔案刪掉,開始建立自定義類。

Java微服務應用測試,走起

初步的目錄結構如下,分別是License,LicenseServiceController

Java微服務應用測試,走起

兩個類的內容如下:

package com.jeanron.licensesservice.domain; public class License{ private String licenseId; private String organizationId; private String productName; private String licenseType; public String getLicenseId() { return licenseId; } public void setLicenseId(String licenseId) { this.licenseId = licenseId; } public String getOrganizationId() { return organizationId; } public void setOrganizationId(String organizationId) { this.organizationId = organizationId; } public String getProductName() { return productName; } public void setProductName(String productName) { this.productName = productName; } public String getLicenseType() { return licenseType; } public void setLicenseType(String licenseType) { this.licenseType = licenseType; } public License withLicenseId(String licenseId){ this.setLicenseId( licenseId ); return this; } public License withOrganizationId(String organizationId){ this.setOrganizationId(organizationId); return this; } public License withProductName(String productName){ this.setProductName(productName); return this; } public License withLicenseType(String licenseType){ this.setLicenseType(licenseType); return this; }
}
package com.jeanron.licensesservice.controller; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestController; import com.jeanron.licensesservice.domain.License; /** * 該註解相當於@ResponseBody + @Controller合在一起的作用 * 會將request/response序列化/反序列化為JSON格式 */ @RestController @RequestMapping(value="v1/organizations/{organizationId}/licenses") public class LicenseServiceController { @RequestMapping(value="/{licenseId}",method = RequestMethod.GET)
public License getLicenses( @PathVariable("organizationId") String organizationId, @PathVariable("licenseId")
String licenseId) { //@PathVariable能將URL中{organizationId}、{licenseId}對映到方法的變數organizationId、licenseId return new License() .withLicenseId(licenseId) .withOrganizationId(organizationId) .withProductName("OpsAPI") .withLicenseType("Database"); }
@RequestMapping(value="{licenseId}",method = RequestMethod.PUT) public String updateLicenses( @PathVariable("licenseId") String licenseId) { return String.format("This is the put"); }
@RequestMapping(value="{licenseId}",method = RequestMethod.POST) public String saveLicenses( @PathVariable("licenseId") String licenseId) { return String.format("This is the post"); }
@RequestMapping(value="{licenseId}",method = RequestMethod.DELETE)
@ResponseStatus(HttpStatus.NO_CONTENT) public String deleteLicenses( @PathVariable("licenseId") String licenseId) { return String.format("This is the Delete");
}
}

看程式碼還OK,但是實際配置的時候,第二個類的環境配置是個難點,很容易漏掉一些東西,導致編譯失敗。我能給出的建議就是耐心一些,再耐心一些。

啟動下服務,idea裡面啟動還是很方便的。

Java微服務應用測試,走起

通過瀏覽器,我們能夠根據輸入的引數,經過處理,封裝成一個json串返回。

Java微服務應用測試,走起

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/23718752/viewspace-2153170/,如需轉載,請註明出處,否則將追究法律責任。

相關文章