本文整理汇总了Golang中github.com/cloudfoundry/yagnats.NewClient函数的典型用法代码示例。如果您正苦于以下问题:Golang NewClient函数的具体用法?Golang NewClient怎么用?Golang NewClient使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewClient函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: NewHandlerProvider
func NewHandlerProvider(settings boshsettings.Service, logger boshlog.Logger) (p mbusHandlerProvider) {
p.settings = settings
p.handlers = map[string]Handler{
"nats": newNatsHandler(settings, logger, yagnats.NewClient()),
}
return
}
示例2: main
func main() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
log.Printf("Sending messages...\n")
client := yagnats.NewClient()
err := client.Connect(&yagnats.ConnectionInfo{"127.0.0.1:4222", "nats", "nats"})
if err != nil {
log.Fatalf("Error connecting: %s\n", err)
}
seen := 0
client.Subscribe("foo", func(msg *yagnats.Message) {
seen += 1
})
go func() {
for {
client.Publish("foo", []byte("hi"))
}
}()
<-c
log.Printf("Messages processed: %d\n", seen)
}
示例3: connectToMessageBus
func connectToMessageBus(l logger.Logger, conf *config.Config) yagnats.NATSClient {
members := []yagnats.ConnectionProvider{}
for _, natsConf := range conf.NATS {
members = append(members, &yagnats.ConnectionInfo{
Addr: fmt.Sprintf("%s:%d", natsConf.Host, natsConf.Port),
Username: natsConf.User,
Password: natsConf.Password,
})
}
connectionInfo := &yagnats.ConnectionCluster{
Members: members,
}
natsClient := yagnats.NewClient()
err := natsClient.Connect(connectionInfo)
if err != nil {
l.Error("Failed to connect to the message bus", err)
os.Exit(1)
}
return natsClient
}
示例4: Start
func (runner *NATSRunner) Start() {
if runner.natsSession != nil {
panic("starting an already started NATS runner!!!")
}
_, err := exec.LookPath("gnatsd")
if err != nil {
fmt.Println("You need gnatsd installed!")
os.Exit(1)
}
cmd := exec.Command("gnatsd", "-p", strconv.Itoa(runner.port))
sess, err := gexec.Start(
cmd,
gexec.NewPrefixedWriter("\x1b[32m[o]\x1b[34m[gnatsd]\x1b[0m ", ginkgo.GinkgoWriter),
gexec.NewPrefixedWriter("\x1b[91m[e]\x1b[34m[gnatsd]\x1b[0m ", ginkgo.GinkgoWriter),
)
Ω(err).ShouldNot(HaveOccurred(), "Make sure to have gnatsd on your path")
runner.natsSession = sess
connectionInfo := &yagnats.ConnectionInfo{
Addr: fmt.Sprintf("127.0.0.1:%d", runner.port),
}
messageBus := yagnats.NewClient()
Eventually(func() error {
return messageBus.Connect(connectionInfo)
}, 5, 0.1).ShouldNot(HaveOccurred())
runner.MessageBus = messageBus
}
示例5: TestHandlerProviderGetReturnsNatsHandler
func TestHandlerProviderGetReturnsNatsHandler(t *testing.T) {
deps, provider := buildProvider("nats://0.0.0.0")
handler, err := provider.Get(deps.platform, deps.dirProvider)
assert.NoError(t, err)
assert.IsType(t, NewNatsHandler(deps.settings, deps.logger, yagnats.NewClient()), handler)
}
示例6: Get
func (p mbusHandlerProvider) Get() (handler Handler, err error) {
if p.handler != nil {
handler = p.handler
return
}
mbusUrl, err := url.Parse(p.settings.GetMbusUrl())
if err != nil {
err = bosherr.WrapError(err, "Parsing handler URL")
return
}
switch mbusUrl.Scheme {
case "nats":
handler = newNatsHandler(p.settings, p.logger, yagnats.NewClient())
case "https":
handler = newHttpsHandler(mbusUrl, p.logger)
default:
err = bosherr.New("Message Bus Handler with scheme %s could not be found", mbusUrl.Scheme)
}
p.handler = handler
return
}
示例7: main
func main() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
log.Printf("Receiving messages...\n")
client := yagnats.NewClient()
err := client.Connect(&yagnats.ConnectionInfo{"127.0.0.1:4222", "nats", "nats"})
if err != nil {
log.Fatalf("Error connecting: %s\n", err)
}
seen := 0
client.Subscribe("foo", func(msg *yagnats.Message) {
for i := 0; i < 1000000; i++ {
fmt.Printf("")
}
seen += 1
fmt.Printf("got it! %d\n", seen)
})
<-c
log.Printf("Messages processed: %d\n", seen)
}
示例8: Get
func (p MbusHandlerProvider) Get(
platform boshplatform.Platform,
dirProvider boshdir.DirectoriesProvider,
) (handler boshhandler.Handler, err error) {
if p.handler != nil {
handler = p.handler
return
}
mbusURL, err := url.Parse(p.settings.GetMbusURL())
if err != nil {
err = bosherr.WrapError(err, "Parsing handler URL")
return
}
switch mbusURL.Scheme {
case "nats":
handler = NewNatsHandler(p.settings, p.logger, yagnats.NewClient())
case "https":
handler = micro.NewHTTPSHandler(mbusURL, p.logger, platform.GetFs(), dirProvider)
default:
err = bosherr.New("Message Bus Handler with scheme %s could not be found", mbusURL.Scheme)
}
p.handler = handler
return
}
示例9: init
func init() {
Describe("Testing with Ginkgo", func() {
It("handler provider get returns nats handler", func() {
deps, provider := buildProvider("nats://0.0.0.0")
handler, err := provider.Get(deps.platform, deps.dirProvider)
Expect(err).ToNot(HaveOccurred())
assert.IsType(GinkgoT(), NewNatsHandler(deps.settings, deps.logger, yagnats.NewClient()), handler)
})
It("handler provider get returns https handler", func() {
deps, provider := buildProvider("https://0.0.0.0")
handler, err := provider.Get(deps.platform, deps.dirProvider)
Expect(err).ToNot(HaveOccurred())
assert.IsType(GinkgoT(), micro.HttpsHandler{}, handler)
})
It("handler provider get returns an error if not supported", func() {
deps, provider := buildProvider("foo://0.0.0.0")
_, err := provider.Get(deps.platform, deps.dirProvider)
Expect(err).To(HaveOccurred())
})
})
}
示例10: NewHandlerProvider
func NewHandlerProvider(settings boshsettings.Settings) (p mbusHandlerProvider) {
p.settings = settings
p.handlers = map[string]Handler{
"nats": newNatsHandler(yagnats.NewClient(), settings),
}
return
}
示例11: connect
func (adapter *NatsAdapter) connect() error {
addr := fmt.Sprintf("%s:%d", adapter.host, adapter.port)
client := nats.NewClient()
client.ConnectedCallback = func() {
if adapter.connectedCallback != nil {
adapter.connectedCallback()
}
}
if adapter.logger != nil {
client.Logger = adapter.logger
}
err := client.Connect(&nats.ConnectionInfo{
Addr: addr,
Username: adapter.user,
Password: adapter.password,
})
if err != nil {
return err
}
adapter.client = client
adapter.rand = rand.New(rand.NewSource(time.Now().UnixNano()))
for _, sub := range adapter.subscriptions {
subscribeInNats(adapter, sub)
}
return nil
}
示例12: main
func main() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
log.Printf("Sending messages...\n")
client := yagnats.NewClient()
err := client.Connect(&yagnats.ConnectionInfo{
Addr: "127.0.0.1:4222",
Username: "nats",
Password: "nats",
})
if err != nil {
log.Fatalf("Error connecting: %s\n", err)
}
bigbyte := make([]byte, 512000)
go func() {
for {
client.Publish("foo", bigbyte)
}
}()
<-c
log.Printf("Bye!\n")
}
示例13: buildMessageBus
func buildMessageBus(registrar *Registrar) (messageBus yagnats.NATSClient) {
messageBus = yagnats.NewClient()
natsServers := []yagnats.ConnectionProvider{}
for _, server := range registrar.Config.MessageBusServers {
registrar.logger.Info(
"Adding NATS server",
lager.Data{"server": server},
)
natsServers = append(natsServers, &yagnats.ConnectionInfo{
server.Host,
server.User,
server.Password,
nil,
})
}
natsInfo := &yagnats.ConnectionCluster{natsServers}
err := messageBus.Connect(natsInfo)
if err != nil {
registrar.logger.Info(
"Error connecting to NATS",
lager.Data{"error": err.Error()},
)
panic("Failed to connect to NATS bus.")
}
registrar.logger.Info("Successfully connected to NATS.")
return
}
示例14: main
func main() {
flag.Parse()
nats := yagnats.NewClient()
natsMembers := []yagnats.ConnectionProvider{}
if *natsAddresses == "" {
log.Fatalln("must specify at least one nats address (-natsAddresses=1.2.3.4:5678)")
}
if *ip == "" {
log.Fatalln("must specify IP to route to (-ip=X)")
}
for _, addr := range strings.Split(*natsAddresses, ",") {
log.Println("configuring nats server:", addr)
natsMembers = append(natsMembers, &yagnats.ConnectionInfo{
Addr: addr,
Username: *natsUsername,
Password: *natsPassword,
})
}
if len(natsMembers) == 0 {
log.Fatalln("must specify at least one nats address")
}
natsInfo := &yagnats.ConnectionCluster{natsMembers}
for {
err := nats.Connect(natsInfo)
if err == nil {
break
}
log.Println("failed to connect to NATS:", err)
time.Sleep(1 * time.Second)
}
client := gibson.NewCFRouterClient(*ip, nats)
client.Greet()
for _, route := range strings.Split(*routes, ",") {
routePair := strings.Split(route, ":")
if len(routePair) != 2 {
log.Fatalln("invalid route configuration:", *routes)
}
port, err := strconv.Atoi(routePair[0])
if err != nil {
log.Fatalln("invalid route port:", err)
}
client.Register(port, routePair[1])
}
select {}
}
示例15: NewRouter
func NewRouter(c *config.Config) *Router {
router := &Router{
config: c,
}
// setup number of procs
if router.config.GoMaxProcs != 0 {
runtime.GOMAXPROCS(router.config.GoMaxProcs)
}
router.mbusClient = yagnats.NewClient()
router.registry = registry.NewCFRegistry(router.config, router.mbusClient)
router.registry.StartPruningCycle()
router.varz = varz.NewVarz(router.registry)
args := proxy.ProxyArgs{
EndpointTimeout: router.config.EndpointTimeout,
Ip: router.config.Ip,
TraceKey: router.config.TraceKey,
Registry: router.registry,
Reporter: router.varz,
Logger: access_log.CreateRunningAccessLogger(router.config),
}
router.proxy = proxy.NewProxy(args)
var host string
if router.config.Status.Port != 0 {
host = fmt.Sprintf("%s:%d", router.config.Ip, router.config.Status.Port)
}
varz := &vcap.Varz{
UniqueVarz: router.varz,
}
varz.LogCounts = log.Counter
healthz := &vcap.Healthz{
LockableObject: router.registry,
}
router.component = &vcap.VcapComponent{
Type: "Router",
Index: router.config.Index,
Host: host,
Credentials: []string{router.config.Status.User, router.config.Status.Pass},
Config: router.config,
Varz: varz,
Healthz: healthz,
InfoRoutes: map[string]json.Marshaler{
"/routes": router.registry,
},
}
vcap.StartComponent(router.component)
return router
}