我们都知道在启动类上添加注解@EnableDiscoveryClient 或@EnableEurekaClient并且加上相关依赖,并进行相应配置,即可将微服务注册到服务发现组件上。
相同点:都是能够让注册中心能够发现,扫描到该服务
不同点:@EnableEurekaClient只适用于Eureka作为注册中心@EnableDiscoveryClient 可以是其他注册中心
示例:
(1)添加添加Eureka Client依赖
<!-- eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
(2)写启动类
package com.leyou;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class LySearchApplication {
public static void main(String[] args) {
SpringApplication.run(LySearchApplication.class,args);
}
}
(3)写配置文件
server:
port: 8083
spring:
application:
name: search-service
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:10086/eureka
registry-fetch-interval-seconds: 5
instance:
prefer-ip-address: true #当你获取host时,返回的不是主机名,而是ip
ip-address: 127.0.0.1
lease-expiration-duration-in-seconds: 10 #10秒不发送就过期
lease-renewal-interval-in-seconds: 5 #每隔5秒发一次心跳