forked from zhoustone/middle-ware-parent
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
cicadasmile
committed
Jul 4, 2019
1 parent
678804e
commit e428fe1
Showing
18 changed files
with
584 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<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"> | ||
<parent> | ||
<artifactId>middle-ware-parent</artifactId> | ||
<groupId>com.boot.parent</groupId> | ||
<version>1.0-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>com.redis.cluster</groupId> | ||
<artifactId>ware-redis-cluster</artifactId> | ||
<packaging>jar</packaging> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-data-redis</artifactId> | ||
<version>${spring-boot.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>redis.clients</groupId> | ||
<artifactId>jedis</artifactId> | ||
<version>${redis-client.version}</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
<version>${spring-boot.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<version>${spring-boot.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-aop</artifactId> | ||
<version>${spring-boot.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-configuration-processor</artifactId> | ||
<version>${spring-boot.version}</version> | ||
<optional>true</optional> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-context-support</artifactId> | ||
<version>${spring.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>joda-time</groupId> | ||
<artifactId>joda-time</artifactId> | ||
<version>${joda.time.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.alibaba</groupId> | ||
<artifactId>fastjson</artifactId> | ||
<version>${fastjson.version}</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
<!-- 项目构建 --> | ||
<build> | ||
<finalName>${project.artifactId}</finalName> | ||
<resources> | ||
<resource> | ||
<directory>src/main/resources</directory> | ||
<filtering>true</filtering> | ||
</resource> | ||
</resources> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>2.3.2</version> | ||
<configuration> | ||
<source>1.8</source> | ||
<target>1.8</target> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
12 changes: 12 additions & 0 deletions
12
ware-redis-cluster/src/main/java/com/redis/cluster/RedisApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.redis.cluster; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; | ||
|
||
@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class}) | ||
public class RedisApplication { | ||
public static void main(String[] args) { | ||
SpringApplication.run(RedisApplication.class,args) ; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
ware-redis-cluster/src/main/java/com/redis/cluster/config/RedisConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.redis.cluster.config; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.data.redis.connection.RedisConnectionFactory; | ||
import org.springframework.data.redis.core.StringRedisTemplate; | ||
|
||
@Configuration | ||
public class RedisConfig { | ||
|
||
@Bean | ||
public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory factory) { | ||
StringRedisTemplate stringRedisTemplate = new StringRedisTemplate(); | ||
stringRedisTemplate.setConnectionFactory(factory); | ||
return stringRedisTemplate; | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
ware-redis-cluster/src/main/java/com/redis/cluster/config/RedisLock.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.redis.cluster.config; | ||
|
||
import org.springframework.stereotype.Component; | ||
import redis.clients.jedis.Jedis; | ||
import redis.clients.jedis.JedisSentinelPool; | ||
import javax.annotation.Resource; | ||
|
||
@Component | ||
public class RedisLock { | ||
private static String keyPrefix = "RedisLock:"; | ||
@Resource | ||
private JedisSentinelPool jedisSentinelPool; | ||
|
||
public boolean addLock(String key, long expire) { | ||
Jedis jedis = null; | ||
try { | ||
jedis = jedisSentinelPool.getResource(); | ||
/* | ||
* nxxx的值只能取NX或者XX,如果取NX,则只有当key不存在是才进行set,如果取XX,则只有当key已经存在时才进行set | ||
* expx的值只能取EX或者PX,代表数据过期时间的单位,EX代表秒,PX代表毫秒。 | ||
*/ | ||
String value = jedis.set(keyPrefix + key, "1", "nx", "ex", expire); | ||
return value != null; | ||
} catch (Exception e){ | ||
e.printStackTrace(); | ||
}finally { | ||
if (jedis != null) jedis.close(); | ||
} | ||
return false; | ||
} | ||
|
||
public void removeLock(String key) { | ||
Jedis jedis = null; | ||
try { | ||
jedis = jedisSentinelPool.getResource(); | ||
jedis.del(keyPrefix + key); | ||
} finally { | ||
if (jedis != null) jedis.close(); | ||
} | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
ware-redis-cluster/src/main/java/com/redis/cluster/config/RedisParam.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package com.redis.cluster.config; | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
|
||
@ConfigurationProperties(prefix = "spring.redis.sentinel") | ||
public class RedisParam { | ||
|
||
private String nodes ; | ||
private String master ; | ||
private Integer maxTotal ; | ||
private Integer minIdle ; | ||
private Integer maxWaitMillis ; | ||
private Integer timeBetweenEvictionRunsMillis ; | ||
private boolean testWhileIdle ; | ||
private boolean testOnBorrow ; | ||
private boolean testOnReturn ; | ||
|
||
public String getNodes() { | ||
return nodes; | ||
} | ||
public void setNodes(String nodes) { | ||
this.nodes = nodes; | ||
} | ||
public String getMaster() { | ||
return master; | ||
} | ||
public void setMaster(String master) { | ||
this.master = master; | ||
} | ||
|
||
public Integer getMaxTotal() { | ||
return maxTotal; | ||
} | ||
|
||
public void setMaxTotal(Integer maxTotal) { | ||
this.maxTotal = maxTotal; | ||
} | ||
|
||
public Integer getMinIdle() { | ||
return minIdle; | ||
} | ||
|
||
public void setMinIdle(Integer minIdle) { | ||
this.minIdle = minIdle; | ||
} | ||
|
||
public Integer getMaxWaitMillis() { | ||
return maxWaitMillis; | ||
} | ||
|
||
public void setMaxWaitMillis(Integer maxWaitMillis) { | ||
this.maxWaitMillis = maxWaitMillis; | ||
} | ||
|
||
public Integer getTimeBetweenEvictionRunsMillis() { | ||
return timeBetweenEvictionRunsMillis; | ||
} | ||
|
||
public void setTimeBetweenEvictionRunsMillis(Integer timeBetweenEvictionRunsMillis) { | ||
this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis; | ||
} | ||
|
||
public boolean isTestWhileIdle() { | ||
return testWhileIdle; | ||
} | ||
|
||
public void setTestWhileIdle(boolean testWhileIdle) { | ||
this.testWhileIdle = testWhileIdle; | ||
} | ||
|
||
public boolean isTestOnBorrow() { | ||
return testOnBorrow; | ||
} | ||
|
||
public void setTestOnBorrow(boolean testOnBorrow) { | ||
this.testOnBorrow = testOnBorrow; | ||
} | ||
|
||
public boolean isTestOnReturn() { | ||
return testOnReturn; | ||
} | ||
|
||
public void setTestOnReturn(boolean testOnReturn) { | ||
this.testOnReturn = testOnReturn; | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
ware-redis-cluster/src/main/java/com/redis/cluster/config/RedisPool.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.redis.cluster.config; | ||
|
||
import com.redis.cluster.listen.RedisListener; | ||
import com.redis.cluster.utils.SpringUtil; | ||
import org.apache.commons.pool2.impl.GenericObjectPoolConfig; | ||
import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import redis.clients.jedis.JedisSentinelPool; | ||
import javax.annotation.Resource; | ||
import java.util.Arrays; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
@Configuration | ||
@EnableConfigurationProperties(RedisParam.class) | ||
public class RedisPool { | ||
|
||
@Resource | ||
private RedisParam redisParam ; | ||
|
||
@Bean("jedisSentinelPool") | ||
public JedisSentinelPool getRedisPool (){ | ||
Set<String> sentinels = new HashSet<>(); | ||
sentinels.addAll(Arrays.asList(redisParam.getNodes().split(","))); | ||
GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig(); | ||
poolConfig.setMaxTotal(redisParam.getMaxTotal()); | ||
poolConfig.setMinIdle(redisParam.getMinIdle()); | ||
poolConfig.setMaxWaitMillis(redisParam.getMaxWaitMillis()); | ||
poolConfig.setTestWhileIdle(redisParam.isTestWhileIdle()); | ||
poolConfig.setTestOnBorrow(redisParam.isTestOnBorrow()); | ||
poolConfig.setTestOnReturn(redisParam.isTestOnReturn()); | ||
poolConfig.setTimeBetweenEvictionRunsMillis(redisParam.getTimeBetweenEvictionRunsMillis()); | ||
JedisSentinelPool redisPool = new JedisSentinelPool(redisParam.getMaster(), sentinels, poolConfig); | ||
return redisPool; | ||
} | ||
@Bean | ||
SpringUtil springUtil() { | ||
return new SpringUtil(); | ||
} | ||
@Bean | ||
RedisListener redisListener() { | ||
return new RedisListener(); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
ware-redis-cluster/src/main/java/com/redis/cluster/controller/MsgBody.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.redis.cluster.controller; | ||
|
||
import java.util.Date; | ||
|
||
public class MsgBody { | ||
|
||
private String name ; | ||
|
||
private String desc ; | ||
|
||
private Date createTime ; | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String getDesc() { | ||
return desc; | ||
} | ||
|
||
public void setDesc(String desc) { | ||
this.desc = desc; | ||
} | ||
|
||
public Date getCreateTime() { | ||
return createTime; | ||
} | ||
|
||
public void setCreateTime(Date createTime) { | ||
this.createTime = createTime; | ||
} | ||
} |
Oops, something went wrong.