轻量级 go 业务框架。

Overview

Sniper['snaɪpər] 轻量级业务框架

Sniper 的前世今生

有兴趣的同学也可以加我的微信haitaotao我拉大家进群讨论。

系统要求

  1. 类 UNIX 系统
  2. go v1.12+
  3. protoc
  4. protoc-gen-go

目录结构

├── cmd         # 服务子命令
├── dao         # 数据访问层
├── main.go     # 项目总入口
├── rpc         # 接口描述文件
├── server      # 控制器层
├── service     # 业务逻辑层
├── sniper.toml # 配置文件
└── util        # 业务工具库

快速入门

Comments
  • twirp error unknown: failed to write response, 0 of 59 bytes written: http: Handler timeout

    twirp error unknown: failed to write response, 0 of 59 bytes written: http: Handler timeout

    获取数据并返回Todo的对象的json的代码块

    func (s *Server) Read(ctx context.Context, req *pb.ReadRequest) (*pb.ReadResponse, error) {
    	var ida int64
    	var title string
    	var desc string
    	result, err := todo.Read(1)
    
    	defer result.Close()
    	for result.Next() {
    		result.Scan(&ida, &title, &desc)
    		break
    	}
    	log.Get(ctx).Infof("%d %s %s", ida, title, desc)
    
    	todo := &pb.ToDo{Id: ida, Title: title, Description: desc}
    	resp := &pb.ReadResponse{Api: "1", ToDo: todo}
    	log.Get(ctx).Infof("todo=%v", todo)
    	log.Get(ctx).Infof("resp=%v", resp)
    	return resp, err
    }
    
    

    程序执行结果的部分,todo和resp的结果都OK,write response时提示超时的错误信息。

    INFO[0016] todo=id:1 title:"a" description:"b"           app_id=localapp device="<nil>" env=dev instance_id=mac.local mobi_app="<nil>" platform="<nil>" trace_id=bc7f7c4690d3ef5 uid="<nil>" version="<nil>"
    INFO[0016] resp=api:"1" toDo:<id:1 title:"a" description:"b" >   app_id=localapp device="<nil>" env=dev instance_id=mac.local mobi_app="<nil>" platform="<nil>" trace_id=bc7f7c4690d3ef5 uid="<nil>" version="<nil>"
    ERRO[0176] twirp error unknown: failed to write response, 0 of 59 bytes written: http: Handler timeout  app_id=localapp device="<nil>" env=dev instance_id=mac.local mobi_app="<nil>" platform="<nil>" trace_id=bc7f7c4690d3ef5 uid="<nil>" version="<nil>"
    
    

    service.twirp.go的代码出现的错误 函数:serveReadForm

    	if n, err := resp.Write(respBytes); err != nil {
    		msg := fmt.Sprintf("failed to write response, %d of %d bytes written: %s", n, len(respBytes), err.Error())
    
    opened by andyjia 24
  • 脚手架使用单独入口之后出现问题

    脚手架使用单独入口之后出现问题

    安装所需扩展、执行git clone https://github.com/bilibili/sniper.git .、执行go run cmd/sniper/main.go rpc --service=foo均无报错. 但执行go run main.go server --port=8080时出错 build command-line-arguments: cannot load sniper/cmd/falsework: malformed module path "sniper/cmd/falsework": missing dot in first path element 删除掉main.go的falsework内容后重新执行,出错改为

    # sniper/cmd/server
    cmd/server/http.go:28:33: cannot use hooks (type *"github.com/bilibili/twirp".ServerHooks) as type *"github.com/twitchtv/twirp".ServerHooks in argument to foo_v1.NewFooServer
    cmd/server/main.go:137:9: too many arguments in call to initMux
    	have (*http.ServeMux, bool)
    	want (*http.ServeMux)
    

    请问如何解决?望回复,谢谢

    opened by ghost 7
  • service层的作用是什么,rpc参数是推荐传递到dao层吗?

    service层的作用是什么,rpc参数是推荐传递到dao层吗?

    GetArticleDetail这个服务做例子: rpc GetArticleDetail(GetArticleDetailRequest) returns (GetArticleDetailResponse)

    目前sniper的访问顺序依次是: server(param GetArticleDetailRequest) --> service (param GetArticleDetailRequest)--> dao(param GetArticleDetailRequest)

    那么,

    1. service主要的角色是什么?负责处理的业务逻辑是哪块?和server层的分工是什么?
    2. 还有就是GetArticleDetailRequest这个rpc的参数推荐一直传递service和dao层吗?
    3. 这个service层能复用于多个server层吗?
    opened by flazx 6
  • codahale/hdrhistogram repo url has been transferred under the github HdrHstogram umbrella

    codahale/hdrhistogram repo url has been transferred under the github HdrHstogram umbrella

    Problem

    The codahale/hdrhistogram repo has been transferred under the github HdrHstogram umbrella with the help from the original author in Sept 2020 (new repo url https://github.com/HdrHistogram/hdrhistogram-go). The main reasons are to group all implementations under the same roof and to provide more active contribution from the community as the original repository was archived several years ago.

    The dependency URL should be modified to point to the new repository URL. The tag "v0.9.0" was applied at the point of transfer and will reflect the exact code that was frozen in the original repository.

    If you are using Go modules, you can update to the exact point of transfer using the @v0.9.0 tag in your go get command.

    go mod edit -replace github.com/codahale/hdrhistogram=github.com/HdrHistogram/[email protected]
    

    Performance Improvements

    From the point of transfer, up until now (mon 16 aug 2021), we've released 3 versions that aim support the standard HdrHistogram serialization/exposition formats, and deeply improve READ performance. We recommend to update to the latest version.

    opened by filipecosta90 3
  • Cannot use 'loginHooks' (type *

    Cannot use 'loginHooks' (type *"github.com/bilibili/twirp".ServerHooks) as type *"github.com/twitchtv/twirp".ServerHooks

    路径:cmd/server/http.go

    错误信息:Cannot use 'loginHooks' (type *"github.com/bilibili/twirp".ServerHooks) as type *"github.com/twitchtv/twirp".ServerHooks

    如图: image

    opened by learninto 3
  • sniper中dao的model和proto自动生成的model如何映射?

    sniper中dao的model和proto自动生成的model如何映射?

    在使用过程中,发觉model不太好处理,我准备用的是http://gorm.io。目前方案有两种:

    1. 在dao中创建orm的model,然后和pb生成model做mapping,可以采用自动生成的orm model
    2. 直接采用pb生成model,但需要做protoc的custom tag 。 请问你们在实践过程中是怎么处理的?
    opened by flazx 3
  • 拜托分享sniper使用envoy的实践

    拜托分享sniper使用envoy的实践

    请问sniper在部署的时候如何使用envoy?是不是所有的http访问都先经过envoy,由envoy中转。能否分享一下sniper使用envoy的实践经验。由于envoy的文章很少,根据thought.md中的描述

    对于 redis 和 http 服务,我们用的是 envoy。 拜托分享一下envoy的实践。

    opened by flazx 3
  • 不清楚为什么install一直EOF

    不清楚为什么install一直EOF

    安装不了

    go install github.com/go-kiss/sniper/cmd/[email protected]
    go: github.com/go-kiss/sniper/cmd/[email protected]: module github.com/go-kiss/sniper/cmd/sniper: Get "https://proxy.golang.com.cn/github.com/go-kiss/sniper/cmd/sniper/@v/list": EOF
    
    opened by xiaods 2
  • 关于sniper的各层版本命名问题

    关于sniper的各层版本命名问题

    默认rpc下会生成版本v1,例如 app/v1/service.proto 对应的server会生成 --> appserver1 请问是不是应当对应的创建: rpc(app/v1/service.proto) --> server(appserver1)-->service(appservice1) --> dao(appdao1)

    opened by flazx 2
  • 实践中dao层如何复用?

    实践中dao层如何复用?

    我们在使用sniper的过程中,dao层的复用拿捏不太准,例如:有两个服务 --shopAdmin(商城后端服务接口) -- shopAdminDao --shopHome(商城前端服务接口) --shopHomeDao 在dao层有很多方法可以复用(例如:获取商品详情),这个应当通过[内部服务service的接口]去调用,还是dao层直接通过代码依赖来调用来实现代码的复用呢?那种调用方式更合理呢?

    opened by flazx 2
  • 新增错误消息返回方法,用于返回给前台业务错误

    新增错误消息返回方法,用于返回给前台业务错误

    为了避免与httpStatus错误冲突,错误码及错误信息将放在meta中。 返回 httpStatus 为 200。 返回案例如下: { "code": "", "msg": "", "meta": { "code": "@code", "msg": "@err" } }

    opened by learninto 1
Owner
keep it simple, stupid
null
🚀 gnet is a high-performance, lightweight, non-blocking, event-driven networking framework written in pure Go./ gnet 是一个高性能、轻量级、非阻塞的事件驱动 Go 网络框架。

English | ???? 中文 ?? Introduction gnet is an event-driven networking framework that is fast and lightweight. It makes direct epoll and kqueue syscalls

Andy Pan 7.2k Jan 2, 2023
一款依赖 etcd 作为注册中心的 Golang 轻量级 GRPC 框架

Golang 微服务 GRPC 标准框架(轻量级) 特性介绍 可使用 etcd 集群或单节点作为注册中心 客户端请求服务端自带负载均衡 服务端启动后自动向 etcd 注册,默认每 10s 进行一次心跳续租 自带优雅停止 panic recover 服务端无需指定启动端口,当然你也可以通过 WithP

兰陵美酒郁金香丶❀ 2 Nov 11, 2021