MQ中的使用场景

什么是削峰限流

1.使用场景

    秒杀活动,一般会因为流量过大,导致应用挂掉,为了解决这个问题,一般在应用前端加入消息队列。

作用:1.可以控制活动人数,超过此一定法治的订单直接丢弃

           2.可以缓解短时间的高流量压垮应用(应用程序按自己的最大处理能力获取订单)。

什么是应用解耦

1.使用场景

     双11的购物节,用户下单后,订单系统需要通知库存系统,传统的做法是订单系统调用库存系统的接口

缺点:当库存系统出现故障时,订单就失败。

进行应用解耦。

电商平台的“秒杀抢购”:

采用“消息队列中间件”技术,在高并发环境下充当重要角色。

如何用测试用例来做高并发

1.不是使用httpClient而是使用RestTemplate做并发。

2.先定义一个并发数200,用for循环来定200个线程

3.使用CountDownLatch来保障200个线程是并发执行而不是串行执行。

4.最后在测试用例添加seelp的目的是保障线程都能执行完成,

package com.zte.power.dexcloud.test;

import java.util.concurrent.CountDownLatch;

import org.junit.Test;
import org.springframework.web.client.RestTemplate;

@SpringBooTest(classes = TripApplication.class)
@Runwith(SpringJunit4ClassRunner.class)
public class MultithreadTest {
	private final String url = "";
	
	RestTemplate template = new RestTemplate();//不用http发请求,使用httpclient发送请求
	
	private static final int user_num = 200;//并发数,200
	
	private static CountDownLatch cdl = new CountDownLatch(user_num);
	
	
	
	@Test
	public void  TestInvoke{
		
		for(int i = 0 ;i <user_num;i++){
			new Thread(new TicketRequest()).start();
			//到目前为止都是串行执行,需要使用countDownlatch,等待来得到并发执行
			cdl.countDown();
		}
		Thread.currentThread().sleep(3000);//目的是主程序等待,使得其他线程能执行完成
        //这里也可以使用this.join()来解决
	}
	
	public class TicketRequest implements Runnable{

		@Override
		public void run() {
			try{
				cdl.await();//
			}catch(InterruptedException e){
				e.printStackTrace();
			}
			
		}
		
		//调用http
		String result = template.getForEntity(url, String.class).getBody();
		//System.out.println(result);
	    System.out.println(result);
		
		
	}
}

猜你喜欢

转载自blog.csdn.net/yinni11/article/details/82354086