• SpringCloud-Gateway路由动态配置Nacos实现


    1. 编写配置类
      @Component
      public class NacosConfig {
          public static String GROUP_ID ;
          public static String DATA_ID ;
          @Value("${nacos.gateway.route.config.group}")
          public void setGroupId(String groupId){
              GROUP_ID = groupId ;
          }
          @Value("${nacos.gateway.route.config.data-id}")
          public void setDataId(String dataId){
              DATA_ID = dataId ;
          }
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
    2. properties添加配置
      nacos.gateway.route.config.group=DEFAULT_GROUP
      nacos.gateway.route.config.data-id=hello-gateway-router.json
      
      • 1
      • 2
    3. 自定义RouteDefinitionLocator
      @Slf4j
      @Component
      public class NcosRouteDefinitionLocator implements RouteDefinitionLocator, InitializingBean {
          private volatile List<RouteDefinition> routeDefinitions = new CopyOnWriteArrayList<>() ;
          @Autowired
          private NacosConfigManager nacosConfigManager ;
          @Autowired
          private ObjectMapper objectMapper ;
          @Autowired
          private ApplicationEventPublisher eventPublisher ;
          private ConfigService configService ;
          @Override
          public Flux<RouteDefinition> getRouteDefinitions() {
              return Flux.fromIterable(routeDefinitions);
          }
          @Override
          public void afterPropertiesSet() throws Exception {
              // 从nacos获取routDefinition
              configService = nacosConfigManager.getConfigService();
              // 读取配置并装载
              this.initNacosConfig();
              // 添加listener,当数据变化时接收通知
              this.initNacosListener();
          }
          private void initNacosConfig(){
              try {
                  String content = configService.getConfig(
                          NacosConfig.DATA_ID,
                          NacosConfig.GROUP_ID,
                          3000L
                  );
                  CollectionType collectionType = objectMapper.getTypeFactory()
                          .constructCollectionType(ArrayList.class, RouteDefinition.class);
                  routeDefinitions = objectMapper.readValue(content, collectionType);
              }catch (NacosException e){
                  log.info("nacos config NacosException ", e);
              } catch (JsonProcessingException e) {
                  log.info("nacos config JsonProcessingException ", e);
              }
          }
          private void initNacosListener() throws NacosException {
              configService.addListener(
                      NacosConfig.DATA_ID,
                      NacosConfig.GROUP_ID,
                      new DataChangeListener());
          }
          class DataChangeListener implements Listener{
      
              @Override
              public Executor getExecutor() {
                  return null;
              }
              @Override
              public void receiveConfigInfo(String content) {
                  try {
                      log.info("gateway config change : {}", content);
                      CollectionType collectionType = objectMapper.getTypeFactory()
                              .constructCollectionType(ArrayList.class, RouteDefinition.class);
                      routeDefinitions = objectMapper.readValue(content, collectionType);
                      // 当数据发生变化后发布刷新事件,通知CachingRouteLocator需要重新加载route定义
                      eventPublisher.publishEvent(new RefreshRoutesEvent(content));
                  }catch (JsonProcessingException e) {
                      log.info("nacos config JsonProcessingException ", e);
                  }
              }
          }
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28
      • 29
      • 30
      • 31
      • 32
      • 33
      • 34
      • 35
      • 36
      • 37
      • 38
      • 39
      • 40
      • 41
      • 42
      • 43
      • 44
      • 45
      • 46
      • 47
      • 48
      • 49
      • 50
      • 51
      • 52
      • 53
      • 54
      • 55
      • 56
      • 57
      • 58
      • 59
      • 60
      • 61
      • 62
      • 63
      • 64
      • 65
      • 66
      • 67
    4. 编写GatewayDynamicConfiguration配置类
      @Configuration
      public class GatewayDynamicConfiguration {
          @Bean
          @ConditionalOnProperty(value = "spring.cloud.gateway.dynamic.config.enable", matchIfMissing = true)
          public NacosRouteDefinitionLocator routeDefinitionLocator(){
              return new NacosRouteDefinitionLocator() ;
          }
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
  • 相关阅读:
    EdgeNeXt轻量化学习笔记
    获取随机维基页面的Python模块实现
    Ble Mesh的Heatbeat
    idea快捷键
    【C++提高编程-02】----C++泛型编程之类模板实战
    Vue 中使用事件总线来进行组件间通信($emit()、$on() 和 $off())
    被CTO推荐的SQL总结
    iuv_5g组网问题表
    ESXI7.0.0升级到ESXI7.0.3
    STARK Low Degree Testing——FRI
  • 原文地址:https://blog.csdn.net/yichengjie_c/article/details/133364685