first commit

This commit is contained in:
realJackSun 2022-10-26 11:56:58 +08:00
commit d04ea8feff
5 changed files with 245 additions and 0 deletions

1
README.md Normal file
View File

@ -0,0 +1 @@
# apollo-2-nacos-migration

107
pom.xml Normal file
View File

@ -0,0 +1,107 @@
<?xml version="1.0" encoding="UTF-8"?>
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>apollo-2-nacos</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<!-- <configuration>-->
<!-- <archive>-->
<!-- <manifest>-->
<!-- <addClasspath>true</addClasspath>-->
<!-- <mainClass>com.kuae.NacosDemo</mainClass>-->
<!-- <includeSystemScope>true</includeSystemScope>-->
<!-- </manifest>-->
<!-- </archive>-->
<!-- </configuration>-->
</plugin>
<!-- Maven Assembly Plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<!-- get all project dependencies -->
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<!-- MainClass in mainfest make a executable jar -->
<archive>
<manifest>
<mainClass>com.kuae.NacosDemo</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<!-- bind to the packaging phase -->
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.alibaba.edas.acm</groupId>
<artifactId>acm-sdk</artifactId>
<version>1.0.9</version>
</dependency>
<!-- 有日志实现,下面可去掉 -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client-mse-extension</artifactId>
<version>1.4.2-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
<version>3.8.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11-beta-1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,87 @@
package com.kuae;
import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Properties;
// 该类为Apollo 迁移Nacks的脚本
public class Transfer {
static final Properties propertiesForMse;
static final String keyId = "alias/acs/mse";
static final String regionId = "cn-hangzhou";
// 阿里云账号的AK/SK
static final String ak = "{ak}";
static final String sk = "{sk}";
static {
propertiesForMse = new Properties();
// 给访问MSE的授予基础信息
propertiesForMse.put("keyId", keyId);
propertiesForMse.put("regionId", regionId);
// 给访问MSE的Properties授予AK/SK
propertiesForMse.put("accessKey", ak);
propertiesForMse.put("secretKey", sk);
}
public static void main(String[] args) throws IOException, NacosException {
// 期望发布配置的目标MSE地址
String mseServerAddr = "{mse-clusterId}-p.nacos-ans.mse.aliyuncs.com";
// 传入NamespaceId如果希望发布到public则将其namespaceId置为""
String namespaceId = "test-to";
// 导出配置之后的根目录
String rootPath = "/Users/sunli/lab/configs/ns1";
propertiesForMse.put("serverAddr", mseServerAddr);
propertiesForMse.put("namespace", namespaceId);
String[] groups = getFileNames(rootPath);
if (groups == null) {
return;
}
for (String group : groups) {
String groupPath = rootPath + "/" + group;
String[] dataIds = getFileNames(groupPath);
if (dataIds == null) {
continue;
}
for (String dataId : dataIds) {
String dataIdPath = groupPath + "/" + dataId;
System.out.println("Reading " + dataIdPath);
byte[] bytes = Files.readAllBytes(Paths.get(dataIdPath));
String content = new String(bytes, StandardCharsets.UTF_8);
publishMSE(dataId, group, content);
}
}
}
public static String[] getFileNames(String path) {
File dir = new File(path);
String[] children = dir.list();
if (children == null) {
return null;
}
else {
for (int i=0; i< children.length; i++) {
String filename = children[i];
System.out.println(filename);
}
}
return children;
}
public static boolean publishMSE(String dataId, String group, String content) throws NacosException {
ConfigService configService = NacosFactory.createConfigService(propertiesForMse);
boolean result = configService.publishConfig(dataId, group, content);
System.out.println("dataId: " + dataId + " group: " + group + "transfered");
return result;
}
}

View File

@ -0,0 +1,17 @@
#
# Copyright 1999-2021 Alibaba Group Holding Ltd.
#
# Licensed 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.
#
com.alibaba.nacos.client.aliyun.AliyunConfigFilter

View File

@ -0,0 +1,33 @@
package com.kuae;
import com.alibaba.nacos.common.utils.StringUtils;
import junit.framework.TestCase;
import org.junit.Assert;
import org.junit.Test;
public class NacosConfigDemoTest extends TestCase {
public String getServerId(String serverIdWithNs) {
return serverIdWithNs.split("_")[0];
}
private String getNamespace(String serverIdWithNs) {
if (!serverIdWithNs.contains("_")) {
return StringUtils.EMPTY;
} else {
return serverIdWithNs.split("_")[1];
}
}
@Test
public void testGetServerId() {
String serverId = getServerId("cn-hangzhou_ns");
Assert.assertEquals("cn-hangzhou", serverId);
}
@Test
public void testGetNamespace() {
String namespace = getNamespace("cn-hangzhou_ns");
Assert.assertEquals("ns", namespace);
}
}