Commit 1b2259d5 by yuwei

项目初始化

parent c94ec921
...@@ -24,5 +24,10 @@ ...@@ -24,5 +24,10 @@
<groupId>org.apache.commons</groupId> <groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId> <artifactId>commons-pool2</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson-spring-boot-starter</artifactId>
<version>${redisson.version}</version>
</dependency>
</dependencies> </dependencies>
</project> </project>
\ No newline at end of file
package cn.datax.common.redis.config;
import cn.datax.common.redis.service.impl.RedissonLocker;
import cn.datax.common.redis.utils.LockUtil;
import lombok.AllArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
@EnableConfigurationProperties({RedisProperties.class})
@AllArgsConstructor
public class RedissonConfig {
private RedisProperties redisProperties;
@Bean
public RedissonClient redissonClient() {
Config config = new Config();
if(redisProperties.getCluster() != null){
// 集群模式配置
config.useClusterServers()
.addNodeAddress(redisProperties.getCluster().getNodes().stream().toArray(String[]::new))
.setPassword(StringUtils.isBlank(redisProperties.getPassword()) ? null : redisProperties.getPassword());
}else if(redisProperties.getSentinel() != null){
//添加哨兵配置
config.useMasterSlaveServers().setMasterAddress(redisProperties.getSentinel().getMaster())
.addSlaveAddress(redisProperties.getSentinel().getNodes().stream().toArray(String[]::new))
.setPassword(StringUtils.isBlank(redisProperties.getPassword()) ? null : redisProperties.getPassword());
}else {
//单节点
config.useSingleServer().setAddress("redis://" + redisProperties.getHost() + ":" + redisProperties.getPort())
.setPassword(StringUtils.isBlank(redisProperties.getPassword()) ? null : redisProperties.getPassword());
}
return Redisson.create(config);
}
@Bean
public RedissonLocker redissonLocker(RedissonClient redissonClient) {
RedissonLocker locker = new RedissonLocker(redissonClient);
// 设置LockUtil的锁处理对象
LockUtil.setLocker(locker);
return locker;
}
}
package cn.datax.common.redis.service;
import org.redisson.api.RLock;
import java.util.concurrent.TimeUnit;
/**
* 锁接口
*/
public interface Locker {
/**
* 获取锁,如果锁不可用,则当前线程处于休眠状态,直到获得锁为止。
*
* @param lockKey
*/
RLock lock(String lockKey);
/**
* 释放锁
*
* @param lockKey
*/
RLock unlock(String lockKey);
/**
* 获取锁,如果锁不可用,则当前线程处于休眠状态,直到获得锁为止。如果获取到锁后,执行结束后解锁或达到超时时间后会自动释放锁
*
* @param lockKey
* @param timeout
*/
RLock lock(String lockKey, int timeout);
/**
* 获取锁,如果锁不可用,则当前线程处于休眠状态,直到获得锁为止。如果获取到锁后,执行结束后解锁或达到超时时间后会自动释放锁
*
* @param lockKey
* @param unit
* @param timeout
*/
RLock lock(String lockKey, TimeUnit unit, int timeout);
/**
* 尝试获取锁,获取到立即返回true,未获取到立即返回false
*
* @param lockKey
* @return
*/
boolean tryLock(String lockKey);
/**
* 尝试获取锁,在等待时间内获取到锁则返回true,否则返回false,如果获取到锁,则要么执行完后程序释放锁,
* 要么在给定的超时时间leaseTime后释放锁
*
* @param lockKey
* @param waitTime
* @param leaseTime
* @param unit
* @return
*/
boolean tryLock(String lockKey, long waitTime, long leaseTime, TimeUnit unit);
/**
* 锁是否被任意一个线程锁持有
*
* @param lockKey
* @return
*/
boolean isLocked(String lockKey);
}
package cn.datax.common.redis.service.impl;
import cn.datax.common.redis.service.Locker;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import java.util.concurrent.TimeUnit;
/**
* 基于Redisson的分布式锁
*/
public class RedissonLocker implements Locker {
private RedissonClient redissonClient;
public RedissonLocker(RedissonClient redissonClient) {
super();
this.redissonClient = redissonClient;
}
public void setRedissonClient(RedissonClient redissonClient) {
this.redissonClient = redissonClient;
}
@Override
public RLock lock(String lockKey) {
RLock lock = redissonClient.getLock(lockKey);
lock.lock();
return lock;
}
@Override
public RLock unlock(String lockKey) {
RLock lock = redissonClient.getLock(lockKey);
lock.unlock();
return lock;
}
@Override
public RLock lock(String lockKey, int leaseTime) {
RLock lock = redissonClient.getLock(lockKey);
lock.lock(leaseTime, TimeUnit.SECONDS);
return lock;
}
@Override
public RLock lock(String lockKey, TimeUnit unit, int timeout) {
RLock lock = redissonClient.getLock(lockKey);
lock.lock(timeout, unit);
return lock;
}
@Override
public boolean tryLock(String lockKey) {
RLock lock = redissonClient.getLock(lockKey);
return lock.tryLock();
}
@Override
public boolean tryLock(String lockKey, long waitTime, long leaseTime, TimeUnit unit) {
RLock lock = redissonClient.getLock(lockKey);
try {
return lock.tryLock(waitTime, leaseTime, unit);
} catch (InterruptedException e) {
return false;
}
}
@Override
public boolean isLocked(String lockKey) {
RLock lock = redissonClient.getLock(lockKey);
return lock.isLocked();
}
}
package cn.datax.common.redis.utils;
import cn.datax.common.redis.service.Locker;
import java.util.concurrent.TimeUnit;
/**
* redis分布式锁工具类
*/
public final class LockUtil {
private static Locker locker;
/**
* 设置工具类使用的locker
*
* @param locker
*/
public static void setLocker(Locker locker) {
LockUtil.locker = locker;
}
/**
* 获取锁
*
* @param lockKey
*/
public static void lock(String lockKey) {
locker.lock(lockKey);
}
/**
* 释放锁
*
* @param lockKey
*/
public static void unlock(String lockKey) {
locker.unlock(lockKey);
}
/**
* 获取锁,超时释放
*
* @param lockKey
* @param timeout
*/
public static void lock(String lockKey, int timeout) {
locker.lock(lockKey, timeout);
}
/**
* 获取锁,超时释放,指定时间单位
*
* @param lockKey
* @param unit
* @param timeout
*/
public static void lock(String lockKey, TimeUnit unit, int timeout) {
locker.lock(lockKey, unit, timeout);
}
/**
* 尝试获取锁,获取到立即返回true,获取失败立即返回false
*
* @param lockKey
* @return
*/
public static boolean tryLock(String lockKey) {
return locker.tryLock(lockKey);
}
/**
* 尝试获取锁,在给定的waitTime时间内尝试,获取到返回true,获取失败返回false,获取到后再给定的leaseTime时间超时释放
*
* @param lockKey
* @param waitTime
* @param leaseTime
* @param unit
* @return
* @throws InterruptedException
*/
public static boolean tryLock(String lockKey, long waitTime, long leaseTime, TimeUnit unit) {
return locker.tryLock(lockKey, waitTime, leaseTime, unit);
}
/**
* 锁释放被任意一个线程持有
*
* @param lockKey
* @return
*/
public static boolean isLocked(String lockKey) {
return locker.isLocked(lockKey);
}
}
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
cn.datax.common.redis.config.RedisConfig,\ cn.datax.common.redis.config.RedisConfig,\
cn.datax.common.redis.aspectj.RedisCacheAspect cn.datax.common.redis.aspectj.RedisCacheAspect,\
cn.datax.common.redis.config.RedissonConfig
<template> <template>
<div class="app-container"> <div class="app-container">
<el-card class="box-card" shadow="always"> <el-card class="box-card" shadow="always">
<div slot="header" class="clearfix">
<span>{{ title }}</span>
</div>
<div :style="classCardbody"> <div :style="classCardbody">
<el-row> <div v-if="searchExecuting">
<el-col :span="24"> <el-row>
<el-input placeholder="请输入内容" class="input-with-select"> <el-col :span="12" :offset="6">
<el-select slot="prepend" placeholder="请选择"> <el-input placeholder="请输入内容" v-model="keyword">
<el-option label="餐厅名" value="1"></el-option> <el-select v-model="type" slot="prepend" placeholder="请选择" style="width: 100px;">
<el-option label="订单号" value="2"></el-option> <el-option label="数据库" value="1"></el-option>
<el-option label="用户电话" value="3"></el-option> <el-option label="数据表" value="2"></el-option>
<el-option label="数据元" value="3"></el-option>
</el-select>
<el-button slot="append" icon="el-icon-search"></el-button>
</el-input>
</el-col>
</el-row>
<el-row>
<el-col :span="6">
6
</el-col>
<el-col :span="18">
18
</el-col>
</el-row>
</div>
<div v-else>
<div class="search-container">
<el-input placeholder="请输入内容" v-model="keyword">
<el-select v-model="type" slot="prepend" placeholder="请选择" style="width: 100px;">
<el-option label="数据库" value="1"></el-option>
<el-option label="数据表" value="2"></el-option>
<el-option label="数据元" value="3"></el-option>
</el-select> </el-select>
<el-button slot="append" icon="el-icon-search"></el-button> <el-button slot="append" icon="el-icon-search" @click="search"></el-button>
</el-input> </el-input>
</el-col> </div>
</el-row> </div>
<el-row>
<el-col :span="6">
6
</el-col>
<el-col :span="18">
18
</el-col>
</el-row>
</div> </div>
</el-card> </el-card>
</div> </div>
...@@ -37,14 +48,32 @@ export default { ...@@ -37,14 +48,32 @@ export default {
return { return {
classCardbody: { classCardbody: {
overflow: 'auto', overflow: 'auto',
height: document.body.offsetHeight - 240 + 'px' height: document.body.offsetHeight - 170 + 'px'
}, },
title: '数据检索' searchExecuting: false,
keyword: '',
type: ''
}
},
methods: {
search () {
this.searchExecuting = true
} }
} }
} }
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>
.search-container {
min-height: 100%;
width: 100%;
text-align: center;
margin-top: 15%;
.el-input {
position: relative;
width: 520px;
max-width: 100%;
margin: 0 auto;
}
}
</style> </style>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment