提交 51dddbfd 编写于 作者: H huangjian888 提交者: Liang Zhang

Registry center for Nacos (#3150)

* check codes by checkstyle
上级 0656fea7
......@@ -30,5 +30,6 @@
<modules>
<module>sharding-orchestration-reg-api</module>
<module>sharding-orchestration-reg-zookeeper-curator</module>
<module>sharding-orchestration-reg-nacos</module>
</modules>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Licensed to the Apache Software Foundation (ASF) under one or more
~ contributor license agreements. See the NOTICE file distributed with
~ this work for additional information regarding copyright ownership.
~ The ASF licenses this file to You under the Apache License, Version 2.0
~ (the "License"); you may not use this file except in compliance with
~ the License. You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-orchestration-reg</artifactId>
<version>4.0.0-RC3-SNAPSHOT</version>
</parent>
<artifactId>sharding-orchestration-reg-nacos</artifactId>
<name>${project.artifactId}</name>
<properties>
<nacos-client.verison>1.0.0</nacos-client.verison>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-orchestration-reg-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>${nacos-client.verison}</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.shardingsphere.orchestration.reg.nacos;
import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.Listener;
import com.alibaba.nacos.api.exception.NacosException;
import com.google.common.base.Strings;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.orchestration.reg.api.RegistryCenter;
import org.apache.shardingsphere.orchestration.reg.api.RegistryCenterConfiguration;
import org.apache.shardingsphere.orchestration.reg.listener.DataChangedEvent;
import org.apache.shardingsphere.orchestration.reg.listener.DataChangedEventListener;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.Executor;
@Slf4j
public class NacosRegistryCenter implements RegistryCenter {
private ConfigService configService;
@Getter
@Setter
private Properties properties = new Properties();
@Override
public void init(final RegistryCenterConfiguration config) {
try {
configService = NacosFactory.createConfigService(config.getServerLists());
} catch (NacosException e) {
log.debug("exception for: {}", e.toString());
}
}
@Override
public String get(final String key) {
return getDirectly(key);
}
@Override
public String getDirectly(final String key) {
try {
String dataId = key.replace("/", ".");
String group = properties.getProperty("group", "SHARDING_SPHERE_DEFAULT_GROUP");
long timeoutMs = Long.parseLong(properties.getProperty("timeout", "3000"));
return configService.getConfig(dataId, group, timeoutMs);
} catch (NacosException e) {
log.debug("exception for: {}", e.toString());
return null;
}
}
@Override
public boolean isExisted(final String key) {
return !Strings.isNullOrEmpty(getDirectly(key));
}
@Override
public List<String> getChildrenKeys(final String key) {
return null;
}
@Override
public void persist(final String key, final String value) {
update(key, value);
}
@Override
public void update(final String key, final String value) {
try {
String dataId = key.replace("/", ".");
String group = properties.getProperty("group", "SHARDING_SPHERE_DEFAULT_GROUP");
configService.publishConfig(dataId, group, value);
} catch (NacosException e) {
log.debug("exception for: {}", e.toString());
}
}
@Override
public void persistEphemeral(final String key, final String value) {
}
@Override
public void watch(final String key, final DataChangedEventListener dataChangedEventListener) {
try {
String dataId = key.replace("/", ".");
String group = properties.getProperty("group", "SHARDING_SPHERE_DEFAULT_GROUP");
configService.addListener(dataId, group, new Listener() {
@Override
public Executor getExecutor() {
return null;
}
@Override
public void receiveConfigInfo(final String configInfo) {
dataChangedEventListener.onChange(new DataChangedEvent(key, configInfo, DataChangedEvent.ChangedType.UPDATED));
}
});
} catch (NacosException e) {
log.debug("exception for: {}", e.toString());
}
}
@Override
public void close() {
}
@Override
public void initLock(final String key) {
}
@Override
public boolean tryLock() {
return false;
}
@Override
public void tryRelease() {
}
@Override
public String getType() {
return "nacos";
}
}
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
org.apache.shardingsphere.orchestration.reg.nacos.NacosRegistryCenter
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.shardingsphere.orchestration.reg.nacos;
import org.apache.shardingsphere.orchestration.reg.api.RegistryCenter;
import org.apache.shardingsphere.orchestration.reg.api.RegistryCenterConfiguration;
import org.junit.BeforeClass;
import org.junit.Test;
import java.util.Properties;
public class NacosRegistryCenterTest {
private static RegistryCenter nacosRegistryCenter = new NacosRegistryCenter();
@BeforeClass
public static void init() {
Properties properties = new Properties();
properties.setProperty("group", "SHARDING_SPHERE_DEFAULT_GROUP");
properties.setProperty("timeout", "3000");
RegistryCenterConfiguration configuration = new RegistryCenterConfiguration(nacosRegistryCenter.getType(), properties);
configuration.setServerLists("127.0.0.1:8848");
nacosRegistryCenter.init(configuration);
}
@Test
public void assertPersist() {
nacosRegistryCenter.persist("sharding/test", "value1");
}
@Test
public void assertUpdate() {
nacosRegistryCenter.update("sharding/test", "value2");
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册