示例#1
0
//启动dm303
func Start(port int, serviceName string) {
	transportFactory := thrift.NewTFramedTransportFactory(thrift.NewTTransportFactory())
	protocolFactory := thrift.NewTBinaryProtocolFactoryDefault()
	networkAddr := fmt.Sprintf(":%d", port)
	serverTransport, err := thrift.NewTServerSocket(networkAddr)
	if err != nil {
		panic(err.Error())
	}
	handler := NewDomobBase(serviceName)
	processor := NewDomobServiceProcessor(handler)
	server := thrift.NewTSimpleServer4(processor, serverTransport, transportFactory, protocolFactory)
	err = server.Serve()
	if err != nil {
		panic(err.Error())
	}
}
示例#2
0
// BootStrap a thrift Server
func BootStrapThriftServer(processor thrift.TProcessor, customizedConfig func()) {
	initGloabl(customizedConfig)

	transportFactory := thrift.NewTFramedTransportFactory(thrift.NewTTransportFactory())
	protocolFactory := thrift.NewTBinaryProtocolFactoryDefault()
	port, _ := config.GlobalConfiger.Int("serverport")
	networkAddr := fmt.Sprintf(":%d", port)
	serverTransport, err := thrift.NewTServerSocket(networkAddr)
	if err != nil {
		bootStrapLogger.Errorf("Error! %s", err)
		os.Exit(1)
	}
	thriftServer = thrift.NewTSimpleServer4(processor, serverTransport, transportFactory, protocolFactory)
	fmt.Printf("thrift server in %s\n", networkAddr)
	if err := thriftServer.Serve(); err != nil {
		bootStrapLogger.Errorf("server start error: %s", err)
	}
}
示例#3
0
// 新建一个client的具体操作
func (p *Pool) newThriftClient() (thriftClient ThriftClient, err error) {
	// transport 层可能有两种transport
	var transportFactory thrift.TTransportFactory
	if p.Framed {
		transportFactory = thrift.NewTTransportFactory()
		transportFactory = thrift.NewTFramedTransportFactory(transportFactory)
	} else {
		transportFactory = thrift.NewTBufferedTransportFactory(8192)
	}
	// protocol 层只用binary protocol
	protocolFactory := thrift.NewTBinaryProtocolFactoryDefault()

	// 随机选取第一个可用的host,如果所有host都不可用那么抛出异常
	hostLen := len(p.Host)
	randHostSlect := rand.Perm(hostLen)
	// host := p.Host[hostLen/randNum]
	var transport thrift.TTransport
	for _, idx := range randHostSlect {
		host := p.Host[idx]
		transport, err = thrift.NewTSocket(host)
		if err != nil {
			continue
		}
		transport = transportFactory.GetTransport(transport)
		err = transport.Open()
		if err == nil {
			break
		}
	}
	// 如果所有主机的连接都打不开,那么抛异常
	if err != nil {
		return nil, err
	}

	clientFactory := reflect.ValueOf(p.ClientFactory)
	args := []reflect.Value{
		reflect.ValueOf(transport), reflect.ValueOf(protocolFactory)}
	res := clientFactory.Call(args)
	thriftClient = res[0].Interface()
	return thriftClient, nil

}