关于rabbitMQ与redis整合所需要所需要的包有:
spring-webmvc 、spring-data-redis、jedis、redisson、amqp-client、log4j-core、log4j-api、log4j-core、log4j-api、spring-rabbit、log4j、jackson-core、jackson-databind、jackson-annotations等
在porm.xml 下配置好:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>s69-rabbitmq</groupId>
<artifactId>s69-rabbitmq</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>s69-rabbitmq</name>
<description />
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>4.3.20.RELEASE</spring.version>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-redis -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.7.2.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
<version>3.5.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.rabbitmq/amqp-client -->
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>5.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.11.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-api -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.11.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.9.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-api -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.9.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.amqp/spring-rabbit -->
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!--Jackson required鍖�-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.1</version>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>javax.servlet.jsp.jstl</artifactId>
<version>1.2.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.2</version>
<configuration>
<version>3.1</version>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
</project>
然后za再把配置文件配置好,首先配置redis和redisson,这里不加赘述。可以在上篇的
https://blog.csdn.net/qq_37186247/article/details/84197708
再配置好spring-task定时器配置文件,代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">
<!-- <task:scheduled-tasks> <task:scheduled ref="miaoshaTask" method="removeExpireUser"
cron=""/> </task:scheduled-tasks> -->
<!-- 开启定时服务注解 -->
<task:annotation-driven />
</beans>
再把关于定时的javada代码写好:
package com.service;
import java.util.concurrent.TimeUnit;
import javax.ejb.Schedule;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
@Service
public class TaskMiaosha {
@Autowired
private RedisTemplate redisTemplate;
@Autowired
private RedissonClient redissonClient;
@Scheduled(cron="0/5 * * * * ? ")
public void removeExpireUser() throws InterruptedException {
RLock rLock = redissonClient.getLock("task_lock");
boolean f = rLock.tryLock(0,5,TimeUnit.SECONDS);
if(f){
long t = System.currentTimeMillis() - 24 * 60 * 60 * 1000;
redisTemplate.boundZSetOps("miaosha_iphone").removeRange(0, t);
Thread.sleep(4000);
rLock.unlock();
System.out.println("执行了删除");
}else{
System.out.println("跳过了这次操作");
}
}
}
上面代码中的@scheduled(cron)是设置一个定时,其中里面的 0/5 * * * * ? 指的是秒、分、时、日、月、星期,其中‘?’是指让其自动计算,根据不同的日子计算不同的星期, 0/5 * * * * ?指的是每隔在5秒执行一次(比如0 5 10 .。。。。);
tryLock()方法:等待几秒后执行,执行多久解锁,时间单位。
if里面的是删除redis中的在24小时前的数据。
在写Miaosha这个方法。
package com.service;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
@Service
public class MiaoShaService {
@Autowired
private RedisTemplate redisTemplate;
@Autowired
private RedissonClient redissonClient;
@Autowired
private RabbitTemplate rabbitTemplate;
public int miaosha(String username) {
// 加锁
RLock rLock =redissonClient.getLock("lock_redis");
rLock.lock();
// 得库存
int num = Integer.parseInt(redisTemplate.boundValueOps("iphone").get()
.toString());
if (num < 1)
return -1;
//如果用户已经秒过就不能再秒
Long result = redisTemplate.boundZSetOps("miaosha_users").rank(username+":iphone");
if (result!=null)
{
System.out.println("用户:"+username+"已经秒过,不能重复秒");
return -2;
}
// 数量-1
redisTemplate.boundValueOps("iphone").increment(-1);
//秒成功,加入集合
redisTemplate.boundZSetOps("miaosha_users").add(username+":iphone", System.currentTimeMillis());
//发消息队列
rabbitTemplate.convertAndSend("miaosha", username+":iphone");
//解锁
rLock.unlock();
return 0;//成功
}
}
在写rabbitMQ的配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:rabbit="http://www.springframework.org/schema/rabbit"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/rabbit
http://www.springframework.org/schema/rabbit/spring-rabbit-1.2.xsd">
<!-- 连接rabbitmq服务器 -->
<rabbit:connection-factory id="conn" host="192.168.10.130" port="5672" username="admin" password="123456"/>
<!-- 创建队列(声明) -->
<rabbit:queue name="miaosha"></rabbit:queue>
<!-- 获取权限:否则队列无法创建 -->
<rabbit:admin connection-factory="conn"/>
<!-- 模板:用于发消息 -->
<rabbit:template id="rabbitTemplate" connection-factory="conn"></rabbit:template>
<!-- 收消息 -->
<bean id="miaoshaListener" class="com.mq.MiaoshaListen"></bean>
<!-- 注册监听器 -->
<bean class="org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer">
<property name="queueNames" value="miaosha"></property>
<property name="messageListener" ref="miaoshaListener"></property>
<property name="connectionFactory" ref="conn"></property>
</bean>
</beans>
在编写一个接收的方法:
package com.mq;
import java.io.UnsupportedEncodingException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
public class MiaoshaListen implements MessageListener {
@Override
public void onMessage(Message msg) {
try {
String str = new String(msg.getBody(),"utf-8");
System.out.println("接收到消息:"+str);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
再ji进行测试:
package com.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.service.MiaoShaService;
public class 测试基本秒杀 {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
MiaoShaService miaoShaService = applicationContext.getBean(MiaoShaService.class);
miaoShaService.miaosha("杀");
}
}