随笔分类 -  Go kit

摘要:还是三步骤创建EndPoint,创建Transport,调用请求 第一步创建transport package Services import ( "context" "encoding/json" "errors" "github.com/tidwall/gjson" "io/ioutil" "net/http" ) func Deco... 阅读全文
posted @ 2019-12-24 15:24 离地最远的星 阅读(908) 评论(1) 推荐(0) 编辑
摘要:token设置过期时间 package main import ( "fmt" "github.com/dgrijalva/jwt-go" "io/ioutil" "log" "time" ) type UserClaim struct { Uname string `json:"username"` jwt.... 阅读全文
posted @ 2019-12-24 13:36 离地最远的星 阅读(6455) 评论(0) 推荐(0) 编辑
摘要:生成公钥和私钥代码 package utils import ( "crypto/rand" "crypto/rsa" "crypto/x509" "encoding/pem" "fmt" "io/ioutil" ) func GenRSAPubAndPri(bits int,filepath string ) error { //... 阅读全文
posted @ 2019-12-24 12:09 离地最远的星 阅读(3869) 评论(0) 推荐(0) 编辑
摘要:使用第三方库jwt-go进行加密和验证 package main import ( "fmt" "github.com/dgrijalva/jwt-go" ) type UserClaim struct { Uname string `json:"username"` jwt.StandardClaims //嵌套了... 阅读全文
posted @ 2019-12-24 11:28 离地最远的星 阅读(694) 评论(0) 推荐(0) 编辑
摘要:使用中间件的方式包装日志输出 package Services import ( "context" "fmt" "github.com/go-kit/kit/endpoint" "github.com/go-kit/kit/log" "golang.org/x/time/rate" "gomicro/utils" "strconv" )... 阅读全文
posted @ 2019-12-24 00:51 离地最远的星 阅读(789) 评论(0) 推荐(0) 编辑
摘要:package Services import ( "context" "fmt" "github.com/go-kit/kit/endpoint" "github.com/go-kit/kit/log" "golang.org/x/time/rate" "gomicro/utils" "os" "strconv" ) typ... 阅读全文
posted @ 2019-12-23 23:15 离地最远的星 阅读(486) 评论(0) 推荐(0) 编辑
摘要:熔断器一般部署在客户端或者网关里面 封装到服务端 package main import ( "fmt" "github.com/afex/hystrix-go/hystrix" "gomicro2/util" "log" "time" ) func main() { configA := hystrix.CommandConfig{... 阅读全文
posted @ 2019-12-23 21:57 离地最远的星 阅读(375) 评论(0) 推荐(0) 编辑
摘要:package main import ( "fmt" "github.com/afex/hystrix-go/hystrix" "math/rand" "sync" "time" ) type Product struct { ID int Title string Price int } func getPro... 阅读全文
posted @ 2019-12-23 19:35 离地最远的星 阅读(1593) 评论(0) 推荐(0) 编辑
摘要:熔断器控制最大并发数 package main import ( "fmt" "github.com/afex/hystrix-go/hystrix" "math/rand" "sync" "time" ) type Product struct { ID int Title string Price int } fun... 阅读全文
posted @ 2019-12-23 16:41 离地最远的星 阅读(416) 评论(0) 推荐(0) 编辑
摘要:异步执行和服务降级,使用hystrix.Go()函数的返回值是chan err package main import ( "fmt" "github.com/afex/hystrix-go/hystrix" "math/rand" "time" ) type Product struct { ID int Title string ... 阅读全文
posted @ 2019-12-23 16:05 离地最远的星 阅读(343) 评论(0) 推荐(0) 编辑
摘要:在hystrix的超时回调函数中处理超时推荐其他商品 package main import ( "errors" "fmt" "github.com/afex/hystrix-go/hystrix" "math/rand" "time" ) type Product struct { ID int Title string ... 阅读全文
posted @ 2019-12-23 15:51 离地最远的星 阅读(402) 评论(0) 推荐(0) 编辑
摘要:## 使用hystrix来实现监控超时 ```go package main import ( "fmt" "github.com/afex/hystrix-go/hystrix" "math/rand" "time" ) type Product struct { ID int Title string Price int... 阅读全文
posted @ 2019-12-23 15:34 离地最远的星 阅读(789) 评论(0) 推荐(0) 编辑
摘要:简单来说就是浏览器想要拿到最终的结果需要需要经过A,B,C三个节点,A调用B,B调用C最终返回结果,但是如果C崩溃了,我们应该怎么做呢 先模拟一个延迟调用的例子 package main import ( "fmt" "math/rand" "time" ) type Product struct { ID int Title st... 阅读全文
posted @ 2019-12-23 15:20 离地最远的星 阅读(329) 评论(0) 推荐(0) 编辑
摘要:自定义错误结构体 package utils type MyError struct { Code int Message string } func NewMyError(code int, msg string) error { return &MyError{Code: code, Messa 阅读全文
posted @ 2019-12-23 14:31 离地最远的星 阅读(380) 评论(0) 推荐(0) 编辑
摘要:首先放上httptransport.NewServer的源碼 func NewServer( e endpoint.Endpoint, dec DecodeRequestFunc, enc EncodeResponseFunc, options ...ServerOption, //这里的不定长参数可以用来自定义错误处理 ) *Server { s := ... 阅读全文
posted @ 2019-12-23 14:13 离地最远的星 阅读(679) 评论(0) 推荐(0) 编辑
摘要:限流代码 package Services import ( "context" "errors" "fmt" "github.com/go-kit/kit/endpoint" "golang.org/x/time/rate" "gomicro/utils" "strconv" ) type UserRequest struct { /... 阅读全文
posted @ 2019-12-22 23:56 离地最远的星 阅读(420) 评论(0) 推荐(0) 编辑
摘要:package main import ( "fmt" "golang.org/x/time/rate" "time" ) func main() { r := rate.NewLimiter(1, 5) //1表示每次放进筒内的数量,桶内的令牌数是5,最大令牌数也是5,这个筒子是自动补充的,你只要取了令牌不管你取多少个,这里都会在每次取完后自动加1个进来,因为... 阅读全文
posted @ 2019-12-22 23:38 离地最远的星 阅读(818) 评论(0) 推荐(0) 编辑
摘要:package main import ( "context" "fmt" "golang.org/x/time/rate" "log" "time" ) func main() { r := rate.NewLimiter(1, 5) //1表示每次放进筒内的数量,桶内的令牌数是5,最大令牌数也是5,这个筒子是自动补充的,你只要... 阅读全文
posted @ 2019-12-22 22:34 离地最远的星 阅读(491) 评论(0) 推荐(0) 编辑
摘要:package main import ( "context" "fmt" "github.com/go-kit/kit/endpoint" "github.com/go-kit/kit/log" "github.com/go-kit/kit/sd" "github.com/go-kit/kit/sd/consul" "github.com... 阅读全文
posted @ 2019-12-22 22:07 离地最远的星 阅读(488) 评论(0) 推荐(0) 编辑
摘要:Client package main import ( "context" "fmt" "github.com/go-kit/kit/endpoint" "github.com/go-kit/kit/log" "github.com/go-kit/kit/sd" "github.com/go-kit/kit/sd/consul" "gi... 阅读全文
posted @ 2019-12-22 22:05 离地最远的星 阅读(830) 评论(0) 推荐(0) 编辑