GithubHelp home page GithubHelp logo

suguyun / gin-api Goto Github PK

View Code? Open in Web Editor NEW

This project forked from why444216978/gin-api

1.0 0.0 0.0 1.36 MB

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

License: MIT License

Dockerfile 0.25% Go 99.52% Makefile 0.23%

gin-api's Introduction

gin-api

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

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

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

联系我

QQ群:909211071
个人QQ:444216978
微信:AirGo___

目前已支持

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

后续逐渐支持

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

目录结构

- gin-api 
  - app //用户应用目录
    - conf //服务配置文件目录
      - dev
      - liantiao
      - online
      - qa
    - config //启动加载配置目录
      - app.go //应用配置
    - loader //资源加载
    - resource
      - resource.go //全局资源
    - response
      - response.go //http响应
    - router
      router.go //路由定义和中间件注册
    - rpc //三方rpc调用封装
      - gin-api //gin-api服务
    - module //各模块核心实现,按照业务边界划分目录
      - module1 //模块1
        - api //对外暴露api
        - job //离线任务
        - responsitory //存储层
        - service //核心业务代码
      - module1 //模块2
        - api //对外暴露api
        - job //离线任务
        - responsitory //存储层
        - service //核心业务代码
    - main.go //app入口文件
  - bootstrap //应用启动
  - client
    - codec //编码
    - grpc //grpc客户端
    - http //http客户端
  - server
    - grpc //grpc服务端
    - http //http服务端
  - library //基础组件库,不建议修改
    - apollo //阿波罗
    - cache //分布式缓存
    - config //配置加载
    - endless //endless
    - etcd //etcd
    - grpc //grpc封装
    - jaeger //jaeger分布式链路追踪
    - job //离线任务
    - lock //分布式锁
    - logger //日志
    - orm //db orm
    - rabbitmq //rabbitmq
    - redis //redis
    - registry //注册中心
    - selector //负载均衡器
    - servicer //下游服务
  .gitignore
  Dockerfile
  LICENSE
  Makefile
  README.md
  go.mod
  go.sum

配置相关

基于三方组件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/app$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}

gin-api's People

Contributors

why444216978 avatar maolonglong avatar

Stargazers

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