Giter Site home page Giter Site logo

graduate's Introduction

评论系统优化

原项目中app与网页等不同主题内容皆需要评论,现参照评论系统设计将评论系统独立出来,作为公共的服务

项目结构参照 beer-shop 目录结构,基于 kratos 框架搭建

1. 项目模块

  • account-service 账户服务
  • comment-service 评论服务
  • comment-job 评论消息消费任务
  • baseapp-interface BFF层

2. 依赖组件

  • consul 服务注册发现
  • kafka 消息队列
  • redis 缓存
  • gorm orm框架

3. 并行请求示例

// 查询评论信息后,查询相关的用户信息以及评论点赞信息
func (uc *CommentUsecase) GetCommentList(ctx context.Context, subject *CommentSubject,
	page, size, replyCount int) ([]*Comment, error) {
	comments, err := uc.repo.GetCommentList(ctx, subject, page, size, replyCount)
	if err != nil {
		return nil, err
	}
	// 并发查询附加信息
	group, _ := errgroup.WithContext(ctx)
	var accounts []*Account
	likedMap := make(map[uint64]bool)
	group.Go(func() error {
		accounts, err = uc.accountRepo.ListByIds(ctx, getCommentMemberIds(comments))
		return err
	})
	// 如果有用户信息,查询用户是否点过赞
	if uid, err := token.ExtractUid(ctx); err == nil {
		group.Go(func() error {
			temp, err := uc.repo.GetLikeItem(ctx, uid, getCommentIds(comments))
			likedMap = temp
			return err
		})
	}
	if err = group.Wait(); err != nil {
		return nil, err
	}
	concatMemberInfo(comments, accounts, likedMap)
	return comments, nil
}

4. 错误码使用

接口使用protobuf定义错误,并在接口返回时使用,系统内部使用 kratos errors.new(code, reason, message)方法包装错误,依据code进行判定。 数据库错误将被包装,避免orm框架的错误传播,产生强依赖。

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.