赞
踩
就写到这了,也算是给这段时间的面试做一个总结,查漏补缺,祝自己好运吧,也希望正在求职或者打算跳槽的 程序员看到这个文章能有一点点帮助或收获,我就心满意足了。多思考,多问为什么。希望小伙伴们早点收到满意的offer! 越努力越幸运!
金九银十已经过了,就目前国内的面试模式来讲,在面试前积极的准备面试,复习整个 Java 知识体系将变得非常重要,可以很负责任的说一句,复习准备的是否充分,将直接影响你入职的成功率。但很多小伙伴却苦于没有合适的资料来回顾整个 Java 知识体系,或者有的小伙伴可能都不知道该从哪里开始复习。我偶然得到一份整理的资料,不论是从整个 Java 知识体系,还是从面试的角度来看,都是一份含技术量很高的资料。
//加入上文提供的授权配置
securityManager.setRealm(myRealm);
DefaultSubjectDAO subjectDAO = new DefaultSubjectDAO();
DefaultSessionStorageEvaluator defaultSessionStorageEvaluator = new DefaultSessionStorageEvaluator();
defaultSessionStorageEvaluator.setSessionStorageEnabled(false);
subjectDAO.setSessionStorageEvaluator(defaultSessionStorageEvaluator);
securityManager.setSubjectDAO(subjectDAO);
//使用redis缓存有关信息(实现在下文)
securityManager.setCacheManager(redisCacheManager());
return securityManager;
}
//下面的代码是添加注解支持
@Bean
@DependsOn(“lifecycleBeanPostProcessor”)
public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator = new DefaultAdvisorAutoProxyCreator();
defaultAdvisorAutoProxyCreator.setProxyTargetClass(true);
defaultAdvisorAutoProxyCreator.setUsePrefix(true);
defaultAdvisorAutoProxyCreator.setAdvisorBeanNamePrefix(“_no_advisor”);
return defaultAdvisorAutoProxyCreator;
}
//管理shiro生命周期
@Bean
public static LifecycleBeanPostProcessor lifecycleBeanPostProcessor() {
return new LifecycleBeanPostProcessor();
}
//开启shiro注解
@Bean
public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(DefaultWebSecurityManager securityManager) {
AuthorizationAttributeSourceAdvisor advisor = new AuthorizationAttributeSourceAdvisor();
advisor.setSecurityManager(securityManager);
return advisor;
}
// 使用redis缓存用户信息 ,并设置redis
public RedisCacheManager redisCacheManager() {
RedisCacheManager redisCacheManager = new RedisCacheManager();
//配置redis实例
redisCacheManager.setRedisManager(redisManager());
//redis中针对不同用户缓存(此处的id需要对应user实体中的id字段,用于唯一标识)
redisCacheManager.setPrincipalIdFieldName(“id”);
//用户权限信息缓存时间
redisCacheManager.setExpire(200000);
return redisCacheManager;
}
//连接redsi,分为单机与集群
@Bean
public IRedisManager redisManager() {
log.info(“===============(2)创建RedisManager,连接Redis…”);
IRedisManager manager;
// redis 单机支持,在集群为空,或者集群无机器时候使用 add by jzyadmin@163.com
if (lettuceConnectionFactory.getClusterConfiguration() == null || lettuceConnectionFactory.getClusterConfiguration().getClusterNodes().isEmpty()) {
RedisManager redisManager = new RedisManager();
redisManager.setHost(lettuceConnectionFactory.getHostName());
redisManager.setPort(lettuceConnectionFactory.getPort());
redisManager.setDatabase(lettuceConnectionFactory.getDatabase());
redisManager.setTimeout(0);
if (!StringUtils.isEmpty(lettuceConnectionFactory.getPassword())) {
redisManager.setPassword(lettuceConnectionFactory.getPassword());
}
manager = redisManager;
}else{
// redis集群支持,优先使用集群配置
RedisClusterManager redisManager = new RedisClusterManager();
Set portSet = new HashSet<>();
lettuceConnectionFactory.getClusterConfiguration().getClusterNodes().forEach(node -> portSet.add(new HostAndPort(node.getHost() , node.getPort())));
//update-begin–Author:scott Date:20210531 for:修改集群模式下未设置redis密码的bug issues/I3QNIC
if (oConvertUtils.isNotEmpty(lettuceConnectionFactory.getPassword())) {
JedisCluster jedisCluster = new JedisCluster(portSet, 2000, 2000, 5,
lettuceConnectionFactory.getPassword(), new GenericObjectPoolConfig());
redisManager.setPassword(lettuceConnectionFactory.getPassword());
redisManager.setJedisCluster(jedisCluster);
} else {
JedisCluster jedisCluster = new JedisCluster(portSet);
redisManager.setJedisCluster(jedisCluster);
}
manager = redisManager;
}
return manager;
}
}
这里不详细讲解,主要描述前台传来的签名是否合法。
根据application-dev.yml配置获取是否开启第三方接入验证。@ConfigurationProperties(prefix = "third-app.type")
获取配置文件中的third-app.type的value值。
主要负责将excel中的数据转换为数据字典。
通过获取application-dev.yml,主要判断是否有CLOUD_SERVER_KEY = "spring.cloud.nacos.discovery.server-addr";
的key值。
context.getEnvironment().getProperty(CommonConstant.CLOUD_SERVER_KEY);
是从application-dev.yml生成的map获取value值。
在服务间调用时设置连接时长,如果单体应用,改配置没有使用。
从application-dev.yml获取配置,设置静态参数初始化。
Swagger2文档配置类,如果有需要请执行复制使用。
springboot的常用配置,如跨域配置,精度丢失配置,静态资源配置。都是固定写法,如果需要请自行参考。
springboot提供的websocket的start配置方式,如果有疑问可以参考博主之前的博文-websocket的集成使用
主要提供了日志相关的curd,不多做描述。
主要提供了一些功能的实现类与使用方法,不多说 ,比较简单。
1.TransmitUserTokenFilter、UserTokenContext
主要负责将token放在上下文中。
2.JeecgRedisListerer、RedisReceiver
这里是发送消息模板的封装。核心是从上下文中的getbean方法动态的指定想要调用的JeecgRedisListerer实现类。一种多态的实现方式。
3.JeecgRedisCacheWriter
可以看到思想还是上文所说,将RedisCacheWriter类中的方法全部复制出来,并生成新类JeecgRedisCacheWriter,在新类中修改,他的目的是信息模块在存入缓存时,有统一的前缀。在使用时,注入使用JeecgRedisCacheWriter即可,跟修改源码有这一样效果,但是更加优雅。可以看出spring的设计思想是多牛批。
主要负责调用其他功能,没啥实质意义。
下图类是xxljob执行定时任务时的写法,可以看一看。
主要为业务代码包,这里找几个着重讲解一下。
1.api.controller
为微服务为其他服务提供基础应用接口的包,如果没有微服务该包不生效。
2.message
该模块为消息模块业务,其中使用了quartz和spring提供的websocket start。如果有兴趣可以参考博主给的连接。
3.monitor
提供了redis监控的接口。如果需要可以自行查看,比较简单。
4.quartz
定时任务start,如果想具体了解可以参考博主文章:quartz集成全解析
springboot的核心就是提供各种各样的start,在jeecg中也提供了很多的start,分别是上述的四个,其中job为xxl开源项目、cloud为在服务间调用时,将token放再头中,这里不详细讲解。
下文示例均在jeecg-cloud-module/jeecg-cloud-system-start中。
1.MqListener、BaseRabbiMqHandler
在监听消息队列时,使用以下方法即可。原因是必须加入ack与nack。防止队列占用。
使用时,该类继承了BaseRabbiMqHandler,并使用父类的方法,并使用new MqListener<BaseMap>()
函数式方法获取消息队列中的信息。
@Slf4j
@RabbitListener(queues = CloudConstant.MQ_JEECG_PLACE_ORDER)
@RabbitComponent(value = “helloReceiver1”)
public class HelloReceiver1 extends BaseRabbiMqHandler {
@RabbitHandler
public void onMessage(BaseMap baseMap, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag) {
//使用了父类的方法
super.onMessage(baseMap, deliveryTag, channel, new MqListener() {
@Override
public void handler(BaseMap map, Channel channel) {
//业务处理
String orderId = map.get(“orderId”).toString();
log.info("MQ Receiver1,orderId : " + orderId);
}
});
}
}
BaseRabbiMqHandler主要的功能是提供了ack与nack,并将token放入头中。
@Slf4j
public class BaseRabbiMqHandler {
private String token= UserTokenContext.getToken();
public void onMessage(T t, Long deliveryTag, Channel channel, MqListener mqListener) {
try {
UserTokenContext.setToken(token);
mqListener.handler(t, channel);
channel.basicAck(deliveryTag, false);
} catch (Exception e) {
log.info(“接收消息失败,重新放回队列”);
try {
/**
deliveryTag:该消息的index
multiple:是否批量.true:将一次性拒绝所有小于deliveryTag的消息。
requeue:被拒绝的是否重新入队列
*/
channel.basicNack(deliveryTag, false, true);
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
public interface MqListener {
default void handler(T map, Channel channel) {
}
}
2.RabbitMqClient
主要在队列初始化时实现队列的初始化,而是否初始化根据使用时的@RabbitListener、@RabbitComponent
判断。
public interface MqListener {
default void handler(T map, Channel channel) {
}
}
@Bean
public void initQueue() {
//获取带RabbitComponent注解的类
Map<String, Object> beansWithRqbbitComponentMap = this.applicationContext.getBeansWithAnnotation(RabbitComponent.class);
Class<? extends Object> clazz = null;
//循环map
for (Map.Entry<String, Object> entry : beansWithRqbbitComponentMap.entrySet()) {
log.info(“初始化队列…”);
//获取到实例对象的class信息
clazz = entry.getValue().getClass();
Method[] methods = clazz.getMethods();
//判断是否有RabbitListener注解
RabbitListener rabbitListener = clazz.getAnnotation(RabbitListener.class);
//类上有注解 就创建队列
if (ObjectUtil.isNotEmpty(rabbitListener)) {
createQueue(rabbitListener);
}
//方法上有注解 就创建队列
for (Method method : methods) {
RabbitListener methodRabbitListener = method.getAnnotation(RabbitListener.class);
if (ObjectUtil.isNotEmpty(methodRabbitListener)) {
createQueue(methodRabbitListener);
}
}
}
}
/**
初始化队列
@param rabbitListener
*/
private void createQueue(RabbitListener rabbitListener) {
String[] queues = rabbitListener.queues();
//创建交换机
DirectExchange directExchange = createExchange(DelayExchangeBuilder.DELAY_EXCHANGE);
rabbitAdmin.declareExchange(directExchange);
//创建队列
if (ObjectUtil.isNotEmpty(queues)) {
for (String queueName : queues) {
Properties result = rabbitAdmin.getQueueProperties(queueName);
if (ObjectUtil.isEmpty(result)) {
Queue queue = new Queue(queueName);
addQueue(queue);
Binding binding = BindingBuilder.bind(queue).to(directExchange).with(queueName);
rabbitAdmin.declareBinding(binding);
log.info(“创建队列:” + queueName);
}else{
log.info(“已有队列:” + queueName);
}
}
}
}
3.RabbitMqConfig
为消息队列的常用配置方式。这里不多描述。
4.event
这个包主要是为使用mq发送消息使用,多类别的消息会实现JeecgBusEventHandler
类,而BaseApplicationEvent
通过消息类型传入的不同的参数选择合适的业务类发送消息。
5.DelayExchangeBuilder
为延时队列的交换机声明与绑定。
1.如何使用分布式锁
使用时有两种方式,一种是使用注解方式,一种是使用redisson提供的API。
@Scheduled(cron = “0/5 * * * * ?”)
@JLock(lockKey = CloudConstant.REDISSON_DEMO_LOCK_KEY1)
public void execute() throws InterruptedException {
log.info(“执行execute任务开始,休眠三秒”);
Thread.sleep(3000);
System.out.println(“=业务逻辑1=======”);
Map map = new BaseMap();
map.put(“orderId”, “BJ0001”);
rabbitMqClient.sendMessage(CloudConstant.MQ_JEECG_PLACE_ORDER, map);
//延迟10秒发送
map.put(“orderId”, “NJ0002”);
rabbitMqClient.sendMessage(CloudConstant.MQ_JEECG_PLACE_ORDER, map, 10000);
log.info(“execute任务结束,休眠三秒”);
}
public DemoLockTest() {
}
/**
*/
//@Scheduled(cron = “0/5 * * * * ?”)
public void execute2() throws InterruptedException {
if (redissonLock.tryLock(CloudConstant.REDISSON_DEMO_LOCK_KEY2, -1, 6000)) {
log.info(“执行任务execute2开始,休眠十秒”);
Thread.sleep(10000);
System.out.println(“=业务逻辑2=======”);
log.info(“定时execute2结束,休眠十秒”);
redissonLock.unlock(CloudConstant.REDISSON_DEMO_LOCK_KEY2);
} else {
log.info(“execute2获取锁失败”);
}
}
2.RepeatSubmitAspect
通过公平锁判断是否是多次点击按钮。
@Around(“pointCut(jRepeat)”)
public Object repeatSubmit(ProceedingJoinPoint joinPoint,JRepeat jRepeat) throws Throwable {
String[] parameterNames = new LocalVariableTableParameterNameDiscoverer().getParameterNames(((MethodSignature) joinPoint.getSignature()).getMethod());
if (Objects.nonNull(jRepeat)) {
// 获取参数
Object[] args = joinPoint.getArgs();
// 进行一些参数的处理,比如获取订单号,操作人id等
StringBuffer lockKeyBuffer = new StringBuffer();
String key =getValueBySpEL(jRepeat.lockKey(), parameterNames, args,“RepeatSubmit”).get(0);
// 公平加锁,lockTime后锁自动释放
boolean isLocked = false;
try {
isLocked = redissonLockClient.fairLock(key, TimeUnit.SECONDS, jRepeat.lockTime());
// 如果成功获取到锁就继续执行
if (isLocked) {
// 执行进程
return joinPoint.proceed();
} else {
// 未获取到锁
throw new Exception(“请勿重复提交”);
}
} finally {
// 如果锁还存在,在方法执行完成后,释放锁
if (isLocked) {
redissonLockClient.unlock(key);
}
}
}
return joinPoint.proceed();
}
3.DistributedLockHandler
该类主要是jLock的切面类,通过jLock注解参数,判断需要加锁的类型,同时加锁的方法也不相同。
//jLock切面,进行加锁
@SneakyThrows
@Around(“@annotation(jLock)”)
public Object around(ProceedingJoinPoint joinPoint, JLock jLock) {
Object obj = null;
log.info(“进入RedisLock环绕通知…”);
RLock rLock = getLock(joinPoint, jLock);
boolean res = false;
//获取超时时间
long expireSeconds = jLock.expireSeconds();
//等待多久,n秒内获取不到锁,则直接返回
long waitTime = jLock.waitTime();
//执行aop
if (rLock != null) {
try {
if (waitTime == -1) {
res = true;
//一直等待加锁
rLock.lock(expireSeconds, TimeUnit.MILLISECONDS);
} else {
res = rLock.tryLock(waitTime, expireSeconds, TimeUnit.MILLISECONDS);
}
if (res) {
obj = joinPoint.proceed();
} else {
log.error(“获取锁异常”);
}
} finally {
if (res) {
rLock.unlock();
}
}
}
log.info(“结束RedisLock环绕通知…”);
return obj;
}
//通过参数判断加锁类型
@SneakyThrows
private RLock getLock(ProceedingJoinPoint joinPoint, JLock jLock) {
//获取key
String[] keys = jLock.lockKey();
if (keys.length == 0) {
throw new RuntimeException(“keys不能为空”);
}
//获取参数
String[] parameterNames = new LocalVariableTableParameterNameDiscoverer().getParameterNames(((MethodSignature) joinPoint.getSignature()).getMethod());
Object[] args = joinPoint.getArgs();
LockModel lockModel = jLock.lockModel();
if (!lockModel.equals(LockModel.MULTIPLE) && !lockModel.equals(LockModel.REDLOCK) && keys.length > 1) {
throw new RuntimeException(“参数有多个,锁模式为->” + lockModel.name() + “.无法锁定”);
}
RLock rLock = null;
String keyConstant = jLock.keyConstant();
//判断锁类型
if (lockModel.equals(LockModel.AUTO)) {
if (keys.length > 1) {
lockModel = LockModel.REDLOCK;
} else {
lockModel = LockModel.REENTRANT;
}
}
//根据不同的锁类型执行不同的加锁方式
switch (lockModel) {
case FAIR:
rLock = redissonClient.getFairLock(getValueBySpEL(keys[0], parameterNames, args, keyConstant).get(0));
break;
case REDLOCK:
List rLocks = new ArrayList<>();
for (String key : keys) {
List valueBySpEL = getValueBySpEL(key, parameterNames, args, keyConstant);
for (String s : valueBySpEL) {
rLocks.add(redissonClient.getLock(s));
}
}
RLock[] locks = new RLock[rLocks.size()];
int index = 0;
for (RLock r : rLocks) {
locks[index++] = r;
}
rLock = new RedissonRedLock(locks);
break;
case MULTIPLE:
rLocks = new ArrayList<>();
for (String key : keys) {
List valueBySpEL = getValueBySpEL(key, parameterNames, args, keyConstant);
for (String s : valueBySpEL) {
rLocks.add(redissonClient.getLock(s));
}
}
locks = new RLock[rLocks.size()];
index = 0;
for (RLock r : rLocks) {
locks[index++] = r;
}
rLock = new RedissonMultiLock(locks);
break;
case REENTRANT:
List valueBySpEL = getValueBySpEL(keys[0], parameterNames, args, keyConstant);
//如果spel表达式是数组或者LIST 则使用红锁
if (valueBySpEL.size() == 1) {
rLock = redissonClient.getLock(valueBySpEL.get(0));
} else {
locks = new RLock[valueBySpEL.size()];
index = 0;
for (String s : valueBySpEL) {
locks[index++] = redissonClient.getLock(s);
}
rLock = new RedissonRedLock(locks);
}
break;
case READ:
rLock = redissonClient.getReadWriteLock(getValueBySpEL(keys[0], parameterNames, args, keyConstant).get(0)).readLock();
break;
case WRITE:
rLock = redissonClient.getReadWriteLock(getValueBySpEL(keys[0], parameterNames, args, keyConstant).get(0)).writeLock();
break;
}
return rLock;
}
4.RedissonLockClient
redisson客户端,提供了一大波方法,请自行查看。
public class RedissonLockClient {
@Autowired
private RedissonClient redissonClient;
@Autowired
private RedisTemplate<String, Object> redisTemplate;
/**
*/
public RLock getLock(String lockKey) {
return redissonClient.getLock(lockKey);
}
/**
加锁操作
@return boolean
*/
public boolean tryLock(String lockName, long expireSeconds) {
return tryLock(lockName, 0, expireSeconds);
}
.
.
.
5.core包
主要通过application.yml配置文件获取redis连接类型,通过根据该参数动态的选择策略类,连接redis。
public class RedissonManager {
public Redisson getRedisson() {
return redisson;
}
//Redisson连接方式配置工厂
static class RedissonConfigFactory {
private RedissonConfigFactory() {
}
private static volatile RedissonConfigFactory factory = null;
public static RedissonConfigFactory getInstance() {
if (factory == null) {
synchronized (Object.class) {
if (factory == null) {
factory = new RedissonConfigFactory();
}
}
}
return factory;
}
//根据连接类型創建连接方式的配置
Config createConfig(RedissonProperties redissonProperties) {
Preconditions.checkNotNull(redissonProperties);
Preconditions.checkNotNull(redissonProperties.getAddress(), “redis地址未配置”);
RedisConnectionType connectionType = redissonProperties.getType();
// 声明连接方式
RedissonConfigStrategy redissonConfigStrategy;
if (connectionType.equals(RedisConnectionType.SENTINEL)) {
redissonConfigStrategy = new SentinelRedissonConfigStrategyImpl();
} else if (connectionType.equals(RedisConnectionType.CLUSTER)) {
redissonConfigStrategy = new ClusterRedissonConfigStrategyImpl();
} else if (connectionType.equals(RedisConnectionType.MASTERSLAVE)) {
redissonConfigStrategy = new MasterslaveRedissonConfigStrategyImpl();
} else {
redissonConfigStrategy = new StandaloneRedissonConfigStrategyImpl();
}
Preconditions.checkNotNull(redissonConfigStrategy, “连接方式创建异常”);
return redissonConfigStrategy.createRedissonConfig(redissonProperties);
}
}
}
//策略实现,此类是指定redis的连接方式是哨兵。
public class SentinelRedissonConfigStrategyImpl implements RedissonConfigStrategy {
首先分享一份学习大纲,内容较多,涵盖了互联网行业所有的流行以及核心技术,以截图形式分享:
(亿级流量性能调优实战+一线大厂分布式实战+架构师筑基必备技能+设计思想开源框架解读+性能直线提升架构技术+高效存储让项目性能起飞+分布式扩展到微服务架构…实在是太多了)
其次分享一些技术知识,以截图形式分享一部分:
Tomcat架构解析:
算法训练+高分宝典:
Spring Cloud+Docker微服务实战:
最后分享一波面试资料:
切莫死记硬背,小心面试官直接让你出门右拐
1000道互联网Java面试题:
Java高级架构面试知识整理:
ClusterRedissonConfigStrategyImpl();
} else if (connectionType.equals(RedisConnectionType.MASTERSLAVE)) {
redissonConfigStrategy = new MasterslaveRedissonConfigStrategyImpl();
} else {
redissonConfigStrategy = new StandaloneRedissonConfigStrategyImpl();
}
Preconditions.checkNotNull(redissonConfigStrategy, “连接方式创建异常”);
return redissonConfigStrategy.createRedissonConfig(redissonProperties);
}
}
}
//策略实现,此类是指定redis的连接方式是哨兵。
public class SentinelRedissonConfigStrategyImpl implements RedissonConfigStrategy {
首先分享一份学习大纲,内容较多,涵盖了互联网行业所有的流行以及核心技术,以截图形式分享:
(亿级流量性能调优实战+一线大厂分布式实战+架构师筑基必备技能+设计思想开源框架解读+性能直线提升架构技术+高效存储让项目性能起飞+分布式扩展到微服务架构…实在是太多了)
其次分享一些技术知识,以截图形式分享一部分:
Tomcat架构解析:
[外链图片转存中…(img-rZqDZGoM-1715401886392)]
算法训练+高分宝典:
[外链图片转存中…(img-qpGfgE2y-1715401886393)]
Spring Cloud+Docker微服务实战:
[外链图片转存中…(img-gOzN3kGC-1715401886393)]
最后分享一波面试资料:
切莫死记硬背,小心面试官直接让你出门右拐
1000道互联网Java面试题:
[外链图片转存中…(img-RPTRluVP-1715401886393)]
Java高级架构面试知识整理:
[外链图片转存中…(img-YeOq5RCa-1715401886394)]
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。