1. 程式人生 > >spring 整合jedis 做秒殺!!!!

spring 整合jedis 做秒殺!!!!

一.spring整合jedis
1.匯入spring開發包 (5.0)

	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-webmvc</artifactId>
		<version>5.0.0.RELEASE</version>
	</dependency>

2.匯入jedis (2.9) ————>redis客戶端

	<dependency>
		<groupId>redis.clients</groupId>
		<artifactId>jedis</artifactId>
		<version>2.9.0</version>
	</dependency>

3.匯入spring data redis(整合包2.0) spring對redis的支援

	<dependency>
		<groupId>org.springframework.data</groupId>
		<artifactId>spring-data-redis</artifactId>
		<version>2.0.0.RELEASE</version>
	</dependency>

4.在spring配置redis模板

<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.83.5"></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.83.5"></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.83.5"></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.83.5"></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.83.5"></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.83.5"></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>

<!-- 儲存序列化 -->  (key 和 value 都是使用 defaultSerializer = new JdkSerializationRedisSerializer(); 進行序列化的。
                            如果專案中只使用了string的 key 和 value ,顯然這樣不適合在sever上進行debug,通過配置,可以改成使用StringRedisSerializer對 key 和 value 進行序列化)


<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>

5.在servic業務層注入模板RedisTemplate (applicationContext.xml中)

   <import resource="spring-redis.xml"/>

二.spring整合redisson

匯入redisson客戶端

	<!-- https://mvnrepository.com/artifact/org.redisson/redisson -->
	<dependency>
		<groupId>org.redisson</groupId>
		<artifactId>redisson</artifactId>
		<version>3.5.0</version>
	</dependency>

2.在spring配置RedissonClient

 <!-- redis叢集配置 -->
 <redisson:client id="redissonClient" >
    <redisson:cluster-servers scan-interval="10000">   <!-- //scan-interval:叢集狀態掃描間隔時間,單位是毫秒 -->
    	<redisson:node-address value="redis://192.168.83.5:7001"></redisson:node-address>
    	<redisson:node-address value="redis://192.168.83.5:7002"></redisson:node-address>
    	<redisson:node-address value="redis://192.168.83.5:7003"></redisson:node-address>
    	<redisson:node-address value="redis://192.168.83.5:7004"></redisson:node-address>
    	<redisson:node-address value="redis://192.168.83.5:7005"></redisson:node-address>
    	<redisson:node-address value="redis://192.168.83.5:7006"></redisson:node-address>
    </redisson:cluster-servers>
</redisson:client>

3.在service業務層注入(applicationContext.xml中)

 <import resource="spring-redisson.xml"/>

4.關鍵程式碼加鎖,解鎖.(com.service MiaoShaService.java中)

註釋注入:(模板和redissonClient)======>掃描在applicationContext.xml中
@Autowired
private RedisTemplate redisTemplate;

@Autowired
private RedissonClient  redissonClient;

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());
	 
	//發訊息佇列 
	System.out.println(username+"秒殺成功了");
	
	//解鎖
	rLock.unlock();
	
	return 0;//成功
}