本文介绍了一些关于SpringCloud Eureka的基本概念与应用。

Eureka角色

服务端:EurekaServer,服务端,注册中心。负责心跳监控,记录服务信息

客户端:EurekaClient,分为消费者和提供者。

Eureka的作用

eureka服务类似注册中心:

  1. 服务消费者和提供者将服务信息注册到注册中心
  2. 消费者向注册中心拉取提供者的新消息
  3. 通过负载均衡进行选择
  4. 实现远程调用

注册中心使用心跳续约,随时监控服务状态

Eureka搭建

搭建服务步骤如下:

  1. 创建模块,引入spring-cloud-starter-netflix-eureka-server依赖
<!-- Eureka服务端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>

如果父工程中已经选好了springcloud的版本以及相关依赖,这里不需要填写版本号

  1. 编写启动类,并添加@EnableEurekaServer注解
  2. 编写yml配置文件,服务端口、服务名称、地址信息(用于eureka集群通信)
# Tomcat
server:
port: 9099

# Spring
spring:
application:
# 应用名称
name: hms_eureka
eureka:
client:
sevice-url:
defaultZone: http://localhost:${server.port}/eureka
# 这里选择不将自己注册
register-with-eureka: false
fetch-registry: false

服务注册

在依赖中引入

<!-- Eureka客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

同时在application.yml中配置eureka相关信息

eureka:
client:
service-url:
defaultZone: http://localhost:9099/eureka

注:启动类上要加上@EnableEurekaServer注解

一直遇到一个问题,日志内容如下:

2023-08-30 16:57:34.306  INFO 1076 --- [           main] com.magus.UserServiceApplication         : No active profile set, falling back to default profiles: default
2023-08-30 16:57:34.580 INFO 1076 --- [ main] o.s.cloud.context.scope.GenericScope : BeanFactory id=24ae86d6-4a71-3421-838c-868a60323ef7
2023-08-30 16:57:34.621 INFO 1076 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$9adaa148] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2023-08-30 16:57:34.632 INFO 1076 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$b6f4a445] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2023-08-30 16:57:34.644 WARN 1076 --- [ main] c.n.c.sources.URLConfigurationSource : No URLs will be polled as dynamic configuration sources.
2023-08-30 16:57:34.644 INFO 1076 --- [ main] c.n.c.sources.URLConfigurationSource : To enable URLs as dynamic configuration sources, define System property archaius.configurationSource.additionalUrls or make config.properties available on classpath.
2023-08-30 16:57:34.649 WARN 1076 --- [ main] c.n.c.sources.URLConfigurationSource : No URLs will be polled as dynamic configuration sources.
2023-08-30 16:57:34.649 INFO 1076 --- [ main] c.n.c.sources.URLConfigurationSource : To enable URLs as dynamic configuration sources, define System property archaius.configurationSource.additionalUrls or make config.properties available on classpath.
2023-08-30 16:57:36.516 INFO 1076 --- [ main] o.s.cloud.commons.util.InetUtils : Cannot determine local hostname
2023-08-30 16:57:38.145 INFO 1076 --- [ main] o.s.cloud.commons.util.InetUtils : Cannot determine local hostname
2023-08-30 16:57:38.203 INFO 1076 --- [ main] o.s.c.n.eureka.InstanceInfoFactory : Setting initial instance status as: STARTING
2023-08-30 16:57:38.224 INFO 1076 --- [ main] com.netflix.discovery.DiscoveryClient : Initializing Eureka in region us-east-1
2023-08-30 16:57:38.855 INFO 1076 --- [ main] c.n.d.provider.DiscoveryJerseyProvider : Using JSON encoding codec LegacyJacksonJson
2023-08-30 16:57:38.855 INFO 1076 --- [ main] c.n.d.provider.DiscoveryJerseyProvider : Using JSON decoding codec LegacyJacksonJson
2023-08-30 16:57:38.928 INFO 1076 --- [ main] c.n.d.provider.DiscoveryJerseyProvider : Using XML encoding codec XStreamXml
2023-08-30 16:57:38.929 INFO 1076 --- [ main] c.n.d.provider.DiscoveryJerseyProvider : Using XML decoding codec XStreamXml
2023-08-30 16:57:39.042 INFO 1076 --- [ main] c.n.d.s.r.aws.ConfigClusterResolver : Resolving eureka endpoints via configuration
2023-08-30 16:57:39.460 INFO 1076 --- [ main] com.netflix.discovery.DiscoveryClient : Disable delta property : false
2023-08-30 16:57:39.461 INFO 1076 --- [ main] com.netflix.discovery.DiscoveryClient : Single vip registry refresh property : null
2023-08-30 16:57:39.461 INFO 1076 --- [ main] com.netflix.discovery.DiscoveryClient : Force full registry fetch : false
2023-08-30 16:57:39.461 INFO 1076 --- [ main] com.netflix.discovery.DiscoveryClient : Application is null : false
2023-08-30 16:57:39.461 INFO 1076 --- [ main] com.netflix.discovery.DiscoveryClient : Registered Applications size is zero : true
2023-08-30 16:57:39.461 INFO 1076 --- [ main] com.netflix.discovery.DiscoveryClient : Application version is -1: true
2023-08-30 16:57:39.461 INFO 1076 --- [ main] com.netflix.discovery.DiscoveryClient : Getting all instance registry info from the eureka server
2023-08-30 16:57:39.535 INFO 1076 --- [ main] com.netflix.discovery.DiscoveryClient : The response status is 200
2023-08-30 16:57:39.536 INFO 1076 --- [ main] com.netflix.discovery.DiscoveryClient : Starting heartbeat executor: renew interval is: 30
2023-08-30 16:57:39.538 INFO 1076 --- [ main] c.n.discovery.InstanceInfoReplicator : InstanceInfoReplicator onDemand update allowed rate per min is 4
2023-08-30 16:57:39.541 INFO 1076 --- [ main] com.netflix.discovery.DiscoveryClient : Discovery Client initialized at timestamp 1693385859540 with initial instances count: 0
2023-08-30 16:57:39.542 INFO 1076 --- [ main] o.s.c.n.e.s.EurekaServiceRegistry : Registering application USER-SERVICE with eureka with status UP
2023-08-30 16:57:39.543 INFO 1076 --- [ main] com.netflix.discovery.DiscoveryClient : Saw local status change event StatusChangeEvent [timestamp=1693385859543, current=UP, previous=STARTING]
2023-08-30 16:57:39.544 INFO 1076 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_USER-SERVICE/localhost:user-service:9999: registering service...
2023-08-30 16:57:39.554 INFO 1076 --- [ main] com.magus.UserServiceApplication : Started UserServiceApplication in 9.058 seconds (JVM running for 10.165)
2023-08-30 16:57:39.556 INFO 1076 --- [ Thread-5] o.s.c.n.e.s.EurekaServiceRegistry : Unregistering application USER-SERVICE with eureka with status DOWN
2023-08-30 16:57:39.557 WARN 1076 --- [ Thread-5] com.netflix.discovery.DiscoveryClient : Saw local status change event StatusChangeEvent [timestamp=1693385859557, current=DOWN, previous=UP]
2023-08-30 16:57:39.561 INFO 1076 --- [ Thread-5] com.netflix.discovery.DiscoveryClient : Shutting down DiscoveryClient ...
2023-08-30 16:57:39.602 INFO 1076 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_USER-SERVICE/localhost:user-service:9999 - registration status: 204
2023-08-30 16:57:39.603 INFO 1076 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_USER-SERVICE/localhost:user-service:9999: registering service...
2023-08-30 16:57:39.614 INFO 1076 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_USER-SERVICE/localhost:user-service:9999 - registration status: 204
2023-08-30 16:57:39.615 INFO 1076 --- [ Thread-5] com.netflix.discovery.DiscoveryClient : Unregistering ...
2023-08-30 16:57:39.630 INFO 1076 --- [ Thread-5] com.netflix.discovery.DiscoveryClient : DiscoveryClient_USER-SERVICE/localhost:user-service:9999 - deregister status: 200
2023-08-30 16:57:39.637 INFO 1076 --- [ Thread-5] com.netflix.discovery.DiscoveryClient : Completed shut down of DiscoveryClient

原因: Eureka客户端(服务提供者/消费者)没有成功引入web的starter(头疼了半小时)。

注册多个实例

可以对服务的配置进行复制然后在VM设置中添加参数-Dserver.port=xxxx

服务拉取

  1. 同注册,引入依赖,配置eureka地址。
  2. 给ResTemplate添加@LoadBanlanced注解(负载均衡)
  3. 用服务提供者的注册服务名称代替url中的一部分进行远程调用,例如 http://userservice/user/1 (restful)

总结

Eureka起到为服务之间提供远程调用的作用,来实现将服务分离又能交互