Enhance nacos example (#11761)

* example stable.

* example stable.

* Update copyright
This commit is contained in:
杨翊 SionYang 2024-02-22 14:58:57 +08:00 committed by GitHub
parent 1710fa32a2
commit fde996d2a6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 63 additions and 7 deletions

View File

@ -55,25 +55,23 @@ public class NamingExample {
NamingService naming = NamingFactory.createNamingService(properties);
naming.registerInstance(INSTANCE_SERVICE_NAME, INSTANCE_IP, INSTANCE_PORT, INSTANCE_CLUSTER_NAME);
System.out.println("[instances after register] " + naming.getAllInstances(INSTANCE_SERVICE_NAME));
Executor executor = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(),
runnable -> {
Thread thread = new Thread(runnable);
thread.setName("test-thread");
return thread;
});
naming.subscribe(INSTANCE_SERVICE_NAME, new AbstractEventListener() {
//EventListener onEvent is sync to handle, If process too low in onEvent, maybe block other onEvent callback.
//So you can override getExecutor() to async handle event.
@Override
public Executor getExecutor() {
return executor;
}
@Override
public void onEvent(Event event) {
System.out.println("[serviceName] " + ((NamingEvent) event).getServiceName());
@ -81,8 +79,14 @@ public class NamingExample {
}
});
naming.deregisterInstance(INSTANCE_SERVICE_NAME, INSTANCE_IP, INSTANCE_PORT, INSTANCE_CLUSTER_NAME);
Thread.sleep(1000);
System.out.println("[instances after register] " + naming.getAllInstances(INSTANCE_SERVICE_NAME));
Thread.sleep(1000);
naming.deregisterInstance(INSTANCE_SERVICE_NAME, INSTANCE_IP, INSTANCE_PORT, INSTANCE_CLUSTER_NAME);
Thread.sleep(1000);
System.out.println("[instances after deregister] " + naming.getAllInstances(INSTANCE_SERVICE_NAME));

View File

@ -0,0 +1,52 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 1999-2023 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.
-->
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"/>
<appender name="PREF_LOG_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>/Users/xiweng.yy/logs/nacos/perf.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>/Users/xiweng.yy/logs/nacos/perf.log.%i</fileNamePattern>
<maxIndex>${JM.LOG.RETAIN.COUNT:-7}</maxIndex>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>${JM.LOG.FILE.SIZE:-10MB}</maxFileSize>
</triggeringPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %p [%-5t:%c{2}] %m%n</pattern>
</encoder>
</appender>
<logger name="org.apache" level="DEBUG" />
<logger name="org.apache.http.wire" level="DEBUG" />
<logger name="org.apache.http.headers" level="INFO" />
<logger name="io.grpc.netty" level="INFO">
<appender-ref ref="STDOUT"/>
</logger>
<logger name="komachi.sion.nacos" level="INFO">
<appender-ref ref="PREF_LOG_FILE"/>
</logger>
<root level="INFO">
<appender-ref ref="STDOUT"/>
</root>
</configuration>