Shenyu官网数据同步设计方案如下面图,同步方式支持
Zookeeper
、Http 长轮询
、Websocket
、Nacos
、Etcd
和Consul等。我们选择的时候,要小心配置参数,这里我以官网http和自实现的nacos为例。
官网示例代码
http方式注册
yml配置admin的账号信息
shenyu:register:registerType: http #zookeeper #etcd #nacos #consulserverLists: http://localhost:9095 #localhost:2181 #http://localhost:2379 #localhost:8848props:username: adminpassword: 123456
nacos方式注册
*** nacos的地址和nacosNameSpace不能错,要和admin的一样。
shenyu:register:registerType: nacos #http #zookeeper #etcd #nacos #consulserverLists: localhost:8848 #http://localhost:9095 #localhost:2181 #http://localhost:2379 #localhost:8848props:
# username: admin
# password: 123456# username: nacos
# password: nacosnacosNameSpace: ShenyuRegisterCenter
Shenyu-admin的配置
注意命名空间nacosNameSpace要和注册服务时配置一样。
shenyu:register:registerType: nacos #http #zookeeper #etcd #nacos #consul#serverLists: #localhost:2181 #http://localhost:2379 #localhost:8848serverLists: localhost:8848 #http://localhost:2379 #localhost:8848props:sessionTimeout: 5000connectionTimeout: 2000checked: truezombieCheckTimes: 5scheduledTime: 10nacosNameSpace: ShenyuRegisterCenter
Shenyu-Admin数据同步到数据库方案
先从注册服务端看
看控制台日志:metadata client register success: {"appName":"http","contextPath":"/http","path":"/http/order/p*********.....
找到doRegister(metadata, Constants.META_PATH, Constants.META_TYPE)
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方法
这里很清楚看到有配置信息了
@Overridepublic 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 usernamenacosProperties.put(PropertyKeyConst.USERNAME, properties.getProperty(PropertyKeyConst.USERNAME, ""));// the nacos authentication passwordnacosProperties.put(PropertyKeyConst.PASSWORD, properties.getProperty(PropertyKeyConst.PASSWORD, ""));// access key for namespacenacosProperties.put(PropertyKeyConst.ACCESS_KEY, properties.getProperty(PropertyKeyConst.ACCESS_KEY, ""));// secret key for namespacenacosProperties.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的路由会自动注册上来。