最新要闻

广告

手机

iphone11大小尺寸是多少?苹果iPhone11和iPhone13的区别是什么?

iphone11大小尺寸是多少?苹果iPhone11和iPhone13的区别是什么?

警方通报辅警执法直播中被撞飞:犯罪嫌疑人已投案

警方通报辅警执法直播中被撞飞:犯罪嫌疑人已投案

家电

环球视点!SpringCloud-Nacos学习笔记

来源:博客园


【资料图】

spring-cloud-alibaba版本说明

https://github.com/alibaba/spring-cloud-alibaba/wiki/%E7%89%88%E6%9C%AC%E8%AF%B4%E6%98%8ESpring Boot 2.4+ 和以下版本之间变化较大,目前企业级客户老项目相关 Spring Boot 版本仍停留在 Spring Boot 2.4 以下,为了同时满足存量用户和新用户不同需求,社区以 Spring Boot 2.4 为分界线,同时维护 2.2.x 和 2021.x 两个分支迭代Nacos学习官方网址https://nacos.io/zh-cn/docs/what-is-nacos.html名字服务 (Naming Service)提供分布式系统中所有对象(Object)、实体(Entity)的“名字”到关联的元数据之间的映射管理服务,例如 ServiceName -> Endpoints Info, Distributed Lock Name -> Lock Owner/Status Info, DNS Domain Name -> IP List, 服务发现和 DNS 就是名字服务的2大场景。配置服务 (Configuration Service)在服务或者应用运行过程中,提供动态配置或者元数据以及配置管理的服务提供者。OpenAPI可以借助api去查实例,注册实例等。。(某些接口在版本更新得时候未及时更新,比如注销实例)版本对比
2.X1.X
底层的通信协议grpchttp协议
注册中心数据存放位置\data\protocol\raft\naming_persistent_service_v2持久实例存在此目录..\data\naming\data持久实例存在此目录,临时实例存在内存中
集群搭建Nacos2.x版本相比1.X新增了gRPC的通信方式,因此需要增加2个端口。新增端口是在配置的主端口(server.port)基础上,进行一定偏移量自动生成。与主端口的偏移量1000--客户端gRPC请求服务端端口,用于客户端向服务端发起连接和请求,1001--服务端gRPC请求服务端端口,用于服务间同步等

Nacos基本概念

服务 (Service)服务是指一个或一组软件功能(例如特定信息的检索或一组操作的执行),其目的是不同的客户端可以为不同的目的重用(例如通过跨进程的网络调用)。Nacos 支持主流的服务生态,如 Kubernetes Service、gRPC|Dubbo RPC Service 或者 Spring Cloud RESTful Service。服务注册中心 (Service Registry)服务注册中心,它是服务及其实例和元数据的数据库。服务实例在启动时注册到服务注册表,并在关闭时注销。服务和路由器的客户端查询服务注册表以查找服务的可用实例。服务注册中心可能会调用服务实例的健康检查 API 来验证它是否能够处理请求。服务元数据 (Service Metadata)服务元数据是指包括服务端点(endpoints)、服务标签、服务版本号、服务实例权重、路由规则、安全策略等描述服务的数据。服务提供方 (Service Provider)是指提供可复用和可调用服务的应用方。服务消费方 (Service Consumer)是指会发起对某个服务调用的应用方。

Nacos注册中心核心功能

服务注册:Nacos Client会通过发送REST请求的方式向Nacos Server注册自己的服务,提供自身的元数据,比如ip地址、端口等信息。Nacos Server接收到注册请求后,就会把这些元数据信息存储在一个双层的内存Map中。服务心跳:在服务注册后,Nacos Client会维护一个定时心跳来持续通知Nacos Server,说明服务一直处于可用状态,防止被剔除。默认5s发送一次心跳。服务同步:Nacos Server集群之间会互相同步服务实例,用来保证服务信息的一致性。服务发现:服务消费者(Nacos Client)在调用服务提供者的服务时,会发送一个REST请求给Nacos Server,获取上面注册的服务清单,并且缓存在Nacos Client本地,同时会在Nacos Client本地开启一个定时任务定时拉取服务端最新的注册表信息更新到本地缓存服务健康检查:Nacos Server会开启一个定时任务用来检查注册服务实例的健康情况,对于超过15s没有收到客户端心跳的实例会将它的healthy属性置为false(客户端服务发现时不会发现),如果某个实例超过30秒没有收到心跳,直接剔除该实例(被剔除的实例如果恢复发送心跳则会重新注册)

Nacos服务下载

https://github.com/alibaba/nacos/releases

Nacos服务启动

启动脚本里默认是cluster,需要改成standlone配置文件在\conf下application.properties(说明nacos也是基于springboot 实现的)可配置:配置端口,数据源spring.datasource.platform=mysql,如果不配置数据源,默认存在内存中,集群肯定是要配置数据库的

Nacos客户端引入Nacos注册中心

在1.4.1版本之后启动类不需要加EnableDiscoverClient1.引入pom父pom配置
1  2  4     4.0.0 5      6         org.springframework.boot 7         spring-boot-starter-parent 8         2.3.12.RELEASE 9          10     11     com.pppp.qqqq12     spring-cloud-alibaba13     0.0.1-SNAPSHOT14     spring-cloud-alibaba15     pom16     Demo project for vip-spring-cloud-alibaba17 18     19         1.820         Hoxton.SR1221         2.2.8.RELEASE22     23 24     25         26             27                 org.springframework.cloud28                 spring-cloud-dependencies29                 ${spring-cloud.version}30                 pom31                 import32             33             34                 com.alibaba.cloud35                 spring-cloud-alibaba-dependencies36                 ${spring-cloud-alibaba.version}37                 pom38                 import39             40         41     42 
当前项目pom中引入依赖
1 2   com.alibaba.cloud3   spring-cloud-starter-alibaba-nacos-discovery4 
2.配置文件
1 server: 2   port: 8040 3  4 spring: 5   application: 6     name: mall-user  #微服务名称 7  8   #配置nacos注册中心地址 9   cloud:10     nacos:11       discovery:12         server-addr: 127.0.0.1:884813         #需要在nacos中建命名空间取id过来14         namespace: 49c955cf-1cd8-46a9-9823-aeb98ed3d60215         cluster-name: qf16         group: qf17         #ephemeral: false18         #不配也可以,默认就是nacos19         username: nacos20            password: nacos
其他详细配置可参考https://github.com/alibaba/spring-cloud-alibaba/wiki/Nacos-discovery3.启动客户端服务日志
c.a.c.n.registry.NacosServiceRegistry : nacos registry, DEFAULT_GROUP mall-user 192.168.31.20:8040 register finished
4.注册中心界面用openApi查询实例

服务逻辑隔离

Namespace 隔离设计命名空间(Namespace)用于进行租户(用户)粒度的隔离,Namespace 的常用场景之一是不同环境的隔离,例如开发测试环境和生产环境的资源(如配置、服务)隔离等。 修改yml配置
1 spring:2   application:3     name: mall-user  #微服务名称4 5   cloud:6     nacos:7       discovery:8         server-addr: 127.0.0.1:8848   #配置nacos注册中心地址9         namespace: bc50d386-8870-4a26-8803-0187486c57be  # dev 开发环境
group服务分组不同的服务可以归类到同一分组,group也可以起到服务隔离的作用。yml中可以通过spring.cloud.nacos.discovery.group参数配置

临时实例与持久实例

配置方式

1 spring: 2   application: 3     name: mall-user  #微服务名称 4  5   cloud: 6     nacos: 7       discovery: 8         server-addr: 127.0.0.1:8848   #配置nacos注册中心地址 9         namespace: bc50d386-8870-4a26-8803-0187486c57be  # dev 开发环境10         ephemeral: false # 持久化实例
在2.X之后同一个服务不能同时注册临时实例和持久实例持久实例存放路径\data\protocol\raft\naming_persistent_service_v2在定义上区分临时实例和持久化 实例的关键是健康检查的方式。临时实例使用客户端上报模式,而持久化实例使用服务端反向探测模式。 在大中型的公司里,这两种类型的服务往往都有。⼀些基础的组件例如数据库、缓存等,这些往往不能上报心跳,这种类型的服务在注册时,就需要作为持久化实例注册。而上层的业务服务,例如 微服务或者 Dubbo 服务,服务的 Provider 端支持添加汇报心跳的逻辑,此时就可以使用动态服务的注册方式。Nacos 1.x 中持久化及非 持久化的属性是作为实例的⼀个元数据进行存储和识别。Nacos 2.x 中继续沿用了持久化及非持久化的设定,但是有了⼀些调整。在 Nacos2.0 中将是否持久化的数据抽象至服务级别, 且不再允许⼀个服务同时存在持久化实例和非持久化实例,实例的持久化属性继承自服务的持久化属性

核心代码

//springcloud提供的服务注册的接口,如果自己写服务中心,则实现这个接口即可,在spring-cloud-commonsServiceRegistry.java
1 //springcloud提供的服务注册的接口,如果自己写服务中心,则实现这个接口即可,在spring-cloud-commons 2 public interface ServiceRegistry { 3  4    /** 5     * Registers the registration. A registration typically has information about an 6     * instance, such as its hostname and port. 7     * @param registration registration meta data 8     */ 9    void register(R registration);10 11    /**12     * Deregisters the registration.13     * @param registration registration meta data14     */15    void deregister(R registration);16 17    /**18     * Closes the ServiceRegistry. This is a lifecycle method.19     */20    void close();21 22    /**23     * Sets the status of the registration. The status values are determined by the24     * individual implementations.25     * @param registration The registration to update.26     * @param status The status to set.27     * @see org.springframework.cloud.client.serviceregistry.endpoint.ServiceRegistryEndpoint28     */29    void setStatus(R registration, String status);30 31    /**32     * Gets the status of a particular registration.33     * @param registration The registration to query.34     * @param  The type of the status.35     * @return The status of the registration.36     * @see org.springframework.cloud.client.serviceregistry.endpoint.ServiceRegistryEndpoint37     */38     T getStatus(R registration);39 40 }
View CodeNamingService.java
1 public static void main(String[] args) throws NacosException { 2  3     Properties properties = new Properties(); 4     properties.setProperty("serverAddr", "192.168.31.20:8848"); 5     //核心接口 6     NamingService naming = NamingFactory.createNamingService(properties); 7     //服务注册 8     naming.registerInstance("mall-user", "192.168.31.21", 8889, "ppppp"); 9     //服务发现10     System.out.println(naming.getAllInstances("mall-user"));11 }
1 /*  2  * Copyright 1999-2018 Alibaba Group Holding Ltd.  3  *  4  * Licensed under the Apache License, Version 2.0 (the "License");  5  * you may not use this file except in compliance with the License.  6  * You may obtain a copy of the License at  7  *  8  *      http://www.apache.org/licenses/LICENSE-2.0  9  * 10  * Unless required by applicable law or agreed to in writing, software 11  * distributed under the License is distributed on an "AS IS" BASIS, 12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13  * See the License for the specific language governing permissions and 14  * limitations under the License. 15  */ 16  17 package com.alibaba.nacos.api.naming; 18  19 import com.alibaba.nacos.api.exception.NacosException; 20 import com.alibaba.nacos.api.naming.listener.EventListener; 21 import com.alibaba.nacos.api.naming.pojo.Instance; 22 import com.alibaba.nacos.api.naming.pojo.ListView; 23 import com.alibaba.nacos.api.naming.pojo.ServiceInfo; 24 import com.alibaba.nacos.api.selector.AbstractSelector; 25  26 import java.util.List; 27  28 /** 29  * Naming Service. 30  * 31  * @author nkorange 32  */ 33 public interface NamingService { 34      35     /** 36      * register a instance to service. 37      * 38      * @param serviceName name of service 39      * @param ip          instance ip 40      * @param port        instance port 41      * @throws NacosException nacos exception 42      */ 43     void registerInstance(String serviceName, String ip, int port) throws NacosException; 44      45     /** 46      * register a instance to service. 47      * 48      * @param serviceName name of service 49      * @param groupName   group of service 50      * @param ip          instance ip 51      * @param port        instance port 52      * @throws NacosException nacos exception 53      */ 54     void registerInstance(String serviceName, String groupName, String ip, int port) throws NacosException; 55      56     /** 57      * register a instance to service with specified cluster name. 58      * 59      * @param serviceName name of service 60      * @param ip          instance ip 61      * @param port        instance port 62      * @param clusterName instance cluster name 63      * @throws NacosException nacos exception 64      */ 65     void registerInstance(String serviceName, String ip, int port, String clusterName) throws NacosException; 66      67     /** 68      * register a instance to service with specified cluster name. 69      * 70      * @param serviceName name of service 71      * @param groupName   group of service 72      * @param ip          instance ip 73      * @param port        instance port 74      * @param clusterName instance cluster name 75      * @throws NacosException nacos exception 76      */ 77     void registerInstance(String serviceName, String groupName, String ip, int port, String clusterName) 78             throws NacosException; 79      80     /** 81      * register a instance to service with specified instance properties. 82      * 83      * @param serviceName name of service 84      * @param instance    instance to register 85      * @throws NacosException nacos exception 86      */ 87     void registerInstance(String serviceName, Instance instance) throws NacosException; 88      89     /** 90      * register a instance to service with specified instance properties. 91      * 92      * @param serviceName name of service 93      * @param groupName   group of service 94      * @param instance    instance to register 95      * @throws NacosException nacos exception 96      */ 97     void registerInstance(String serviceName, String groupName, Instance instance) throws NacosException; 98      99     /**100      * deregister instance from a service.101      *102      * @param serviceName name of service103      * @param ip          instance ip104      * @param port        instance port105      * @throws NacosException nacos exception106      */107     void deregisterInstance(String serviceName, String ip, int port) throws NacosException;108     109     /**110      * deregister instance from a service.111      *112      * @param serviceName name of service113      * @param groupName   group of service114      * @param ip          instance ip115      * @param port        instance port116      * @throws NacosException nacos exception117      */118     void deregisterInstance(String serviceName, String groupName, String ip, int port) throws NacosException;119     120     /**121      * deregister instance with specified cluster name from a service.122      *123      * @param serviceName name of service124      * @param ip          instance ip125      * @param port        instance port126      * @param clusterName instance cluster name127      * @throws NacosException nacos exception128      */129     void deregisterInstance(String serviceName, String ip, int port, String clusterName) throws NacosException;130     131     /**132      * deregister instance with specified cluster name from a service.133      *134      * @param serviceName name of service135      * @param groupName   group of service136      * @param ip          instance ip137      * @param port        instance port138      * @param clusterName instance cluster name139      * @throws NacosException nacos exception140      */141     void deregisterInstance(String serviceName, String groupName, String ip, int port, String clusterName)142             throws NacosException;143     144     /**145      * deregister instance with full instance information and default groupName.146      *147      * @param serviceName name of service148      * @param instance    instance149      * @throws NacosException nacos exception150      */151     void deregisterInstance(String serviceName, Instance instance) throws NacosException;152     153     /**154      * deregister instance with full instance information.155      *156      * @param serviceName name of service157      * @param groupName   group of service158      * @param instance    instance information159      * @throws NacosException nacos exception160      */161     void deregisterInstance(String serviceName, String groupName, Instance instance) throws NacosException;162     163     /**164      * get all instances of a service.165      *166      * @param serviceName name of service167      * @return A list of instance168      * @throws NacosException nacos exception169      */170     List getAllInstances(String serviceName) throws NacosException;171     172     /**173      * get all instances of a service.174      *175      * @param serviceName name of service176      * @param groupName   group of service177      * @return A list of instance178      * @throws NacosException nacos exception179      */180     List getAllInstances(String serviceName, String groupName) throws NacosException;181     182     /**183      * Get all instances of a service.184      *185      * @param serviceName name of service186      * @param subscribe   if subscribe the service187      * @return A list of instance188      * @throws NacosException nacos exception189      */190     List getAllInstances(String serviceName, boolean subscribe) throws NacosException;191     192     /**193      * Get all instances of a service.194      *195      * @param serviceName name of service196      * @param groupName   group of service197      * @param subscribe   if subscribe the service198      * @return A list of instance199      * @throws NacosException nacos exception200      */201     List getAllInstances(String serviceName, String groupName, boolean subscribe) throws NacosException;202     203     /**204      * Get all instances within specified clusters of a service.205      *206      * @param serviceName name of service207      * @param clusters    list of cluster208      * @return A list of qualified instance209      * @throws NacosException nacos exception210      */211     List getAllInstances(String serviceName, List clusters) throws NacosException;212     213     /**214      * Get all instances within specified clusters of a service.215      *216      * @param serviceName name of service217      * @param groupName   group of service218      * @param clusters    list of cluster219      * @return A list of qualified instance220      * @throws NacosException nacos exception221      */222     List getAllInstances(String serviceName, String groupName, List clusters) throws NacosException;223     224     /**225      * Get all instances within specified clusters of a service.226      *227      * @param serviceName name of service228      * @param clusters    list of cluster229      * @param subscribe   if subscribe the service230      * @return A list of qualified instance231      * @throws NacosException nacos exception232      */233     List getAllInstances(String serviceName, List clusters, boolean subscribe) throws NacosException;234     235     /**236      * Get all instances within specified clusters of a service.237      *238      * @param serviceName name of service239      * @param groupName   group of service240      * @param clusters    list of cluster241      * @param subscribe   if subscribe the service242      * @return A list of qualified instance243      * @throws NacosException nacos exception244      */245     List getAllInstances(String serviceName, String groupName, List clusters, boolean subscribe)246             throws NacosException;247     248     /**249      * Get qualified instances of service.250      *251      * @param serviceName name of service.252      * @param healthy     a flag to indicate returning healthy or unhealthy instances253      * @return A qualified list of instance254      * @throws NacosException nacos exception255      */256     List selectInstances(String serviceName, boolean healthy) throws NacosException;257     258     /**259      * Get qualified instances of service.260      *261      * @param serviceName name of service262      * @param groupName   group of service263      * @param healthy     a flag to indicate returning healthy or unhealthy instances264      * @return A qualified list of instance265      * @throws NacosException nacos exception266      */267     List selectInstances(String serviceName, String groupName, boolean healthy) throws NacosException;268     269     /**270      * Get qualified instances of service.271      *272      * @param serviceName name of service273      * @param healthy     a flag to indicate returning healthy or unhealthy instances274      * @param subscribe   if subscribe the service275      * @return A qualified list of instance276      * @throws NacosException nacos exception277      */278     List selectInstances(String serviceName, boolean healthy, boolean subscribe) throws NacosException;279     280     /**281      * Get qualified instances of service.282      *283      * @param serviceName name of service284      * @param groupName   group of service285      * @param healthy     a flag to indicate returning healthy or unhealthy instances286      * @param subscribe   if subscribe the service287      * @return A qualified list of instance288      * @throws NacosException nacos exception289      */290     List selectInstances(String serviceName, String groupName, boolean healthy, boolean subscribe)291             throws NacosException;292     293     /**294      * Get qualified instances within specified clusters of service.295      *296      * @param serviceName name of service297      * @param clusters    list of cluster298      * @param healthy     a flag to indicate returning healthy or unhealthy instances299      * @return A qualified list of instance300      * @throws NacosException nacos exception301      */302     List selectInstances(String serviceName, List clusters, boolean healthy) throws NacosException;303     304     /**305      * Get qualified instances within specified clusters of service.306      *307      * @param serviceName name of service308      * @param groupName   group of service309      * @param clusters    list of cluster310      * @param healthy     a flag to indicate returning healthy or unhealthy instances311      * @return A qualified list of instance312      * @throws NacosException nacos exception313      */314     List selectInstances(String serviceName, String groupName, List clusters, boolean healthy)315             throws NacosException;316     317     /**318      * Get qualified instances within specified clusters of service.319      *320      * @param serviceName name of service321      * @param clusters    list of cluster322      * @param healthy     a flag to indicate returning healthy or unhealthy instances323      * @param subscribe   if subscribe the service324      * @return A qualified list of instance325      * @throws NacosException nacos exception326      */327     List selectInstances(String serviceName, List clusters, boolean healthy, boolean subscribe)328             throws NacosException;329     330     /**331      * Get qualified instances within specified clusters of service.332      *333      * @param serviceName name of service334      * @param groupName   group of service335      * @param clusters    list of cluster336      * @param healthy     a flag to indicate returning healthy or unhealthy instances337      * @param subscribe   if subscribe the service338      * @return A qualified list of instance339      * @throws NacosException nacos exception340      */341     List selectInstances(String serviceName, String groupName, List clusters, boolean healthy,342             boolean subscribe) throws NacosException;343     344     /**345      * Select one healthy instance of service using predefined load balance strategy.346      *347      * @param serviceName name of service348      * @return qualified instance349      * @throws NacosException nacos exception350      */351     Instance selectOneHealthyInstance(String serviceName) throws NacosException;352     353     /**354      * Select one healthy instance of service using predefined load balance strategy.355      *356      * @param serviceName name of service357      * @param groupName   group of service358      * @return qualified instance359      * @throws NacosException nacos exception360      */361     Instance selectOneHealthyInstance(String serviceName, String groupName) throws NacosException;362     363     /**364      * select one healthy instance of service using predefined load balance strategy.365      *366      * @param serviceName name of service367      * @param subscribe   if subscribe the service368      * @return qualified instance369      * @throws NacosException nacos exception370      */371     Instance selectOneHealthyInstance(String serviceName, boolean subscribe) throws NacosException;372     373     /**374      * select one healthy instance of service using predefined load balance strategy.375      *376      * @param serviceName name of service377      * @param groupName   group of service378      * @param subscribe   if subscribe the service379      * @return qualified instance380      * @throws NacosException nacos exception381      */382     Instance selectOneHealthyInstance(String serviceName, String groupName, boolean subscribe) throws NacosException;383     384     /**385      * Select one healthy instance of service using predefined load balance strategy.386      *387      * @param serviceName name of service388      * @param clusters    a list of clusters should the instance belongs to389      * @return qualified instance390      * @throws NacosException nacos exception391      */392     Instance selectOneHealthyInstance(String serviceName, List clusters) throws NacosException;393     394     /**395      * Select one healthy instance of service using predefined load balance strategy.396      *397      * @param serviceName name of service398      * @param groupName   group of service399      * @param clusters    a list of clusters should the instance belongs to400      * @return qualified instance401      * @throws NacosException nacos exception402      */403     Instance selectOneHealthyInstance(String serviceName, String groupName, List clusters)404             throws NacosException;405     406     /**407      * Select one healthy instance of service using predefined load balance strategy.408      *409      * @param serviceName name of service410      * @param clusters    a list of clusters should the instance belongs to411      * @param subscribe   if subscribe the service412      * @return qualified instance413      * @throws NacosException nacos exception414      */415     Instance selectOneHealthyInstance(String serviceName, List clusters, boolean subscribe)416             throws NacosException;417     418     /**419      * Select one healthy instance of service using predefined load balance strategy.420      *421      * @param serviceName name of service422      * @param groupName   group of service423      * @param clusters    a list of clusters should the instance belongs to424      * @param subscribe   if subscribe the service425      * @return qualified instance426      * @throws NacosException nacos exception427      */428     Instance selectOneHealthyInstance(String serviceName, String groupName, List clusters, boolean subscribe)429             throws NacosException;430     431     /**432      * Subscribe service to receive events of instances alteration.433      *434      * @param serviceName name of service435      * @param listener    event listener436      * @throws NacosException nacos exception437      */438     void subscribe(String serviceName, EventListener listener) throws NacosException;439     440     /**441      * Subscribe service to receive events of instances alteration.442      *443      * @param serviceName name of service444      * @param groupName   group of service445      * @param listener    event listener446      * @throws NacosException nacos exception447      */448     void subscribe(String serviceName, String groupName, EventListener listener) throws NacosException;449     450     /**451      * Subscribe service to receive events of instances alteration.452      *453      * @param serviceName name of service454      * @param clusters    list of cluster455      * @param listener    event listener456      * @throws NacosException nacos exception457      */458     void subscribe(String serviceName, List clusters, EventListener listener) throws NacosException;459     460     /**461      * Subscribe service to receive events of instances alteration.462      *463      * @param serviceName name of service464      * @param groupName   group of service465      * @param clusters    list of cluster466      * @param listener    event listener467      * @throws NacosException nacos exception468      */469     void subscribe(String serviceName, String groupName, List clusters, EventListener listener)470             throws NacosException;471     472     /**473      * Unsubscribe event listener of service.474      *475      * @param serviceName name of service476      * @param listener    event listener477      * @throws NacosException nacos exception478      */479     void unsubscribe(String serviceName, EventListener listener) throws NacosException;480     481     /**482      * unsubscribe event listener of service.483      *484      * @param serviceName name of service485      * @param groupName   group of service486      * @param listener    event listener487      * @throws NacosException nacos exception488      */489     void unsubscribe(String serviceName, String groupName, EventListener listener) throws NacosException;490     491     /**492      * Unsubscribe event listener of service.493      *494      * @param serviceName name of service495      * @param clusters    list of cluster496      * @param listener    event listener497      * @throws NacosException nacos exception498      */499     void unsubscribe(String serviceName, List clusters, EventListener listener) throws NacosException;500     501     /**502      * Unsubscribe event listener of service.503      *504      * @param serviceName name of service505      * @param groupName   group of service506      * @param clusters    list of cluster507      * @param listener    event listener508      * @throws NacosException nacos exception509      */510     void unsubscribe(String serviceName, String groupName, List clusters, EventListener listener)511             throws NacosException;512     513     /**514      * Get all service names from server.515      *516      * @param pageNo   page index517      * @param pageSize page size518      * @return list of service names519      * @throws NacosException nacos exception520      */521     ListView getServicesOfServer(int pageNo, int pageSize) throws NacosException;522     523     /**524      * Get all service names from server.525      *526      * @param pageNo    page index527      * @param pageSize  page size528      * @param groupName group name529      * @return list of service names530      * @throws NacosException nacos exception531      */532     ListView getServicesOfServer(int pageNo, int pageSize, String groupName) throws NacosException;533     534     /**535      * Get all service names from server with selector.536      *537      * @param pageNo   page index538      * @param pageSize page size539      * @param selector selector to filter the resource540      * @return list of service names541      * @throws NacosException nacos exception542      * @since 0.7.0543      */544     ListView getServicesOfServer(int pageNo, int pageSize, AbstractSelector selector) throws NacosException;545     546     /**547      * Get all service names from server with selector.548      *549      * @param pageNo    page index550      * @param pageSize  page size551      * @param groupName group name552      * @param selector  selector to filter the resource553      * @return list of service names554      * @throws NacosException nacos exception555      */556     ListView getServicesOfServer(int pageNo, int pageSize, String groupName, AbstractSelector selector)557             throws NacosException;558     559     /**560      * Get all subscribed services of current client.561      *562      * @return subscribed services563      * @throws NacosException nacos exception564      */565     List getSubscribeServices() throws NacosException;566     567     /**568      * get server health status.569      *570      * @return is server healthy571      */572     String getServerStatus();573     574     /**575      * Shutdown the resource service.576      *577      * @throws NacosException exception.578      */579     void shutDown() throws NacosException;580 }
NamingService 源码/* * 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.api.naming;import com.alibaba.nacos.api.exception.NacosException;import com.alibaba.nacos.api.naming.listener.EventListener;import com.alibaba.nacos.api.naming.pojo.Instance;import com.alibaba.nacos.api.naming.pojo.ListView;import com.alibaba.nacos.api.naming.pojo.ServiceInfo;import com.alibaba.nacos.api.selector.AbstractSelector;import java.util.List;/** * Naming Service. * * @author nkorange */public interface NamingService { /** * register a instance to service. * * @param serviceName name of service * @param ip instance ip * @param port instance port * @throws NacosException nacos exception */ void registerInstance(String serviceName, String ip, int port) throws NacosException; /** * register a instance to service. * * @param serviceName name of service * @param groupName group of service * @param ip instance ip * @param port instance port * @throws NacosException nacos exception */ void registerInstance(String serviceName, String groupName, String ip, int port) throws NacosException; /** * register a instance to service with specified cluster name. * * @param serviceName name of service * @param ip instance ip * @param port instance port * @param clusterName instance cluster name * @throws NacosException nacos exception */ void registerInstance(String serviceName, String ip, int port, String clusterName) throws NacosException; /** * register a instance to service with specified cluster name. * * @param serviceName name of service * @param groupName group of service * @param ip instance ip * @param port instance port * @param clusterName instance cluster name * @throws NacosException nacos exception */ void registerInstance(String serviceName, String groupName, String ip, int port, String clusterName) throws NacosException; /** * register a instance to service with specified instance properties. * * @param serviceName name of service * @param instance instance to register * @throws NacosException nacos exception */ void registerInstance(String serviceName, Instance instance) throws NacosException; /** * register a instance to service with specified instance properties. * * @param serviceName name of service * @param groupName group of service * @param instance instance to register * @throws NacosException nacos exception */ void registerInstance(String serviceName, String groupName, Instance instance) throws NacosException; /** * deregister instance from a service. * * @param serviceName name of service * @param ip instance ip * @param port instance port * @throws NacosException nacos exception */ void deregisterInstance(String serviceName, String ip, int port) throws NacosException; /** * deregister instance from a service. * * @param serviceName name of service * @param groupName group of service * @param ip instance ip * @param port instance port * @throws NacosException nacos exception */ void deregisterInstance(String serviceName, String groupName, String ip, int port) throws NacosException; /** * deregister instance with specified cluster name from a service. * * @param serviceName name of service * @param ip instance ip * @param port instance port * @param clusterName instance cluster name * @throws NacosException nacos exception */ void deregisterInstance(String serviceName, String ip, int port, String clusterName) throws NacosException; /** * deregister instance with specified cluster name from a service. * * @param serviceName name of service * @param groupName group of service * @param ip instance ip * @param port instance port * @param clusterName instance cluster name * @throws NacosException nacos exception */ void deregisterInstance(String serviceName, String groupName, String ip, int port, String clusterName) throws NacosException; /** * deregister instance with full instance information and default groupName. * * @param serviceName name of service * @param instance instance * @throws NacosException nacos exception */ void deregisterInstance(String serviceName, Instance instance) throws NacosException; /** * deregister instance with full instance information. * * @param serviceName name of service * @param groupName group of service * @param instance instance information * @throws NacosException nacos exception */ void deregisterInstance(String serviceName, String groupName, Instance instance) throws NacosException; /** * get all instances of a service. * * @param serviceName name of service * @return A list of instance * @throws NacosException nacos exception */ List getAllInstances(String serviceName) throws NacosException; /** * get all instances of a service. * * @param serviceName name of service * @param groupName group of service * @return A list of instance * @throws NacosException nacos exception */ List getAllInstances(String serviceName, String groupName) throws NacosException; /** * Get all instances of a service. * * @param serviceName name of service * @param subscribe if subscribe the service * @return A list of instance * @throws NacosException nacos exception */ List getAllInstances(String serviceName, boolean subscribe) throws NacosException; /** * Get all instances of a service. * * @param serviceName name of service * @param groupName group of service * @param subscribe if subscribe the service * @return A list of instance * @throws NacosException nacos exception */ List getAllInstances(String serviceName, String groupName, boolean subscribe) throws NacosException; /** * Get all instances within specified clusters of a service. * * @param serviceName name of service * @param clusters list of cluster * @return A list of qualified instance * @throws NacosException nacos exception */ List getAllInstances(String serviceName, List clusters) throws NacosException; /** * Get all instances within specified clusters of a service. * * @param serviceName name of service * @param groupName group of service * @param clusters list of cluster * @return A list of qualified instance * @throws NacosException nacos exception */ List getAllInstances(String serviceName, String groupName, List clusters) throws NacosException; /** * Get all instances within specified clusters of a service. * * @param serviceName name of service * @param clusters list of cluster * @param subscribe if subscribe the service * @return A list of qualified instance * @throws NacosException nacos exception */ List getAllInstances(String serviceName, List clusters, boolean subscribe) throws NacosException; /** * Get all instances within specified clusters of a service. * * @param serviceName name of service * @param groupName group of service * @param clusters list of cluster * @param subscribe if subscribe the service * @return A list of qualified instance * @throws NacosException nacos exception */ List getAllInstances(String serviceName, String groupName, List clusters, boolean subscribe) throws NacosException; /** * Get qualified instances of service. * * @param serviceName name of service. * @param healthy a flag to indicate returning healthy or unhealthy instances * @return A qualified list of instance * @throws NacosException nacos exception */ List selectInstances(String serviceName, boolean healthy) throws NacosException; /** * Get qualified instances of service. * * @param serviceName name of service * @param groupName group of service * @param healthy a flag to indicate returning healthy or unhealthy instances * @return A qualified list of instance * @throws NacosException nacos exception */ List selectInstances(String serviceName, String groupName, boolean healthy) throws NacosException; /** * Get qualified instances of service. * * @param serviceName name of service * @param healthy a flag to indicate returning healthy or unhealthy instances * @param subscribe if subscribe the service * @return A qualified list of instance * @throws NacosException nacos exception */ List selectInstances(String serviceName, boolean healthy, boolean subscribe) throws NacosException; /** * Get qualified instances of service. * * @param serviceName name of service * @param groupName group of service * @param healthy a flag to indicate returning healthy or unhealthy instances * @param subscribe if subscribe the service * @return A qualified list of instance * @throws NacosException nacos exception */ List selectInstances(String serviceName, String groupName, boolean healthy, boolean subscribe) throws NacosException; /** * Get qualified instances within specified clusters of service. * * @param serviceName name of service * @param clusters list of cluster * @param healthy a flag to indicate returning healthy or unhealthy instances * @return A qualified list of instance * @throws NacosException nacos exception */ List selectInstances(String serviceName, List clusters, boolean healthy) throws NacosException; /** * Get qualified instances within specified clusters of service. * * @param serviceName name of service * @param groupName group of service * @param clusters list of cluster * @param healthy a flag to indicate returning healthy or unhealthy instances * @return A qualified list of instance * @throws NacosException nacos exception */ List selectInstances(String serviceName, String groupName, List clusters, boolean healthy) throws NacosException; /** * Get qualified instances within specified clusters of service. * * @param serviceName name of service * @param clusters list of cluster * @param healthy a flag to indicate returning healthy or unhealthy instances * @param subscribe if subscribe the service * @return A qualified list of instance * @throws NacosException nacos exception */ List selectInstances(String serviceName, List clusters, boolean healthy, boolean subscribe) throws NacosException; /** * Get qualified instances within specified clusters of service. * * @param serviceName name of service * @param groupName group of service * @param clusters list of cluster * @param healthy a flag to indicate returning healthy or unhealthy instances * @param subscribe if subscribe the service * @return A qualified list of instance * @throws NacosException nacos exception */ List selectInstances(String serviceName, String groupName, List clusters, boolean healthy, boolean subscribe) throws NacosException; /** * Select one healthy instance of service using predefined load balance strategy. * * @param serviceName name of service * @return qualified instance * @throws NacosException nacos exception */ Instance selectOneHealthyInstance(String serviceName) throws NacosException; /** * Select one healthy instance of service using predefined load balance strategy. * * @param serviceName name of service * @param groupName group of service * @return qualified instance * @throws NacosException nacos exception */ Instance selectOneHealthyInstance(String serviceName, String groupName) throws NacosException; /** * select one healthy instance of service using predefined load balance strategy. * * @param serviceName name of service * @param subscribe if subscribe the service * @return qualified instance * @throws NacosException nacos exception */ Instance selectOneHealthyInstance(String serviceName, boolean subscribe) throws NacosException; /** * select one healthy instance of service using predefined load balance strategy. * * @param serviceName name of service * @param groupName group of service * @param subscribe if subscribe the service * @return qualified instance * @throws NacosException nacos exception */ Instance selectOneHealthyInstance(String serviceName, String groupName, boolean subscribe) throws NacosException; /** * Select one healthy instance of service using predefined load balance strategy. * * @param serviceName name of service * @param clusters a list of clusters should the instance belongs to * @return qualified instance * @throws NacosException nacos exception */ Instance selectOneHealthyInstance(String serviceName, List clusters) throws NacosException; /** * Select one healthy instance of service using predefined load balance strategy. * * @param serviceName name of service * @param groupName group of service * @param clusters a list of clusters should the instance belongs to * @return qualified instance * @throws NacosException nacos exception */ Instance selectOneHealthyInstance(String serviceName, String groupName, List clusters) throws NacosException; /** * Select one healthy instance of service using predefined load balance strategy. * * @param serviceName name of service * @param clusters a list of clusters should the instance belongs to * @param subscribe if subscribe the service * @return qualified instance * @throws NacosException nacos exception */ Instance selectOneHealthyInstance(String serviceName, List clusters, boolean subscribe) throws NacosException; /** * Select one healthy instance of service using predefined load balance strategy. * * @param serviceName name of service * @param groupName group of service * @param clusters a list of clusters should the instance belongs to * @param subscribe if subscribe the service * @return qualified instance * @throws NacosException nacos exception */ Instance selectOneHealthyInstance(String serviceName, String groupName, List clusters, boolean subscribe) throws NacosException; /** * Subscribe service to receive events of instances alteration. * * @param serviceName name of service * @param listener event listener * @throws NacosException nacos exception */ void subscribe(String serviceName, EventListener listener) throws NacosException; /** * Subscribe service to receive events of instances alteration. * * @param serviceName name of service * @param groupName group of service * @param listener event listener * @throws NacosException nacos exception */ void subscribe(String serviceName, String groupName, EventListener listener) throws NacosException; /** * Subscribe service to receive events of instances alteration. * * @param serviceName name of service * @param clusters list of cluster * @param listener event listener * @throws NacosException nacos exception */ void subscribe(String serviceName, List clusters, EventListener listener) throws NacosException; /** * Subscribe service to receive events of instances alteration. * * @param serviceName name of service * @param groupName group of service * @param clusters list of cluster * @param listener event listener * @throws NacosException nacos exception */ void subscribe(String serviceName, String groupName, List clusters, EventListener listener) throws NacosException; /** * Unsubscribe event listener of service. * * @param serviceName name of service * @param listener event listener * @throws NacosException nacos exception */ void unsubscribe(String serviceName, EventListener listener) throws NacosException; /** * unsubscribe event listener of service. * * @param serviceName name of service * @param groupName group of service * @param listener event listener * @throws NacosException nacos exception */ void unsubscribe(String serviceName, String groupName, EventListener listener) throws NacosException; /** * Unsubscribe event listener of service. * * @param serviceName name of service * @param clusters list of cluster * @param listener event listener * @throws NacosException nacos exception */ void unsubscribe(String serviceName, List clusters, EventListener listener) throws NacosException; /** * Unsubscribe event listener of service. * * @param serviceName name of service * @param groupName group of service * @param clusters list of cluster * @param listener event listener * @throws NacosException nacos exception */ void unsubscribe(String serviceName, String groupName, List clusters, EventListener listener) throws NacosException; /** * Get all service names from server. * * @param pageNo page index * @param pageSize page size * @return list of service names * @throws NacosException nacos exception */ ListView getServicesOfServer(int pageNo, int pageSize) throws NacosException; /** * Get all service names from server. * * @param pageNo page index * @param pageSize page size * @param groupName group name * @return list of service names * @throws NacosException nacos exception */ ListView getServicesOfServer(int pageNo, int pageSize, String groupName) throws NacosException; /** * Get all service names from server with selector. * * @param pageNo page index * @param pageSize page size * @param selector selector to filter the resource * @return list of service names * @throws NacosException nacos exception * @since 0.7.0 */ ListView getServicesOfServer(int pageNo, int pageSize, AbstractSelector selector) throws NacosException; /** * Get all service names from server with selector. * * @param pageNo page index * @param pageSize page size * @param groupName group name * @param selector selector to filter the resource * @return list of service names * @throws NacosException nacos exception */ ListView getServicesOfServer(int pageNo, int pageSize, String groupName, AbstractSelector selector) throws NacosException; /** * Get all subscribed services of current client. * * @return subscribed services * @throws NacosException nacos exception */ List getSubscribeServices() throws NacosException; /** * get server health status. * * @return is server healthy */ String getServerStatus(); /** * Shutdown the resource service. * * @throws NacosException exception. */ void shutDown() throws NacosException;}

关键词: 配置文件 服务中心 服务提供者