Beispiel #1
0
func NewNsqdServer(opts *nsqd.Options) (*nsqd.NSQD, *NsqdServer) {
	ip := opts.DecideBroadcast()

	nsqdInstance := nsqd.New(opts)

	s := &NsqdServer{}
	ctx := &context{}
	ctx.nsqd = nsqdInstance
	_, port, _ := net.SplitHostPort(opts.TCPAddress)
	rpcport := opts.RPCPort
	if rpcport != "" {
		ip = opts.BroadcastAddress
		consistence.SetCoordLogger(opts.Logger, opts.LogLevel)
		coord := consistence.NewNsqdCoordinator(opts.ClusterID, ip, port, rpcport, strconv.FormatInt(opts.ID, 10), opts.DataPath, nsqdInstance)
		l := consistence.NewNsqdEtcdMgr(opts.ClusterLeadershipAddresses)
		coord.SetLeadershipMgr(l)
		ctx.nsqdCoord = coord
	} else {
		nsqd.NsqLogger().LogWarningf("Start without nsqd coordinator enabled")
		ctx.nsqdCoord = nil
	}

	s.ctx = ctx

	s.exitChan = make(chan int)

	tlsConfig, err := buildTLSConfig(opts)
	if err != nil {
		nsqd.NsqLogger().LogErrorf("FATAL: failed to build TLS config - %s", err)
		os.Exit(1)
	}
	if tlsConfig == nil && opts.TLSRequired != TLSNotRequired {
		nsqd.NsqLogger().LogErrorf("FATAL: cannot require TLS client connections without TLS key and cert")
		os.Exit(1)
	}
	s.ctx.tlsConfig = tlsConfig
	s.ctx.nsqd.SetPubLoop(s.ctx.internalPubLoop)

	nsqd.NsqLogger().Logf(version.String("nsqd"))
	nsqd.NsqLogger().Logf("ID: %d", opts.ID)

	return nsqdInstance, s
}
Beispiel #2
0
func (l *NSQLookupd) Main() {
	ctx := &Context{l}

	tcpListener, err := net.Listen("tcp", l.opts.TCPAddress)
	if err != nil {
		nsqlookupLog.LogErrorf("FATAL: listen (%s) failed - %s", l.opts.TCPAddress, err)
		os.Exit(1)
	}
	l.Lock()
	l.tcpListener = tcpListener
	l.Unlock()
	nsqlookupLog.Logf("TCP: listening on %s", tcpListener.Addr())
	tcpServer := &tcpServer{ctx: ctx}
	l.waitGroup.Wrap(func() {
		protocol.TCPServer(tcpListener, tcpServer)
		nsqlookupLog.Logf("TCP: closing %s", tcpListener.Addr())
	})

	var node consistence.NsqLookupdNodeInfo
	_, node.HttpPort, _ = net.SplitHostPort(l.opts.HTTPAddress)
	if l.opts.ReverseProxyPort != "" {
		node.HttpPort = l.opts.ReverseProxyPort
	}
	node.NodeIP, node.TcpPort, _ = net.SplitHostPort(l.opts.TCPAddress)
	if l.opts.RPCPort != "" {
		nsqlookupLog.Logf("broadcast option: %s, %s", l.opts.BroadcastAddress, l.opts.BroadcastInterface)
		if l.opts.BroadcastInterface != "" {
			node.NodeIP = getIPv4ForInterfaceName(l.opts.BroadcastInterface)
		}
		if node.NodeIP == "" {
			node.NodeIP = l.opts.BroadcastAddress
		} else {
			l.opts.BroadcastAddress = node.NodeIP
		}
		if node.NodeIP == "0.0.0.0" || node.NodeIP == "" {
			nsqlookupLog.LogErrorf("can not decide the broadcast ip: %v", node.NodeIP)
			os.Exit(1)
		}
		nsqlookupLog.Logf("Start with broadcast ip:%s", node.NodeIP)
		node.RpcPort = l.opts.RPCPort
		node.ID = consistence.GenNsqLookupNodeID(&node, "nsqlookup")

		nsqlookupLog.Logf("balance interval is: %v", l.opts.BalanceInterval)
		coordOpts := &consistence.Options{}

		if len(l.opts.BalanceInterval) == 2 {
			coordOpts.BalanceStart, err = strconv.Atoi(l.opts.BalanceInterval[0])
			if err != nil {
				nsqlookupLog.LogErrorf("invalid balance interval: %v", err)
				os.Exit(1)
			}
			coordOpts.BalanceEnd, err = strconv.Atoi(l.opts.BalanceInterval[1])
			if err != nil {
				nsqlookupLog.LogErrorf("invalid balance interval: %v", err)
				os.Exit(1)
			}
		}

		l.Lock()
		consistence.SetCoordLogger(l.opts.Logger, l.opts.LogLevel)
		l.coordinator = consistence.NewNsqLookupCoordinator(l.opts.ClusterID, &node, coordOpts)
		l.Unlock()
		// set etcd leader manager here
		leadership := consistence.NewNsqLookupdEtcdMgr(l.opts.ClusterLeadershipAddresses)
		l.coordinator.SetLeadershipMgr(leadership)
		err = l.coordinator.Start()
		if err != nil {
			nsqlookupLog.LogErrorf("FATAL: start coordinator failed - %s", err)
			os.Exit(1)
		}
	} else {
		nsqlookupLog.Logf("lookup start without the coordinator enabled.")
		l.Lock()
		l.coordinator = nil
		l.Unlock()
	}

	// wait coordinator ready
	time.Sleep(time.Millisecond * 500)
	httpListener, err := net.Listen("tcp", l.opts.HTTPAddress)
	if err != nil {
		nsqlookupLog.LogErrorf("FATAL: listen (%s) failed - %s", l.opts.HTTPAddress, err)
		os.Exit(1)
	}
	l.Lock()
	l.httpListener = httpListener
	l.Unlock()
	httpServer := newHTTPServer(ctx)
	l.waitGroup.Wrap(func() {
		http_api.Serve(httpListener, httpServer, "HTTP", l.opts.Logger)
	})
}