spring-cloud-alibaba版本说明
https://github.com/alibaba/spring-cloud-alibaba/wiki/%E7%89%88%E6%9C%AC%E8%AF%B4%E6%98%8E Spring 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.X | 1.X | |
底层的通信协议 | grpc | http协议 |
注册中心数据存放位置 | \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/releasesNacos服务启动
启动脚本里默认是cluster,需要改成standlone 配置文件在\conf下application.properties(说明nacos也是基于springboot 实现的) 可配置:配置端口,数据源spring.datasource.platform=mysql,如果不配置数据源,默认存在内存中,集群肯定是要配置数据库的Nacos客户端引入Nacos注册中心
在1.4.1版本之后启动类不需要加EnableDiscoverClient 1.引入pom 父pom配置1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 <parent> 6 <groupId>org.springframework.boot</groupId> 7 <artifactId>spring-boot-starter-parent</artifactId> 8 <version>2.3.12.RELEASE</version> 9 <relativePath/> <!-- lookup parent from repository --> 10 </parent> 11 <groupId>com.pppp.qqqq</groupId> 12 <artifactId>spring-cloud-alibaba</artifactId> 13 <version>0.0.1-SNAPSHOT</version> 14 <name>spring-cloud-alibaba</name> 15 <packaging>pom</packaging> 16 <description>Demo project for vip-spring-cloud-alibaba</description> 17 18 <properties> 19 <java.version>1.8</java.version> 20 <spring-cloud.version>Hoxton.SR12</spring-cloud.version> 21 <spring-cloud-alibaba.version>2.2.8.RELEASE</spring-cloud-alibaba.version> 22 </properties> 23 24 <dependencyManagement> 25 <dependencies> 26 <dependency> 27 <groupId>org.springframework.cloud</groupId> 28 <artifactId>spring-cloud-dependencies</artifactId> 29 <version>${spring-cloud.version}</version> 30 <type>pom</type> 31 <scope>import</scope> 32 </dependency> 33 <dependency> 34 <groupId>com.alibaba.cloud</groupId> 35 <artifactId>spring-cloud-alibaba-dependencies</artifactId> 36 <version>${spring-cloud-alibaba.version}</version> 37 <type>pom</type> 38 <scope>import</scope> 39 </dependency> 40 </dependencies> 41 </dependencyManagement> 42 </project>当前项目pom中引入依赖
1 <dependency> 2 <groupId>com.alibaba.cloud</groupId> 3 <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> 4 </dependency>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:8848 13 #需要在nacos中建命名空间取id过来 14 namespace: 49c955cf-1cd8-46a9-9823-aeb98ed3d602 15 cluster-name: qf 16 group: qf 17 #ephemeral: false 18 #不配也可以,默认就是nacos 19 username: nacos 20 password: nacos其他详细配置可参考https://github.com/alibaba/spring-cloud-alibaba/wiki/Nacos-discovery 3.启动客户端服务日志
c.a.c.n.registry.NacosServiceRegistry : nacos registry, DEFAULT_GROUP mall-user 192.168.31.20:8040 register finished4.注册中心界面
用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-commons ServiceRegistry.java1 //springcloud提供的服务注册的接口,如果自己写服务中心,则实现这个接口即可,在spring-cloud-commons 2 public interface ServiceRegistry<R extends Registration> { 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 data 14 */ 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 the 24 * individual implementations. 25 * @param registration The registration to update. 26 * @param status The status to set. 27 * @see org.springframework.cloud.client.serviceregistry.endpoint.ServiceRegistryEndpoint 28 */ 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 <T> The type of the status. 35 * @return The status of the registration. 36 * @see org.springframework.cloud.client.serviceregistry.endpoint.ServiceRegistryEndpoint 37 */ 38 <T> T getStatus(R registration); 39 40 }View Code NamingService.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 service 103 * @param ip instance ip 104 * @param port instance port 105 * @throws NacosException nacos exception 106 */ 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 service 113 * @param groupName group of service 114 * @param ip instance ip 115 * @param port instance port 116 * @throws NacosException nacos exception 117 */ 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 service 124 * @param ip instance ip 125 * @param port instance port 126 * @param clusterName instance cluster name 127 * @throws NacosException nacos exception 128 */ 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 service 135 * @param groupName group of service 136 * @param ip instance ip 137 * @param port instance port 138 * @param clusterName instance cluster name 139 * @throws NacosException nacos exception 140 */ 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 service 148 * @param instance instance 149 * @throws NacosException nacos exception 150 */ 151 void deregisterInstance(String serviceName, Instance instance) throws NacosException; 152 153 /** 154 * deregister instance with full instance information. 155 * 156 * @param serviceName name of service 157 * @param groupName group of service 158 * @param instance instance information 159 * @throws NacosException nacos exception 160 */ 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 service 167 * @return A list of instance 168 * @throws NacosException nacos exception 169 */ 170 List<Instance> getAllInstances(String serviceName) throws NacosException; 171 172 /** 173 * get all instances of a service. 174 * 175 * @param serviceName name of service 176 * @param groupName group of service 177 * @return A list of instance 178 * @throws NacosException nacos exception 179 */ 180 List<Instance> getAllInstances(String serviceName, String groupName) throws NacosException; 181 182 /** 183 * Get all instances of a service. 184 * 185 * @param serviceName name of service 186 * @param subscribe if subscribe the service 187 * @return A list of instance 188 * @throws NacosException nacos exception 189 */ 190 List<Instance> getAllInstances(String serviceName, boolean subscribe) throws NacosException; 191 192 /** 193 * Get all instances of a service. 194 * 195 * @param serviceName name of service 196 * @param groupName group of service 197 * @param subscribe if subscribe the service 198 * @return A list of instance 199 * @throws NacosException nacos exception 200 */ 201 List<Instance> 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 service 207 * @param clusters list of cluster 208 * @return A list of qualified instance 209 * @throws NacosException nacos exception 210 */ 211 List<Instance> getAllInstances(String serviceName, List<String> clusters) throws NacosException; 212 213 /** 214 * Get all instances within specified clusters of a service. 215 * 216 * @param serviceName name of service 217 * @param groupName group of service 218 * @param clusters list of cluster 219 * @return A list of qualified instance 220 * @throws NacosException nacos exception 221 */ 222 List<Instance> getAllInstances(String serviceName, String groupName, List<String> clusters) throws NacosException; 223 224 /** 225 * Get all instances within specified clusters of a service. 226 * 227 * @param serviceName name of service 228 * @param clusters list of cluster 229 * @param subscribe if subscribe the service 230 * @return A list of qualified instance 231 * @throws NacosException nacos exception 232 */ 233 List<Instance> getAllInstances(String serviceName, List<String> clusters, boolean subscribe) throws NacosException; 234 235 /** 236 * Get all instances within specified clusters of a service. 237 * 238 * @param serviceName name of service 239 * @param groupName group of service 240 * @param clusters list of cluster 241 * @param subscribe if subscribe the service 242 * @return A list of qualified instance 243 * @throws NacosException nacos exception 244 */ 245 List<Instance> getAllInstances(String serviceName, String groupName, List<String> 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 instances 253 * @return A qualified list of instance 254 * @throws NacosException nacos exception 255 */ 256 List<Instance> selectInstances(String serviceName, boolean healthy) throws NacosException; 257 258 /** 259 * Get qualified instances of service. 260 * 261 * @param serviceName name of service 262 * @param groupName group of service 263 * @param healthy a flag to indicate returning healthy or unhealthy instances 264 * @return A qualified list of instance 265 * @throws NacosException nacos exception 266 */ 267 List<Instance> selectInstances(String serviceName, String groupName, boolean healthy) throws NacosException; 268 269 /** 270 * Get qualified instances of service. 271 * 272 * @param serviceName name of service 273 * @param healthy a flag to indicate returning healthy or unhealthy instances 274 * @param subscribe if subscribe the service 275 * @return A qualified list of instance 276 * @throws NacosException nacos exception 277 */ 278 List<Instance> selectInstances(String serviceName, boolean healthy, boolean subscribe) throws NacosException; 279 280 /** 281 * Get qualified instances of service. 282 * 283 * @param serviceName name of service 284 * @param groupName group of service 285 * @param healthy a flag to indicate returning healthy or unhealthy instances 286 * @param subscribe if subscribe the service 287 * @return A qualified list of instance 288 * @throws NacosException nacos exception 289 */ 290 List<Instance> 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 service 297 * @param clusters list of cluster 298 * @param healthy a flag to indicate returning healthy or unhealthy instances 299 * @return A qualified list of instance 300 * @throws NacosException nacos exception 301 */ 302 List<Instance> selectInstances(String serviceName, List<String> clusters, boolean healthy) throws NacosException; 303 304 /** 305 * Get qualified instances within specified clusters of service. 306 * 307 * @param serviceName name of service 308 * @param groupName group of service 309 * @param clusters list of cluster 310 * @param healthy a flag to indicate returning healthy or unhealthy instances 311 * @return A qualified list of instance 312 * @throws NacosException nacos exception 313 */ 314 List<Instance> selectInstances(String serviceName, String groupName, List<String> clusters, boolean healthy) 315 throws NacosException; 316 317 /** 318 * Get qualified instances within specified clusters of service. 319 * 320 * @param serviceName name of service 321 * @param clusters list of cluster 322 * @param healthy a flag to indicate returning healthy or unhealthy instances 323 * @param subscribe if subscribe the service 324 * @return A qualified list of instance 325 * @throws NacosException nacos exception 326 */ 327 List<Instance> selectInstances(String serviceName, List<String> 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 service 334 * @param groupName group of service 335 * @param clusters list of cluster 336 * @param healthy a flag to indicate returning healthy or unhealthy instances 337 * @param subscribe if subscribe the service 338 * @return A qualified list of instance 339 * @throws NacosException nacos exception 340 */ 341 List<Instance> selectInstances(String serviceName, String groupName, List<String> 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 service 348 * @return qualified instance 349 * @throws NacosException nacos exception 350 */ 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 service 357 * @param groupName group of service 358 * @return qualified instance 359 * @throws NacosException nacos exception 360 */ 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 service 367 * @param subscribe if subscribe the service 368 * @return qualified instance 369 * @throws NacosException nacos exception 370 */ 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 service 377 * @param groupName group of service 378 * @param subscribe if subscribe the service 379 * @return qualified instance 380 * @throws NacosException nacos exception 381 */ 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 service 388 * @param clusters a list of clusters should the instance belongs to 389 * @return qualified instance 390 * @throws NacosException nacos exception 391 */ 392 Instance selectOneHealthyInstance(String serviceName, List<String> clusters) throws NacosException; 393 394 /** 395 * Select one healthy instance of service using predefined load balance strategy. 396 * 397 * @param serviceName name of service 398 * @param groupName group of service 399 * @param clusters a list of clusters should the instance belongs to 400 * @return qualified instance 401 * @throws NacosException nacos exception 402 */ 403 Instance selectOneHealthyInstance(String serviceName, String groupName, List<String> clusters) 404 throws NacosException; 405 406 /** 407 * Select one healthy instance of service using predefined load balance strategy. 408 * 409 * @param serviceName name of service 410 * @param clusters a list of clusters should the instance belongs to 411 * @param subscribe if subscribe the service 412 * @return qualified instance 413 * @throws NacosException nacos exception 414 */ 415 Instance selectOneHealthyInstance(String serviceName, List<String> 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 service 422 * @param groupName group of service 423 * @param clusters a list of clusters should the instance belongs to 424 * @param subscribe if subscribe the service 425 * @return qualified instance 426 * @throws NacosException nacos exception 427 */ 428 Instance selectOneHealthyInstance(String serviceName, String groupName, List<String> clusters, boolean subscribe) 429 throws NacosException; 430 431 /** 432 * Subscribe service to receive events of instances alteration. 433 * 434 * @param serviceName name of service 435 * @param listener event listener 436 * @throws NacosException nacos exception 437 */ 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 service 444 * @param groupName group of service 445 * @param listener event listener 446 * @throws NacosException nacos exception 447 */ 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 service 454 * @param clusters list of cluster 455 * @param listener event listener 456 * @throws NacosException nacos exception 457 */ 458 void subscribe(String serviceName, List<String> clusters, EventListener listener) throws NacosException; 459 460 /** 461 * Subscribe service to receive events of instances alteration. 462 * 463 * @param serviceName name of service 464 * @param groupName group of service 465 * @param clusters list of cluster 466 * @param listener event listener 467 * @throws NacosException nacos exception 468 */ 469 void subscribe(String serviceName, String groupName, List<String> clusters, EventListener listener) 470 throws NacosException; 471 472 /** 473 * Unsubscribe event listener of service. 474 * 475 * @param serviceName name of service 476 * @param listener event listener 477 * @throws NacosException nacos exception 478 */ 479 void unsubscribe(String serviceName, EventListener listener) throws NacosException; 480 481 /** 482 * unsubscribe event listener of service. 483 * 484 * @param serviceName name of service 485 * @param groupName group of service 486 * @param listener event listener 487 * @throws NacosException nacos exception 488 */ 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 service 495 * @param clusters list of cluster 496 * @param listener event listener 497 * @throws NacosException nacos exception 498 */ 499 void unsubscribe(String serviceName, List<String> clusters, EventListener listener) throws NacosException; 500 501 /** 502 * Unsubscribe event listener of service. 503 * 504 * @param serviceName name of service 505 * @param groupName group of service 506 * @param clusters list of cluster 507 * @param listener event listener 508 * @throws NacosException nacos exception 509 */ 510 void unsubscribe(String serviceName, String groupName, List<String> clusters, EventListener listener) 511 throws NacosException; 512 513 /** 514 * Get all service names from server. 515 * 516 * @param pageNo page index 517 * @param pageSize page size 518 * @return list of service names 519 * @throws NacosException nacos exception 520 */ 521 ListView<String> getServicesOfServer(int pageNo, int pageSize) throws NacosException; 522 523 /** 524 * Get all service names from server. 525 * 526 * @param pageNo page index 527 * @param pageSize page size 528 * @param groupName group name 529 * @return list of service names 530 * @throws NacosException nacos exception 531 */ 532 ListView<String> 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 index 538 * @param pageSize page size 539 * @param selector selector to filter the resource 540 * @return list of service names 541 * @throws NacosException nacos exception 542 * @since 0.7.0 543 */ 544 ListView<String> 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 index 550 * @param pageSize page size 551 * @param groupName group name 552 * @param selector selector to filter the resource 553 * @return list of service names 554 * @throws NacosException nacos exception 555 */ 556 ListView<String> 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 services 563 * @throws NacosException nacos exception 564 */ 565 List<ServiceInfo> getSubscribeServices() throws NacosException; 566 567 /** 568 * get server health status. 569 * 570 * @return is server healthy 571 */ 572 String getServerStatus(); 573 574 /** 575 * Shutdown the resource service. 576 * 577 * @throws NacosException exception. 578 */ 579 void shutDown() throws NacosException; 580 }NamingService 源码 标签:service,SpringCloud,Nacos,param,NacosException,instance,serviceName,笔记,throws From: https://www.cnblogs.com/PxgComeOn/p/16981600.html