Shenyu官网数据同步设计方案如下面图,同步方式支持
Zookeeper
、Http 长轮询
、Websocket
、Nacos
、Etcd
和Consul等。我们选择的时候,要小心配置参数,这里我以官网http和自实现的nacos为例。
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的地址和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
注意命名空间nacosNameSpace要和注册服务时配置一样。
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
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博客
一个和注册服务信息有关的日志,如下图,是NacosClientServerRegisterRepository打印出来的
这里很清楚看到有配置信息了
@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的路由会自动注册上来。