当前位置: 首页 > news >正文

php开发网站 用java做后台?如何做好网络推广销售

php开发网站 用java做后台?,如何做好网络推广销售,互联网营销是做什么,开发一个app的费用文章目录 前言一、Redission是什么?二、使用场景三、代码实战1.项目结构2.类图3.maven依赖4.yml5.config6.annotation7.aop8.model9.service 四、单元测试总结 前言 在集群环境下非单体应用存在的问题:JVM锁只能控制本地资源的访问,无法控制…

文章目录

  • 前言
  • 一、Redission是什么?
  • 二、使用场景
  • 三、代码实战
    • 1.项目结构
    • 2.类图
    • 3.maven依赖
    • 4.yml
    • 5.config
    • 6.annotation
    • 7.aop
    • 8.model
    • 9.service
  • 四、单元测试
  • 总结


前言

在集群环境下非单体应用存在的问题:JVM锁只能控制本地资源的访问,无法控制多个JVM间的资源访问,所以需要借助第三方中间件来控制整体的资源访问,redis是一个可以实现分布式锁,保证AP的中间件,可以采用setnx命令进行实现,但是在实现细节上也有很多需要注意的点,比如说获取锁、释放锁时机、锁续命问题,而redission工具能够有效降低实现分布式锁的复杂度,看门狗机制有效解决锁续命问题。


一、Redission是什么?

Redisson是一个用于Java的Redis客户端,它提供了许多分布式对象和服务,使得在Java应用中使用Redis变得更加便捷。Redisson提供了对Redis的完整支持,并附带了一系列的功能,如分布式锁、分布式集合、分布式对象、分布式队列等。


二、使用场景

  1. 分布式锁:Redisson提供了强大的分布式锁机制,通过基于Redis的分布式锁,可以保证在分布式环境下的数据一致性。常见的使用场景包括分布式任务调度、防止重复操作、资源竞争控制等。
  2. 分布式集合:Redisson提供了一系列的分布式集合实现,如Set、List、Queue、Deque等。这些集合的特点是数据分布在多台机器上,并且支持并发访问,适用于需要在多个节点之间共享数据的场景。
  3. 分布式对象:Redisson支持将普通Java对象转换为可在Redis中存储和操作的分布式对象。这些对象可以跨JVM进行传输,并保持一致性。使用分布式对象,可以方便地实现分布式缓存、会话管理等功能。
  4. 分布式队列:Redisson提供了高级的分布式队列实现,支持公平锁和非公平锁的排队方式。分布式队列可以在多个线程和多个JVM之间进行数据传输,适用于消息队列、异步任务处理等场景。
  5. 分布式信号量、锁定和闭锁:Redisson提供了分布式信号量、可重入锁和闭锁等机制,用于实现更复杂的并发控制需求。这些工具能够有效地管理并发访问,确保在分布式环境下的数据操作的正确性。

除了以上提到的功能,Redisson还提供了许多其他的分布式应用场景所需的功能组件,如分布式BitSet、分布式地理位置、分布式发布/订阅等。


三、代码实战

通过aop切面编程,可以降低与业务代码的耦合度,便于拓展和维护

1.项目结构

在这里插入图片描述


2.类图

在这里插入图片描述


3.maven依赖

<dependencies><!-- Spring Boot --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><!-- Redisson --><dependency><groupId>org.redisson</groupId><artifactId>redisson</artifactId><version>3.20.0</version></dependency><!-- Spring AOP --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-aop</artifactId></dependency><!-- Spring Expression Language (SpEL) --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><optional>true</optional></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId></dependency>
</dependencies>

4.yml

dserver:port: 8081spring:redis:host: localhostport: 6379# 如果需要密码认证,请使用以下配置# password: your_password

5.config

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;/*** @author 28382*/
@Configuration
public class RedissonConfig {@Value("${spring.redis.host}")private String redisHost;@Value("${spring.redis.port}")private int redisPort;// 如果有密码认证,请添加以下注解,并修改相应的配置://@Value("${spring.redis.password}")//private String redisPassword;@Bean(destroyMethod = "shutdown")public RedissonClient redissonClient() {Config config = new Config();config.useSingleServer().setAddress("redis://" + redisHost + ":" + redisPort);// 如果有密码认证,请添加以下配置://config.useSingleServer().setPassword(redisPassword);return Redisson.create(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.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;/*** @author 28382*/
@Configuration
public class RedisTemplateConfig {@Beanpublic RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {RedisTemplate<String, Object> template = new RedisTemplate<>();template.setConnectionFactory(connectionFactory);template.setKeySerializer(new StringRedisSerializer());template.setValueSerializer(new GenericJackson2JsonRedisSerializer());template.setHashKeySerializer(new StringRedisSerializer());template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());template.afterPropertiesSet();return template;}
}

6.annotation

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;/*** @author 28382*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface DistributedLock {// 自定义业务keysString[] keys() default {};// 租赁时间 单位:毫秒long leaseTime() default 30000;// 等待时间 单位:毫秒long waitTime() default 3000;}

7.aop

支持解析 SpEL

import com.mxf.code.annotation.DistributedLock;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.DefaultParameterNameDiscoverer;
import org.springframework.core.ParameterNameDiscoverer;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.common.LiteralExpression;
import org.springframework.expression.spel.SpelEvaluationException;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;import java.lang.reflect.Method;
import java.util.concurrent.TimeUnit;@Aspect
@Component
@Slf4j
public class DistributedLockAspect {@Autowiredprivate RedissonClient redissonClient;@Around("@annotation(distributedLock)")public Object lockMethod(ProceedingJoinPoint joinPoint, DistributedLock distributedLock) throws Throwable {MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();Method method = methodSignature.getMethod();// 获取自定义业务keysString[] keys = distributedLock.keys();// 租赁时间long leaseTime = distributedLock.leaseTime();// 等待时间long waitTime = distributedLock.waitTime();// 创建参数名发现器ParameterNameDiscoverer parameterNameDiscoverer = new DefaultParameterNameDiscoverer();// 获取方法参数名String[] parameterNames = parameterNameDiscoverer.getParameterNames(method);// 创建 SpEL 解析器ExpressionParser expressionParser = new SpelExpressionParser();// 创建 SpEL 上下文EvaluationContext evaluationContext = new StandardEvaluationContext();// 设置方法参数值到 SpEL 上下文中Object[] args = joinPoint.getArgs();if (parameterNames != null && args != null && parameterNames.length == args.length) {for (int i = 0; i < parameterNames.length; i++) {evaluationContext.setVariable(parameterNames[i], args[i]);}}// 构建完整的锁键名StringBuilder lockKeyBuilder = new StringBuilder();if (keys.length > 0) {for (String key : keys) {if (StringUtils.hasText(key)) {try {// 解析 SpEL 表达式获取属性值Object value = expressionParser.parseExpression(key).getValue(evaluationContext);lockKeyBuilder.append(value).append(":");} catch (SpelEvaluationException ex) {// 如果解析失败,则使用原始字符串作为属性值LiteralExpression expression = new LiteralExpression(key);lockKeyBuilder.append(expression.getValue()).append(":");}}}}// 使用方法名作为最后一部分键名lockKeyBuilder.append(methodSignature.getName());String fullLockKey = lockKeyBuilder.toString();// 获取 Redisson 锁对象RLock lock = redissonClient.getLock(fullLockKey);// 尝试获取分布式锁// boolean tryLock(long waitTime, long leaseTime, TimeUnit unit)boolean success = lock.tryLock(waitTime, leaseTime, TimeUnit.MILLISECONDS);if (success) {try {// 执行被拦截的方法return joinPoint.proceed();} finally {// 释放锁lock.unlock();}} else {log.error("Failed to acquire distributed lock");// 获取锁超时,抛出异常throw new RuntimeException("Failed to acquire distributed lock");}}}

8.model


import lombok.Data;/*** @author 28382*/
@Data
public class User {private Long id;private String name;private String address;public User(Long id, String name) {this.id = id;this.name = name;}
}

9.service

import com.mxf.code.annotation.DistributedLock;
import com.mxf.code.model.User;
import org.springframework.stereotype.Service;/*** @author 28382*/
@Service
public class UserService {int i = 0;@DistributedLockpublic void test01() {System.out.println("执行方法1 , 当前线程:" + Thread.currentThread().getName() + "执行的结果是:" + ++i);sleep();}@DistributedLock(keys = "myKey",leaseTime = 30L)public void test02() {System.out.println("执行方法2 , 当前线程:" + Thread.currentThread().getName() + "执行的结果是:" + ++i);sleep();}@DistributedLock(keys = "#user.id")public User test03(User user) {System.out.println("执行方法3 , 当前线程:" + Thread.currentThread().getName() + "执行的结果是:" + ++i);sleep();return user;}@DistributedLock(keys = {"#user.id", "#user.name"}, leaseTime = 5000, waitTime = 5000)public User test04(User user) {System.out.println("执行方法4 , 当前线程:" + Thread.currentThread().getName() + "执行的结果是:" + ++i);sleep();return user;}private void sleep() {// 模拟业务耗时try {Thread.sleep(1000L);} catch (InterruptedException e) {throw new RuntimeException(e);}}}

四、单元测试

import com.mxf.code.model.User;
import com.mxf.code.service.UserService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;@SpringBootTest(classes = SpringBootLockTest.class)
@SpringBootApplication
public class SpringBootLockTest {@AutowiredUserService userService;private static final Random RANDOM = new Random();public static void main(String[] args) {SpringApplication.run(SpringBootLockTest.class, args);}@Testpublic void test01() throws Exception {ExecutorService executorService = Executors.newFixedThreadPool(10);Runnable task = () -> userService.test01();for (int i = 0; i < 100; i++) {executorService.submit(task);}Thread.sleep(10000);}@Testpublic void test02() throws Exception {ExecutorService executorService = Executors.newFixedThreadPool(10);Runnable task = () -> userService.test02();for (int i = 0; i < 100; i++) {executorService.submit(task);}Thread.sleep(10000L);}@Testpublic void test03() throws Exception {ExecutorService executorService = Executors.newFixedThreadPool(10);Runnable task = () -> userService.test03(new User(1L, "name"));for (int i = 0; i < 100; i++) {executorService.submit(task);}Thread.sleep(10000L);}@Testpublic void test04() throws Exception {ExecutorService executorService = Executors.newFixedThreadPool(10);Runnable task = () -> userService.test04(new User(1L, "name"));for (int i = 0; i < 100; i++) {executorService.submit(task);}Thread.sleep(100000L);}
}

test01

在这里插入图片描述

test02

在这里插入图片描述

test03

在这里插入图片描述

test04

在这里插入图片描述

总结

可以在项目中单独建立一个Module,需要的子系统直接引入,在需要加分布式的业务代码方法上添加注解及配注解属性值即可,存在一个潜在的问题就是如果redis使用主从架构,在主节点和从节点同步加锁信息时主节点挂掉,这时选取一个暂未同步完整节点信息的从节点作为主节点时,存在一定锁失效的问题,这是可以考虑红锁或者zookeeper实现强一致性。

http://www.hkea.cn/news/850689/

相关文章:

  • 欧美风格网站360指数
  • 优秀网站建设公司电话下列哪些店铺适合交换友情链接
  • 58同城乌鲁木齐网站建设重庆网站到首页排名
  • wordpress知言主题山东服务好的seo公司
  • 旅游商务平台网站建设功能需求关键词排名查询官网
  • 做网站要搭建本地服务器么微商引流被加方法精准客源
  • 网站名字要备案吗友情链接怎么弄
  • 江苏网站开发外链网站大全
  • 网站代备案流程图百度关键词优化排名技巧
  • 石狮建设局网站今日头条站长平台
  • 修改公司网站网页站长素材音效
  • 网站速度测速免费访问国外网站的app
  • 常州网站搭建公司宣传推广渠道有哪些
  • 中国建设监理网站广告网络
  • 网站维护费用怎么收路由优化大师官网
  • 如何加入小说网站做打字员合肥网站优化推广方案
  • 网站建设现状关键词在线优化
  • 网站建设就业百度网址导航主页
  • 郑州公司做网站汉狮中囯联通腾迅
  • 专业网上购物平台优化网站的步骤
  • 用web开发一个网站怎么做网站推广优化平台
  • 建设企业网站进去无法显示搜索引擎seo
  • 网站 分辨率百度视频推广
  • 中国红河网seo排名工具
  • 做网站商丘3a汽车集团公司网络营销方案
  • 网络宣传推广策划范文seo如何优化排名
  • 网站 建设 原则新闻今天的最新新闻
  • 服装网站首页设计主要推广手段免费
  • 网站建设公司做销售好不好?seo搜索引擎优化实训总结
  • 江西威乐建设集团有限公司企业网站长春关键词优化公司