Open woshikid opened 2 years ago
POM
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency>
自定义配置
spring.application.name: Eureka server.port: 8761 eureka: #server: #enable-self-preservation: false # 自我保护(发生分区时防止清理) #eviction-interval-timer-in-ms: 60000 # 清理线程运行间隔(默认值) instance: hostname: localhost # 默认自动获取 client: register-with-eureka: false # 单机模式 fetch-registry: false # 单机模式 service-url: "defaultZone": "http://${eureka.instance.hostname}:${server.port}/eureka/" #"defaultZone": "http://server1/eureka/,http://server2/eureka/" # 集群模式
使用Eureka Server
@EnableEurekaServer
<!-- 默认已包含Spring Cloud LoadBalancer --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
spring.application.name: test eureka: instance: hostname: localhost # 默认自动获取 #prefer-ip-address: true # 使用IP而不是hostname #instance-id: ${spring.cloud.client.ip-address}:${server.port:8080} #lease-renewal-interval-in-seconds: 30 # 心跳发送间隔(默认值) #lease-expiration-duration-in-seconds: 90 # 丢失心跳多久后本服务可被清理(默认值) #secure-port-enabled: true # 注册为https服务 client: #healthcheck.enabled: true # 使用Actuator的health检查(默认值) service-url: "defaultZone": "http://localhost:8761/eureka/" #"defaultZone": "http://server1/eureka/,http://server2/eureka/" # 集群模式 #registry-fetch-interval-seconds: 30 # 获取服务信息间隔(默认值) #initial-instance-info-replication-interval-seconds: 40 # 首次发送服务信息延时(默认值) #instance-info-replication-interval-seconds: 30 # 发送服务信息间隔(默认值)
使用EurekaClient
@Autowired private EurekaClient eurekaClient; List<InstanceInfo> instanceInfos = eurekaClient.getApplication(springApplicationName).getInstances(); InstanceInfo instanceInfo = eurekaClient.getNextServerFromEureka(springApplicationName, false); String url = instanceInfo.getHomePageUrl();
使用DiscoveryClient
@Autowired private DiscoveryClient discoveryClient; List<ServiceInstance> serviceInstances = discoveryClient.getInstances(springApplicationName); String url = serviceInstances.get(0).getUri().toString(); //InstanceInfo instanceInfo = ((EurekaServiceInstance)serviceInstances.get(0)).getInstanceInfo();
服务端
POM
自定义配置
使用Eureka Server
客户端
POM
自定义配置
使用EurekaClient
使用DiscoveryClient