Coder Social home page Coder Social logo

caohui123 / gin-api Goto Github PK

View Code? Open in Web Editor NEW

This project forked from why444216978/gin-api

0.0 0.0 0.0 1.05 MB

GoLang 微服务框架,封装各种常用组件,包括配置、日志、DB、RabbitMQ、Redis、缓存处理(解决缓存穿透、击穿、雪崩)、服务注册、服务发现、分布式锁、分布式链路追踪等,目的在于提高Go应用开发效率。

License: MIT License

Dockerfile 0.20% Go 99.57% Makefile 0.23%

gin-api's Introduction

gin-api

基于 Gin 开发的微服务框架,封装各种常用组件,目的在于提高 Go 应用开发效率。

如果您对本框架有任何意见或建议,欢迎随时通过以下方式反馈和完善:

  1. 提 issues 反馈
  2. 通过下方的联系方式直接联系我
  3. 提 PR 共同维护

目前已支持

✅  多格式配置读取
✅  服务优雅关闭
✅  进程结束资源自动回收
✅  日志抽象和标准字段统一(请求、DB、Redis、RPC)
✅  DB
✅  RabbitMQ
✅  Redis
✅  分布式缓存(解决缓存穿透、击穿、雪崩)
✅  分布式链路追踪
✅  分布式锁
✅  服务注册
✅  服务发现
✅  负载均衡
✅  HTTP-RPC 超时传递

后续逐渐支持

gRPC
端口多路复用
日志收集
监控告警
限流
熔断

目录结构

- gin-api 
  - api //对外暴露api
    - module1 //模块1
    - module2 //模块2
  - app //应用启动
    - app.go //app启动和优雅关闭
    - init.go //资源初始化
  - conf //服务配置文件目录
    - dev
    - liantiao
    - online
    - qa
  - config //启动加载配置目录
    - app.go //应用配置
  - library //基础组件库,不建议修改
    - apollo //阿波罗
    - cache //分布式缓存
    - config //配置加载
    - endless //endless
    - etcd //etcd
    - grpc //grpc封装
    - http //http常用工具函数
    - jaeger //jaeger分布式链路追踪
    - job //离线任务
    - lock //分布式锁
    - logging //日志
    - middleware //中间件
    - orm //db orm
    - rabbitmq //rabbitmq
    - redis //redis
    - registry //注册中心
    - rpc //rpc调用封装
      - grpc //grpc
      - http //http
    - selector //负载均衡器
    - servicer //下游服务
    - xhop //rpc xhop
  - resource
    - resource.go //全局资源
  - response
    - response.go //http响应
  - router
    router.go //路由定义和中间件注册
  - rpc //三方rpc调用封装
    - gin-api //gin-api服务
  - services //模块核心实现
    - goods //商品模块
    - test //测试模块
    .gitignore
    Dockerfile
    LICENSE
    Makefile
    README.md
    go.mod
    go.sum
    main.go

配置相关

基于三方组件viper,文件配置需放到main.go同级目录conf/xxx下

统一日志

基于 zap 二次封装,抽象统一接口、数据库、缓存、RPC 调用日志结构,便于后期日志收集和搜索

服务发现

目前支持 etcd, 集群搭建教程,相关配置无需更改的情况下,按照教程搭建运行即可。

测试

检测接口:http://localhost:777/ping
panic 接口:http://localhost:777/test/panic
db 和 redis测试接口:http://localhost:777/test/conn (依赖 mysql 和 redis)
分布式链路追踪+服务注册+服务发现接口:http://localhost:777/test/rpc (依赖 mysql 和 redis)

分布式链路追踪

jaeger


运行

  1. app 目录下的资源初始化和服务注册是留给开发者自己扩展的,您可自行调整资源加载。
  2. 查看 conf/xxx 目录的各个配置文件,改成符合自己的
  3. log 配置中的目录确保本地存在且有写入权限
  4. go run main.go -env dev(不带 -env 参数默认用 dev 配置)

注意:测试 /test/conn 和 /test/rpc 接口时,应完成如下几项:

  1. 创建 test 库
  2. 创建 test 表并随意插入数据
CREATE TABLE `test` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `goods_id` bigint(20) unsigned NOT NULL,
  `name` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL DEFAULT '',
  PRIMARY KEY (`id`),
  KEY `idx_goods` (`goods_id`)
) ENGINE=InnoDB AUTO_INCREMENT=29 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin 
[why@localhost] ~/Desktop/go/gin-api$go run main.go -env dev
Actual pid is 34592
The environment is :dev
[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
 - using env:   export GIN_MODE=release
 - using code:  gin.SetMode(gin.ReleaseMode)

[GIN-debug] GET    /ping                     --> github.com/why444216978/gin-api/api/ping.Ping (5 handlers)
[GIN-debug] POST   /test/rpc                 --> github.com/why444216978/gin-api/api/test.Rpc (5 handlers)
[GIN-debug] POST   /test/rpc1                --> github.com/why444216978/gin-api/api/test.Rpc1 (5 handlers)
[GIN-debug] POST   /test/panic               --> github.com/why444216978/gin-api/api/test.Panic (5 handlers)
[GIN-debug] POST   /test/conn                --> github.com/why444216978/gin-api/api/conn.Do (5 handlers)
start by server
Start with :777
watching prefix:gin-api now...
service gin-api  put key: gin-api.192.168.1.104.777 val: {"Host":"192.168.1.104","Port":777}

联系我

QQ群:909211071
个人QQ:444216978
微信:AirGo___
有疑问随时联系本人

gin-api's People

Contributors

why444216978 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.