Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the example of Selector in Nacos 2.0.4 #69

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion nacos-cmdb-plugin-example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-api</artifactId>
<version>0.7.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
Expand Down
89 changes: 89 additions & 0 deletions nacos-selector-plugin-exmaple/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ pom.xml
~ Copyright 2021 Qunhe Tech, all rights reserved.
~ Qunhe PROPRIETARY/CONFIDENTIAL, any form of usage is subject to approval.
-->

<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-examples</artifactId>
<groupId>com.alibaba.nacos</groupId>
<version>0.2.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>nacos-selector-plugin-exmaple</artifactId>

<name>nacos-selector-plugin-example</name>
<url>http://nacos.io</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-api</artifactId>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright 1999-2018 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.exmaple.selector.plugin;

import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.alibaba.nacos.api.selector.AbstractCmdbSelector;
import com.alibaba.nacos.api.selector.context.CmdbContext;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
* Example use of selector.
*
* @author chenglu
* @date 2021-08-09 19:33
*/
public class ExampleCmdbSelector extends AbstractCmdbSelector<Instance> {

private List<String> tags = new ArrayList<>();

public List<String> getTags() {
return tags;
}

public void setTags(List<String> tags) {
this.tags = tags;
}

@Override
protected List<Instance> doSelect(CmdbContext<Instance> context) {
return context.getProviders().stream()
.filter(ci -> tags.contains(ci.getEntity().getName()))
.map(CmdbContext.CmdbInstance::getInstance)
.collect(Collectors.toList());
}

@Override
protected void doParse(String expression) throws NacosException {
tags.addAll(Arrays.asList(expression.split(",")));
}

@Override
public String getType() {
return "example";
}
}
Original file line number Diff line number Diff line change
@@ -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.exmaple.selector.plugin.tag;

import com.alibaba.nacos.api.naming.pojo.Instance;

import java.util.List;

/**
* @author chenglu
* @date 2021-10-09 18:05
*/
public class TagContext {

private List<TagInstance> tagInstances;

public List<TagInstance> getTagInstances() {
return tagInstances;
}

public void setTagInstances(List<TagInstance> tagInstances) {
this.tagInstances = tagInstances;
}

static class TagInstance {
private Instance instance;

private String tag;

public Instance getInstance() {
return instance;
}

public void setInstance(Instance instance) {
this.instance = instance;
}

public String getTag() {
return tag;
}

public void setTag(String tag) {
this.tag = tag;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* 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.exmaple.selector.plugin.tag;

import com.alibaba.nacos.api.naming.pojo.Instance;
import com.alibaba.nacos.api.selector.context.SelectorContextBuilder;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author chenglu
* @date 2021-10-09 18:08
*/
public class TagContextBuilder implements SelectorContextBuilder<TagContext, String, List<Instance>> {

@Override
public TagContext build(String consumer, List<Instance> provider) {
TagContext tagContext = new TagContext();
List<TagContext.TagInstance> tagInstances = provider.stream()
.map(i -> {
TagContext.TagInstance tagInstance = new TagContext.TagInstance();
tagInstance.setInstance(i);
if (i.getIp().equals("127.0.0.1")) {
tagInstance.setTag("A");
} else {
tagInstance.setTag("B");
}
return tagInstance;
})
.collect(Collectors.toList());
tagContext.setTagInstances(tagInstances);
return tagContext;
}

@Override
public String getContextType() {
return "TAG";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* 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.exmaple.selector.plugin.tag;

import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.alibaba.nacos.api.selector.Selector;
import com.alibaba.nacos.api.utils.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author chenglu
* @date 2021-10-09 18:05
*/
public class TagSelector implements Selector<List<Instance>, TagContext, String> {

List<String> tags = new ArrayList<>();

public List<String> getTags() {
return tags;
}

public void setTags(List<String> tags) {
this.tags = tags;
}

private String expression;

public String getExpression() {
return expression;
}

public void setExpression(String expression) {
this.expression = expression;
}

@Override
public Selector<List<Instance>, TagContext, String> parse(String expression) throws NacosException {
if (StringUtils.isBlank(expression)) {
return this;
}
this.expression = expression;
tags.addAll(Arrays.asList(expression.split(",")));
return this;
}

@Override
public List<Instance> select(TagContext context) {
return context.getTagInstances().stream()
.filter(tagInstance -> tags.contains(tagInstance.getTag()))
.map(TagContext.TagInstance::getInstance)
.collect(Collectors.toList());
}

@Override
public String getType() {
return "tag";
}

@Override
public String getContextType() {
return "TAG";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# 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.exmaple.selector.plugin.ExampleCmdbSelector
com.alibaba.nacos.exmaple.selector.plugin.tag.TagSelector
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# 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.exmaple.selector.plugin.tag.TagContextBuilder
Loading