首页 文章 精选 留言 我的

精选列表

搜索[多租户],共10000篇文章
优秀的个人博客,低调大师

java B2B2C 源码 多级分销Springboot多租户电子商城系统-springcloud项目redis分布式锁

在springcloud项目开发中redis分布式锁使用主要有两个场景 需要JAVA Spring Cloud大型企业分布式微服务云构建的B2B2C电子商务平台源码请加企鹅求求 :二一四七七七五六三三 1.订单重复提交或支付提交等,防止刷单2.对某个业务进行锁定,例如:当用户同一时间,进行对账户充值和提现操作,那么这里需要根据用户ID对账户进行锁定,只有一个完成了才可以进行第二个。开发实现方式1.pom.xml中引入jar包,最好引入到基础模块中,其他模块通用 <!-- redis--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> 创建redis操作类RedisGlobalLock(自定义)redis提供RedisTemplate方法redis提供三个方法:(1)lock 获取锁并锁定 本方法是立即获取锁状态,如果获取成功并锁定,如果获取失败(2)tryLock 尝试获取锁并锁定 本方式是在指定时间尝试获取锁(3)unlock 释放锁 当业务处理完毕必须释放锁重点:lock和tryLock区别:lock是实时获取,tryLock是尝试在一段时间内一直在获取 @Service public class RedisGlobalLock { private static Log log = LogFactory.getLog(RedisGlobalLock.class); private static final String TYPE_NAME = RedisGlobalLock.class.getTypeName(); /** 默认30ms尝试一次 */ private final static long LOCK_TRY_INTERVAL = 30L; /** 默认尝试20s */ private final static long LOCK_TRY_TIMEOUT = 20 * 1000L; /** 单个业务持有锁的时间30s,防止死锁 */ private final static long LOCK_EXPIRE = 30 * 1000L; @Autowired private RedisTemplate<String, Object> redisTemplate; /** * 获取锁 * @param key 锁Key * @return 是否获取锁 */ public boolean lock(String key) { return getLock(key, 0, LOCK_EXPIRE, TimeUnit.MILLISECONDS); } /** * 获取锁 * @param key 锁Key * @param expire 有效期 * @param expireUnit 有效期时间单位 * @return 是否获取锁 */ public boolean lock(String key, long expire, TimeUnit expireUnit) { return getLock(key, 0, expire, expireUnit); } /** * 尝试获取锁 * @param key 锁Key * @return 是否获取锁 */ public boolean tryLock(String key) { return tryLock(key, LOCK_TRY_TIMEOUT, TimeUnit.MILLISECONDS); } /** * 尝试获取锁 * @param key 锁Key * @param timeout 等待超时时间 * @param unit 等待超时时间单位 * @return 是否获取锁 */ public boolean tryLock(String key, long timeout, TimeUnit unit) { // 超时时间转成毫秒 timeout = TimeUnit.MILLISECONDS.convert(timeout, unit); return getLock(key,timeout, LOCK_EXPIRE, TimeUnit.MILLISECONDS); } /** * 尝试获取锁 * @param key 锁Key * @param timeout 等待超时时间 * @param timeoutUnit 等待超时时间单位 * @param expire 有效期 * @param expireUnit 有效期时间单位 * @return */ public boolean tryLock(String key, long timeout, TimeUnit timeoutUnit, long expire, TimeUnit expireUnit) { // 超时时间转成毫秒 timeout = TimeUnit.MILLISECONDS.convert(timeout, timeoutUnit); return getLock(key,timeout, expire, expireUnit); } /** * 释放锁 * @param key 锁Key */ public void unlock(String key) { key = getPrefix(TYPE_NAME) + key; Long oldExpireTime = (Long) redisTemplate.opsForValue().get(key); if(null != oldExpireTime && oldExpireTime >= System.currentTimeMillis()) { // 大于过期时间,则删除key redisTemplate.delete(key); } } /** * 获取锁 * @param key 锁键值 * @param timeout 超时时间 * @param time 全局锁生命周期 * @param unit 时间单位 * @return 是否获取到锁 */ private boolean getLock(String key, long timeout, long time, TimeUnit unit) { key = getPrefix(TYPE_NAME) + key; try { long startTimeMillis = System.currentTimeMillis(); do { long newValue = System.currentTimeMillis() + TimeUnit.MILLISECONDS.convert(time, unit); Boolean isOk = redisTemplate.opsForValue().setIfAbsent(key, newValue); if(isOk) { // 获得锁 redisTemplate.expire(key, time, unit); return true; } // 获取过期时间 Long oldExpireTime = (Long) redisTemplate.opsForValue().get(key); if(null == oldExpireTime) { oldExpireTime = 0L; } if(oldExpireTime >= System.currentTimeMillis()) { // 不小于系统时间并且过了超时时间,则不获取锁 if((System.currentTimeMillis() - startTimeMillis) > timeout) { return false; } // 休眠 Thread.sleep(LOCK_TRY_INTERVAL); } // 新的过期时间 long newExpireTime = System.currentTimeMillis() + TimeUnit.MILLISECONDS.convert(time, unit); Long currentExpireTime = (Long) redisTemplate.opsForValue().getAndSet(key, newExpireTime); if(null == currentExpireTime) { currentExpireTime = 0L; } if(currentExpireTime.equals(oldExpireTime)) { // 获取到锁 redisTemplate.expire(key, time, unit); return true; } } while (true); } catch (Exception e) { return false; } } /** * 获取缓存标识前缀 * @param typeName 类名 * @return 前缀 */ protected final String getPrefix(String typeName) { return typeName; } } 在业务逻辑层引入redis操作类 @Resource private RedisGlobalLock redisGlobalLock; // 1、获取分布式锁防止重复调用 ===================================================== String key = PayDistributePrefix.PAY_MEMBER_ACCOUNT + memberId; if(redisGlobalLock.lock(key)) { try{ System.out.println("--处理业务---"); }catch (Exception e){ throw e; }finally { // 4、释放分布式锁 ================================================================ redisGlobalLock.unlock(key); } }else{ // 如果没有获取锁 Ensure.that(true).isTrue("17000706"); } 所有锁业务必须释放锁,防止死锁 Java B2B2C多用户商城 springcloud架构

优秀的个人博客,低调大师

openstack 租户ip 手动配置 openstack静态租户ip

作者:【吴业亮】云计算开发工程师博客:http://blog.csdn.net/wylfengyujiancheng1、综述:在日常开发和生产环境中经常需要将OpenStack虚拟机配置一个静态Ip,但配置完成后发现外面无法访问。原因是openstack每个端口默认只允许一个Ip的数据报文通过。下面介绍如何配置openstack使虚拟机静态IP地址能访问。2、需求将下面虚拟机配置一个172.16.8.200的Ip,且外部可以访问。3、查询该网卡所在的端口 # neutron port-list | grep '10.168.10.13' 1 1 4、检查该IP未被占用 # ping 172.16.8.200 1 1 5、修改该端口,允许通过IP # neutron port-update 1effb238-d49b-4679-91b9-d843a8ff3e30 --allowed-address-pairs type=dict list=true ip_address=172.16.8.200 1 1 6、也可修改通过一个网段 # neutron port-update 1effb238-d49b-4679-91b9-d843a8ff3e30 --allowed-address-pairs type=dict list=true ip_address=172.16.8.0/24 1 1 7、修改虚拟机网卡IP……略过……8、测试 参考:http://docs.openstack.org/

优秀的个人博客,低调大师

云效发布不同租户 k8s 应用

1. 需求 开发环境是开发测试人员使用的应用环境,除了应用运行依赖的云产品,还需要购买开发人员使用的云产品和运维产品。 主要有: 云效持续集成 maven 私有仓库 gitlib 代码仓库 Node模块仓库 镜像仓库购买 开发环境应用运行环境 2.云效与相关配置 2.1 maven 代码仓库: 私有仓库配置: maven 工具的 profiles 节点,增加私有仓库的 repository; settings 文件加入代码库根目录或者根目录下 pom.xml 增加 repository节点,指向私有仓库。 上传私有仓库: 使用migrate-local-repo-tool.jar(来自开源工具) 上传: $ java -jar migrate-local-repo-tool.jar -cd "/$HOME/.m2/repositor

资源下载

更多资源
Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

WebStorm

WebStorm

WebStorm 是jetbrains公司旗下一款JavaScript 开发工具。目前已经被广大中国JS开发者誉为“Web前端开发神器”、“最强大的HTML5编辑器”、“最智能的JavaScript IDE”等。与IntelliJ IDEA同源,继承了IntelliJ IDEA强大的JS部分的功能。

用户登录
用户注册