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

做网站开发的营业执照技能培训网站

做网站开发的营业执照,技能培训网站,做美直播网站,网站可以自己做服务器么Broker 异步调用中用Broker进行事件订阅和调用,完成解耦 没有强依赖,不用担心级联失败 流量削峰 MQ 的下载 1.可以使用命令拉取镜像 docker pull rabbitmq:3-management 2.也可以直接去官网下载tar包,然后上传到虚拟机上面 spring AMQP…

Broker

异步调用中用Broker进行事件订阅和调用,完成解耦

没有强依赖,不用担心级联失败

流量削峰

MQ 的下载

1.可以使用命令拉取镜像

docker pull rabbitmq:3-management

2.也可以直接去官网下载tar包,然后上传到虚拟机上面

spring AMQP        消息队列

Basic Queue 简单队列模型   

只需要简单的引入amqp依赖,

<!--AMQP依赖,包含RabbitMQ-->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

然后配置接收和发送端的地址

spring:rabbitmq:host: 192.168.xxx.xxx # 自己主机名port: 5672 # 端口virtual-host: / # 虚拟主机username: xxxxxx # 用户名password: xxxxxx # 密码

然后调用方法发送或结合搜消息即可

发送:

package cn.itcast.mq.spring;import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringAmqpTest {@Autowiredprivate RabbitTemplate rabbitTemplate;@Testpublic void testSimpleQueue() {// 队列名称String queueName = "simple.queue";// 消息String message = "hello, spring amqp!";// 发送消息rabbitTemplate.convertAndSend(queueName, message);}
}

接收:

package cn.itcast.mq.listener;import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;@Component
public class SpringRabbitListener {@RabbitListener(queues = "simple.queue")public void listenSimpleQueueMessage(String msg) throws InterruptedException {System.out.println("spring 消费者接收到消息:【" + msg + "】");}
}

当然了,如果不是在父工程里面配置的依赖则需要在单个项目里面单独配置

Work Queue  一队列,多消费者

假设编辑五百条消息:

/*** workQueue* 向队列中不停发送消息,模拟消息堆积。*/
@Test
public void testWorkQueue() throws InterruptedException {// 队列名称String queueName = "simple.queue";// 消息String message = "hello, message_";for (int i = 0; i < 500; i++) {// 发送消息rabbitTemplate.convertAndSend(queueName, message + i);Thread.sleep(20);}
}

定义两个接受者用不同效率接收:

@RabbitListener(queues = "simple.queue")
public void listenWorkQueue1(String msg) throws InterruptedException {System.out.println("消费者1接收到消息:【" + msg + "】" + LocalTime.now());Thread.sleep(20);
}@RabbitListener(queues = "simple.queue")
public void listenWorkQueue2(String msg) throws InterruptedException {System.err.println("消费者2........接收到消息:【" + msg + "】" + LocalTime.now());Thread.sleep(200);
}

然后可以看出接受者接收消息并未在预定时间被消费,原因是被队列平均分配了,只要重新定制规则即可:

spring:rabbitmq:listener:simple:prefetch: 1 # 每次只能获取一条消息,处理完成才能获取下一个消息

交换机

Fanout

创建FanoutExchange交换机和Queue队列,然后交换机和队列绑定:

package cn.itcast.mq.config;import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class FanoutConfig {/*** 声明交换机* @return Fanout类型交换机*/@Beanpublic FanoutExchange fanoutExchange(){return new FanoutExchange("itcast.fanout");}/*** 第1个队列*/@Beanpublic Queue fanoutQueue1(){return new Queue("fanout.queue1");}/*** 绑定队列和交换机*/@Beanpublic Binding bindingQueue1(Queue fanoutQueue1, FanoutExchange fanoutExchange){return BindingBuilder.bind(fanoutQueue1).to(fanoutExchange);}/*** 第2个队列*/@Beanpublic Queue fanoutQueue2(){return new Queue("fanout.queue2");}/*** 绑定队列和交换机*/@Beanpublic Binding bindingQueue2(Queue fanoutQueue2, FanoutExchange fanoutExchange){return BindingBuilder.bind(fanoutQueue2).to(fanoutExchange);}
}

绑定完成后就可以写消费者和生产者代码了

生产者

@Test
public void testFanoutExchange() {// 队列名称String exchangeName = "itcast.fanout";// 消息String message = "hello, everyone!";rabbitTemplate.convertAndSend(exchangeName, "", message);
}

消费者

@RabbitListener(queues = "fanout.queue1")
public void listenFanoutQueue1(String msg) {System.out.println("消费者1接收到Fanout消息:【" + msg + "】");
}@RabbitListener(queues = "fanout.queue2")
public void listenFanoutQueue2(String msg) {System.out.println("消费者2接收到Fanout消息:【" + msg + "】");
}

交换机的作用是什么?

* 接收publisher发送的消息
* 将消息按照规则路由到与之绑定的队列
* 不能缓存消息,路由失败,消息丢失
* FanoutExchange的会将消息路由到每个绑定的队列

声明队列、交换机、绑定关系的Bean是什么?

* Queue
* FanoutExchange
* Binding

http://www.15wanjia.com/news/54617.html

相关文章:

  • 电竞网站开发需求报告饥饿营销案例
  • 做淘宝客需要企业网站吗微信社群营销推广方案
  • 网上做兼职网站有哪些长沙seo代理
  • 广州中学生网站制作宁波seo关键词优化教程
  • 公司网站优化推广吸引人的微信软文
  • 合肥网站排名优化公司哪家好营销说白了就是干什么的
  • 老域名网站不收录搜索引擎竞价排名
  • 新网站建设的工作总结电商推广和网络推广的策略
  • dede网站白屏天津搜索引擎优化
  • wordpress 自定义字段 查询武汉关键词seo排名
  • 网站优化怎么做分录优化深圳seo
  • 为什么很少人敢娶外贸女北京网站优化哪家好
  • 建e网全屋设计效果图宁波seo关键词优化方法
  • 美国一般用什么做网站主页软件制作
  • 中国建设银行网站暑假工报名广州seo推广
  • 注册电气师在哪个网站做变更搜狗关键词优化软件
  • 云南省建设培训网站站长工具seo下载
  • 做网站备案微信群推广
  • 网站开发建设需要什么上海单个关键词优化
  • 自己做的网站怎么在移动端访问黑帽seo是什么
  • 企业网站的建立要做的准备淘宝推广
  • 网站设计 尺寸成人电脑培训班办公软件
  • 网站建设活动免费网站在线观看人数在哪直播
  • 威海网站优化公司惠州网站关键词排名
  • 一家做公司点评的网站优化大师软件大全
  • 开发企业网站的公司网站优化方案怎么写
  • app定制公司seo推广视频隐迅推专业
  • 做网站需要什么证明嘛广东网络seo推广公司
  • 二级备案域名泰安网站seo
  • 网站优化如何做pc指数百度推广客服人工电话多少