浏览代码

字典权限配置信息redis刷新

rengb 3 年之前
父节点
当前提交
018da07c58

+ 4 - 0
security-center/pom.xml

@@ -34,6 +34,10 @@
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-aop</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-amqp</artifactId>
+        </dependency>
     </dependencies>
 
     <build>

+ 5 - 6
security-center/src/main/java/com/lantone/security/aop/DictionaryAspect.java

@@ -1,7 +1,6 @@
 package com.lantone.security.aop;
 
-import com.lantone.dblayermbg.facade.DictionaryInfoFacade;
-import org.aspectj.lang.JoinPoint;
+import com.lantone.security.component.MessageSender;
 import org.aspectj.lang.annotation.After;
 import org.aspectj.lang.annotation.Aspect;
 import org.aspectj.lang.annotation.Pointcut;
@@ -18,15 +17,15 @@ import org.springframework.stereotype.Component;
 public class DictionaryAspect {
 
     @Autowired
-    private DictionaryInfoFacade dictionaryInfoFacade;
+    private MessageSender messageSender;
 
-    @Pointcut("execution(public * com.lantone.security.web.DictionaryManagementController.deleteDictionary(..))")
+    @Pointcut("execution(public * com.lantone.security.web.UserManagementController.login(..))")
     public void basicInfoChange() {
     }
 
     @After("basicInfoChange()")
-    public void aspectAfter(JoinPoint joinPoint) throws Throwable {
-        dictionaryInfoFacade.initDicMap();
+    public void aspectAfter() {
+        messageSender.sendRedisCacheRefreshMessage(DictionaryAspect.class.getSimpleName());
     }
 
 }

+ 5 - 6
security-center/src/main/java/com/lantone/security/aop/HospitalSetAspect.java

@@ -1,7 +1,6 @@
 package com.lantone.security.aop;
 
-import com.lantone.dblayermbg.facade.HospitalSetFacade;
-import org.aspectj.lang.JoinPoint;
+import com.lantone.security.component.MessageSender;
 import org.aspectj.lang.annotation.After;
 import org.aspectj.lang.annotation.Aspect;
 import org.aspectj.lang.annotation.Pointcut;
@@ -18,15 +17,15 @@ import org.springframework.stereotype.Component;
 public class HospitalSetAspect {
 
     @Autowired
-    private HospitalSetFacade hospitalSetFacade;
+    private MessageSender messageSender;
 
-    @Pointcut("execution(public * com.lantone.security.web.UserManagementController.login(..))")
+    @Pointcut("execution(public * com.lantone.security.web.UserManagementController.addUser(..))")
     public void basicInfoChange() {
     }
 
     @After("basicInfoChange()")
-    public void aspectAfter(JoinPoint joinPoint) throws Throwable {
-        hospitalSetFacade.initHospitalSetMap();
+    public void aspectAfter() {
+        messageSender.sendRedisCacheRefreshMessage(HospitalSetAspect.class.getSimpleName());
     }
 
 }

+ 12 - 13
security-center/src/main/java/com/lantone/security/aop/ResourceRoleRulesAspect.java

@@ -1,7 +1,6 @@
 package com.lantone.security.aop;
 
-import com.lantone.dblayermbg.facade.ResourceFacade;
-import org.aspectj.lang.JoinPoint;
+import com.lantone.security.component.MessageSender;
 import org.aspectj.lang.annotation.After;
 import org.aspectj.lang.annotation.Aspect;
 import org.aspectj.lang.annotation.Pointcut;
@@ -18,27 +17,27 @@ import org.springframework.stereotype.Component;
 public class ResourceRoleRulesAspect {
 
     @Autowired
-    private ResourceFacade resourceFacade;
+    private MessageSender messageSender;
 
     @Pointcut("execution(public * com.lantone.security.web.RoleManagementController.addRole(..))" +
             "||execution(public * com.lantone.security.web.RoleManagementController.deleteRole(..))" +
             "||execution(public * com.lantone.security.web.RoleManagementController.disableRole(..))" +
-            "||execution(public * com.lantone.security.web.RoleManagementController.updateRole(..))"+
-            "||execution(public * com.lantone.security.web.FuncManagementController.addMenu(..))"+
-            "||execution(public * com.lantone.security.web.FuncManagementController.updateMenu(..))"+
-            "||execution(public * com.lantone.security.web.FuncManagementController.deleteMenu(..))"+
-            "||execution(public * com.lantone.security.web.FuncManagementController.disableMenu(..))"+
-            "||execution(public * com.lantone.security.web.HospitalManagementController.addHospital(..))"+
-            "||execution(public * com.lantone.security.web.HospitalManagementController.updateHospital(..))"+
-            "||execution(public * com.lantone.security.web.HospitalManagementController.disableHospital(..))"+
+            "||execution(public * com.lantone.security.web.RoleManagementController.updateRole(..))" +
+            "||execution(public * com.lantone.security.web.FuncManagementController.addMenu(..))" +
+            "||execution(public * com.lantone.security.web.FuncManagementController.updateMenu(..))" +
+            "||execution(public * com.lantone.security.web.FuncManagementController.deleteMenu(..))" +
+            "||execution(public * com.lantone.security.web.FuncManagementController.disableMenu(..))" +
+            "||execution(public * com.lantone.security.web.HospitalManagementController.addHospital(..))" +
+            "||execution(public * com.lantone.security.web.HospitalManagementController.updateHospital(..))" +
+            "||execution(public * com.lantone.security.web.HospitalManagementController.disableHospital(..))" +
             "||execution(public * com.lantone.security.web.HospitalManagementController.deleteHospital(..))"
     )
     public void basicInfoChange() {
     }
 
     @After("basicInfoChange()")
-    public void aspectAfter(JoinPoint joinPoint) throws Throwable {
-        resourceFacade.initResourceRolesMap();
+    public void aspectAfter() {
+        messageSender.sendRedisCacheRefreshMessage(ResourceRoleRulesAspect.class.getSimpleName());
     }
 
 }

+ 44 - 0
security-center/src/main/java/com/lantone/security/component/MessageReceiver.java

@@ -0,0 +1,44 @@
+package com.lantone.security.component;
+
+import com.lantone.dblayermbg.facade.DictionaryInfoFacade;
+import com.lantone.dblayermbg.facade.HospitalSetFacade;
+import com.lantone.dblayermbg.facade.ResourceFacade;
+import com.lantone.security.aop.DictionaryAspect;
+import com.lantone.security.aop.HospitalSetAspect;
+import com.lantone.security.aop.ResourceRoleRulesAspect;
+import com.rabbitmq.client.Channel;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.amqp.core.Message;
+import org.springframework.amqp.rabbit.annotation.RabbitListener;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+/**
+ * @Description: 基础信息变更导致redis需要刷新-消息的处理者
+ * @author: rengb
+ * @time: 2021/1/5 18:27
+ */
+@Slf4j
+@Component
+public class MessageReceiver {
+
+    @Autowired
+    private DictionaryInfoFacade dictionaryInfoFacade;
+    @Autowired
+    private HospitalSetFacade hospitalSetFacade;
+    @Autowired
+    private ResourceFacade resourceFacade;
+
+    @RabbitListener(queues = "emrais.redis.cache.refresh.queue", ackMode = "MANUAL")
+    public void RedisCacheRefreshHandle(String type, Message message, Channel channel) throws Exception {
+        if (type.equals(DictionaryAspect.class.getSimpleName())) {
+            dictionaryInfoFacade.initDicMap();
+        } else if (type.equals(HospitalSetAspect.class.getSimpleName())) {
+            hospitalSetFacade.initHospitalSetMap();
+        } else if (type.equals(ResourceRoleRulesAspect.class.getSimpleName())) {
+            resourceFacade.initResourceRolesMap();
+        }
+        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
+    }
+
+}

+ 26 - 0
security-center/src/main/java/com/lantone/security/component/MessageSender.java

@@ -0,0 +1,26 @@
+package com.lantone.security.component;
+
+import com.lantone.security.enums.QueueEnum;
+import org.springframework.amqp.rabbit.core.RabbitTemplate;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+/**
+ * @Description: 消息的发出者
+ * @author: rengb
+ * @time: 2021/1/5 18:27
+ */
+@Component
+public class MessageSender {
+
+    @Autowired
+    private RabbitTemplate rabbitTemplate;
+
+    /**
+     * 基础信息变更导致redis需要刷新—消息发送
+     */
+    public void sendRedisCacheRefreshMessage(String type) {
+        rabbitTemplate.convertAndSend(QueueEnum.QUEUE_REDIS_CACHE_REFRESH.getExchange(), QueueEnum.QUEUE_REDIS_CACHE_REFRESH.getRouteKey(), type);
+    }
+
+}

+ 48 - 0
security-center/src/main/java/com/lantone/security/config/RabbitMqConfig.java

@@ -0,0 +1,48 @@
+package com.lantone.security.config;
+
+import com.lantone.security.enums.QueueEnum;
+import org.springframework.amqp.core.Binding;
+import org.springframework.amqp.core.BindingBuilder;
+import org.springframework.amqp.core.DirectExchange;
+import org.springframework.amqp.core.ExchangeBuilder;
+import org.springframework.amqp.core.Queue;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+/**
+ * @Description: 消息队列相关配置
+ * @author: rengb
+ * @time: 2021/1/5 18:27
+ */
+@Configuration
+public class RabbitMqConfig {
+
+    /**
+     * 基础信息变更导致redis需要刷新—队列所绑定的交换机
+     */
+    @Bean
+    DirectExchange redisCacheRefreshDirect() {
+        return ExchangeBuilder.directExchange(QueueEnum.QUEUE_REDIS_CACHE_REFRESH.getExchange()).durable(true).build();
+    }
+
+    /**
+     * 基础信息变更导致redis需要刷新—队列
+     */
+    @Bean
+    public Queue redisCacheRefreshQueue() {
+        return new Queue(QueueEnum.QUEUE_REDIS_CACHE_REFRESH.getName(), true, false, false);
+    }
+
+
+    /**
+     * 将-基础信息变更导致redis需要刷新-队列绑定到交换机
+     */
+    @Bean
+    Binding redisCacheRefreshBinding(DirectExchange redisCacheRefreshDirect, Queue redisCacheRefreshQueue) {
+        return BindingBuilder
+                .bind(redisCacheRefreshQueue)
+                .to(redisCacheRefreshDirect)
+                .with(QueueEnum.QUEUE_REDIS_CACHE_REFRESH.getRouteKey());
+    }
+
+}

+ 39 - 0
security-center/src/main/java/com/lantone/security/enums/QueueEnum.java

@@ -0,0 +1,39 @@
+package com.lantone.security.enums;
+
+import lombok.Getter;
+
+/**
+ * @Description: 消息队列枚举配置
+ * @author: rengb
+ * @time: 2021/1/5 18:27
+ */
+@Getter
+public enum QueueEnum {
+
+    /**
+     * 消息通知队列-基础信息变更导致redis需要刷新
+     */
+    QUEUE_REDIS_CACHE_REFRESH("emrais.redis.cache.refresh.direct", "emrais.redis.cache.refresh.queue", "emrais.redis.cache.refresh.routekey");
+
+    /**
+     * 交换名称
+     */
+    private String exchange;
+
+    /**
+     * 队列名称
+     */
+    private String name;
+
+    /**
+     * 路由键
+     */
+    private String routeKey;
+
+    QueueEnum(String exchange, String name, String routeKey) {
+        this.exchange = exchange;
+        this.name = name;
+        this.routeKey = routeKey;
+    }
+
+}

+ 35 - 0
security-center/src/main/java/com/lantone/security/rabbitmq/ConfirmCallbackService.java

@@ -0,0 +1,35 @@
+package com.lantone.security.rabbitmq;
+
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.amqp.rabbit.connection.CorrelationData;
+import org.springframework.amqp.rabbit.core.RabbitTemplate;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.PostConstruct;
+
+/**
+ * @Description: 交换机接受消息确认回调
+ * @author: rengb
+ * @time: 2021/9/1 18:49
+ */
+@Slf4j
+@Component
+public class ConfirmCallbackService implements RabbitTemplate.ConfirmCallback {
+
+    @Autowired
+    private RabbitTemplate rabbitTemplate;
+
+    @PostConstruct
+    public void init() {
+        rabbitTemplate.setConfirmCallback(this);
+    }
+
+    @Override
+    public void confirm(CorrelationData correlationData, boolean ack, String cause) {
+        if (!ack) {
+            log.info("发送者未收到确认,correlationData={} ,ack={}, cause={}", correlationData.getId(), ack, cause);
+        }
+    }
+
+}

+ 33 - 0
security-center/src/main/java/com/lantone/security/rabbitmq/ReturnCallbackService.java

@@ -0,0 +1,33 @@
+package com.lantone.security.rabbitmq;
+
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.amqp.core.Message;
+import org.springframework.amqp.rabbit.core.RabbitTemplate;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.PostConstruct;
+
+/**
+ * @Description: 队列接受消息确认回调
+ * @author: rengb
+ * @time: 2021/9/1 18:51
+ */
+@Slf4j
+@Component
+public class ReturnCallbackService implements RabbitTemplate.ReturnCallback {
+
+    @Autowired
+    private RabbitTemplate rabbitTemplate;
+
+    @PostConstruct
+    public void init() {
+        rabbitTemplate.setReturnCallback(this);
+    }
+
+    @Override
+    public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
+        log.info("returnedMessage ===> replyCode={} ,replyText={} ,exchange={} ,routingKey={}", replyCode, replyText, exchange, routingKey);
+    }
+
+}

+ 33 - 0
security-center/src/main/resources/bootstrap.yml

@@ -18,6 +18,39 @@ spring:
         max-wait: -1 # 连接池最大阻塞等待时间(使用负值表示没有限制)
         min-idle: 0 # 连接池中的最小空闲连接
     timeout: 20000 # 连接超时时间(毫秒)
+  rabbitmq:
+    host: localhost
+    port: 5672
+    username: rengb
+    password: 123456
+    virtual-host: /emrais # 虚拟host
+    publisher-confirm-type: correlated # 确认消息已发送到交换机(Exchange)  发送确认
+    publisher-returns: true # 确认消息已发送到队列(Queue)  发送回调
+    connection-timeout: 4000
+    template:
+      mandatory: true # 当exchange无法找到任何一个合适的queue时,将消息return给生产者
+      receive-timeout: 30000 # 消费端接收的响应时间
+      reply-timeout: 30000 # 发送端等待接收消费端给出return msg的时间,相当于在message同步时,发送并消费成功的时间
+      retry:
+        enabled: true
+        max-attempts: 6 # 最大重试次数
+        initial-interval: 2000 # 重试初始间隔时间
+        multiplier: 2 # 间隔时间乘子,间隔时间*乘子=下一次的间隔时间,最大不能超过设置的最大间隔时间
+        max-interval: 64000 # 最大重试时间间隔
+    listener:
+      simple:
+        acknowledge-mode: auto # 自动确认
+        auto-startup: true # 是否启动时自动启动容器
+        concurrency: 1 # 最小的消费者数量
+        max-concurrency: 5 # 最大的消费者数量
+        default-requeue-rejected: true # 投递失败时是否重新排队;默认是true(与参数acknowledge-mode有关系)
+        retry:
+          enabled: true
+          max-attempts: 6
+          initial-interval: 2000
+          multiplier: 2
+          max-interval: 64000
+          stateless: true # 不论重试是有状态的还是无状态的
 
 eureka:
   client: