Giter Site home page Giter Site logo

starryrbs / kfan Goto Github PK

View Code? Open in Web Editor NEW
42.0 42.0 7.0 1.66 MB

基于Golang+Kratos+MySQL+Redis+Kafka+elk+Opentracing实现的微服务项目

License: MIT License

Dockerfile 2.00% Makefile 9.27% Go 71.97% HTML 0.92% JavaScript 4.57% CSS 0.66% Vue 10.61%
go kratos microservice

kfan's Introduction

看房网

基于Golang+Kratos+MySQL+Redis+Kafka+elk+Opentracing实现的微服务项目

效果展示

功能描述

  • 房源列表
  • 房源详情 (TODO)
  • 点击房源title,代表查看了房源,会生成一条查看历史记录
  • 输入用户名进行登录
  • 看房历史记录 (TODO)

技术点

  • 微服务架构(BFF、Service、Admin、Job、Task 分模块)
  • API 设计(包括 API 定义、错误码规范、Error 的使用)
  • gRPC 的使用
  • Go 项目工程化(项目结构、DI、代码分层、ORM 框架)
  • 并发的使用(errgroup 的并行链路请求)
  • 微服务中间件的使用(ELK、Opentracing、Prometheus、Kafka) (链路追踪)
  • 缓存的使用优化(一致性处理、Pipeline 优化)

项目目录结构

.
├── README.md
├── api
│   ├── account 
│   ├── history
│   └── house
├── app
│   ├── account
│   ├── history
│   └── house
├── deploy
│   └── docker
├── docs
│   └── images
├── gen
├── go.mod
├── go.sum
├── openapi.yaml
├── pkg
│   └── job
├── third_party
│   ├── README.md
│   ├── errors
│   ├── google
│   ├── protoc-gen-openapiv2
│   └── validate
└── web
    ├── admin
    └── kfan

微服务项目结构

通过kratos构建工具生成,新增了job子目录

.
├── Dockerfile
├── Makefile
├── README.md
├── cmd
│   ├── history
│   └── job
├── configs
│   └── config.yaml
├── generate.go
└── internal
    ├── biz
    ├── conf
    ├── data
    ├── job 
    ├── server
    └── service

运行

  1. docker-compose运行
cd deploy/docker && make kfan-run
  1. 清除容器
make clean

并发的使用

errgroup 的并行链路请求

并行请求多个服务获取详情信息

代码位置

eg, ctx := errgroup.WithContext(ctx)
for _, history := range histories {
  history := history
  eg.Go(func() error {
      house, err := h.data.h1.GetHouse(ctx, &housepb.GetHouseRequest{Id: history.ObjId})
      if err != nil {
          return err
      }
      history.ObjDetail = structs.Map(house)
      return nil
  })
  eg.Go(func() error {
      account, err := h.data.a1.GetAccount(ctx, &accountpb.GetAccountRequest{Id: int32(history.UserId)})
      if err != nil {
          return err
      }
      history.Username = account.GetName()
      return nil
  })
}
err = eg.Wait()

开发命令

  1. 生成proto代码

    kratos proto client api/account/service/v1/account.proto

docker部署

  1. 创建volume
docker volume create kfan-db

调试

  1. SaveHistory
grpcurl -d '{"obj_id": 2,"obj_type": "house","user_id": 1}' -plaintext 127.0.0.1:9002 api.history.service.v1.History/SaveHistory
  1. GetHistory
grpcurl -d '{"user_id": 1}' -plaintext 127.0.0.1:9002 api.history.service.v1.History/GetHistory

kfan's People

Contributors

starryrbs avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

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.