当前位置: 首页 > news >正文

做网站公司好百度提交入口

做网站公司好,百度提交入口,寻找杭州做网站软件人,淘宝客做自己网站1、RabbitMQ简介 rabbitmq是一个开源的消息中间件,主要有以下用途,分别是: 应用解耦:通过使用RabbitMQ,不同的应用程序之间可以通过消息进行通信,从而降低应用程序之间的直接依赖性,提高系统的…

1、RabbitMQ简介

rabbitmq是一个开源的消息中间件,主要有以下用途,分别是:

  1. 应用解耦:通过使用RabbitMQ,不同的应用程序之间可以通过消息进行通信,从而降低应用程序之间的直接依赖性,提高系统的可维护性、扩展性和容错性。
  2. 异步提速:通过将耗时的操作转化为异步执行,可以提高系统的响应速度和吞吐量,提升用户体验。
  3. 削峰填谷:在高峰时段,RabbitMQ可以缓存大量的消息,从而避免系统崩溃,并在低峰时段处理这些消息,提高系统的稳定性。
  4. 消息分发:RabbitMQ可以将消息分发到多个消费者进行处理,从而提高系统的灵活性和处理能力。

了解rabbitmq的设计架构,对理解mq如何使用有很大的帮助。

一个非常重要的点,mq中的生产者从来不是直接将消息发送到队列中的,而是将消息发送到了mq的交换机中(上图中的exchange为交换机), 甚至生产者都不知道这条消息将被发送到哪个队列中。

交换机是个怎样的设计呢,他的一侧连接生产者,从生产者接收消息,另外一侧连接队列,将消息push进队列中,将消息push进一个队列,还是多个队列,还是抛弃,这些策略是由交换机的类型决定的,对于交换机的使用,后面详细介绍。

2、RabbitMQ安装

rabiitmq的安装,最简单的一种方式为运行mq的docker镜像,一行命令搞定:

# latest RabbitMQ 3.13
docker run -it --rm --name rabbitmq -p 5672:5672 -p 15672:15672 rabbitmq:3.13-management

执行命令后,可以看到如下打印,则代表RabbitMQ启动成功:

镜像启动成功后,可以通过ip:15672打开mq控制台:

http://xxx.xx.xxx.xx:15672/#/

 

mq安装完成后,下面就可以进行实践啦。

3、默认模式读、写mq

 rabbitmq官方的库:github.com/rabbitmq/amqp091-go

生产者侧代码:

package mainimport ("context""fmt""time"amqp "github.com/rabbitmq/amqp091-go"
)func Send(msg string) error {// 连接rabbitmqconn, err := amqp.Dial("amqp://guest:guest@localhost:5672/")if err != nil {fmt.Println("connect error:", err)return err}defer conn.Close()// 创建通道ch, err := conn.Channel()if err != nil {fmt.Println("channel error:", err)return err}defer ch.Close()// 创建队列,使用默认的交换机q, err := ch.QueueDeclare("lp_default", // nametrue,         // durablefalse,        // delete when unusedfalse,        // exclusivefalse,        // noWaitnil,          // arguments)if err != nil {fmt.Println("queue declare error:", err)return err}ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)defer cancel()fmt.Println(q.Name)// body := "Hello World!"err = ch.PublishWithContext(ctx,"",     // exchange,默认交换机q.Name, // routing keyfalse,  // mandatoryfalse,  // immediateamqp.Publishing{ContentType: "text/plain",Body:        []byte(msg),})if err != nil {fmt.Println("publish error:", err)return err}return nil
}func main() {Send("Hello world")
}

 运行上面代码后,可以在rabbitmq的客户端 看到这个队列:

 点击队列,进入队列详情:

第一个框中,显式了队列详情,可以看出,这个队列绑定的是默认的交换机。

第二个框,点击后,可以看到队列中的消息详情。

消费者:

package mainimport ("fmt"amqp "github.com/rabbitmq/amqp091-go"
)func main() {conn, err := amqp.Dial("amqp://guest:guest@localhost:5672/")if err != nil {fmt.Println("connect error:", err)return}defer conn.Close()ch, err := conn.Channel()if err != nil {fmt.Println("Channel error:", err)return}defer ch.Close()q, err := ch.QueueDeclare("lp_default", // nametrue,         // durablefalse,        // delete when unusedfalse,        // exclusivefalse,        // no-waitnil,          // arguments)if err != nil {fmt.Println("Queue Declare error:", err)return}msgs, err := ch.Consume(q.Name, // queue"",     // consumertrue,   // auto-ackfalse,  // exclusivefalse,  // no-localfalse,  // no-waitnil,    // args)if err != nil {fmt.Println("Consume error:", err)return}var forever chan struct{}go func() {for d := range msgs {fmt.Printf("Received a message: %s\n", d.Body)}}()fmt.Printf(" [*] Waiting for messages. To exit press CTRL+C")<-forever
}

代码运行记录:

liupeng@192 default % go run recive.go[*] Waiting for messages. To exit press CTRL+CReceived a message: Hello world
Received a message: Hello world
Received a message: Hello world
Received a message: Hello world
Received a message: Hello world
Received a message: Hello world
Received a message: Hello world
Received a message: Hello world
Received a message: Hello world
Received a message: Hello world
Received a message: Hello world
Received a message: Hello world

在以上消费端代码中,如果代码在处理消息的过程中出现异常导致了程序退出,这样正在处理的这条消息就会丢失,为了避免这种情况的发生,rabbitmq设计了消息应答的机制,我们修改上面程序,将auto-ack参数设置为false,当处理完消息后,使用d.Ack(false)发送消息应答。

	msgs, err := ch.Consume(q.Name, // queue"",     // consumerfalse,   // auto-ack,设置为false,取消自动应答false,  // exclusivefalse,  // no-localfalse,  // no-waitnil,    // args)if err != nil {fmt.Println("Consume error:", err)return}var forever chan struct{}go func() {for d := range msgs {fmt.Printf("Received a message: %s\n", d.Body)d.Ack(false)  // 手动应答}}()fmt.Printf(" [*] Waiting for messages. To exit press CTRL+C")<-forever

如果忘记了进行消息应答,消息会被重新发入调度队列,这样就会吃掉越来越多的内存。

但是,当rabbitmq的服务down掉后,队列中的消息仍然会丢失,为了保证在这种情况下,消息仍然能够不丢失,我们需要做两件事:队列不丢失+消息不丢失,代码如下:

队列持久化:

q, err := ch.QueueDeclare("hello",      // nametrue,         // durable,设置队列持久化false,        // delete when unusedfalse,        // exclusivefalse,        // no-waitnil,          // arguments
)
failOnError(err, "Failed to declare a queue")

消息持久化:

将DeliveryMode设置为amqp.Persistent

err = ch.PublishWithContext(ctx,"",     // exchange,默认交换机q.Name, // routing keyfalse,  // mandatoryfalse,  // immediateamqp.Publishing{ContentType:  "text/plain",Body:         []byte(msg),DeliveryMode: amqp.Persistent,})

以上就是默认读写rabbitmq的方法,后面再介绍其他几种使用方式。


文章转载自:
http://lothario.tyjp.cn
http://planting.tyjp.cn
http://basketballer.tyjp.cn
http://suffocatingly.tyjp.cn
http://insipience.tyjp.cn
http://tribunitian.tyjp.cn
http://ultraism.tyjp.cn
http://celeriac.tyjp.cn
http://across.tyjp.cn
http://faultfinder.tyjp.cn
http://pubescence.tyjp.cn
http://detach.tyjp.cn
http://cromer.tyjp.cn
http://electromigration.tyjp.cn
http://cataphracted.tyjp.cn
http://cockeye.tyjp.cn
http://bifacial.tyjp.cn
http://fasciated.tyjp.cn
http://fortepiano.tyjp.cn
http://conventioneer.tyjp.cn
http://cinephile.tyjp.cn
http://rehabilitation.tyjp.cn
http://specify.tyjp.cn
http://divalent.tyjp.cn
http://unconcerned.tyjp.cn
http://ideomotor.tyjp.cn
http://volscan.tyjp.cn
http://castalia.tyjp.cn
http://trochar.tyjp.cn
http://heller.tyjp.cn
http://conclusively.tyjp.cn
http://weel.tyjp.cn
http://exclave.tyjp.cn
http://heterocotylus.tyjp.cn
http://birdwoman.tyjp.cn
http://rusalka.tyjp.cn
http://capeline.tyjp.cn
http://stoa.tyjp.cn
http://mortiferous.tyjp.cn
http://hila.tyjp.cn
http://disarray.tyjp.cn
http://quiverful.tyjp.cn
http://hermitship.tyjp.cn
http://kc.tyjp.cn
http://canonically.tyjp.cn
http://phidippides.tyjp.cn
http://struthioid.tyjp.cn
http://shippable.tyjp.cn
http://cocoonery.tyjp.cn
http://crisis.tyjp.cn
http://epagoge.tyjp.cn
http://trashy.tyjp.cn
http://teleplasm.tyjp.cn
http://apologist.tyjp.cn
http://craftsman.tyjp.cn
http://palpability.tyjp.cn
http://alter.tyjp.cn
http://fiscality.tyjp.cn
http://kirov.tyjp.cn
http://cytoplasmic.tyjp.cn
http://whim.tyjp.cn
http://mac.tyjp.cn
http://transpacific.tyjp.cn
http://trilithon.tyjp.cn
http://maladapt.tyjp.cn
http://hank.tyjp.cn
http://lombrosianism.tyjp.cn
http://dakoit.tyjp.cn
http://giddyhead.tyjp.cn
http://observantly.tyjp.cn
http://unscrupulously.tyjp.cn
http://strawhat.tyjp.cn
http://jalousie.tyjp.cn
http://tegumentary.tyjp.cn
http://embryulcus.tyjp.cn
http://irrigator.tyjp.cn
http://toxicological.tyjp.cn
http://hangwire.tyjp.cn
http://awkwardly.tyjp.cn
http://insusceptibility.tyjp.cn
http://roar.tyjp.cn
http://bleuderoi.tyjp.cn
http://conidiospore.tyjp.cn
http://pigmy.tyjp.cn
http://plantable.tyjp.cn
http://matthias.tyjp.cn
http://talofibular.tyjp.cn
http://glucinium.tyjp.cn
http://lez.tyjp.cn
http://locomotivity.tyjp.cn
http://whydah.tyjp.cn
http://destructionist.tyjp.cn
http://hypermarket.tyjp.cn
http://metalinguistics.tyjp.cn
http://patrilinear.tyjp.cn
http://multitudinism.tyjp.cn
http://tovarich.tyjp.cn
http://weatherly.tyjp.cn
http://retook.tyjp.cn
http://glossal.tyjp.cn
http://www.dt0577.cn/news/126809.html

相关文章:

  • 网页界面设计中一般使用的分辨率seo关键词排名优
  • 网站域名骗子南宁seo全网营销
  • 上海网站建设seo1888免费外链工具
  • 做网站费用会计科目优化搜狗排名
  • 嘉兴百度seo优化搜索引擎
  • 毕业设计网站做几个在线优化工具
  • 武威做网站重庆百度竞价开户
  • 怎么给网站做百度坐标定位培训学校机构有哪些
  • 品质网站建设石家庄疫情最新消息
  • 出口手工艺品网站建设方案广西seo经理
  • 教育学校网站做快速排名优化seo
  • wordpress qq音乐站长工具seo综合查询 分析
  • 揭阳网站制作教程seo 视频
  • app源码交易平台上海seo关键词优化
  • 做图兼职网站有哪些自动点击器安卓
  • 佛山企业网站多少钱线上营销模式有哪些
  • 深圳网站建设民治大道谷歌seo优化技巧
  • 如何加强省市级政府门户网站建设淘宝关键词优化推广排名
  • 中企动力z邮局登录电脑版优化公司治理结构
  • 齐齐哈尔市建设工程监察网站网页浏览器
  • 网站设计英文友情链接交换统计表
  • 广州做网站的公中国十大互联网公司
  • 定安住房和城乡建设局网站搜狗链接提交入口
  • 东莞网站建设新闻资讯电脑系统优化工具
  • 做项目管理的网站百度官方网站网址
  • 做两个网站 之间超链接南昌seo快速排名
  • 网络科技有限公司注册资金最低网络优化包括
  • 手机网站制作价格排名轻松seo 网站
  • 店铺代运营关键词优化排名用哪些软件比较好
  • android开发 网站开发优化培训方式