Microservice architecture case (02): business architecture design, system hierarchical management

Time:2020-1-1

Source code: GitHub, click here, gitee, click here

Update progress (6 sections in total):

01: brief introduction of project technology selection and schematic description of structure

02: business architecture design, system hierarchical management

1、 Business architecture design

1. Basic concepts

The architecture design of service determines the business support ability of software. Clear business design can help developers understand the system. In the process of business architecture design, we need to determine product design, framework building, service division and database planning according to user needs as the core direction. If the demand is relatively single and a single application service can be supported, it is not necessary to design a complex microservice system. If business concurrency will occur in a period of time according to the judgment of business, it is better to consider the business expansion and the support ability of the architecture at the beginning.

2. Case structure chart

Microservice architecture case (02): business architecture design, system hierarchical management

Based on the architecture design of the project, the following describes the business design one by one.

2、 Business process

1. Data warehousing service

  • Flow diagram

Microservice architecture case (02): business architecture design, system hierarchical management

  • Process description
1. Request warehousing service interface;
2. Search data, write to es server through feign interface of search service;
3. Write the same search data into the user database;
  • Program entry

Code package: mopsz data form

@RestController
@RequestMapping("/search/data")
public class SearchDataController {
    //Call search service feign interface
    @Resource
    private BookInfoEsFeign bookInfoEsFeign ;
    @Resource
    private BookInfoService bookInfoService ;
    @RequestMapping("/batchSave")
    public String batchSave (){
        //Omit business code
    }
}

2. User API service

  • Flow diagram

Microservice architecture case (02): business architecture design, system hierarchical management

  • Process description
1. The user requests to enter and the gateway service intercepts;
2. Call the token management service, verify the user's identity token, and use redis to store the token;
3. If the authentication passes, the gateway will release the user request;
4. Perform user search request processing;
5. Call es search service according to search conditions and return results;
6. Encapsulate the user's search action and request MQ service;
7. MQ service requests messages to be forwarded to data analysis service;
8. Data analysis service installation strategy analysis user request, storage analysis results;
  • Program entry

(1) , gateway interception

Code package: mopsz cloud gateway

@Component
public class FilterConfig extends ZuulFilter {
    public static final Logger LOGGER = LoggerFactory.getLogger(FilterConfig.class) ;
    private static final String GET_TOKEN = "/token/getToken";
    private static final String VERIFY_TOKEN = "/token/verifyToken";
    private static final String REFRESH_TOKEN = "/token/refreshToken";
    /**
     *Interception processing
     */
    @Override
    public Object run() throws ZuulException {
        RequestContext requestContext = RequestContext.getCurrentContext() ;
        try {
            doTokenProcess (requestContext);
        } catch (Exception e){
            Logger. Info ("exception: {}", e.getmessage());
            throw new ZuulException(e.getMessage(), 403, e.getMessage());
        }
        return null ;
    }
    public void doTokenProcess (RequestContext requestContext) throws Exception {
        HttpServletRequest request = requestContext.getRequest() ;
        String reqUri = request.getRequestURI() ;
        if (!reqUri.contains(GET_TOKEN)) {
            String token = request.getHeader("token") ;
            boolean flag = userTokenFeign.refreshToken(token) ;
            if (!flag){
                Throw new serviceexception ("token verification failed");
            }
            Logger.info ("token verification passed");
        }
    }
}

(2) , token management

Code package: mopsz basis token

@RestController
@RequestMapping("/token")
public class UserTokenController implements UserTokenFeign {
    @Resource
    private UserTokenService tokenService ;
    /**
     *Get token
     */
    @Override
    @RequestMapping("/getToken")
    public RespObject getToken (@RequestParam("userName") String userName,
                                @RequestParam("passWord") String passWord){
        try {
            String token = tokenService.getToken(userName,passWord) ;
            return RespObject.ok().put(Constant.MAP_KEY,token) ;
        } catch (Exception e){
            e.printStackTrace();
            return RespObject.error() ;
        }
    }
    /**
     *Verify token
     */
    @Override
    @RequestMapping("/verifyToken")
    public RespObject verifyToken(String token) {
        try {
            Integer userId = tokenService.verifyToken(token) ;
            return RespObject.ok().put(Constant.MAP_KEY,userId) ;
        } catch (Exception e){
            e.printStackTrace();
            return RespObject.error() ;
        }
    }
    /**
     *Refresh token
     */
    @Override
    @RequestMapping("/refreshToken")
    public boolean refreshToken(String token) {
        try {
            return tokenService.refreshToken(token) ;
        } catch (Exception e){
            e.printStackTrace();
            return false ;
        }
    }
}

(3) , search interface

Code package: mopsz user client

@RestController
@RequestMapping("/search/book/")
public class BookSearchController {
    @Resource
    private BookInfoEsFeign bookInfoEsFeign ;
    @Resource
    private UserSearchFeign userSearchFeign ;
    /**
     *Keyword full text search
     */
    @RequestMapping("/getByKeyWord")
    public List<EsBookInfo> getByKeyWord (@RequestParam("keyWord") String keyWord,
                                          @RequestParam("userId") Integer userId){
        //Search engine execution
        List<EsBookInfo> esBookInfoList = bookInfoEsFeign.getByKeyWord(keyWord) ;
        //Perform asynchronous analysis
        if (StringUtils.isNotEmpty(keyWord) && esBookInfoList != null){
            KeySearchModel keySearchModel = new KeySearchModel() ;
            keySearchModel.setUserId(userId);
            keySearchModel.setKeyWord(keyWord);
            keySearchModel.setSearchResult(esBookInfoList);
            userSearchFeign.sendBookSearch(JsonUtil.objToJson(keySearchModel));
        }
        return esBookInfoList ;
    }
}

(4) . request analysis

Code package: mopsz data anly

@RestController
public class BookEsDataController implements BookEsAnalyFeign {
    @Resource
    private BookEsDataService bookEsDataService ;
    @Override
    public void sendBookEsMsg(String msgBody) {
        bookEsDataService.saveBookEsData(msgBody);
    }
}

3. System management service

  • Flow diagram

Microservice architecture case (02): business architecture design, system hierarchical management

  • Process description

Integrate the development of spring security, JWT and other components. Code package: mopsz admin client.

1. System user login, security configuration: securityconfig;
2. Log in successfully: loginsuccesshandler;
3. Login failure handling: loginfailhandler;
4. The system service has the functions of management: business database, cache data, ES service, etc;

4. Data analysis service

  • Flow diagram

Microservice architecture case (02): business architecture design, system hierarchical management

  • Process description

Code package: mopsz data anly

1. Receive the data analysis request forwarded by MQ service;
2. Analyze data and store analysis results according to specified policies;

3、 System hierarchical management

1. Layering diagram

Microservice architecture case (02): business architecture design, system hierarchical management

2. Structure level description

  • Common code block layer

Management system tool class, data table structure entity class, mapper layer, service layer, etc. are divided according to database service planning as the common dependency code block of the whole system.

  • Data management

Management data warehousing service API and data analysis service API are two modules.

  • Client interface layer

Manage user service API, background system service API.

  • Microservice interface layer

The call between microservices adopts the way of feign interface, which is encapsulated according to the feign interface provided by different services, and managed uniformly in this layer to make the request call between services.

  • General business service layer

Manage the general Token service and message sending service as the business service layer shared by the system.

  • Middleware service layer

Manage rocketmq Message Queuing service, redis cache service, quart timer service, ES search service, and provide unified feign service interface.

  • Microservice component layer

Manage the basic micro service components of the whole system, such as gateway interception service, Eureka service registration and discovery.

4、 Source code address

GitHub · address
https://github.com/cicadasmile/husky-spring-cloud
Gitee · address
https://gitee.com/cicadasmile/husky-spring-cloud

Microservice architecture case (02): business architecture design, system hierarchical management