Soul源码学习(九) - Nacos数据同步

本文将介绍一下Soul网关中Nacos的数据同步使用;

启动步骤

  1. 修改soul-admin中的配置;
  2. 修改soul-bootstrap中的配置;
  3. 启动本地Nacos;
  4. 启动soul-admin和soul-bootstrap工厂;
  • soul-admin中的配置
soul:
  database:
    dialect: mysql
    init_script: "META-INF/schema.sql"
  sync:
#    websocket:
#      enabled: true
#    zookeeper:
#      url: localhost:2181
#      sessionTimeout: 5000
#      connectionTimeout: 2000
#    http:
#      enabled: true
      nacos:
        url: localhost:8848
        namespace: 1c10d748-af86-43b9-8265-75f487d20c6c
        acm:
          enabled: false
          endpoint: acm.aliyun.com
          namespace:
          accessKey:
          secretKey:
  • soul-bootstrap中的配置
soul :
    file:
      enabled: true
    corss:
      enabled: true
    dubbo :
      parameter: multi
    sync:
#        websocket :
#             urls: ws://localhost:9095/websocket

#    zookeeper:
#      url: localhost:2181
#      sessionTimeout: 5000
#      connectionTimeout: 2000
#      http:
#        url : http://localhost:9095
        nacos:
              url: localhost:8848
              namespace: 1c10d748-af86-43b9-8265-75f487d20c6c
              acm:
                enabled: false
                endpoint: acm.aliyun.com
                namespace:
                accessKey:
                secretKey:

同步步骤

  1. 启动bootstrap以后会直接执行NacosSyncDataConfigurationnacosSyncDataService, nacosConfigService, nacosConfig三个bean注入到容器中;
  2. nacosSyncDataService在启动后会自动创建一个监听器监听nacos;
    NacosSyncDataService.java
    /**
     * Start.
     */
    public void start() {
           
        watcherData(PLUGIN_DATA_ID, this::updatePluginMap);
        watcherData(SELECTOR_DATA_ID, this::updateSelectorMap);
        watcherData(RULE_DATA_ID, this::updateRuleMap);
        watcherData(META_DATA_ID, this::updateMetaDataMap);
        watcherData(AUTH_DATA_ID, this::updateAuthMap);
    }
    
    NacosCacheHandler.java
    protected void watcherData(final String dataId, final OnChange oc) {
           
        Listener listener = new Listener() {
           
            @Override
            public void receiveConfigInfo(final String configInfo) {
           
                oc.change(configInfo);
            }
    
            @Override
            public Executor getExecutor() {
           
                return null;
            }
        };
        oc.change(getConfigAndSignListener(dataId, listener));
        LISTENERS.getOrDefault(dataId, new ArrayList<>()).add(listener);
    }
    
  3. 用户在Soul网关控制台的操作,都会出发DataChangedEventDispatcher中的onApplicationEvent里的监听事件;
    ...
    @Override
    @SuppressWarnings("unchecked")
    public void onApplicationEvent(final DataChangedEvent event) {
           
        for (DataChangedListener listener : listeners) {
           
            switch (event.getGroupKey()) {
           
                case APP_AUTH:
                    listener.onAppAuthChanged((List<AppAuthData>) event.getSource(), event.getEventType());
                    break;
                case PLUGIN:
                    listener.onPluginChanged((List<PluginData>) event.getSource(), event.getEventType());
                    break;
                case RULE:
                    listener.onRuleChanged((List<RuleData>) event.getSource(), event.getEventType());
                    break;
                case SELECTOR:
                    listener.onSelectorChanged((List<SelectorData>) event.getSource(), event.getEventType());
                    break;
                case META_DATA:
                    listener.onMetaDataChanged((List<MetaData>) event.getSource(), event.getEventType());
                    break;
                default:
                    throw new IllegalStateException("Unexpected value: " + event.getGroupKey());
            }
        }
    }
    ...
    
  4. onApplicationEvent将同时出发NacosDataChangedListener中的方法进行数据同步,加载数据到JVM缓存中;

你可能感兴趣的:(Soul网关,java)