Skip to content

Commit

Permalink
version 1.0.3.0 update
Browse files Browse the repository at this point in the history
完善抢购微服务接口, 修复若干BUG
  • Loading branch information
ZongXR committed Apr 4, 2022
1 parent 7d17d44 commit bfc7139
Show file tree
Hide file tree
Showing 22 changed files with 164 additions and 85 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,17 @@
<li>修正检索微服务和商品微服务调用异常的BUG</li>
</ul>
</td>
<td>2022年4月1日</td>
</tr>
<tr>
<td>1.0.3.0</td>
<td>
<ul>
<li>完善抢购微服务接口</li>
<li>修复若干BUG</li>
</ul>
</td>
<td>2022年4月4日</td>
</tr>
</table>

Expand Down Expand Up @@ -532,6 +543,7 @@
<td>body</td><td>无</td><td><code>@RequestBody</code></td>
</tr>
</table></li>
<li><code>@ExceptionHandler</code>注解修饰的方法别忘了加<code>@ResponseBody</code>注解,否则捕获不到异常</li>
</ul>
</body>
</html>
2 changes: 1 addition & 1 deletion cart/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<systemPath>${project.basedir}/../common/target/supermarket-common.jar</systemPath>
<groupId>com.supermarket.common</groupId>
<artifactId>supermarket-common</artifactId>
<version>1.0-SNAPSHOT</version>
<version>1.1.0.0</version>
<exclusions>
<exclusion>
<groupId>com.fasterxml.jackson.dataformat</groupId>
Expand Down
2 changes: 1 addition & 1 deletion common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>${project.parent.artifactId}-common</artifactId>
<groupId>com.supermarket.common</groupId>
<version>1.0-SNAPSHOT</version>
<version>1.1.0.0</version>
<packaging>jar</packaging>

<dependencies>
Expand Down
42 changes: 8 additions & 34 deletions common/src/main/java/com/supermarket/common/vo/CommonResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;

import java.util.List;

/**
* 通用返回对象
*/
@Setter
@Getter
@ApiModel(description = "vo对象")
public class CommonResult<T> {

Expand All @@ -21,26 +25,20 @@ public class CommonResult<T> {
* 提示信息
*/
@ApiModelProperty("传送的信息")
private String msg;
private String message;

/**
* 数据封装
*/
@ApiModelProperty("传送的数据")
private T data;

/**
* 封装的一堆数据
*/
@ApiModelProperty("封装的一堆数据")
private List<T> dataList;


protected CommonResult() {
}

protected CommonResult(Integer status, String message, T data) {
public CommonResult(Integer status, String message, T data) {
this.status = status;
this.msg = message;
this.message = message;
this.data = data;
}

Expand Down Expand Up @@ -132,28 +130,4 @@ public static <T> CommonResult<T> unauthorized(T data) {
public static <T> CommonResult<T> forbidden(T data) {
return new CommonResult<T>(ResultCode.FORBIDDEN.getCode(), ResultCode.FORBIDDEN.getMessage(), data);
}

public long getCode() {
return this.status;
}

public void setCode(Integer status) {
this.status = status;
}

public String getMessage() {
return this.msg;
}

public void setMessage(String message) {
this.msg = message;
}

public T getData() {
return data;
}

public void setData(T data) {
this.data = data;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.supermarket.common.test;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.supermarket.common.vo.CommonResult;
import org.junit.Test;

public class CommonResultTest {

@Test
public void testCommonResult() throws JsonProcessingException {
CommonResult<Object> result = CommonResult.failed("怎么回事?");
ObjectMapper mapper = new ObjectMapper();
String s = mapper.writeValueAsString(result);
System.out.println(s);
}
}
2 changes: 1 addition & 1 deletion image/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<systemPath>${project.basedir}/../common/target/supermarket-common.jar</systemPath>
<groupId>com.supermarket.common</groupId>
<artifactId>supermarket-common</artifactId>
<version>1.0-SNAPSHOT</version>
<version>1.1.0.0</version>
<exclusions>
<exclusion>
<groupId>com.fasterxml.jackson.dataformat</groupId>
Expand Down
14 changes: 12 additions & 2 deletions instant-buy/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>${project.parent.artifactId}-instantbuy</artifactId>
<groupId>com.supermarket.instantbuy</groupId>
<version>1.0-SNAPSHOT</version>
<version>1.1.0.0</version>
<packaging>jar</packaging>

<dependencies>
Expand All @@ -20,7 +20,7 @@
<systemPath>${project.basedir}/../common/target/supermarket-common.jar</systemPath>
<groupId>com.supermarket.common</groupId>
<artifactId>supermarket-common</artifactId>
<version>1.0-SNAPSHOT</version>
<version>1.1.0.0</version>
<exclusions>
<exclusion>
<groupId>com.fasterxml.jackson.dataformat</groupId>
Expand Down Expand Up @@ -130,6 +130,16 @@
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
</dependency>
<!-- common-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<!-- 监控 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.supermarket.instantbuy.config;


import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

/**
* swagger配置
* http://192.168.137.147:10008/swagger-ui/
* 可访问接口文档
*/
@Configuration
public class SwaggerConfig {

@Value("server.port")
private String port = null;

@Bean
public Docket createRestApi() {
// 创建 Docket 对象
return new Docket(DocumentationType.SWAGGER_2) // 文档类型,使用 Swagger2
.apiInfo(this.apiInfo()) // 设置 API 信息
// 扫描 Controller 包路径,获得 API 接口
.select()
.apis(RequestHandlerSelectors.basePackage("com.supermarket.instantbuy.controller"))
.paths(PathSelectors.any())
// 构建出 Docket 对象
.build();
}

/**
* 创建 API 信息
*/
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("抢购微服务应用接口文档")
.description("后台文档")
.version("1.1.0.0") // 版本号
.contact(new Contact("宗祥瑞", "https://github.com/GoogleLLP/SuperMarket", "zxr@tju.edu.cn")) // 联系人
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package com.supermarket.instantbuy.controller;

import com.supermarket.common.domain.InstantBuyItem;
import com.supermarket.common.vo.SysResult;
import com.supermarket.common.vo.CommonResult;
import com.supermarket.instantbuy.exception.MsgException;
import com.supermarket.instantbuy.service.InstantBuyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
Expand All @@ -18,40 +21,42 @@
@Api(tags = "抢购微服务")
//@RequestMapping("/instantbuy")
public class InstantBuyController {

private static final Logger LOGGER = LogManager.getLogger(InstantBuyController.class);

@Autowired
private InstantBuyService instantBuyService = null;

/**
* 查询所有秒杀商品
*
* @return 可以秒杀商品的列表
*/
@ApiOperation("查询所有秒杀商品")
@ApiOperation("查询所有秒杀商品,该接口慎用,将全表扫描")
@RequestMapping(value = "/manage/list", method = RequestMethod.GET)
@ResponseBody
public List<InstantBuyItem> queryItems() {
return this.instantBuyService.queryItems();
public CommonResult<List<InstantBuyItem>> queryItems() {
List<InstantBuyItem> result = this.instantBuyService.queryItems();
return CommonResult.success(result);
}

/**
* 查询一个秒杀商品
*
* @param itemId 秒杀商品id
* @return 秒杀商品
*/
@ApiOperation("查询一个秒杀商品")
@ApiImplicitParam(name = "itemId", value = "商品id")
@RequestMapping(value = "/manage/detail", method = RequestMethod.GET)
@ResponseBody
public InstantBuyItem queryItem(
public CommonResult<InstantBuyItem> queryItem(
@RequestParam("itemId") String itemId
) {
return this.instantBuyService.queryItem(itemId);
InstantBuyItem result = this.instantBuyService.queryItem(itemId);
return CommonResult.success(result);
}

/**
* 发起秒杀
*
* @param itemId 商品id
* @param userName 用户名
* @return vo
Expand All @@ -63,20 +68,29 @@ public InstantBuyItem queryItem(
})
@RequestMapping(value = "/manage/{itemId}/{userName}", method = RequestMethod.GET)
@ResponseBody
public SysResult startBuy(
public CommonResult<?> startBuy(
@PathVariable("itemId") String itemId,
@PathVariable("userName") String userName
) {
try {
if (userName == null)
throw new MsgException("用户尚未登录");
this.instantBuyService.startBuy(itemId, userName);
return SysResult.ok();
} catch (MsgException e) {
return SysResult.build(201, e.getMessage(), e);
} catch (Exception e) {
e.printStackTrace();
return SysResult.build(500, e.getMessage(), e);
}
if (userName == null)
return CommonResult.unauthorized("用户尚未登录");
this.instantBuyService.startBuy(itemId, userName);
return CommonResult.success("开始抢购");
}

/**
* 统一的异常处理器
* @param e 异常
* @return 返回结果
*/
@ExceptionHandler(Throwable.class)
@ResponseBody
public CommonResult<Object> handleException(
Exception e
){
InstantBuyController.LOGGER.error(ExceptionUtils.getStackTrace(e));
if (e instanceof MsgException)
return new CommonResult<Object>(201, e.getMessage(), e);
return CommonResult.failed(e.getMessage());
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package com.supermarket.instantbuy.schedule;
package com.supermarket.instantbuy.task;

import com.supermarket.common.domain.InstantBuyItem;
import com.supermarket.instantbuy.dao.InstantBuyDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
Expand Down
7 changes: 6 additions & 1 deletion instant-buy/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,9 @@ spring.rabbitmq.password=guest

# 配置微服务
spring.application.name=instantbuy
eureka.client.service-url.defaultZone=http://supermarket-eureka-1:10000/eureka
eureka.client.service-url.defaultZone=http://supermarket-eureka-1:10000/eureka

# 监控
management.endpoints.web.exposure.include=*
management.endpoint.shutdown.enabled=true
management.endpoint.health.show-details=always
2 changes: 1 addition & 1 deletion order/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<systemPath>${project.basedir}/../common/target/supermarket-common.jar</systemPath>
<groupId>com.supermarket.common</groupId>
<artifactId>supermarket-common</artifactId>
<version>1.0-SNAPSHOT</version>
<version>1.1.0.0</version>
<exclusions>
<exclusion>
<groupId>com.fasterxml.jackson.dataformat</groupId>
Expand Down
2 changes: 1 addition & 1 deletion product/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<systemPath>${project.basedir}/../common/target/supermarket-common.jar</systemPath>
<groupId>com.supermarket.common</groupId>
<artifactId>supermarket-common</artifactId>
<version>1.0-SNAPSHOT</version>
<version>1.1.0.0</version>
<exclusions>
<exclusion>
<groupId>com.fasterxml.jackson.dataformat</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ public CommonResult<List<Product>> queryProducts(){
* @return 返回结果
*/
@ExceptionHandler
@ResponseBody
public CommonResult<Object> handleException(
Exception e
){
Expand Down
Loading

0 comments on commit bfc7139

Please sign in to comment.