----初始化项目
This commit is contained in:
39
plugin/trace/pom.xml
Normal file
39
plugin/trace/pom.xml
Normal file
@ -0,0 +1,39 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!--
|
||||
~ 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.
|
||||
-->
|
||||
|
||||
<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">
|
||||
<parent>
|
||||
<artifactId>nacos-plugin</artifactId>
|
||||
<groupId>com.alibaba.nacos</groupId>
|
||||
<version>${revision}</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>nacos-trace-plugin</artifactId>
|
||||
<name>nacos-trace-plugin ${project.version}</name>
|
||||
<url>https://nacos.io</url>
|
||||
<description>Nacos trace plugin pom.xml file</description>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.nacos</groupId>
|
||||
<artifactId>nacos-common</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@ -0,0 +1,59 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
package com.alibaba.nacos.plugin.trace;
|
||||
|
||||
import com.alibaba.nacos.common.spi.NacosServiceLoader;
|
||||
import com.alibaba.nacos.plugin.trace.spi.NacosTraceSubscriber;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* Nacos trace event subscriber manager.
|
||||
*
|
||||
* @author xiweng.yy
|
||||
*/
|
||||
public class NacosTracePluginManager {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(NacosTracePluginManager.class);
|
||||
|
||||
private static final NacosTracePluginManager INSTANCE = new NacosTracePluginManager();
|
||||
|
||||
private final Map<String, NacosTraceSubscriber> traceSubscribers;
|
||||
|
||||
private NacosTracePluginManager() {
|
||||
this.traceSubscribers = new ConcurrentHashMap<>();
|
||||
Collection<NacosTraceSubscriber> plugins = NacosServiceLoader.load(NacosTraceSubscriber.class);
|
||||
for (NacosTraceSubscriber each : plugins) {
|
||||
this.traceSubscribers.put(each.getName(), each);
|
||||
LOGGER.info("[TracePluginManager] Load NacosTraceSubscriber({}) name({}) successfully.", each.getClass(),
|
||||
each.getName());
|
||||
}
|
||||
}
|
||||
|
||||
public static NacosTracePluginManager getInstance() {
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
public Collection<NacosTraceSubscriber> getAllTraceSubscribers() {
|
||||
return new HashSet<>(traceSubscribers.values());
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,60 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
package com.alibaba.nacos.plugin.trace.spi;
|
||||
|
||||
import com.alibaba.nacos.common.trace.event.TraceEvent;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
/**
|
||||
* Nacos trace event subscriber.
|
||||
*
|
||||
* @author xiweng.yy
|
||||
*/
|
||||
public interface NacosTraceSubscriber {
|
||||
|
||||
/**
|
||||
* Get the plugin name, if the same name has loaded by nacos, the older one will be replaced by new one.
|
||||
*
|
||||
* @return plugin name
|
||||
*/
|
||||
String getName();
|
||||
|
||||
/**
|
||||
* Event callback.
|
||||
*
|
||||
* @param event {@link TraceEvent}
|
||||
*/
|
||||
void onEvent(TraceEvent event);
|
||||
|
||||
/**
|
||||
* Returns which trace events are this subscriber interested in.
|
||||
*
|
||||
* @return The interested event types.
|
||||
*/
|
||||
List<Class<? extends TraceEvent>> subscribeTypes();
|
||||
|
||||
/**
|
||||
* It is up to the listener to determine whether the callback is asynchronous or synchronous.
|
||||
*
|
||||
* @return {@link Executor}
|
||||
*/
|
||||
default Executor executor() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,47 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
package com.alibaba.nacos.plugin.trace;
|
||||
|
||||
import com.alibaba.nacos.plugin.trace.spi.NacosTraceSubscriber;
|
||||
import org.junit.Assert;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
|
||||
public class NacosTracePluginManagerTest {
|
||||
|
||||
@BeforeClass
|
||||
public static void setUp() {
|
||||
NacosTracePluginManager.getInstance();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAllTraceSubscribers() {
|
||||
assertFalse(NacosTracePluginManager.getInstance().getAllTraceSubscribers().isEmpty());
|
||||
assertContainsTestPlugin();
|
||||
}
|
||||
|
||||
private void assertContainsTestPlugin() {
|
||||
for (NacosTraceSubscriber each : NacosTracePluginManager.getInstance().getAllTraceSubscribers()) {
|
||||
if ("trace-plugin-mock".equals(each.getName())) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
Assert.fail("No found plugin named 'trace-plugin-mock'");
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,39 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
package com.alibaba.nacos.plugin.trace.mock;
|
||||
|
||||
import com.alibaba.nacos.common.trace.event.TraceEvent;
|
||||
import com.alibaba.nacos.plugin.trace.spi.NacosTraceSubscriber;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class MockNacosTraceSubscriber implements NacosTraceSubscriber {
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "trace-plugin-mock";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onEvent(TraceEvent event) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Class<? extends TraceEvent>> subscribeTypes() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@ -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
|
||||
#r
|
||||
# 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.plugin.trace.mock.MockNacosTraceSubscriber
|
||||
Reference in New Issue
Block a user