diff --git a/nacos-cmdb-plugin-example/pom.xml b/nacos-cmdb-plugin-example/pom.xml index d954457e1..70fd18043 100644 --- a/nacos-cmdb-plugin-example/pom.xml +++ b/nacos-cmdb-plugin-example/pom.xml @@ -24,7 +24,6 @@ com.alibaba.nacos nacos-api - 0.7.0 junit diff --git a/nacos-selector-plugin-exmaple/pom.xml b/nacos-selector-plugin-exmaple/pom.xml new file mode 100644 index 000000000..af62256c4 --- /dev/null +++ b/nacos-selector-plugin-exmaple/pom.xml @@ -0,0 +1,89 @@ + + + + + + nacos-examples + com.alibaba.nacos + 0.2.0-SNAPSHOT + + 4.0.0 + + nacos-selector-plugin-exmaple + + nacos-selector-plugin-example + http://nacos.io + + + UTF-8 + 1.8 + 1.8 + + + + + com.alibaba.nacos + nacos-api + + + + junit + junit + 4.11 + test + + + + + + + org.apache.maven.plugins + maven-assembly-plugin + + + jar-with-dependencies + + + + + + + + maven-clean-plugin + 3.0.0 + + + + maven-resources-plugin + 3.0.2 + + + maven-compiler-plugin + 3.7.0 + + + maven-surefire-plugin + 2.20.1 + + + maven-jar-plugin + 3.0.2 + + + maven-install-plugin + 2.5.2 + + + maven-deploy-plugin + 2.8.2 + + + + + \ No newline at end of file diff --git a/nacos-selector-plugin-exmaple/src/main/java/com/alibaba/nacos/exmaple/selector/plugin/ExampleCmdbSelector.java b/nacos-selector-plugin-exmaple/src/main/java/com/alibaba/nacos/exmaple/selector/plugin/ExampleCmdbSelector.java new file mode 100644 index 000000000..46a82a956 --- /dev/null +++ b/nacos-selector-plugin-exmaple/src/main/java/com/alibaba/nacos/exmaple/selector/plugin/ExampleCmdbSelector.java @@ -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 { + + private List tags = new ArrayList<>(); + + public List getTags() { + return tags; + } + + public void setTags(List tags) { + this.tags = tags; + } + + @Override + protected List doSelect(CmdbContext 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"; + } +} diff --git a/nacos-selector-plugin-exmaple/src/main/java/com/alibaba/nacos/exmaple/selector/plugin/tag/TagContext.java b/nacos-selector-plugin-exmaple/src/main/java/com/alibaba/nacos/exmaple/selector/plugin/tag/TagContext.java new file mode 100644 index 000000000..0ad5cc10d --- /dev/null +++ b/nacos-selector-plugin-exmaple/src/main/java/com/alibaba/nacos/exmaple/selector/plugin/tag/TagContext.java @@ -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 tagInstances; + + public List getTagInstances() { + return tagInstances; + } + + public void setTagInstances(List 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; + } + } +} diff --git a/nacos-selector-plugin-exmaple/src/main/java/com/alibaba/nacos/exmaple/selector/plugin/tag/TagContextBuilder.java b/nacos-selector-plugin-exmaple/src/main/java/com/alibaba/nacos/exmaple/selector/plugin/tag/TagContextBuilder.java new file mode 100644 index 000000000..9ba295992 --- /dev/null +++ b/nacos-selector-plugin-exmaple/src/main/java/com/alibaba/nacos/exmaple/selector/plugin/tag/TagContextBuilder.java @@ -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> { + + @Override + public TagContext build(String consumer, List provider) { + TagContext tagContext = new TagContext(); + List 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"; + } +} diff --git a/nacos-selector-plugin-exmaple/src/main/java/com/alibaba/nacos/exmaple/selector/plugin/tag/TagSelector.java b/nacos-selector-plugin-exmaple/src/main/java/com/alibaba/nacos/exmaple/selector/plugin/tag/TagSelector.java new file mode 100644 index 000000000..d85ba60c4 --- /dev/null +++ b/nacos-selector-plugin-exmaple/src/main/java/com/alibaba/nacos/exmaple/selector/plugin/tag/TagSelector.java @@ -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, TagContext, String> { + + List tags = new ArrayList<>(); + + public List getTags() { + return tags; + } + + public void setTags(List tags) { + this.tags = tags; + } + + private String expression; + + public String getExpression() { + return expression; + } + + public void setExpression(String expression) { + this.expression = expression; + } + + @Override + public Selector, 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 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"; + } +} diff --git a/nacos-selector-plugin-exmaple/src/main/resources/META-INF/services/com.alibaba.nacos.api.selector.Selector b/nacos-selector-plugin-exmaple/src/main/resources/META-INF/services/com.alibaba.nacos.api.selector.Selector new file mode 100644 index 000000000..5f6147f18 --- /dev/null +++ b/nacos-selector-plugin-exmaple/src/main/resources/META-INF/services/com.alibaba.nacos.api.selector.Selector @@ -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 \ No newline at end of file diff --git a/nacos-selector-plugin-exmaple/src/main/resources/META-INF/services/com.alibaba.nacos.api.selector.context.SelectorContextBuilder b/nacos-selector-plugin-exmaple/src/main/resources/META-INF/services/com.alibaba.nacos.api.selector.context.SelectorContextBuilder new file mode 100644 index 000000000..06f95c748 --- /dev/null +++ b/nacos-selector-plugin-exmaple/src/main/resources/META-INF/services/com.alibaba.nacos.api.selector.context.SelectorContextBuilder @@ -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 \ No newline at end of file diff --git a/pom.xml b/pom.xml index 6e7176d5b..313ea6b85 100644 --- a/pom.xml +++ b/pom.xml @@ -15,5 +15,16 @@ nacos-sentinel-example nacos-cmdb-plugin-example nacos-health-plugin-example + nacos-selector-plugin-exmaple + + + + + com.alibaba.nacos + nacos-api + 2.0.4 + + +