func init() { l := &MockListener{rc: rc, txc: txc} rc := turbo.NewRemotingConfig( "remoting-127.0.0.1:13800", 2000, 16*1024, 16*1024, 10000, 10000, 10*time.Second, 160000) kc := server.NewKiteQConfig(server.MockServerOption(), rc) kiteQ = server.NewKiteQServer(kc) // 创建客户端 manager = NewKiteClientManager("localhost:2181", "ps-trade-a", "123456", l) manager.SetPublishTopics([]string{"trade"}) // 设置接收类型 manager.SetBindings( []*binding.Binding{ binding.Bind_Direct("ps-trade-a", "trade", "pay-succ", 1000, true), }, ) kiteQ.Start() time.Sleep(10 * time.Second) manager.Start() }
func NewKiteClientManager(zkAddr, groupId, secretKey string, listen listener.IListener) *KiteClientManager { flowstat := stat.NewFlowStat("kiteclient-" + groupId) rc := turbo.NewRemotingConfig( "remoting-"+groupId, 50, 16*1024, 16*1024, 10000, 10000, 10*time.Second, 160000) //重连管理器 reconnManager := c.NewReconnectManager(true, 30*time.Second, 100, handshake) //构造pipeline的结构 pipeline := pipe.NewDefaultPipeline() clientm := c.NewClientManager(reconnManager) pipeline.RegisteHandler("kiteclient-packet", chandler.NewPacketHandler("kiteclient-packet")) pipeline.RegisteHandler("kiteclient-heartbeat", chandler.NewHeartbeatHandler("kiteclient-heartbeat", 10*time.Second, 5*time.Second, clientm)) pipeline.RegisteHandler("kiteclient-accept", chandler.NewAcceptHandler("kiteclient-accept", listen)) pipeline.RegisteHandler("kiteclient-remoting", pipe.NewRemotingHandler("kiteclient-remoting", clientm)) manager := &KiteClientManager{ ga: c.NewGroupAuth(groupId, secretKey), kiteClients: make(map[string][]*kiteClient, 10), topics: make([]string, 0, 10), pipeline: pipeline, clientManager: clientm, rc: rc, flowstat: flowstat, zkAddr: zkAddr} //开启流量统计 manager.remointflow() manager.flowstat.Start() return manager }
func init() { rc := turbo.NewRemotingConfig( "turbo-server:localhost:28888", 1000, 16*1024, 16*1024, 10000, 10000, 10*time.Second, 160000) remoteServer = NewRemotionServer("localhost:28888", rc, packetDispatcher) remoteServer.ListenAndServer() conn, _ := dial("localhost:28888") // //重连管理器 reconnManager := client.NewReconnectManager(false, -1, -1, handshake) clientManager = client.NewClientManager(reconnManager) rcc := turbo.NewRemotingConfig( "turbo-client:localhost:28888", 1000, 16*1024, 16*1024, 10000, 10000, 10*time.Second, 160000) remoteClient := client.NewRemotingClient(conn, clientPacketDispatcher, rcc) remoteClient.Start() auth := &client.GroupAuth{} auth.GroupId = "a" auth.SecretKey = "123" clientManager.Auth(auth, remoteClient) go func() { for { log.Println(clientf.Monitor()) time.Sleep(1 * time.Second) } }() }
func TestParse(t *testing.T) { rc := turbo.NewRemotingConfig( "remoting-localhost:13800", 2000, 16*1024, 16*1024, 10000, 10000, 10*time.Second, 160000) so := MockServerOption() so.db = "mysql://localhost:3306,localhost:3306?db=kite&username=root" kc := NewKiteQConfig(MockServerOption(), rc) kiteqName, _ := os.Hostname() store := parseDB(kc, kiteqName) store.Delete("123456") }
func BenchmarkRemotingServer(t *testing.B) { //初始化存储 var kiteClient *client.KiteQClient var kiteQServer *KiteQServer var c int32 = 0 var lc int32 = 0 rc := turbo.NewRemotingConfig( "remoting-localhost:13800", 2000, 16*1024, 16*1024, 10000, 10000, 10*time.Second, 160000) kc := NewKiteQConfig("kiteq-localhost:13800", "localhost:13800", "localhost:2181", true, 1*time.Second, 10, 1*time.Minute, []string{"trade"}, "memory://", rc) kiteQServer = NewKiteQServer(kc) kiteQServer.Start() log.Println("KiteQServer START....") kiteClient = client.NewKiteQClient("localhost:2181", "s-trade-a", "123456", &defualtListener{}) kiteClient.SetTopics([]string{"trade"}) kiteClient.SetBindings([]*binding.Binding{ binding.Bind_Direct("s-trade-a", "trade", "pay-succ", 1000, true), }) kiteClient.Start() go func() { for { time.Sleep(1 * time.Second) log.Printf("%d\n", (c - lc)) lc = c } }() t.SetParallelism(4) t.RunParallel(func(pb *testing.PB) { for pb.Next() { err := kiteClient.SendStringMessage(buildStringMessage("1")) if nil != err { t.Logf("SEND MESSAGE |FAIL|%s\n", err) } } }) kiteClient.Destory() kiteQServer.Shutdown() }
func TestParse(t *testing.T) { rc := turbo.NewRemotingConfig( "remoting-localhost:13800", 2000, 16*1024, 16*1024, 10000, 10000, 10*time.Second, 160000) kc := NewKiteQConfig("kiteq-localhost:138000", "localhost:138000", "localhost:2181", true, 1*time.Second, 8000, 5*time.Second, strings.Split("trade", ","), "mysql://localhost:3306,localhost:3306?db=kite&username=root", rc) store := parseDB(kc) store.Delete("123456") }
func main() { go func() { http.ListenAndServe(":13800", nil) }() rc := turbo.NewRemotingConfig( "turbo-server:localhost:28888", 1000, 16*1024, 16*1024, 20000, 20000, 10*time.Second, 160000) remoteServer := server.NewRemotionServer("localhost:28888", rc, packetDispatcher) remoteServer.ListenAndServer() select {} }
func main() { //加载启动参数 so := server.Parse() runtime.GOMAXPROCS(runtime.NumCPU()) rc := turbo.NewRemotingConfig( "remoting", 20000, 16*1024, 16*1024, 10000, 10000, 10*time.Second, 160000) kc := server.NewKiteQConfig(so, rc) qserver := server.NewKiteQServer(kc) qserver.Start() var s = make(chan os.Signal, 1) signal.Notify(s, syscall.SIGKILL, syscall.SIGUSR1) //是否收到kill的命令 for { cmd := <-s if cmd == syscall.SIGKILL { break } else if cmd == syscall.SIGUSR1 { //如果为siguser1则进行dump内存 unixtime := time.Now().Unix() path := fmt.Sprintf("./heapdump-kiteq-%d", unixtime) f, err := os.Create(path) if nil != err { continue } else { debug.WriteHeapDump(f.Fd()) } } } qserver.Shutdown() log.InfoLog("kite_server", "KiteQServer IS STOPPED!") }
func TestRemotingServer(t *testing.T) { //初始化存储 var kiteClient *client.KiteQClient var kiteQServer *KiteQServer var c int32 = 0 var lc int32 = 0 rc := turbo.NewRemotingConfig( "remoting-localhost:13800", 2000, 16*1024, 16*1024, 10000, 10000, 10*time.Second, 160000) kc := NewKiteQConfig("kiteq-localhost:13800", "localhost:13800", "localhost:2181", true, 1*time.Second, 10, 1*time.Minute, []string{"trade"}, "memory://", rc) kiteQServer = NewKiteQServer(kc) kiteQServer.Start() log.Println("KiteQServer START....") kiteClient = client.NewKiteQClient("localhost:2181", "s-trade-a", "123456", &defualtListener{}) kiteClient.SetTopics([]string{"trade"}) kiteClient.SetBindings([]*binding.Binding{ binding.Bind_Direct("s-trade-a", "trade", "pay-succ", 1000, true), }) kiteClient.Start() go func() { for { time.Sleep(1 * time.Second) log.Printf("%d\n", (c - lc)) lc = c } }() err := kiteClient.SendStringMessage(buildStringMessage("1")) if nil != err { t.Logf("SEND MESSAGE |FAIL|%s\n", err) t.Fail() } err = kiteClient.SendStringMessage(buildStringMessage("2")) if nil != err { t.Logf("SEND MESSAGE |FAIL|%s\n", err) t.Fail() } err = kiteClient.SendStringMessage(buildStringMessage("3")) if nil != err { t.Logf("SEND MESSAGE |FAIL|%s\n", err) t.Fail() } msg := buildStringMessage("4") msg.GetHeader().Commit = proto.Bool(false) err = kiteClient.SendStringMessage(msg) if nil != err { t.Logf("SEND MESSAGE |FAIL|%s\n", err) t.Fail() } time.Sleep(10 * time.Second) kiteClient.Destory() kiteQServer.Shutdown() }
func main() { go func() { http.ListenAndServe(":13801", nil) }() // 重连管理器 reconnManager := client.NewReconnectManager(false, -1, -1, handshake) clientManager := client.NewClientManager(reconnManager) rcc := turbo.NewRemotingConfig( "turbo-client:localhost:28888", 1000, 16*1024, 16*1024, 20000, 20000, 10*time.Second, 160000) go func() { for { log.Println(rcc.FlowStat.Stat()) time.Sleep(1 * time.Second) } }() //创建物理连接 conn, _ := func(hostport string) (*net.TCPConn, error) { //连接 remoteAddr, err_r := net.ResolveTCPAddr("tcp4", hostport) if nil != err_r { log.Printf("KiteClientManager|RECONNECT|RESOLVE ADDR |FAIL|remote:%s\n", err_r) return nil, err_r } conn, err := net.DialTCP("tcp4", nil, remoteAddr) if nil != err { log.Printf("KiteClientManager|RECONNECT|%s|FAIL|%s\n", hostport, err) return nil, err } return conn, nil }("localhost:28888") remoteClient := client.NewRemotingClient(conn, clientPacketDispatcher, rcc) remoteClient.Start() auth := &client.GroupAuth{} auth.GroupId = "a" auth.SecretKey = "123" clientManager.Auth(auth, remoteClient) //echo command p := packet.NewPacket(1, []byte("echo")) //find a client tmp := clientManager.FindRemoteClients([]string{"a"}, func(groupid string, c *client.RemotingClient) bool { return false }) for i := 0; i < 1; i++ { go func() { for { //write command and wait for response _, err := tmp["a"][0].WriteAndGet(*p, 100*time.Millisecond) if nil != err { log.Printf("WAIT RESPONSE FAIL|%s\n", err) break } else { // log.Printf("WAIT RESPONSE SUCC|%s\n", string(resp.([]byte))) } } }() } select {} }
func main() { fly := flag.Bool("fly", false, "-fly=true //开启服务端优先投递,false为优先存储") logxml := flag.String("logxml", "./log/log.xml", "-logxml=./log/log.xml") bindHost := flag.String("bind", ":13800", "-bind=localhost:13800") zkhost := flag.String("zkhost", "localhost:2181", "-zkhost=localhost:2181") topics := flag.String("topics", "", "-topics=trade,a,b") dlqHourPerDay := flag.Int("dlqHourPerDay", 2, "-dlqExecHour=2 过期消息迁移时间点") db := flag.String("db", "memory://initcap=100000&maxcap=200000", "-db=mysql://master:3306,slave:3306?db=kite&username=root&password=root&maxConn=500&batchUpdateSize=1000&batchDelSize=1000&flushSeconds=1000") pprofPort := flag.Int("pport", -1, "pprof port default value is -1 ") flag.Parse() //加载log4go的配置 log.LoadConfiguration(*logxml) flag.VisitAll(func(f *flag.Flag) { log.InfoLog("kite_server", "KiteQ[%s:%s]", f.Name, f.Value.String()) }) runtime.GOMAXPROCS(runtime.NumCPU()) host, port, _ := net.SplitHostPort(*bindHost) rc := turbo.NewRemotingConfig( "remoting-"+*bindHost, 20000, 16*1024, 16*1024, 10000, 10000, 10*time.Second, 160000) kc := server.NewKiteQConfig("kiteq-"+*bindHost, *bindHost, *zkhost, *fly, 5*time.Second, 8000, 5*time.Second, *dlqHourPerDay /*每天凌晨2点执行*/, strings.Split(*topics, ","), *db, rc) qserver := server.NewKiteQServer(kc) qserver.Start() go func() { if *pprofPort > 0 { http.HandleFunc("/stat", qserver.HandleStat) http.HandleFunc("/binds", qserver.HandleBindings) log.Error(http.ListenAndServe(host+":"+strconv.Itoa(*pprofPort), nil)) } }() var s = make(chan os.Signal, 1) signal.Notify(s, syscall.SIGKILL, syscall.SIGUSR1) //是否收到kill的命令 for { cmd := <-s if cmd == syscall.SIGKILL { break } else if cmd == syscall.SIGUSR1 { //如果为siguser1则进行dump内存 unixtime := time.Now().Unix() path := "./heapdump-kiteq-" + host + "_" + port + fmt.Sprintf("%d", unixtime) f, err := os.Create(path) if nil != err { continue } else { debug.WriteHeapDump(f.Fd()) } } } qserver.Shutdown() log.InfoLog("kite_server", "KiteQServer IS STOPPED!") }