-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add java sdk-examples nacos1xnaming #48
- Loading branch information
Showing
4 changed files
with
217 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
target/ | ||
out/ | ||
*.iml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
<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>com.my.nacos</groupId> | ||
<artifactId>naming1x</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
<packaging>jar</packaging> | ||
|
||
<name>naming</name> | ||
<url>http://maven.apache.org</url> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<!-- | ||
<nacos.client.version>1.4.4</nacos.client.version> | ||
<nacos.client.version>2.2.4</nacos.client.version> | ||
--> | ||
<nacos.client.version>1.3.3</nacos.client.version> | ||
|
||
</properties> | ||
|
||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.alibaba.nacos</groupId> | ||
<artifactId>nacos-client</artifactId> | ||
<version>${nacos.client.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.slf4j</groupId> | ||
<artifactId>slf4j-api</artifactId> | ||
<version>2.0.7</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>ch.qos.logback</groupId> | ||
<artifactId>logback-core</artifactId> | ||
<version>1.3.14</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>ch.qos.logback</groupId> | ||
<artifactId>logback-classic</artifactId> | ||
<version>1.3.14</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>3.8.1</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<!--maven-jar-plugin的作用是配置mainClass和指定classpath。--> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-jar-plugin</artifactId> | ||
<configuration> | ||
<archive> | ||
<manifest> | ||
<!--是否在manifest文件中添加classpath。默认为false。--> | ||
<addClasspath>true</addClasspath> | ||
<!--指定类路径前缀,也就是依赖的jar包所在的文件夹--> | ||
<classpathPrefix>lib/</classpathPrefix> | ||
<!--指定启动类--> | ||
<mainClass>com.my.nacos.naming1x.App</mainClass> | ||
</manifest> | ||
</archive> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-dependency-plugin</artifactId> | ||
<executions> | ||
<execution> | ||
<id>copy-dependencies</id> | ||
<phase>prepare-package</phase> | ||
<goals> | ||
<goal>copy-dependencies</goal> | ||
</goals> | ||
<configuration> | ||
<outputDirectory> | ||
${project.build.directory}/lib | ||
</outputDirectory> | ||
</configuration> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<configuration> | ||
<source>1.8</source> | ||
<target>1.8</target> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
97 changes: 97 additions & 0 deletions
97
sdk-examples/java/nacos1xnaming/src/main/java/com/my/nacos/naming1x/App.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package com.my.nacos.naming1x; | ||
|
||
import com.alibaba.nacos.api.NacosFactory; | ||
import com.alibaba.nacos.api.PropertyKeyConst; | ||
import com.alibaba.nacos.api.naming.NamingService; | ||
import com.alibaba.nacos.api.naming.listener.Event; | ||
import com.alibaba.nacos.api.naming.listener.EventListener; | ||
import com.alibaba.nacos.api.naming.listener.NamingEvent; | ||
import com.alibaba.nacos.api.naming.pojo.Instance; | ||
import com.alibaba.nacos.api.naming.pojo.ListView; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Properties; | ||
|
||
/** | ||
*/ | ||
public class App | ||
{ | ||
static Logger logger = LoggerFactory.getLogger(App.class); | ||
public static void main(String[] args) throws IOException | ||
{ | ||
logger.info("start naming demo"); | ||
logger.info("args: {}",args.length,args); | ||
String ip="192.168.1.2"; | ||
int port = 3000; | ||
if(args.length>0) { | ||
try { | ||
port = Integer.parseInt(args[0]); | ||
} | ||
catch (Exception ex){ | ||
//pass | ||
} | ||
|
||
} | ||
if(args.length>1){ | ||
ip = args[1]; | ||
} | ||
runNaming(ip,port); | ||
System.in.read(); | ||
} | ||
|
||
public static void runNaming(String ip,int port){ | ||
try{ | ||
String serverAddr = "127.0.0.1:8848"; | ||
Properties properties = new Properties(); | ||
properties.put("serverAddr", serverAddr); | ||
properties.put(PropertyKeyConst.USERNAME, "nacos"); | ||
properties.put(PropertyKeyConst.PASSWORD, "nacos"); | ||
final NamingService namingService= NacosFactory.createNamingService(properties); | ||
final String serviceName01 = "foo.api01"; | ||
final String serviceName02 = "foo.api02"; | ||
EventListener eventListener = new EventListener() { | ||
@Override | ||
public void onEvent(Event event) { | ||
if(event instanceof NamingEvent){ | ||
NamingEvent namingEvent = (NamingEvent)event; | ||
StringBuilder sb = new StringBuilder(); | ||
sb.append("subscribe event:\n\t") | ||
.append("serviceName:").append(namingEvent.getServiceName()) | ||
.append(",group:").append(namingEvent.getGroupName()) | ||
.append(",clusters:").append(namingEvent.getClusters()) | ||
.append(",instance size:").append(namingEvent.getInstances().size()); | ||
for(Instance instance:namingEvent.getInstances()){ | ||
sb.append("\n\t") | ||
.append(instance.getInstanceId()) | ||
.append(",").append(instance.getIp()) | ||
.append(":").append(instance.getPort()) | ||
.append(",").append(instance.getClusterName()); | ||
} | ||
logger.info(sb.toString()); | ||
} | ||
else{ | ||
logger.info("subscribe event class:"+event.getClass().getName()); | ||
} | ||
} | ||
}; | ||
|
||
namingService.subscribe(serviceName01,eventListener); | ||
namingService.subscribe(serviceName02,eventListener); | ||
Thread.sleep(1000); | ||
logger.info("registerInstance start"); | ||
namingService.registerInstance(serviceName01,ip,port); | ||
namingService.registerInstance(serviceName02,ip,port); | ||
Thread.sleep(1000); | ||
ListView<String> serviceList = namingService.getServicesOfServer(1,10000); | ||
logger.info("serviceList:"+serviceList); | ||
|
||
} | ||
catch (Exception ex){ | ||
logger.error("run error:",ex); | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
sdk-examples/java/nacos1xnaming/src/main/resources/logback.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<configuration> | ||
<property name = "ENCODER_PATTERN" value = "%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{80} - %msg%n"/> | ||
<!-- 控制台日志:输出全部日志到控制台 --> | ||
<appender name="console" class="ch.qos.logback.core.ConsoleAppender"> | ||
<!-- 控制输出流对象,默认System.out 改为System.err--> | ||
<!--<target>System.err</target>--> | ||
<!-- 日志消息格式配置--> | ||
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> | ||
<pattern>${ENCODER_PATTERN}</pattern> | ||
</encoder> | ||
</appender> | ||
<!-- root Logger 配置--> | ||
<root> | ||
<appender-ref ref="console"></appender-ref> | ||
</root> | ||
</configuration> |