1.服务端代码
【备注:这是服务端没秒发送一个数据导发布频道里面】
package main import ( "fmt" "github.com/go-redis/redis" "math/rand" "time" ) func main(){ redisConnect() } func redisConnect(){ server:=redis.NewClient(&redis.Options{ Addr:"127.0.0.1:6379", DB:0, }) for i:=0;i<20;i++ { time.Sleep(time.Second) rand.Seed(time.Now().Unix()) num:=rand.Intn(1000) n,_:=server.Publish("channel",num).Result() fmt.Println(n) } }
2.这是客户端代码
package main import ( "fmt" "github.com/go-redis/redis" ) func main(){ redisClient() } func redisClient(){ client:=redis.NewClient(&redis.Options{ Addr:"127.0.0.1:6379", DB:0, }) pubsub:=client.Subscribe("channel") defer pubsub.Close() for msg:= range pubsub.Channel() { fmt.Println(msg.Payload) } }
问题思考:
1.第一如果订阅端挂了怎么办
2.第二发布数据过多处理不过来怎么做
3.重复发布怎么办