Git Product home page Git Product logo

gotools's Introduction

gotools

mq连接池

1、导入package:

go get github.com/plm-lee/gotools/mq

2、使用连接池:

在mq中声明了一个全局的mq连接池,[var ConnPool *Pool], 因此在使用mq包时不需要调用者自己声明,mq包内部会自动从mq池中取空闲的连接

package main

import (
 "github.com/plm-lee/gotools/mq"
 "log"
)

//InitConnPool() 可以传入三个mq连接地址,默认第一个连接失败会切换到其他地址
func Init(){
    mq.InitConnPool(Addr,Addr1, Addr2, MaxIdle)
}

func main()  {
	Init() //初始化
	
	//mq 推送消息为json字符串
	strtmp := `{name:"test"}`
	
    mq.Publish("3rd.in.exchange", "direct", "sms.key", strtmp, true) //publish
    
    worker := CommonConsumer(RouterMqExchange, RouterMqQueue, RouterMqKey, RouterMqTag)
    
    //启动消费者
    go worker.StartUp()
    
    select{}
}

//创建消费者
func CommonConsumer(exchange, queue, key, ctag string) *mq.Consumer {
	consumer, err := mq.NewConsumer(
		exchange,
		"direct",
		queue,
		key,
		ctag,    //simple-consumer
		handler) // call back Fun
	log.Fatal(err, "[ERROR] Consumer 启动失败")
	return consumer
}

//消费者业务逻辑
func handler(in string) (out string, err error) {

	log.Println("[mq json] json:", in)
	
	//TODO...
	return "", nil
}

redis

1、导入package

go get github.com/plm-lee/gotools/redispool

2、使用redis连接池

var ConnPool *redis.Pool
var ConnPoolLocalNet *redis.Pool

redis内部声明了两个连接池,(本地redis连接池及远程连接池,本质上都是一样)

package main

import "github.com/plm-lee/gotools/redispool"

func main()  {
	//初始化
    redispool.InitConnPool(Addr, Password, Db, MaxIdle)
    redispool.InitConnPoolLocal(Addr, Password, Db, MaxIdle)
    
    //获取连接
    rc := redispool.ConnPool.Get()
    defer rc.Close() //使用完记得close
    
    //或者
    rc := redispool.ConnPoolLocalNet.Get()
    defer rc.Close() //使用完记得close
    
    //使用Do()方法
    rc.Do("RPOP", "key-test")   
}

gotools's People

Contributors

plm-lee avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.