12 2019 档案

摘要:编写Models.proto syntax = "proto3"; package Services; message ProdModel { // @inject_tag: json:"pid" int32 ProdID = 1; // @inject_tag: json:"pname" string ProdName = 2; } 编写ProdServ... 阅读全文
posted @ 2019-12-26 20:44 离地最远的星 阅读(553) 评论(0) 推荐(0) 编辑
摘要:调用http api:引入protobuf、生成参数和响应模型,定义proto文件 syntax = "proto3"; package Models; message ProdModel { // @inject_tag: json:"pid" int32 ProdID = 1; // @inje 阅读全文
posted @ 2019-12-26 18:18 离地最远的星 阅读(1249) 评论(0) 推荐(0) 编辑
摘要:处理模型中json tag不一致的问题 举个例子服务端返回的地址插件如下,使用的tag所以服务端返回的json字段名为pid,和pname type ProdModel struct { ProdId int `json:"pid"` ProdName string `json:"pname"` } 阅读全文
posted @ 2019-12-26 18:17 离地最远的星 阅读(625) 评论(0) 推荐(0) 编辑
摘要:定义proto文件 syntax = "proto3"; package Models; message ProdModel { //定义一个结构体,因为这里返回值是一个切片,作为返回值的元素,要和server中定义的返回值中的元素的字段是一样的 int32 ProdID = 1; string P 阅读全文
posted @ 2019-12-26 16:49 离地最远的星 阅读(626) 评论(0) 推荐(0) 编辑
摘要:我们先启动一个server然后再去调用server api package main import ( "github.com/gin-gonic/gin" "github.com/micro/go-micro/registry" "github.com/micro/go-micro/web" "g 阅读全文
posted @ 2019-12-26 16:19 离地最远的星 阅读(648) 评论(0) 推荐(0) 编辑
摘要:当服务很简单的时候可以用这种方法来做基本调用 package main import ( "fmt" "github.com/micro/go-micro/client/selector" "github.com/micro/go-micro/registry" "github.com/micro/ 阅读全文
posted @ 2019-12-26 14:49 离地最远的星 阅读(613) 评论(0) 推荐(0) 编辑
摘要:看下面的代码调用了server.Init()之后就支持命令行读取配置了 package main import ( "github.com/gin-gonic/gin" "github.com/micro/go-micro/registry" "github.com/micro/go-micro/w 阅读全文
posted @ 2019-12-25 16:23 离地最远的星 阅读(562) 评论(0) 推荐(0) 编辑
摘要:我们会使用主站api调用商品api 使用go-micro访问consul获取服务信息 package main import ( "fmt" "github.com/micro/go-micro/client/selector" "github.com/micro/go-micro/registry 阅读全文
posted @ 2019-12-25 16:09 离地最远的星 阅读(2634) 评论(0) 推荐(0) 编辑
摘要:安装go-plugins go get -v github.com/micro/go-plugins,原来go-micro consul的支持已经迁移到了go-plugins里面 package main import ( "github.com/gin-gonic/gin" "github.com 阅读全文
posted @ 2019-12-25 15:52 离地最远的星 阅读(1512) 评论(0) 推荐(0) 编辑
摘要:package main import ( "github.com/gin-gonic/gin" "github.com/micro/go-micro/web" ) func main() { ginRouter := gin.Default() ginRouter.Handle("GET", "/ 阅读全文
posted @ 2019-12-25 15:08 离地最远的星 阅读(629) 评论(0) 推荐(0) 编辑
摘要:先下载Go Micro框架 go get -v github.com/micro/go-micro 启动一个简单的Server package main import ( "github.com/micro/go-micro/web" "net/http" ) func main() { /*第一种 阅读全文
posted @ 2019-12-25 14:55 离地最远的星 阅读(406) 评论(0) 推荐(0) 编辑
摘要:还是三步骤创建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 离地最远的星 阅读(921) 评论(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 离地最远的星 阅读(6518) 评论(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 离地最远的星 阅读(3910) 评论(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 离地最远的星 阅读(702) 评论(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 离地最远的星 阅读(798) 评论(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 离地最远的星 阅读(495) 评论(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 离地最远的星 阅读(379) 评论(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 离地最远的星 阅读(1610) 评论(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 离地最远的星 阅读(422) 评论(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 离地最远的星 阅读(346) 评论(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 离地最远的星 阅读(409) 评论(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 离地最远的星 阅读(794) 评论(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 离地最远的星 阅读(343) 评论(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 离地最远的星 阅读(384) 评论(0) 推荐(0) 编辑
摘要:首先放上httptransport.NewServer的源碼 func NewServer( e endpoint.Endpoint, dec DecodeRequestFunc, enc EncodeResponseFunc, options ...ServerOption, //这里的不定长参数可以用来自定义错误处理 ) *Server { s := ... 阅读全文
posted @ 2019-12-23 14:13 离地最远的星 阅读(685) 评论(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 离地最远的星 阅读(426) 评论(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 离地最远的星 阅读(827) 评论(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 离地最远的星 阅读(497) 评论(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 离地最远的星 阅读(494) 评论(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 离地最远的星 阅读(843) 评论(0) 推荐(0) 编辑
摘要:注册初始化代码 package utils import ( "fmt" "github.com/google/uuid" consulapi "github.com/hashicorp/consul/api" "log" "strconv" ) var ConsulClient *consulapi.Client var ServiceID st... 阅读全文
posted @ 2019-12-22 21:47 离地最远的星 阅读(649) 评论(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" htt... 阅读全文
posted @ 2019-12-22 20:52 离地最远的星 阅读(3232) 评论(0) 推荐(0) 编辑
摘要:TransPoint代码 package Services import ( "context" "encoding/json" "errors" "net/http" "strconv" ) func GetUserInfo_Request(_ context.Context, request *http.Request, r inter... 阅读全文
posted @ 2019-12-22 19:54 离地最远的星 阅读(657) 评论(0) 推荐(0) 编辑
摘要:注册和反注册代码 package utils import ( consulapi "github.com/hashicorp/consul/api" "log" ) var ConsulClient *consulapi.Client func init() { config := consulapi.DefaultConfig() config.Addr... 阅读全文
posted @ 2019-12-22 18:08 离地最远的星 阅读(759) 评论(0) 推荐(0) 编辑
摘要:编写注册函数 package utils import ( consulapi "github.com/hashicorp/consul/api" "log" ) func RegService() { config := consulapi.DefaultConfig() config.Address = "192.168.3.14:8500" re... 阅读全文
posted @ 2019-12-22 17:41 离地最远的星 阅读(1751) 评论(0) 推荐(0) 编辑
摘要:package main import ( httptransport "github.com/go-kit/kit/transport/http" mymux "github.com/gorilla/mux" "gomicro/Services" "net/http" ) func main() { user := Services.UserService{} ... 阅读全文
posted @ 2019-12-22 10:48 离地最远的星 阅读(1339) 评论(0) 推荐(0) 编辑
摘要:package Services import ( "context" "fmt" "github.com/go-kit/kit/endpoint" ) type UserRequest struct { //封装User请求结构体,包含参数名和方法 Uid int `json:"uid"` Method string } type User... 阅读全文
posted @ 2019-12-21 11:56 离地最远的星 阅读(669) 评论(0) 推荐(0) 编辑
摘要:package main import ( httptransport "github.com/go-kit/kit/transport/http" "github.com/gorilla/mux" "gomicro/Services" "net/http" ) func main() { user := Services.UserService... 阅读全文
posted @ 2019-12-21 11:29 离地最远的星 阅读(738) 评论(0) 推荐(0) 编辑
摘要:package main import ( "github.com/go-kit/kit/transport/http" "gomicro/Services" ) func main() { user := Services.UserService{} endp := Services.GenUserEnPoint(user) http.NewServ... 阅读全文
posted @ 2019-12-21 11:09 离地最远的星 阅读(924) 评论(0) 推荐(0) 编辑
摘要:第一步创建UserService package Services type IUserService interface { GetName(userid int) string } type UserService struct{} func (this UserService) GetName(userid int) string { if useri... 阅读全文
posted @ 2019-12-21 10:22 离地最远的星 阅读(1792) 评论(0) 推荐(0) 编辑
摘要:首先你要知道; 无论是Myisam和Innodb引擎,如果在建表的时候没有显示的定义一行主键列的话,他内部都会自动创建一个隐藏的主键索引; 主键索引以外的索引假设称为次索引;首先Myisam和Innodb两个都是默认采用的btree索引,可以脑补一颗二叉树; myisam引擎的数据在物理磁盘上是按照 阅读全文
posted @ 2019-12-21 09:40 离地最远的星 阅读(362) 评论(0) 推荐(0) 编辑
摘要:将user_sys表分成3个区 id 6的 强制使用某一个分区去查询数据 我们按照主键值区间分区之后查询效率也会提升,因为查询数据时会到分区中查找,分区的数据比原来小很多,所以检索的速度会更加快,分区的时候还可以指定磁盘,这样从不同的磁盘读取数据,也减小了io的压力 还有hash分区和key分区自行了解来自为知笔记(Wiz) 阅读全文
posted @ 2019-12-21 09:37 离地最远的星 阅读(122) 评论(0) 推荐(0) 编辑
摘要:事务的四大特性 数据库事务 transanction 正确执行的四个基本要素。ACID,原子性(Atomicity)、一致性(Correspondence)、隔离性(Isolation)、持久性(Durability)。 (1)原子性:整个事务中的所有操作,要么全部完成,要么全部不完成,不可能停滞在中间某个环节。事务在执行过程中发生错误,会被回滚(Rollback)到事务开始前的状态,就像... 阅读全文
posted @ 2019-12-21 09:36 离地最远的星 阅读(3118) 评论(0) 推荐(2) 编辑
摘要:set autocommit=0; //设置autocommit为0会取消自动提交 insert into topic_classes values(320,'火箭班','牛逼') --执行了这句话不执行commit并不会提交数据到数据库,只有在自己的会话中可以看到改变了,其实并没有真正的提交,新开一个会话会发现查询不到这个会话提交的内容,如果没有提交的话 COMMIT --执行这个才会把in... 阅读全文
posted @ 2019-12-20 01:00 离地最远的星 阅读(84) 评论(0) 推荐(0) 编辑
摘要:MYISAM在查询时会默认加上读锁,插入时默认加上写锁 读锁 加上后 其他线程可读不可写 写锁加上后 别的线程不能读不能写,要等持锁的session释放锁 表锁一般用在查询为主,只有少量按索引条件更新数据的应用,行锁更适合有大量按索引并发更新少量(因为行锁很耗资源,大量并发更新,会使得资源开销很大)不同数据,又有并发查询的应用 MySQL中的表锁兼容性 请求锁模... 阅读全文
posted @ 2019-12-20 00:59 离地最远的星 阅读(217) 评论(0) 推荐(0) 编辑
摘要:当表很大的时候可以设计冗余字段,避免与大表连表查询造成性能低下 比如日志表和用户表,日志表通常到后期会相当的大可以做一个username的冗余字段,避免查看username的时候去和user表关联 当分页过大时的优化策略 下图的例子是直接使用where去检索一个非索引列,结果是采用全文检索的方式 通过加上order by 索引列把上图查询的type优化成index 使用BTr... 阅读全文
posted @ 2019-12-20 00:58 离地最远的星 阅读(435) 评论(0) 推荐(0) 编辑
摘要:将加锁等等耗时操作都用在主服务器上,数据同步到从服务器,然后查询都去从服务器查,增删改都在主服务器 实现读写分离 主从复制原理 主从复制步骤 主库将所有的写操作记录在binlog日志中,并生成log dump线程,将binlog日志传给从库的I/O线程 从库生成两个线程,一个是I/O线程,另一个是SQL线程 I/O线程去请求主库的binlog日志,并将binlog日志中的文件... 阅读全文
posted @ 2019-12-20 00:58 离地最远的星 阅读(242) 评论(0) 推荐(0) 编辑
摘要:mycat(阿里巴巴搞的一个东西)搞了一个虚拟节点,通过这个虚拟节点对分表和分区的操作就好像是在一张表上进行的 可以设置双主架构避免主从模式下有一台主挂掉了,他们互为主从 mycat都是通过配置schema.xml来配置服务器的 mycat分片查询机制 如果查询是不走指定分片字段id字段,则route路由所有的分片,走所有的分片进行查询,简单的查询走指定分片字段的直接到该分片查询数据,... 阅读全文
posted @ 2019-12-20 00:57 离地最远的星 阅读(227) 评论(0) 推荐(0) 编辑
摘要:来自为知笔记(Wiz) 阅读全文
posted @ 2019-12-20 00:57 离地最远的星 阅读(74) 评论(0) 推荐(0) 编辑
摘要:一:为什么要分表? 如果一个表的每条记录的内容很大,那么就需要更多的IO操作,如果字段值比较大,而使用频率相对比较低,可以将大字段移到另一张表中,当查询不查大字段的时候,这样就减少了I/O操作 如果一个表的数据量很少,那么查询就很快;如果表的数据量非常非常大,那么查询就变的比较慢;也就是表的数据量影响这查询的性能。 表中的数据本来就有独立性,例如分别记录各个地区的数据或者不同时期的数据,特别... 阅读全文
posted @ 2019-12-20 00:56 离地最远的星 阅读(836) 评论(0) 推荐(0) 编辑
摘要:来自为知笔记(Wiz) 阅读全文
posted @ 2019-12-20 00:56 离地最远的星 阅读(236) 评论(0) 推荐(0) 编辑
摘要:首先你要知道; 无论是Myisam和Innodb引擎,如果在建表的时候没有显示的定义一行主键列的话,他内部都会自动创建一个隐藏的主键索引; 主键索引以外的索引假设称为次索引;首先Myisam和Innodb两个都是默认采用的btree索引,可以脑补一颗二叉树; myisam引擎的数据在物理磁盘上是按照顺序存储的,而innodb引擎的表数据是随机分布的; myisam的主键索引的叶子节点只... 阅读全文
posted @ 2019-12-20 00:55 离地最远的星 阅读(1170) 评论(0) 推荐(0) 编辑
摘要:show table status from test //查看test数据库构件情况 select的时候MyISAM明显快于InnoDB,一般用在新闻发布这些,用户登陆这些可能会用到事务的表用innodb 使用唯一索引可以避免重复数据插入,并且还能起到加速查询的作用 sql中变量 select id,username into @gid,@username from user_... 阅读全文
posted @ 2019-12-20 00:52 离地最远的星 阅读(174) 评论(0) 推荐(0) 编辑
摘要:extra属性显示查询用了哪些资源,当select索引列的时候可以看到是使用了索引去查询的速度就很快 下图的查询方式虽然order_by是根据索引去排序的但是select*返回了多个列,有的列不是索引列,所以需要从磁盘中去查询,下图extra是filesort 实战中优化的一些小技巧 查询id用到主键,所以type是const,最好最快的一种,当type是all时是全表扫... 阅读全文
posted @ 2019-12-20 00:51 离地最远的星 阅读(474) 评论(0) 推荐(0) 编辑
摘要:package util import ( "context" "fmt" "go.etcd.io/etcd/clientv3" "time" ) type Service struct { client *clientv3.Client } func NewService() *Service { config := clientv3.Con... 阅读全文
posted @ 2019-12-20 00:50 离地最远的星 阅读(174) 评论(0) 推荐(0) 编辑
摘要:SQL UNION 操作符 SQL UNION 操作符合并两个或多个 SELECT 语句的结果。 SQL UNION 操作符 UNION 操作符用于合并两个或多个 SELECT 语句的结果集。 请注意,UNION 内部的每个 SELECT 语句必须拥有相同数量的列。列也必须拥有相似的数据类型。同时,每个 SELECT 语句中的列的顺序必须相同。 SQL UNION 语法 SEL... 阅读全文
posted @ 2019-12-20 00:50 离地最远的星 阅读(1102) 评论(0) 推荐(0) 编辑
摘要:package util import ( "context" "fmt" "go.etcd.io/etcd/clientv3" "time" ) type Service struct { client *clientv3.Client } func NewService() *Service { config := clientv3.Con... 阅读全文
posted @ 2019-12-20 00:49 离地最远的星 阅读(136) 评论(0) 推荐(0) 编辑
摘要:文件比较少比较小的时候使用文件md5值来监控配置文件 来自为知笔记(Wiz) 阅读全文
posted @ 2019-12-20 00:49 离地最远的星 阅读(395) 评论(0) 推荐(0) 编辑
摘要:实现部分 package util import ( "context" "fmt" "go.etcd.io/etcd/clientv3" "time" ) type Service struct { client *clientv3.Client } func NewService() *Service { config := client... 阅读全文
posted @ 2019-12-20 00:48 离地最远的星 阅读(462) 评论(0) 推荐(0) 编辑
摘要:package util import ( "context" "go.etcd.io/etcd/clientv3" "io/ioutil" "net/http" "regexp" "time" ) type Client struct { client *clientv3.Client Services []*Servi... 阅读全文
posted @ 2019-12-20 00:48 离地最远的星 阅读(558) 评论(0) 推荐(0) 编辑
摘要:package util import ( "context" "go.etcd.io/etcd/clientv3" "regexp" "time" ) type Client struct { client *clientv3.Client Services []*ServiceInfo } type ServiceInfo struct... 阅读全文
posted @ 2019-12-20 00:47 离地最远的星 阅读(143) 评论(0) 推荐(0) 编辑
摘要:package util import ( "math/rand" "time" ) type LoadBalance struct { Servers []*ServiceInfo } func NewloadBalance(servers []*ServiceInfo) *LoadBalance { return &LoadBalance{Servers... 阅读全文
posted @ 2019-12-20 00:47 离地最远的星 阅读(87) 评论(0) 推荐(0) 编辑
摘要:改成动态更新配置文件,如下每五秒重新生成配置文件 confd与etcd的使用 Add keys This guide assumes you have a working etcd, or consul server up and running and the ability to add new keys. /tmp/test-etcd/etcdctl ... 阅读全文
posted @ 2019-12-20 00:46 离地最远的星 阅读(1600) 评论(0) 推荐(0) 编辑
摘要:平滑重启的第三方库overseer package main import ( "crypto/md5" "encoding/hex" "flag" "fmt" "github.com/jpillora/overseer" "gopkg.in/ini.v1" "io" "log" "net/http" "os" ... 阅读全文
posted @ 2019-12-20 00:46 离地最远的星 阅读(298) 评论(0) 推荐(0) 编辑
摘要:package main import ( "context" "flag" "fmt" "github.com/gorilla/mux" uuid "github.com/satori/go.uuid" "goetcd/util" "log" "net/http" "os" "os/signal" "str... 阅读全文
posted @ 2019-12-20 00:45 离地最远的星 阅读(194) 评论(0) 推荐(0) 编辑
摘要:使用go初步調用etcd package main import ( "context" "go.etcd.io/etcd/clientv3" "time" ) func main() { config := clientv3.Config{ Endpoints: []string{"106.12.72.181:23791", "106.1... 阅读全文
posted @ 2019-12-20 00:45 离地最远的星 阅读(1002) 评论(0) 推荐(0) 编辑
摘要:docker run -d --name etcd1 --network etcdnet --ip 172.25.0.101 -p 23791:2379 -e ETCDCTL_API=3 -v /root/etcd:/etcd etcd:my etcd --config-file /etcd/conf/etcd.yml curl http://localhost:2... 阅读全文
posted @ 2019-12-20 00:44 离地最远的星 阅读(340) 评论(0) 推荐(0) 编辑
摘要:下载etcd代码然后拷贝到服务器 来自为知笔记(Wiz) 阅读全文
posted @ 2019-12-20 00:44 离地最远的星 阅读(588) 评论(0) 推荐(0) 编辑
摘要:优雅的启动服务和退出 package main import ( "fmt" "github.com/gorilla/mux" "goetcd/util" "log" "net/http" "os" "os/signal" "strconv" "syscall" ) func main() { router :=... 阅读全文
posted @ 2019-12-20 00:43 离地最远的星 阅读(121) 评论(0) 推荐(0) 编辑
摘要:如上存放一些服务的key到etcd中,商品有两个,主要是为了负载均衡的key func NewService() *Service { config := clientv3.Config{ Endpoints: []string{"106.12.72.181:23791", "106.12.72.181:23792"}, DialTimeout... 阅读全文
posted @ 2019-12-20 00:43 离地最远的星 阅读(428) 评论(0) 推荐(0) 编辑
摘要:/go # etcdctl lease grant 200 //设置一个200秒过期的租约 lease 08e86eea8129eb12 granted with TTL(200s) /go # etcdctl lease timetolive 08e86eea8129eb12 //查看该租约的剩余到期时间 lease 08e86eea8129eb12 granted with TTL(2... 阅读全文
posted @ 2019-12-20 00:42 离地最远的星 阅读(288) 评论(0) 推荐(0) 编辑
摘要:因为是在windows上编译的代码,在linux上不可执行,所以需要交叉编译 package main import ( "flag" "gopkg.in/ini.v1" "log" "net/http" "strconv" ) func main() { port := flag.Int("p", 8080, "服务端口") fl... 阅读全文
posted @ 2019-12-20 00:41 离地最远的星 阅读(173) 评论(0) 推荐(0) 编辑
摘要:![image-20191209191301618](C:\Users\AneroKissinger\AppData\Roaming\Typora\typora-user-images\image-20191209191301618.png root@instance-emh5jlpa:~# etcdctl put /user/101/name xiahualou OK root@insta... 阅读全文
posted @ 2019-12-20 00:41 离地最远的星 阅读(306) 评论(0) 推荐(0) 编辑
摘要:package main import ( "github.com/gin-gonic/gin" "net/http" ) type Topic struct { TopicID int } func main() { m := make(map[string]interface{}) m["username"] = "jerry" rout... 阅读全文
posted @ 2019-12-20 00:40 离地最远的星 阅读(162) 评论(0) 推荐(0) 编辑
摘要:{ b:=2 fmt.Println(b) } fmt.Println(b) //这里读取不到上面代码块中的b,花括号可以什么条件都不跟单独为代码块来自为知笔记(Wiz) 阅读全文
posted @ 2019-12-20 00:39 离地最远的星 阅读(116) 评论(0) 推荐(0) 编辑
摘要:package main import ( "github.com/gin-gonic/gin" "topic.jtthink.com/src" ) func main() { router := gin.Default() v1 := router.Group("/v1/topics") { v1.GET("", src.GetTo... 阅读全文
posted @ 2019-12-20 00:38 离地最远的星 阅读(210) 评论(0) 推荐(0) 编辑
摘要:func init() { DBHelper, err = gorm.Open("mysql", "root:root@(115.159.59.129:3306)/test?charset=utf8&parseTime=True&loc=Local") DBHelper.SingularTable(true) DBHelper.DB().SetMaxIdleConns(... 阅读全文
posted @ 2019-12-20 00:38 离地最远的星 阅读(190) 评论(0) 推荐(0) 编辑
摘要:package main import ( "github.com/gin-gonic/gin" "net/http" ) func main() { //代码块 router := gin.Default() v1:=router.Group("/v1/topics") { v1.GET("", func(context *gin... 阅读全文
posted @ 2019-12-20 00:37 离地最远的星 阅读(131) 评论(0) 推荐(0) 编辑
摘要:优雅关闭web服务 DBHelper, err = gorm.Open("mysql", "root:root@(115.159.59.129:3306)/test?charset=utf8&parseTime=True&loc=Local") if err != nil { log.Fatal("数据库初始化错误", err) //log.Fatal输出日志并且退出主程... 阅读全文
posted @ 2019-12-20 00:36 离地最远的星 阅读(693) 评论(0) 推荐(0) 编辑
摘要:实现redis缓存装饰器,路由函数 func GetTopicDetail(context *gin.Context) { tid:=context.Param("topic_id") topics:=Topics{} DBHelper.Find(&topics,tid)//从数据库取 context.Set("dbResult",topics) //这里d... 阅读全文
posted @ 2019-12-20 00:36 离地最远的星 阅读(313) 评论(0) 推荐(0) 编辑
摘要:api的url设计规则 router := gin.Default() router.GET("/topic/:topic_id", func(context *gin.Context) { context.String(http.StatusOK,"获取帖子Id为%s",context.Param("topic_id")) }) router.Run() package m... 阅读全文
posted @ 2019-12-20 00:34 离地最远的星 阅读(715) 评论(0) 推荐(0) 编辑
摘要:来自为知笔记(Wiz) 阅读全文
posted @ 2019-12-20 00:34 离地最远的星 阅读(81) 评论(0) 推荐(0) 编辑
摘要:String get(String key) { String value = redis.get(key); if (value == null) { if (redis.setnx(key_mutex, "1")) { //设置分布式锁,同一时刻只能有一个线程能进行此操作 // 3 min timeout to avoid mutex... 阅读全文
posted @ 2019-12-20 00:34 离地最远的星 阅读(1022) 评论(0) 推荐(0) 编辑
摘要:var RedisDefaultPool *redis.Pool func newPool(addr string) *redis.Pool { return &redis.Pool{ MaxIdle: 3, MaxActive: 0, IdleTimeout: 240 * time.Second, Dial: ... 阅读全文
posted @ 2019-12-20 00:33 离地最远的星 阅读(2783) 评论(0) 推荐(0) 编辑
摘要:gin内置验证器使用 func TopicUrl(f1 validator.FieldLevel) bool { return true //返回true表示验证成功 } func main(){ router:=gin.Default() if v, ok := binding.Validator.Engine().(*validator.Validate); ok ... 阅读全文
posted @ 2019-12-19 23:06 离地最远的星 阅读(3484) 评论(0) 推荐(1) 编辑
摘要:type TopicQuery struct { UserName string `form:"username" json:"username"` Page int `form:"page" json:"page" binding:"required"` //binding:required表示必须有这个参数,否则err := context.Bind(query)... 阅读全文
posted @ 2019-12-19 23:02 离地最远的星 阅读(237) 评论(0) 推荐(0) 编辑
摘要:work,工作模式 一个消息只能被一个消费者获取 工作模式就是simple模式多了几个消费者,其他一样来自为知笔记(Wiz) 阅读全文
posted @ 2019-12-19 23:01 离地最远的星 阅读(219) 评论(0) 推荐(0) 编辑
摘要:gorm会默认给struct名称设置小写,并且加上复数形式 tc := &src.TopicClass{} //用orm去建表时驼峰个是你会用下划线隔开,所有TopicClass对应表为topic_class db.First(tc,2) //只取topic_class查询结果第一条,后面的2是可选参数,加上表示取主键=2的数据集的第一条,结构体必须标注了主键才可以查询到结果,如下面clas... 阅读全文
posted @ 2019-12-19 23:01 离地最远的星 阅读(236) 评论(0) 推荐(0) 编辑
摘要:simple模式代码 package RabbitMQ import ( "fmt" "github.com/streadway/amqp" "log" ) const MQURL = "amqp://guest:guest@127.0.0.1:5672/imooc" //创建rabbitmq结构体实例 type RabbitMQ struct { conn... 阅读全文
posted @ 2019-12-19 22:59 离地最远的星 阅读(238) 评论(0) 推荐(0) 编辑
摘要:rabbitmq是面向消息的中间件,用于组件之间的解耦,主要体现在消息的发送者和消费者之间无强依赖关系 rabbitmq特点: 高可用 拓展性,多语言客户端,管理界面 使用场景:流量削峰,异步处理,应用解耦等 VirtualHost RabbitMq的VirtualHost(虚拟消息服务器),每个VirtualHost相当于一个相对独立的RabbitMQ服务器;每个VirtualHost之... 阅读全文
posted @ 2019-12-19 22:58 离地最远的星 阅读(166) 评论(0) 推荐(0) 编辑
摘要:Publish/Subscribe,订阅模式 消息被路由投递给多个队列,一个消息可以被多个消费者获取 订阅模式代码 package RabbitMQ import ( "fmt" "github.com/streadway/amqp" "log" ) const MQURL = "amqp://guest:guest@127.0.0.1:5672/imooc" ... 阅读全文
posted @ 2019-12-19 22:54 离地最远的星 阅读(166) 评论(0) 推荐(0) 编辑
摘要:routing,路由模式 一个消息被多个消费者获取,并且消息的目标队列可被生产者指定 package RabbitMQ import ( "fmt" "github.com/streadway/amqp" "log" ) const MQURL = "amqp://guest:guest@127.0.0.1:5672/imooc" //创建rabbitmq结构体实... 阅读全文
posted @ 2019-12-19 22:52 离地最远的星 阅读(188) 评论(0) 推荐(0) 编辑
摘要:Redis的五大数据类型 redis的五大数据类型是,String(字符串),Hash(哈希),List(列表),Set(集合),zset(sorted set: 有序集合) string字符串 string是redis最基本的类型,一个key对应一个value Str1 := "hello" St 阅读全文
posted @ 2019-12-19 22:51 离地最远的星 阅读(510) 评论(0) 推荐(0) 编辑
摘要:go操作redis package main import ( "fmt" "github.com/garyburd/redigo/redis" ) func main() { //连接到redis conn, err := redis.Dial("tcp", "localhost:6379") if err != nil { fmt.Println... 阅读全文
posted @ 2019-12-19 22:50 离地最远的星 阅读(287) 评论(0) 推荐(0) 编辑
摘要:package main import ( "fmt" "sort" ) type ServerSlice []Server type Server struct { Weight int } //只要实现了下面三个方法就可以传入sort方法使用 func (p ServerSlice) Len() int { return len(p) } fu... 阅读全文
posted @ 2019-12-19 22:49 离地最远的星 阅读(389) 评论(0) 推荐(0) 编辑
摘要:package util import ( "fmt" "hash/crc32" "math/rand" "sort" "time" ) type HttpServer struct { //目标server类 Host string Weight int CWeight int //当前权重 ... 阅读全文
posted @ 2019-12-19 22:48 离地最远的星 阅读(1045) 评论(0) 推荐(0) 编辑
摘要:package main import ( "log" "net/http" "net/http/httputil" "net/url" "os" "os/signal" "regexp" "反向代理/util" ) type ProxyHandler struct { } func (*ProxyHandler) ServeH... 阅读全文
posted @ 2019-12-19 22:47 离地最远的星 阅读(446) 评论(0) 推荐(0) 编辑
摘要:来自为知笔记(Wiz) 阅读全文
posted @ 2019-12-19 22:46 离地最远的星 阅读(210) 评论(0) 推荐(0) 编辑
摘要:package util import ( "fmt" "hash/crc32" "math/rand" "time" ) type HttpServer struct { //目标server类 Host string Weight int } func NewHttpServer(host string, weight int) *Ht... 阅读全文
posted @ 2019-12-19 22:45 离地最远的星 阅读(301) 评论(0) 推荐(0) 编辑
摘要:func (this *LoadBalance) RoundRobinByWeight3() *HttpServer { //平滑加权轮询 for _, s := range this.Servers { s.CWeight = s.CWeight + s.Weight } sort.Sort(this.Servers) max := this.... 阅读全文
posted @ 2019-12-19 22:44 离地最远的星 阅读(208) 评论(0) 推荐(0) 编辑
摘要:来自为知笔记(Wiz) 阅读全文
posted @ 2019-12-19 22:43 离地最远的星 阅读(288) 评论(0) 推荐(0) 编辑
摘要:package util import ( "math/rand" "time" ) type HttpServer struct { //目标server类 Host string } func NewHttpServer(host string) *HttpServer { return &HttpServer{Host: host} } type Lo... 阅读全文
posted @ 2019-12-19 22:41 离地最远的星 阅读(244) 评论(0) 推荐(0) 编辑
摘要:Go实现 在头里设置WWW-Authenticate 返回401 func (webhandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) { writer.Header().Set("WWW-Authenticate", `Basic realm="您必须输入用户名和密码"`) writer... 阅读全文
posted @ 2019-12-19 22:41 离地最远的星 阅读(178) 评论(0) 推荐(0) 编辑
摘要:package main import ( "io/ioutil" "log" "net/http" "os" "os/signal" ) type ProxyHandler struct { } func (*ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { d... 阅读全文
posted @ 2019-12-19 22:40 离地最远的星 阅读(120) 评论(0) 推荐(0) 编辑
摘要:func (this *LoadBalance) RoundRobinByWeight() *HttpServer { server := this.Servers[ServerIndices[this.CurIndex]] this.CurIndex = (this.CurIndex + 1) % len(ServerIndices) //ServersIndices存放的是按照... 阅读全文
posted @ 2019-12-19 22:39 离地最远的星 阅读(182) 评论(0) 推荐(0) 编辑
摘要:func (this *LoadBalance) SelectByWeightBetter(ip string) *HttpServer { rand.Seed(time.Now().UnixNano()) sumList := make([]int, len(this.Servers)) //this.servers是服务器列表 sum := 0 for ... 阅读全文
posted @ 2019-12-19 22:38 离地最远的星 阅读(156) 评论(0) 推荐(0) 编辑
摘要:package util import ( "fmt" "hash/crc32" "math/rand" "time" ) type HttpServer struct { //目标server类 Host string Weight int } func NewHttpServer(host st 阅读全文
posted @ 2019-12-19 22:11 离地最远的星 阅读(327) 评论(0) 推荐(0) 编辑
摘要:![](https://xiahualou.oss-cn-shanghai.aliyuncs.com/img/20191219143156.png) 来自为知笔记(Wiz) 阅读全文
posted @ 2019-12-19 22:08 离地最远的星 阅读(528) 评论(0) 推荐(0) 编辑
摘要:创建服务端并调用 syntax = "proto3"; package services; message ProdRequest { int32 prod_id = 1; //传入id } message ProdResponse { int32 prod_stock = 1; //商品库存 } 阅读全文
posted @ 2019-12-19 22:06 离地最远的星 阅读(234) 评论(0) 推荐(0) 编辑
摘要:package main import ( "io/ioutil" "log" "net/http" "os" "os/signal" . "反向代理/util" ) type ProxyHandler struct { } func (*ProxyHandler) ServeHTTP(w http 阅读全文
posted @ 2019-12-19 22:06 离地最远的星 阅读(233) 评论(0) 推荐(0) 编辑
摘要:Models.proto syntax = "proto3"; package services; //商品模型 message ProdModel { int32 prod_id = 1; string prod_name = 2; float prod_price = 3; } Prod.pro 阅读全文
posted @ 2019-12-19 22:04 离地最远的星 阅读(725) 评论(0) 推荐(0) 编辑
摘要:package main import ( "context" "fmt" "google.golang.org/grpc" "grpccli/services" "log" ) func main() { conn, err := grpc.Dial("localhost:8081", grpc. 阅读全文
posted @ 2019-12-19 22:02 离地最远的星 阅读(144) 评论(0) 推荐(0) 编辑
摘要:上面的的是通用的,可以生成java php python等等之类的通用版本 Go版本安装 go get -v github.com/golang/protobuf/protoc-gen-go protoc --go_out=../services Prod.proto 通过这个命令根据proto文件 阅读全文
posted @ 2019-12-19 21:56 离地最远的星 阅读(278) 评论(0) 推荐(0) 编辑
摘要:服务端流 syntax = "proto3"; package services; import "Models.proto"; message UserScoreRequest { repeated UserInfo users = 1; } message UserScoreResponse { 阅读全文
posted @ 2019-12-19 21:55 离地最远的星 阅读(748) 评论(0) 推荐(0) 编辑
摘要:定义Models.proto syntax = "proto3"; package services; import "google/protobuf/timestamp.proto"; //引入timestamp的proto文件 //商品模型 message ProdModel { int32 p 阅读全文
posted @ 2019-12-19 21:53 离地最远的星 阅读(157) 评论(0) 推荐(0) 编辑
摘要:使用第三方库protoc-gen-validate,validate.proto是我从下载的库里面复制出来的,可以直接指定目录,我为了避免麻烦直接拷贝出来了 syntax = "proto3"; package services; import "google/protobuf/timestamp. 阅读全文
posted @ 2019-12-19 21:32 离地最远的星 阅读(1768) 评论(0) 推荐(0) 编辑
摘要:package main import ( "context" "fmt" "google.golang.org/grpc" "google.golang.org/grpc/credentials" "grpccli/services" "log" ) func main() { creds, er 阅读全文
posted @ 2019-12-19 21:30 离地最远的星 阅读(121) 评论(0) 推荐(0) 编辑
摘要:使用枚举、获取分区商品库存 syntax = "proto3"; package services; import "google/api/annotations.proto"; message ProdRequest { int32 prod_id = 1; //传入id } message Pr 阅读全文
posted @ 2019-12-19 21:30 离地最远的星 阅读(188) 评论(0) 推荐(0) 编辑
摘要:package main import ( "google.golang.org/grpc" "google.golang.org/grpc/credentials" "grpcpro/services" "log" "net" ) func main() { creds, err := crede 阅读全文
posted @ 2019-12-19 21:28 离地最远的星 阅读(152) 评论(0) 推荐(0) 编辑
摘要:服务端代码 package main import ( "crypto/tls" "crypto/x509" "google.golang.org/grpc" "google.golang.org/grpc/credentials" "grpcpro/services" "io/ioutil" "n 阅读全文
posted @ 2019-12-19 21:24 离地最远的星 阅读(1888) 评论(0) 推荐(0) 编辑
摘要:syntax = "proto3"; package services; import "Models.proto"; message UserScoreRequest { repeated UserInfo users = 1; } message UserScoreResponse { repe 阅读全文
posted @ 2019-12-19 21:21 离地最远的星 阅读(284) 评论(0) 推荐(0) 编辑
摘要:下载go get -v github.com/grpc-ecosystem/grpc-gateway,然后把这个包下面的third-party下面的google文件夹拷贝到Prod.proto的同级目录下 syntax = "proto3"; package services; import "go 阅读全文
posted @ 2019-12-19 21:20 离地最远的星 阅读(1128) 评论(0) 推荐(0) 编辑
摘要:重新编写proto文件 syntax = "proto3"; package services; import "google/api/annotations.proto"; message ProdRequest { int32 prod_id = 1; //传入id } message Prod 阅读全文
posted @ 2019-12-19 21:17 离地最远的星 阅读(1782) 评论(0) 推荐(0) 编辑
摘要:package main import ( "google.golang.org/grpc" "google.golang.org/grpc/credentials" "grpcpro/services" "log" "net/http" ) func main() { creds, err := 阅读全文
posted @ 2019-12-19 21:16 离地最远的星 阅读(1402) 评论(0) 推荐(0) 编辑
摘要:将crt文件和server_no_passwd.key这两个文件拷贝到服务端的keys文件夹中 来自为知笔记(Wiz) 阅读全文
posted @ 2019-12-19 21:15 离地最远的星 阅读(228) 评论(0) 推荐(0) 编辑
摘要:models.proto syntax = "proto3"; package services; import "google/protobuf/timestamp.proto"; //引入timestamp的proto文件 //商品模型 message ProdModel { int32 pro 阅读全文
posted @ 2019-12-19 21:11 离地最远的星 阅读(536) 评论(0) 推荐(0) 编辑
摘要:等待一组协程结束,用sync.WaitGroup操作 package main import ( "fmt" "sync" "time" ) func main() { wg := sync.WaitGroup{} for i := 0; i < 10; i++ { wg.Add(1) go calc(&wg, i) } wg... 阅读全文
posted @ 2019-12-19 20:59 离地最远的星 阅读(337) 评论(0) 推荐(0) 编辑
摘要:Go语言之unsafe包介绍及使用 unsafe内容介绍 type ArbitraryType int type Pointer *ArbitraryType func Sizeof(x ArbitraryType) uintptr func Offsetof(x ArbitraryType) uintptr func Alignof(x ArbitraryType) uintptr uns... 阅读全文
posted @ 2019-12-19 20:57 离地最远的星 阅读(632) 评论(0) 推荐(0) 编辑
摘要:server package main import ( "fmt" "net" ) func main() { listener, err := net.ListenUDP("udp", &net.UDPAddr{ IP: net.IPv4(0, 0, 0, 0), Port: 30000, }) if err !... 阅读全文
posted @ 2019-12-19 20:56 离地最远的星 阅读(280) 评论(0) 推荐(0) 编辑
摘要:server package main import ( "fmt" "net" ) //单独处理连接的函数 func process(conn net.Conn) { var buf = make([]byte, 1024) n, err := conn.Read(buf) // 返回读取到的字节数和错误信息 if err != nil { ... 阅读全文
posted @ 2019-12-19 20:54 离地最远的星 阅读(290) 评论(0) 推荐(0) 编辑
摘要:struct和byte类型转换 import ( "fmt" "unsafe" ) type TestStructTobytes struct { data int64 } type SliceMock struct { addr uintptr len int cap int } func main() { var testStruct = &TestStruc... 阅读全文
posted @ 2019-12-19 20:53 离地最远的星 阅读(2063) 评论(0) 推荐(0) 编辑
摘要:type T struct { f string `one:"1" two:"2"blank:""` } func main() { t := reflect.TypeOf(T{}) f, _ := t.FieldByName("f") fmt.Println(f.Tag) // one:"1" two:"2"blank:"" v, ok := f.Tag.... 阅读全文
posted @ 2019-12-19 20:53 离地最远的星 阅读(264) 评论(0) 推荐(0) 编辑
摘要:package main import "fmt" func tryRecover() int { defer func() { r := recover() if err, ok := r.(error); ok { fmt.Println("Error occurred", err) } else { ... 阅读全文
posted @ 2019-12-19 20:51 离地最远的星 阅读(296) 评论(0) 推荐(0) 编辑
摘要:panic的参数是一个空接口类型 interface{},所以任意类型的变量都可以传递给panic recover只有在defer后面的函数体里直接调用才能捕获panic终止异常,否则返回nil异常继续向外传递 defer recover() //这种写法捕捉失败 defer fmt.Println(recover()) //这种写法捕捉失败 defer func(){ func(){ ... 阅读全文
posted @ 2019-12-19 20:50 离地最远的星 阅读(1215) 评论(0) 推荐(0) 编辑
摘要:三次握手 ​ 主动发起请求端,发送SYN(SYN表示建立连接) ​ 被动建立连接请求端,会去应答ACK同时发送SYN ​ 主动发起请求端,发送应答ACK (ACK表示响应) 四次挥手 1主动关闭连接请求端,发送FIN (FIN表示关闭连接) 2被动关闭连接请求端,应答ACK 半关闭完成 3被动关闭连接请求端,发送FIN 4主动关闭连接请求端,应答ACK 因... 阅读全文
posted @ 2019-12-19 20:49 离地最远的星 阅读(309) 评论(0) 推荐(0) 编辑
摘要:字典被设置成not addressble,所以不可以直接修改value成员(结构体或数组) 正确做法是返回整个value,待修改后再设置字典键值,或直接用指针类型 如果返回的是指针,可以透过指针直接修改目标对象 在创建时预先准备足够空间有助于提升性能,减少扩张时的内存分配和重新哈希操作来自为知笔记(Wiz) 阅读全文
posted @ 2019-12-19 20:45 离地最远的星 阅读(275) 评论(0) 推荐(0) 编辑
摘要:itoa可以套公式,下面的依旧会按照公式运算 package main import "fmt" func main() { const ( b = 1 << (10 * iota) kb mb tb pb ) fmt.Println(b, kb, mb, tb, pb) //1 1024... 阅读全文
posted @ 2019-12-19 20:43 离地最远的星 阅读(159) 评论(0) 推荐(0) 编辑
摘要:io包 type Reader interface{ Read(p []byte)(n int,err error) } type Writer interface{ Write(p []byte)(n int,err err) } for循环中一次读出文件 n := -1 for { n, err = file.Read(bs) if n== 0 || e... 阅读全文
posted @ 2019-12-19 20:37 离地最远的星 阅读(268) 评论(0) 推荐(0) 编辑
摘要:go启动一个简单的httpserver package main import ( "fmt" "log" "net/http" ) func main() { http.HandleFunc("/", sayhelloName) //设置访问的路由 err := http.ListenAndServe(":8080", nil) //设置监听的端口 i... 阅读全文
posted @ 2019-12-19 20:33 离地最远的星 阅读(99) 评论(0) 推荐(0) 编辑
摘要:Go语言runtime.Gosched()函数浅析 这个函数的作用是让当前goroutine让出CPU,好让其它的goroutine获得执行的机会。同时,当前的goroutine也会在未来的某个时间点继续运行。 请看下面这个例子(show.go): package main import ( "fmt" ) func showNumber (i int) { fmt.Pr... 阅读全文
posted @ 2019-12-19 20:29 离地最远的星 阅读(414) 评论(0) 推荐(0) 编辑
摘要:package main import ( "fmt" "runtime" ) func main() { n := runtime.NumCPU() fmt.Print(n) runtime.GOMAXPROCS(n - 1) //设置cpu运行的数目 } 来自为知笔记(Wiz) 阅读全文
posted @ 2019-12-19 20:26 离地最远的星 阅读(830) 评论(0) 推荐(0) 编辑
摘要:go奉行通过通信来共享内存,不像c和c++通过共享内存来通信 协程是轻量级的线程,编译器做优化** 有独立的栈空间 共享程序堆空间 调度由用户控制 协程是轻量级的线程 并行:多个cpu共同执行 并发 多个程序在cpu上不停的切换 go协程也是抢占式的 情景:在多线程情况下如果多个线程去下载同一个视频,这样多个线程可能会同时被io阻塞 这段时间所有线程都阻塞了,线程轮询所有线程都... 阅读全文
posted @ 2019-12-19 20:18 离地最远的星 阅读(494) 评论(0) 推荐(0) 编辑
摘要:sync.Cond代表条件变量,需要配置锁才能有用 package main import ( "fmt" "runtime" "sync" "time" ) func main() { runtime.GOMAXPROCS(4) testCond() } func testCond() { c := sync.NewCond(... 阅读全文
posted @ 2019-12-19 20:17 离地最远的星 阅读(220) 评论(0) 推荐(0) 编辑
摘要:判断io读取是否结束,尽量用if n==0这种方式,因为可以判断很多种情况 package main import ( "fmt" "io" "net/http" ) func handler(w http.ResponseWriter, r *http.Request) { w.Write([]byte("hello")) fmt.Println(r.... 阅读全文
posted @ 2019-12-19 20:15 离地最远的星 阅读(495) 评论(0) 推荐(0) 编辑
摘要:Go函数类型的使用 type myfunc func() string // 声明函数变量 func main() { fn := func() string { return "bbb" } // 声明map myMap := make(map[string]myfunc) // map赋值 myMap["fn"] = fn fmt.Printl... 阅读全文
posted @ 2019-12-19 20:13 离地最远的星 阅读(298) 评论(0) 推荐(0) 编辑
摘要:import "sync" var ( myMap = make(map[int]int, 10) lock sync.Mutex //声明一个全局的互斥锁 //sync 包 同步 // Mutex:是互斥 ) func test(n int){ res := 1 for i:=1;i<=n;i++{ res *=1 ... 阅读全文
posted @ 2019-12-19 20:13 离地最远的星 阅读(221) 评论(0) 推荐(0) 编辑
摘要:Go序列化和反序列化 package main import ( "bufio" "encoding/json" "fmt" "os" ) type Monster struct { Name string Age int Skill string } func (m *Monster) Store(path string) { b, ... 阅读全文
posted @ 2019-12-19 20:09 离地最远的星 阅读(108) 评论(0) 推荐(0) 编辑
摘要:https://blog.csdn.net/zzhongcy/article/details/97243826来自为知笔记(Wiz) 阅读全文
posted @ 2019-12-19 20:07 离地最远的星 阅读(393) 评论(0) 推荐(0) 编辑
摘要:package main import ( "fmt" "html/template" "net/http" ) type User struct { UserName string Age int } func info(w http.ResponseWriter, r *http.Request) { t, err := temp... 阅读全文
posted @ 2019-12-19 20:06 离地最远的星 阅读(689) 评论(0) 推荐(0) 编辑
摘要:channel的基本介绍 channel的本质是一个数据结构队列 数据是先进先出 FIFO 线程安全,多goroutine访问时,不需要加锁,就是说channel本身是线程安全的 channel是由类型的,一个string的channel只能存放string类型数据 无缓冲的channel关闭后,再往外读数据读到的是该管道数据类型的初始值 有缓冲的channel的channel关闭后,如果管道... 阅读全文
posted @ 2019-12-19 20:04 离地最远的星 阅读(1056) 评论(0) 推荐(0) 编辑
摘要:ARP与RARP详细解析 原创zlnnjit 发布于2016-04-03 15:12:15 阅读数 9544 收藏 展开 地址解析协议 ARP和逆地址解析协议RARP 1.基本关系: ​ 2.地址解析协议 ARP的实现过程: 不管网络层使用的是什么协议,在实际网络的链路上传送数据帧时,最终还是必须使用硬件地址。 ‚每一个主机都设有一个 ARP高速缓存(AR... 阅读全文
posted @ 2019-12-19 20:02 离地最远的星 阅读(355) 评论(0) 推荐(0) 编辑
摘要:package main import ( "errors" "fmt" "reflect" ) type Student struct { Name string `json:"name"` Age int `json:"age"` } type Teacher struct { name string } func (Studen... 阅读全文
posted @ 2019-12-19 20:00 离地最远的星 阅读(94) 评论(0) 推荐(0) 编辑
摘要:Go 系列教程 —— 31. 自定义错误 使用 New 函数创建自定义错误 创建自定义错误最简单的方法是使用 errors 包中的 New 函数。 在使用 New 函数 创建自定义错误之前,我们先来看看 New 是如何实现的。如下所示,是 errors 包 中的 New 函数的实现。 // Package errors implements functions to manipulate ... 阅读全文
posted @ 2019-12-19 19:57 离地最远的星 阅读(201) 评论(0) 推荐(0) 编辑
摘要:package main import ( "fmt" "unsafe" ) type W struct { a byte b int32 c int32 } func main() { var w = W{b: 32, c: 64} t := unsafe.Pointer(&w) ... 阅读全文
posted @ 2019-12-19 19:53 离地最远的星 阅读(982) 评论(0) 推荐(0) 编辑
摘要:GO语言变量逃逸分析 空格键_11aa · 2019-07-09 21:32:41 · 274 次点击 · 预计阅读时间 1 分钟 · 大约1分钟之前 开始浏览 这是一个创建于 2019-07-09 21:32:41 的文章,其中的信息可能已经有所发展或是发生改变。 引言 ​ 内存管理的灵活性是让C/C++程序猿们又爱又恨的东西,比如malloc或new一块内存我可以整个进程使用... 阅读全文
posted @ 2019-12-19 19:53 离地最远的星 阅读(1040) 评论(0) 推荐(0) 编辑
摘要:字符串常用函数 package main import ( "fmt" "strings" ) func main() { str := "I love my work and i love my family too" ret := strings.Split(str, " ") fmt.Println(ret) ... 阅读全文
posted @ 2019-12-19 19:53 离地最远的星 阅读(120) 评论(0) 推荐(0) 编辑
摘要:jsoniter高性能json库 非常快,支持java和go marshal使用的一些坑 package main import ( "encoding/json" "fmt" ) type User struct { Id int `json:"id,string"` //加了这个序列化到json中int类型会被转换成string类型 Name st... 阅读全文
posted @ 2019-12-19 19:52 离地最远的星 阅读(1141) 评论(0) 推荐(0) 编辑
摘要:package main import ( "log" "net/http" "os" "os/signal" ) type webhandler struct { } type web2handler struct { } func (webhandler) ServeHTTP(writer http.ResponseWriter, request *htt... 阅读全文
posted @ 2019-12-19 19:50 离地最远的星 阅读(103) 评论(0) 推荐(0) 编辑
摘要:使用Fprintln追加字符串到文件末尾 package main import ( "fmt" "os" ) func main() { f,err := os.Create("test.txt") defer f.Close() if err != nil{ return } if err!=nil{ ... 阅读全文
posted @ 2019-12-19 19:49 离地最远的星 阅读(140) 评论(0) 推荐(0) 编辑
摘要:package main import ( "fmt" "os" ) func main() { f, err := os.Open("/test.txt") if err, ok := err.(*os.PathError); ok { fmt.Println("File at path", err.Path, "failed to open"... 阅读全文
posted @ 2019-12-19 19:48 离地最远的星 阅读(217) 评论(0) 推荐(0) 编辑
摘要:互斥锁 package main import ( "fmt" "sync" "time" ) var mux sync.Mutex func Printer(s string) { mux.Lock() for _, v := range s { fmt.Printf("%c", v) time.Sleep(300 ... 阅读全文
posted @ 2019-12-19 19:47 离地最远的星 阅读(109) 评论(0) 推荐(0) 编辑
摘要:数组 如果元素支持==和!=操作符,那么数组也支持此操作 a := [...]int{1, 2, 3} b := [...]int{4, 5, 6} println(a == b) // false println(a != b) // true //下面的代码会抛出异常,因为map类型不支持==和!=运算 c := [...]map[int]str... 阅读全文
posted @ 2019-12-19 19:46 离地最远的星 阅读(93) 评论(0) 推荐(0) 编辑
摘要:这种方式是Go所推荐的,优点就是安全,尽管这种操作会发生内存拷贝,导致性能上会有所损耗,这在处理一般业务时这种损耗是可以忽略的。但如果是拷贝频繁的情况下,想要进行性能优化时,就需要引入unsafe.Pointer了: func main() { var s = []byte("我永远喜欢藤原千花.jpg") Res := *(*string)(unsafe.Pointer(&s... 阅读全文
posted @ 2019-12-19 19:45 离地最远的星 阅读(424) 评论(0) 推荐(0) 编辑
摘要:package main import ( "fmt" "unsafe" ) type TestStructTobytes struct { data int64 s int8 } type SliceMock struct { addr uintptr len int cap int } func main() { ... 阅读全文
posted @ 2019-12-19 19:44 离地最远的星 阅读(1281) 评论(0) 推荐(0) 编辑
摘要:package main import ( "bufio" "fmt" "io" "strings" ) type intGen func() int func (g intGen) Read(p []byte) (n int, err error) { next := g() if next > 10000 { //因为是斐波那契额数列,所以... 阅读全文
posted @ 2019-12-19 19:43 离地最远的星 阅读(194) 评论(0) 推荐(0) 编辑
摘要:package main import ( "fmt" "sync" "time" ) func add(i *int, mutex *sync.Mutex) { mutex.Lock() *i++ mutex.Unlock() } func main() { var mutex sync.Mutex //声明锁的时候不要用指针,锁需要... 阅读全文
posted @ 2019-12-19 19:42 离地最远的星 阅读(385) 评论(0) 推荐(0) 编辑
摘要:命令行参数,向整个函数传参 go run xxx.go argv1 argv2 os.Args package main import ( "fmt" "os" ) func main() { fmt.Println(os.Args) } 来自为知笔记(Wiz) 阅读全文
posted @ 2019-12-19 19:40 离地最远的星 阅读(76) 评论(0) 推荐(0) 编辑
摘要:slice的实现 切片的结构定义 type SliceHeader struct{ Data uintptr Len int Cap int } //由上述代码可以看出切片的开头部分和Go字符串一样,但是切片多了一个Cap成员来表示切片指向的内存空间的最大容量(对应元素的个数,而不是字节数) 切片的定义方式 var ( a []int b = []int{} ... 阅读全文
posted @ 2019-12-19 19:40 离地最远的星 阅读(221) 评论(0) 推荐(0) 编辑
摘要:类型断言传递的是实例值的副本,如果实例是指针,则是指针的副本 package main import "fmt" func main() { var a = 34 var i interface{} = &a o := i.(*int) fmt.Println(i, o) } // output: 0xc4200160a0 0xc4200160a0来自为... 阅读全文
posted @ 2019-12-19 19:39 离地最远的星 阅读(95) 评论(0) 推荐(0) 编辑
摘要:package main import "fmt" type A struct{ } func (a *A) Say(){ fmt.Print("haha") } func main(){ var a A a.Say() //方法绑定的是实例指针编译器会自动把实例转成指针类型传到方法中,反之如果方法绑定的是实例,指针也会被编译器自动转成实例传入到方法内部 (&a).Say(... 阅读全文
posted @ 2019-12-19 19:38 离地最远的星 阅读(125) 评论(0) 推荐(0) 编辑
摘要:支持匿名接口类型,可直接用为变量定义,或结构体字段 package main type data struct { } func (data) string() string { return "haha" } type node struct { data interface { string() string //匿名接口类型 } } fun... 阅读全文
posted @ 2019-12-19 19:38 离地最远的星 阅读(106) 评论(0) 推荐(0) 编辑
摘要:package main import ( "bufio" "fmt" "io" "strings" ) type intGen func() int //正规的函数式编程都是先定义函数类型 func fibonacci() intGen { //声明一个返回intGen类型的斐波那契生成器 a, b := 0, 1 return func... 阅读全文
posted @ 2019-12-19 19:36 离地最远的星 阅读(94) 评论(0) 推荐(0) 编辑
摘要:package main import ( "fmt" "net" "strings" "time" ) type Client struct { C chan string Name string Addr string } //创建全局map,存储在现空间 var onlineMap map[string]Client //... 阅读全文
posted @ 2019-12-19 19:35 离地最远的星 阅读(97) 评论(0) 推荐(0) 编辑
摘要:使用反射机制,编写函数的适配器,桥连接 test1 := func(v1 int, v2 int){ t.Log(v1,v2) } 基本介绍 反射可以在运行时动态获取变量的各种信息,比如变量的类型,类别 如果是结构体变量,还可以获取到结构体本身的信息(包括字段,方法) 通过反射,可以修改变量的值可以调用关联的方法 使用反射,需要import("reflect") 变量 inte... 阅读全文
posted @ 2019-12-19 19:35 离地最远的星 阅读(144) 评论(0) 推荐(0) 编辑
摘要:主要内容: 由对象获取反射类型,由对象获取反射值 由反射值获取反射类型 反射值重新转换成对象 遍历字段 遍历方法 易知go语言的struct是值类型,如果修改需要使用引用传递。反射修改值类型,要获取其指针的值类型。此方法适用于int string 以及struct。 反射调用的注意点: 类方法要大写,以便能被其它包,也就是reflect包调用。小写的方法可以由反射查询,但是不能调用。 要获取... 阅读全文
posted @ 2019-12-19 19:35 离地最远的星 阅读(217) 评论(0) 推荐(0) 编辑
摘要:周期定时器 func main() { timer := time.NewTicker(1 * time.Second) //NewTicker是周期定时器,NewTimer是普通定时器只能用一次 for { <-timer.C fmt.Println("good") } } 定时器 time.Sleep(time.Secon... 阅读全文
posted @ 2019-12-19 19:34 离地最远的星 阅读(1130) 评论(0) 推荐(0) 编辑
摘要:go语言中的包 一个目录下的统计文件归属一个包,package的声明要一致 package声明的包和对应的目录名可以不一致,但习惯上还是可以写成一直的 包可以嵌套 同包下的函数不需要导入包,可以直接使用 main包,main函数所在的包,其他包不可以引用 点操作 import ( . "fmt" ) func main(){ println("haha") //使用... 阅读全文
posted @ 2019-12-19 19:33 离地最远的星 阅读(126) 评论(0) 推荐(0) 编辑
摘要:闭包对闭包外的环境引入是直接引用,编译器监测到闭包,会将闭包饮用的外部变量分配到堆上 package main func fa(a int) func(i int) int { return func(i int) int { fmt.println(&a) a = a + i return a } } func main() ... 阅读全文
posted @ 2019-12-19 19:33 离地最远的星 阅读(73) 评论(0) 推荐(0) 编辑
摘要:const定义在main函数里面就是局部常量 package main import "fmt" func add(a int) func(int) int { return func(i int) int { fmt.Println(&a) a += i return a } } func main() { cons... 阅读全文
posted @ 2019-12-19 19:33 离地最远的星 阅读(81) 评论(0) 推荐(0) 编辑
摘要:## 创建巨大字符串时性能提升的方法 package main import ( "bytes" "fmt" ) func test() string{ var b bytes.Buffer b.Grow(1000) //将buffer的容量提升到1000,然后写入到字符中 for i :=0;i<1000;i++{ b.WriteS... 阅读全文
posted @ 2019-12-19 19:33 离地最远的星 阅读(173) 评论(0) 推荐(0) 编辑
摘要:package main import "fmt" type optionClient func(*options) func setAge(a int) optionClient { return func(o *options) { o.Age = a } } func setSex(s int) optionClient { return func(o ... 阅读全文
posted @ 2019-12-19 19:31 离地最远的星 阅读(237) 评论(0) 推荐(0) 编辑
摘要:package main import ( "fmt" "runtime" ) func test() { defer fmt.Println("ccccccccc") //只要成功注册了无论是return 还是goexit都会被调用 runtime.Goexit() //退出函数所在当前go程 defer fmt.Println("dddddddddd... 阅读全文
posted @ 2019-12-19 19:31 离地最远的星 阅读(99) 评论(0) 推荐(0) 编辑
摘要:go语言在for循环中遍历的临时变量地址是一样的 func main() { //SetLogConfToEtcd() for i := 0; i < 5; i++ { a := i fmt.Printf("%p\n", &i) //可以看到i的地址都是一样的,也就是说i是复用的 fmt.Printf("%p\n", &a) //... 阅读全文
posted @ 2019-12-19 19:30 离地最远的星 阅读(342) 评论(0) 推荐(0) 编辑
摘要:单行模式(?s:(.?))万能用法尽量匹配少的文本,最关键的是可以匹配换行的文本,直接写.?不能匹配\n package main import ( "fmt" "regexp" ) func main() { str := "3.14 23.22 abc a7c 2.43 mfc cat 8ca azc cba" //解析,编译正则表达式 ret :... 阅读全文
posted @ 2019-12-19 19:29 离地最远的星 阅读(490) 评论(0) 推荐(0) 编辑
摘要:cancel package main import ( "context" "fmt" "time" ) func gen(ctx context.Context) <-chan int { dst := make(chan int) n := 1 go func() { for { select { ... 阅读全文
posted @ 2019-12-19 19:27 离地最远的星 阅读(474) 评论(0) 推荐(0) 编辑

点击右上角即可分享
微信分享提示