【网关】Shenyu网关自动注册和同步元数据和URL,Shenyu-admin从nacos同步数据方案

Shenyu官网数据同步设计方案如下面图,同步方式支持 ZookeeperHttp 长轮询WebsocketNacosEtcd 和 Consul等。我们选择的时候,要小心配置参数,这里我以官网http和自实现的nacos为例。

 官网示例代码

 http方式注册

【网关】Shenyu网关自动注册和同步元数据和URL,Shenyu-admin从nacos同步数据方案_第1张图片

yml配置admin的账号信息

shenyu:
  register:
    registerType: http #zookeeper #etcd #nacos #consul
    serverLists: http://localhost:9095 #localhost:2181 #http://localhost:2379 #localhost:8848
    props:
      username: admin
      password: 123456

 nacos方式注册

【网关】Shenyu网关自动注册和同步元数据和URL,Shenyu-admin从nacos同步数据方案_第2张图片

*** nacos的地址和nacosNameSpace不能错,要和admin的一样。

shenyu:
  register:
    registerType: nacos #http #zookeeper #etcd #nacos #consul
    serverLists: localhost:8848 #http://localhost:9095 #localhost:2181 #http://localhost:2379 #localhost:8848
    props:
#      username: admin
#      password: 123456

#      username: nacos
#      password: nacos
      nacosNameSpace: ShenyuRegisterCenter

Shenyu-admin的配置

 注意命名空间nacosNameSpace要和注册服务时配置一样。

【网关】Shenyu网关自动注册和同步元数据和URL,Shenyu-admin从nacos同步数据方案_第3张图片

shenyu:
  register:
    registerType: nacos #http #zookeeper #etcd #nacos #consul
    #serverLists: #localhost:2181 #http://localhost:2379 #localhost:8848
    serverLists: localhost:8848 #http://localhost:2379 #localhost:8848
    props:
      sessionTimeout: 5000
      connectionTimeout: 2000
      checked: true
      zombieCheckTimes: 5
      scheduledTime: 10
      nacosNameSpace: ShenyuRegisterCenter

Shenyu-Admin数据同步到数据库方案

先从注册服务端看

看控制台日志:metadata client register success: {"appName":"http","contextPath":"/http","path":"/http/order/p*********.....

【网关】Shenyu网关自动注册和同步元数据和URL,Shenyu-admin从nacos同步数据方案_第4张图片

找到doRegister(metadata, Constants.META_PATH, Constants.META_TYPE)

【网关】Shenyu网关自动注册和同步元数据和URL,Shenyu-admin从nacos同步数据方案_第5张图片

 org.apache.shenyu.client.core.disruptor.subcriber.ShenyuClientMetadataExecutorSubscriber

    public void start(final ShenyuClientRegisterRepository shenyuClientRegisterRepository) {
        RegisterClientExecutorFactory factory = new RegisterClientExecutorFactory();
        factory.addSubscribers(new ShenyuClientMetadataExecutorSubscriber(shenyuClientRegisterRepository));
        factory.addSubscribers(new ShenyuClientURIExecutorSubscriber(shenyuClientRegisterRepository));
        factory.addSubscribers(new ShenyuClientApiDocExecutorSubscriber(shenyuClientRegisterRepository));
        providerManage = new DisruptorProviderManage<>(factory);
        providerManage.startup();
    }

在上一篇disruptor相关介绍

【并发编程】ShenyuAdmin里面数据同步用到的无锁环形队列LMAX Disruptor并发框架_wenchun001的博客-CSDN博客

从Shenyu-Admin端查看

一个和注册服务信息有关的日志,如下图,是NacosClientServerRegisterRepository打印出来的

NacosClientServerRegisterRepository

init方法

这里很清楚看到有配置信息了

@Override
    public void init(final ShenyuClientServerRegisterPublisher publisher,
                     final ShenyuRegisterCenterConfig config) {
        this.publisher = publisher;
        String serverAddr = config.getServerLists();
        Properties properties = config.getProps();
        Properties nacosProperties = new Properties();
        nacosProperties.put(PropertyKeyConst.SERVER_ADDR, serverAddr);
        nacosProperties.put(PropertyKeyConst.NAMESPACE, properties.getProperty("nacosNameSpace"));
        // the nacos authentication username
        nacosProperties.put(PropertyKeyConst.USERNAME, properties.getProperty(PropertyKeyConst.USERNAME, ""));
        // the nacos authentication password
        nacosProperties.put(PropertyKeyConst.PASSWORD, properties.getProperty(PropertyKeyConst.PASSWORD, ""));
        // access key for namespace
        nacosProperties.put(PropertyKeyConst.ACCESS_KEY, properties.getProperty(PropertyKeyConst.ACCESS_KEY, ""));
        // secret key for namespace
        nacosProperties.put(PropertyKeyConst.SECRET_KEY, properties.getProperty(PropertyKeyConst.SECRET_KEY, ""));

        try {
            this.configService = ConfigFactory.createConfigService(nacosProperties);
            this.namingService = NamingFactory.createNamingService(nacosProperties);
        } catch (NacosException e) {
            throw new ShenyuException(e);
        }

        subscribe();
    }

注册服务方法

    private void publishMetadata(final String data) {
        LOGGER.info("publish metadata: {}", data);
        publisher.publish(Lists.newArrayList(GsonUtils.getInstance().fromJson(data, MetaDataRegisterDTO.class)));
    }

自实现的注册服务

整个框架默认使用的是nacos作为服务注册中心,暂时未开源出来。

代理路由选择器

如下图scm的路由会自动注册上来。

【网关】Shenyu网关自动注册和同步元数据和URL,Shenyu-admin从nacos同步数据方案_第6张图片

 

你可能感兴趣的:(数据同步,软件研发,Java面试题,spring,后端,java)