Giter Site home page Giter Site logo

micro-service-sso's Introduction

目标

集群秒杀

前端工程

page/seckill

已配置反向代理到Zuul中

有同学知道如何使用gateway做服务网关,通过Security认证可以帮忙配置下

UI框架

mint-ui

服务端启动顺序

  1. config-server

    配置中心

    配置读取规则:

    http://localhost:8000/sso/application-dev.yml

    http://localhost:8000/分支/应用名称-配置.yml

  2. eureka

    注册中心

  3. zuul

  4. oauth-jwt

  5. provide-user

  6. provide-sales

订单前置服务

RESTful API 设计指南

RESTful API 设计指南

本工程中遵循的指南

参考provide-sales工程

编码规划

Java

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));
  }
}
  1. 使用 @RestController@RequestMapping("account")标记入口类
  2. 使用文档【五、HTTP动词、六、过滤信息(Filtering)】)中的要求设计接口访问方式和参数传递方式
  3. 使用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

JavaScript

  1. 判断返回状态码
  2. 按照规定判断,并且与接口约定各个状态码都代表什么返回提示,在前端代码中写入提示
  3. 如:
/**
 * 获取账户信息
 * @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}`)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.