b9306894128c2e283aa717975c2c976fa57f43c3.svn-base 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package org.jeecg.modules.system.service.impl;
  2. import cn.hutool.core.util.ObjectUtil;
  3. import com.alibaba.fastjson.JSON;
  4. import com.alibaba.fastjson.JSONObject;
  5. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  6. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  7. import lombok.extern.slf4j.Slf4j;
  8. import org.jeecg.common.base.BaseMap;
  9. import org.jeecg.common.constant.CacheConstant;
  10. import org.jeecg.common.constant.GlobalConstants;
  11. import org.jeecg.modules.system.entity.SysGatewayRoute;
  12. import org.jeecg.modules.system.mapper.SysGatewayRouteMapper;
  13. import org.jeecg.modules.system.service.ISysGatewayRouteService;
  14. import org.springframework.beans.factory.annotation.Autowired;
  15. import org.springframework.data.redis.core.RedisTemplate;
  16. import org.springframework.stereotype.Service;
  17. import org.springframework.transaction.annotation.Transactional;
  18. import java.util.List;
  19. /**
  20. * @Description: gateway路由管理
  21. * @Author: jeecg-boot
  22. * @Date: 2020-05-26
  23. * @Version: V1.0
  24. */
  25. @Service
  26. @Slf4j
  27. public class SysGatewayRouteServiceImpl extends ServiceImpl<SysGatewayRouteMapper, SysGatewayRoute> implements ISysGatewayRouteService {
  28. @Autowired
  29. private RedisTemplate<String, Object> redisTemplate;
  30. @Override
  31. public void addRoute2Redis(String key) {
  32. List<SysGatewayRoute> ls = this.list(new LambdaQueryWrapper<SysGatewayRoute>());
  33. redisTemplate.opsForValue().set(key, JSON.toJSONString(ls));
  34. }
  35. @Override
  36. public void deleteById(String id) {
  37. this.removeById(id);
  38. this.resreshRouter();
  39. }
  40. @Override
  41. @Transactional(rollbackFor = Exception.class)
  42. public void updateAll(JSONObject json) {
  43. log.info("--gateway 路由配置修改--");
  44. try {
  45. json = json.getJSONObject("router");
  46. String id = json.getString("id");
  47. SysGatewayRoute route = getById(id);
  48. if (ObjectUtil.isEmpty(route)) {
  49. route = new SysGatewayRoute();
  50. }
  51. route.setRouterId(json.getString("routerId"));
  52. route.setName(json.getString("name"));
  53. route.setPredicates(json.getString("predicates"));
  54. String filters = json.getString("filters");
  55. if (ObjectUtil.isEmpty(filters)) {
  56. filters = "[]";
  57. }
  58. route.setFilters(filters);
  59. route.setUri(json.getString("uri"));
  60. if (json.get("status") == null) {
  61. route.setStatus(1);
  62. } else {
  63. route.setStatus(json.getInteger("status"));
  64. }
  65. this.saveOrUpdate(route);
  66. resreshRouter();
  67. } catch (Exception e) {
  68. log.error("路由配置解析失败", e);
  69. resreshRouter();
  70. e.printStackTrace();
  71. }
  72. }
  73. /**
  74. * 更新redis路由缓存
  75. */
  76. private void resreshRouter() {
  77. //更新redis路由缓存
  78. addRoute2Redis(CacheConstant.GATEWAY_ROUTES);
  79. BaseMap params = new BaseMap();
  80. params.put(GlobalConstants.HANDLER_NAME, "loderRouderHandler");
  81. //刷新网关
  82. redisTemplate.convertAndSend(GlobalConstants.REDIS_TOPIC_NAME, params);
  83. }
  84. @Override
  85. public void clearRedis() {
  86. redisTemplate.opsForValue().set(CacheConstant.GATEWAY_ROUTES, null);
  87. }
  88. }