*    Copyright (c) 2018-2025, lengleng All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng ([email protected])

package com.github.pig.admin.common.listener;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.github.pig.admin.service.SysZuulRouteService;
import com.github.pig.common.constant.CommonConstant;
import com.github.pig.common.entity.SysZuulRoute;
import com.xiaoleilu.hutool.collection.CollUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.embedded.EmbeddedServletContainerInitializedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

 * @author lengleng
 * @date 2018/5/16
public class RouteConfigInitListener{
    private RedisTemplate redisTemplate;
    private SysZuulRouteService sysZuulRouteService;

     * Callback used to run the bean.
     * 初始化路由配置的数据,避免gateway 依赖业务模块
    @EventListener(value = {EmbeddedServletContainerInitializedEvent.class})
    public void init() {
        EntityWrapper wrapper = new EntityWrapper();
        wrapper.eq(CommonConstant.DEL_FLAG, CommonConstant.STATUS_NORMAL);
        List<SysZuulRoute> routeList = sysZuulRouteService.selectList(wrapper);
        if (CollUtil.isNotEmpty(routeList)) {
            redisTemplate.opsForValue().set(CommonConstant.ROUTE_KEY, routeList);
            log.info("更新Redis中路由配置数据:{}条", routeList.size());