First, preparation

1. Learn nacOS

The nacOS website (nacos.io/) describes it as a dynamic service discovery, configuration management, and service management platform that makes it easier to build cloud-native applications. an easy-to-use dynamic service discovery, configuration and service management platform for building cloud native applications.

We can assume that NacOS is similar to Eureka in functionality, but more powerful in at least two respects: more access protocol support (at least it says cloud native); Through the console.

Here are three official features:

  • Dynamically configured service

Dynamic configuration services enable you to manage the configuration of all environments in a centralized, external, and dynamic manner. Dynamic configuration eliminates the need to redeploy applications and services when configuration changes. Centralized configuration management makes it easier to implement stateless services and to extend services flexibly on demand.

  • Service discovery and management

Service discovery and managing dynamic service discovery is key to a service-oriented approach to application architecture, such as microservices and cloud native. Nacos supports dnS-BASED and RPC-based (Dubbo, gRPC) service discovery modes. Nacos also provides real-time health checks to prevent requests from being sent to unhealthy hosts or service instances. With Nacos, you can more easily implement circuit breakers for your services.

  • Dynamic DNS Service

By supporting weighted routing, the dynamic DNS service enables you to easily implement middle-layer load balancing, more flexible routing policies, traffic control, and simple DNS resolution services on the Intranet of simple data centers. Dynamic DNS services also make it easier to implement DNS protocol-based service discovery to eliminate the risk of coupling to vendor proprietary service discovery apis.

Key words: centralization, microservices and cloud native, real-time health check

2. Start NACOS

1) Download nacOS and start itsh startup.sh -m standalone

Using lsof -I :8848 and jps-lVM, you find that nacOS listens on port 8848 by default

2) startsoul-adminsoul-bootstrap

We’ve learned a lot about data synchronization today, so the soul-Bootstrap POM file will include the starter configuration for NACOS

<! --soul data sync start use nacos--> <dependency> <groupId>org.dromara</groupId> <artifactId>soul-spring-boot-starter-sync-data-nacos</artifactId> <version>${project.version}</version> </dependency>Copy the code

Use NACOS to synchronize data to the gateway

1, fromsoul-bootstrapDependant on soul-spring-boot-starter-sync-data-nacos

See what beans are initialized and what does the bean do?

public class NacosSyncDataService extends NacosCacheHandler implements AutoCloseable.SyncDataService {

    /**
     * Instantiates a new Nacos sync data service.
     *
     * @param configService         the config service
     * @param pluginDataSubscriber the plugin data subscriber
     * @param metaDataSubscribers   the meta data subscribers
     * @param authDataSubscribers   the auth data subscribers
     */
    public NacosSyncDataService(final ConfigService configService, final PluginDataSubscriber pluginDataSubscriber,
                                final List<MetaDataSubscriber> metaDataSubscribers, final List<AuthDataSubscriber> authDataSubscribers) {

        super(configService, pluginDataSubscriber, metaDataSubscribers, authDataSubscribers);
        start();
    }

    /** * Start. */
    public void start(a) {
        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);
    }

    @Override
    public void close(a) { LISTENERS.forEach((dataId, lss) -> { lss.forEach(listener -> getConfigService().removeListener(dataId, GROUP, listener)); lss.clear(); }); LISTENERS.clear(); }}Copy the code

The general guess is that the constant configuration is listened at startup time, to update the gateway service local cache.

2, from the console event listener method

This few days have been the console class DataChangedEventDispatcher data synchronization has been on the event listeners.

This is the server nacOS configuration

    /** * The type Nacos listener. */
    @Configuration
    @ConditionalOnProperty(prefix = "soul.sync.nacos", name = "url")
    @Import(NacosConfiguration.class)
    static class NacosListener {

        /**
         * Data changed listener data changed listener.
         *
         * @param configService the config service
         * @return the data changed listener
         */
        @Bean
        @ConditionalOnMissingBean(NacosDataChangedListener.class)
        public DataChangedListener nacosDataChangedListener(final ConfigService configService) {
            return newNacosDataChangedListener(configService); }}Copy the code

Let’s take a random permission update event and look at the NacosDataChangedListener#onAppAuthChanged method

    @Override
    @SneakyThrows
    public void onAppAuthChanged(final List<AppAuthData> changed, final DataEventTypeEnum eventType) {
        updateAuthMap(getConfig(AUTH_DATA_ID));
        switch (eventType) {
            case DELETE:
                changed.forEach(appAuth -> AUTH_MAP.remove(appAuth.getAppKey()));
                break;
            case REFRESH:
            case MYSELF:
                Set<String> set = new HashSet<>(AUTH_MAP.keySet());
                changed.forEach(appAuth -> {
                    set.remove(appAuth.getAppKey());
                    AUTH_MAP.put(appAuth.getAppKey(), appAuth);
                });
                AUTH_MAP.keySet().removeAll(set);
                break;
            default:
                changed.forEach(appAuth -> AUTH_MAP.put(appAuth.getAppKey(), appAuth));
                break;
        }
        publishConfig(AUTH_DATA_ID, AUTH_MAP);
    }
Copy the code

Seeing no database involved, the event data is stored in Map and sent to the registered gateway service via NACAS.