關於怎麼在用java在redis上實現秒殺
阿新 • • 發佈:2018-12-22
為什麼用redis來實現秒殺功能 ,這麼不加贅述。
一、需要準備的外掛和包
Spring data redis
jedis
redisson
jdk1.8等
二、思路
想要使用redis來實現秒殺,首先需要連結上redis,對redis進行操作,這裡需要使用jedis來實現。
那麼,先來實現jedis.在source下建立一個(Maven專案)spring-redis.xml檔案。
程式碼如下:
<?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:util="http://www.springframework.org/schema/util" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- 連線池 --> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> </bean> <!-- 叢集節點 --> <bean id="redisClusterConfig" class="org.springframework.data.redis.connection.RedisClusterConfiguration"> <property name="clusterNodes"> <set> <bean class="org.springframework.data.redis.connection.RedisNode"> <constructor-arg name="host" value="192.168.10.130"></constructor-arg> <constructor-arg name="port" value="7001"></constructor-arg> </bean> <bean class="org.springframework.data.redis.connection.RedisNode"> <constructor-arg name="host" value="192.168.10.130"></constructor-arg> <constructor-arg name="port" value="7002"></constructor-arg> </bean> <bean class="org.springframework.data.redis.connection.RedisNode"> <constructor-arg name="host" value="192.168.10.130"></constructor-arg> <constructor-arg name="port" value="7003"></constructor-arg> </bean> <bean class="org.springframework.data.redis.connection.RedisNode"> <constructor-arg name="host" value="192.168.10.130"></constructor-arg> <constructor-arg name="port" value="7004"></constructor-arg> </bean> <bean class="org.springframework.data.redis.connection.RedisNode"> <constructor-arg name="host" value="192.168.10.130"></constructor-arg> <constructor-arg name="port" value="7005"></constructor-arg> </bean> <bean class="org.springframework.data.redis.connection.RedisNode"> <constructor-arg name="host" value="192.168.10.130"></constructor-arg> <constructor-arg name="port" value="7006"></constructor-arg> </bean> </set> </property> </bean> <!-- ReDis連線工廠 --> <bean id="redis4CacheConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <constructor-arg name="clusterConfig" ref="redisClusterConfig" /> <property name="timeout" value="1000" /> <property name="poolConfig" ref="jedisPoolConfig" /> </bean> <!-- 儲存序列化 --> <bean name="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer" /> <!-- 叢集Resis使用模板 --> <bean id="clusterRedisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="redis4CacheConnectionFactory" /> <property name="keySerializer" ref="stringRedisSerializer" /> <property name="hashKeySerializer" ref="stringRedisSerializer" /> <property name="valueSerializer" ref="stringRedisSerializer" /> <property name="hashValueSerializer" ref="stringRedisSerializer" /> </bean> </beans>
不清楚標籤可以自行百度。用時,直接更改Ip和port 即可。
2、為了防止多執行緒併發出現數據混亂的問題,需要對秒殺進行加鎖。使用redisson客戶端即可實現。
在source下建立一個spring-redisson.xml的檔案,配置程式碼如下:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:redisson="http://redisson.org/schema/redisson" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://redisson.org/schema/redisson http://redisson.org/schema/redisson/redisson.xsd"> <!-- <redisson:client id="redissonClient"> <redisson:single-server address="127.0.0.1:6379" connection-pool-size="30"/> </redisson:client> --> <redisson:client id="redissonClient" > <redisson:cluster-servers scan-interval="10000"> <!-- //scan-interval:叢集狀態掃描間隔時間,單位是毫秒 --> <redisson:node-address value="redis://192.168.10.130:7001"></redisson:node-address> <redisson:node-address value="redis://192.168.10.130:7002"></redisson:node-address> <redisson:node-address value="redis://192.168.10.130:7003"></redisson:node-address> <redisson:node-address value="redis://192.168.10.130:7004"></redisson:node-address> <redisson:node-address value="redis://192.168.10.130:7005"></redisson:node-address> <redisson:node-address value="redis://192.168.10.130:7006"></redisson:node-address> </redisson:cluster-servers> </redisson:client> </beans>
只需要更改value值即可。
3、建立一個Spring的配置檔案。用於整合上面的兩個檔案。程式碼如下:
<?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:util="http://www.springframework.org/schema/util" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <context:component-scan base-package="com.service"></context:component-scan> <import resource="spring-redis.xml"/> <import resource="spring-redisson.xml"/> </beans>
4、建立一個java類,實現一個叫做miaosha的方法。程式碼如下:
package com.service;
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.stereotype.Service;
@Service
public class MiaoShaService {
@Autowired
private RedisTemplate redisTemplate;
@Autowired
private RedissonClient redissonClient;
public Integer miaosha(){
//上鎖
RLock lock = redissonClient.getLock("redis_lock");
lock.lock();
//得到庫存
int num = Integer.parseInt(redisTemplate.boundValueOps("iphone").get().toString());
if(num<1){
return 1;
}
redisTemplate.boundValueOps("iphone").increment(-1);
System.out.println("秒殺成功!");
lock.unlink();
return 0;
}
}
4、測試
package com.test;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
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");
final MiaoShaService miaoShaService = applicationContext.getBean(MiaoShaService.class);
//建立執行緒池
ExecutorService es = Executors.newFixedThreadPool(10);
for (int i=0;i<200;i++)
{
es.execute(new Runnable() {
@Override
public void run() {
miaoShaService.miaosha();
}
});
}
}
}
其中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>redis-02</groupId>
<artifactId>redis-02</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>redis-02</name>
<description />
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.0.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-redis -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.redisson/redisson -->
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
<version>3.5.0</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>