博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java微服务应用测试,走起
阅读量:2447 次
发布时间:2019-05-10

本文共 3563 字,大约阅读时间需要 11 分钟。

1524319043518753d4887b3

从近几年的技术发展来看,行业里提到微服务应用场景越来越多,是时候能够提升目前的业务扩展能力了。相比于传统的EJB,SOA的方案,微服务是一种相对于更加轻量的方案,在Java领域有很多种实现,spring cloud算是一种,还有比如dubbo等等。

我们就简单演示下如何通过idea的工具来创建一个简单的微服务应用,以此为切入点,我们会牵扯出整个微服务的额整体设计,来尽可能达到融会贯通的的。

我们创建一个全新的工程。

15243190433340a52df3a9b

注意下如下的配置。

1524319043318b860bd9c46

这里我们选择的sping boot 2.0+的版本。

152431904333767613ab07e

输入项目名

152431904322294fecee422

这是一个创建成功后初步的状态。

15243190433926da4a4cce6

我们把如下的文件删掉,准备加入模块,这样项目变成一个父项目,我们在此基础上创建一个子项目

152431904333837439fd06b

这个时候是 新建一个模块(module)

1524319043398ad983563c7

这个地方选择web和数据源为MySQL,当然你也可以选择其他的,先按照这个配置吧。

1524319043351dc444d19a2

创建成功后的状态如下:

152431904337441b4eeca21

我们把如下的几个文件删掉,开始创建自定义类。

1524319043358ee87447e8d

初步的目录结构如下,分别是License,LicenseServiceController

152431904340213c5b7225f

两个类的内容如下:

 
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里面启动还是很方便的。

1524319043612d450f522cb

通过浏览器,我们能够根据输入的参数,经过处理,封装成一个json串返回。

152431904336941d5c8f59a

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/23718752/viewspace-2153170/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/23718752/viewspace-2153170/

你可能感兴趣的文章
html 目录 规范_规范mp3目录
查看>>
css3渐变—渐变_CSS渐变
查看>>
dijit_使用LESS CSS创建自己的Dijit CSS主题
查看>>
css3旋转缩放_使用CSS3创建旋转,缩放效果
查看>>
锚链接跳转动画 css_使用CSS3动画链接链接
查看>>
probe.war使用_使用console-probe更好的Node.js控制台转储
查看>>
dojo创建浮动工具栏_使用Dojo工具包创建一个可清除的文本框
查看>>
隐藏wordpress后台_在WordPress中隐藏管理栏
查看>>
用某个值快速填充数组_用顺序值填充数组
查看>>
查看全局安装包_停止全局安装软件包
查看>>
检查损坏文件_链接检查器损坏
查看>>
npm 查看npm安装地址_慢速测量以测量NPM安装时间
查看>>
find命令递归查找_从命令行递归查找
查看>>
命令行查看wifi密码_从命令行获取WiFi密码
查看>>
虚拟机声音沙哑_防止沙哑的犯错
查看>>
css textarea_使用CSS自定义Textarea大小调整
查看>>
使用Wraith创建和比较网页屏幕截图
查看>>
css清除css样式_CSS选择样式
查看>>
唯一数组值
查看>>
使用JavaScript进行反向查询
查看>>