1、概述
大家好,我是欧阳方超。
今天实践加网上搜索发现一个现象,我们都知道@EnableDiscoveryClient注解是把本服务的信息暴露给注册中心,也就是让自己被发现,但是现在新版本的SpringCloud中不使用该注解也能将服务注册到注册中心。
2、事情经过
在一个SpringCloud项目中,父pom的dependencyManagement标签中有如下内容,
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba-version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud-version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
在子模块的pom中也添加了spring-cloud-starter-alibaba-nacos-discovery,如下,此时并没有在该子模块的main方法所在的类上添加@EnableDiscoveryClient注解,因为忘加了,把该模块启动后,发现该服务是可以出现在nacos中的
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
搜了一下,发现官网也有相关说明了,可以添加DiscoveryClient的实现到类路径中,这种SpringBoot应用就可以注册到服务发现服务器中。
当然配置文件中还是需要配置nacos地址的,
spring:
application:
name: saveservice
cloud:
nacos:
discovery:
service-addr: localhost:8848
如何禁止服务注册到注册中心
可以在配置文件中通过spring.cloud.discovery.enabled=false实现禁止服务被发现的功能,该配置项的值默认为true,也就是默认是会注册的,下面是禁用此功能的相关配置项:
spring:
application:
name: saveservice
cloud:
nacos:
discovery:
service-addr: localhost:8848
discovery:
enabled: false
3、总结
时代在变化,版本在更新,知识该更新的也要及时更新。
我是欧阳方超,把事情做好了自然就有兴趣了,如果你喜欢我的文章,欢迎点赞、转发、评论加关注。