集群秒杀
page/seckill
已配置反向代理到Zuul中
有同学知道如何使用gateway做服务网关,通过Security认证可以帮忙配置下
-
config-server
配置中心
配置读取规则:
http://localhost:8000/sso/application-dev.yml
http://localhost:8000/分支/应用名称-配置.yml
-
eureka
注册中心
-
zuul
-
oauth-jwt
-
provide-user
-
provide-sales
订单前置服务
参考
provide-sales
工程
package cn.com.jonpad.providesales.controller;
import org.springframework.http.ResponseEntity;
/**
* @author Jon Chan
* @date 2019/1/27 16:54
*/
@RestController
@RequestMapping("account")
public class AccountController {
@Autowired
AccountService accountService;
@GetMapping("{id}")
@ApiOperation(value = "获取一个用户的账号", notes = "获取一个用户的账号")
public ResponseEntity<Account> getOne(
@ApiParam("搜索关键字")
@PathVariable("id") Long userId
){
return ResponseEntity.ok(accountService.getOne(userId));
}
}
- 使用
@RestController
和@RequestMapping("account")
标记入口类 - 使用文档【五、HTTP动词、六、过滤信息(Filtering)】)中的要求设计接口访问方式和参数传递方式
- 使用Exception抛出异常,而不要手动返回异常描述,如下:
@ResponseStatus(value = HttpStatus.NOT_FOUND)
public class NotFoundException extends RuntimeException {}
package cn.com.jonpad.providesales.service;
@Service
public class AccountService {
@Autowired
AccountRepository accountRepository;
public Account getOne(Long userId){
QAccount qAccount = QAccount.account;
Predicate predicate = qAccount.userId.eq(userId);
Optional<Account> one = accountRepository.findOne(predicate);
if (one.isPresent()) {
return one.get();
}else {
throw new NotFoundException();
}
}
}
NotFoundException
中使用@ResponseStatus
注解,并且规定了返回值HttpStatus
,HttpStatus
取值请参照文档文档【七、状态码(Status Codes)】)中的要求设置 此时使用HTTP访问将会返回:
Http Status : 404 Not Found
- 判断返回状态码
- 按照规定判断,并且与接口约定各个状态码都代表什么返回提示,在前端代码中写入提示
- 如:
/**
* 获取账户信息
* @param userId
*/
export function getAccount(userId) {
return request({
url: `${SALES_SERVICE}/account/${userId}`,
method: 'get',
errorDefined: {
404: `用户未找到`
}
})
}
let msg = '操作失败,请重试'
if (error.response && error.response.status) {
if (error.config.errorDefined) {
msg = error.config.errorDefined[error.response.status]
}
}
MessageBox.alert(`${msg}`)