springboot 集成 eureka

按之前的方法将 Eureka集群 搭建好后,我们需要在业务代码工程中进行集成,总的来说在springboot 中集成 eureka 可以说非常简单,依然是三部曲:

  1. pom 依赖的添加
  2. eureka 客户端注解的开启
  3. 配置文件配置 eureka-server 的地址及相关参数

一、pom 添加依赖
本文示例使用 springboot 2.1.18 RELEASE 版本,因为版本不同配置参数可能会不一样

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

二、开启 Eureka 客户端
在启动类上新增注解 @EnableFeignClients

@EnableEurekaClient
@SpringBootApplication
public class ShanhyService2Application {

	public static void main(String[] args) {
		SpringApplication.run(ShanhyService2Application.class, args);
	}

}

三、配置文件
配置文件主要内容参数如下示例:

# 服务端口号
server.port=8080
# Eureka Server 地址
eureka.client.serviceUrl.defaultZone=http://192.168.1.164:8761/eureka/,http://192.168.1.164:8762/eureka/,http://192.168.1.164:8763/eureka/
# eureka实例ID
eureka.instance.instance-id=${spring.application.name}:${spring.cloud.client.ip-address}:${spring.application.instance_id:${server.port}}
# 是否检索服务(获取eureka服务列表)
eureka.client.fetch-registry=true
#修改缓存清单的更新时间,默认值为30
eureka.client.registry-fetch-interval-seconds=30
# 是否向服务注册中心注册自己(如果仅作为调用者,不提供服务,可以为false)	
eureka.client.register-with-eureka=true
# 将IP注册到eureka中,如果为false默认注册主机名(这个是重点,不然你可能会遇到坑)
eureka.instance.prefer-ip-address=true

至此启动服务就可以在 eureka UI控制台上看到注册的服务信息了。


(END)

发布了378 篇原创文章 · 获赞 1419 · 访问量 632万+

猜你喜欢

转载自blog.csdn.net/catoop/article/details/101011182