Eureka Server建立好了,接下来,通过Eureka Client注册服务。
在 https://start.spring.io/ 建立项目——web-first,依赖包:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
上面建立了一个 Web服务。
导入Eclipse,启动(启动前,,Eureka Server已经启动)。部分日志如下:
iguration$LoadBalancerCaffeineWarnLogger : Spring Cloud LoadBalancer is currently working with the default cache. You can switch to using
Caffeine cache, by adding it and org.springframework.cache.caffeine.CaffeineCacheManager to the classpath.
com.netflix.discovery.DiscoveryClient : Initializing Eureka in region us-east-1
c.n.d.s.r.aws.ConfigClusterResolver : Resolving eureka endpoints via configuration
com.netflix.discovery.DiscoveryClient : Discovery Client initialized at timestamp 1626934699656 with initial instances count: 1
o.s.c.n.e.s.EurekaServiceRegistry : Registering application UNKNOWN with eureka with status UP
com.netflix.discovery.DiscoveryClient : Saw local status change event StatusChangeEvent [timestamp=1626934699658, current=UP, previous=STARTING]
com.netflix.discovery.DiscoveryClient : DiscoveryClient_UNKNOWN/DESKTOP-BDNTQQ3: registering service...
com.netflix.discovery.DiscoveryClient : DiscoveryClient_UNKNOWN/DESKTOP-BDNTQQ3 - registration status: 204
访问Eureka Server的 http://localhost:8761/ :
注册成功,但是,Application 为 UNKNOWN。在Status中,超链接的名字为 电脑的 计算机名,地址为 http://desktop-bdntqq3:8080/actuator/info ,但是,访问失败——需要配置hosts才可以,
其中的 /actuator/info 像是 actuator的端点,但是,项目没有引入 actuator,因此无法访问。
1)没有任何配置,怎么就注册成功了呢?一定是 依赖包spring-cloud-starter-netflix-eureka-client 自动做了一些事情;
2)注册中心,Status下的超链接怎么不能访问,要怎么配置?
解决Application为UNKNOWN的问题
在web-first项目中添加配置:
spring.application.name=web-first
再次启动,部分日志:
com.netflix.discovery.DiscoveryClient : DiscoveryClient_WEB-FIRST/DESKTOP-BDNTQQ3:web-first: registering service...
com.netflix.discovery.DiscoveryClient : DiscoveryClient_WEB-FIRST/DESKTOP-BDNTQQ3:web-first - registration status: 204
注册中心显示:
解决注册中心下Status超链接地址为主机名称的问题
在web-first项目中添加下面的配置:
eureka.instance.prefer-ip-address=true
再次启动项目,此时,注册中心下Status的超链接现实为IP地址:
http://192.168.125.197:8080/actuator/info
但这个接口 还是无法访问。
更改注册中心的端口
更改Eureka Server注册服务地址为 8769,再次启动 服务器、客户端两个服务,此时,客户端 web-first项目 无法注册成功,启动时报错:
c.n.d.s.t.d.RedirectingEurekaHttpClient : Request execution error. endpoint=DefaultEndpoint{ serviceUrl='http://localhost:8761/eureka/},
exception=I/O error on GET request for "http://localhost:8761/eureka/apps/": Connect to localhost:8761 [localhost/127.0.0.1,
localhost/0:0:0:0:0:0:0:1] failed: Connection refused: connect; nested exception is org.apache.http.conn.HttpHostConnectException:
Connect to localhost:8761 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused: connect
stacktrace=org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://localhost:8761/eureka/apps/":
Connect to localhost:8761 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused: connect; nested exception is
org.apache.http.conn.HttpHostConnectException: Connect to localhost:8761 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1]
failed: Connection refused: connect
web-first项目 启动了,但是,注册失败。启动后,仍然会 每隔30秒 执行一次注册:
[nfoReplicator-0] c.n.d.s.t.d.RedirectingEurekaHttpClient : Request execution error. endpoint=DefaultEndpoint{ serviceUrl='http://localhost:8761/eureka/},
exception=I/O error on POST request for "http://localhost:8761/eureka/apps/WEB-FIRST": Connect to localhost:8761 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1]
failed: Connection refused: connect; nested exception is org.apache.http.conn.HttpHostConnectException: Connect to localhost:8761
[localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused: connect stacktrace=org.springframework.web.client.ResourceAccessException:
I/O error on POST request for "http://localhost:8761/eureka/apps/WEB-FIRST": Connect to localhost:8761 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed:
Connection refused: connect; nested exception is org.apache.http.conn.HttpHostConnectException: Connect to localhost:8761 [localhost/127.0.0.1,
localhost/0:0:0:0:0:0:0:1] failed: Connection refused: connect
[nfoReplicator-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_WEB-FIRST/DESKTOP-BDNTQQ3:web-first - registration failed Cannot execute
request on any known server
[freshExecutor-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_WEB-FIRST/DESKTOP-BDNTQQ3:web-first - was unable to refresh its cache!
This periodic background refresh will be retried in 30 seconds. status = Cannot execute request on any known server stacktrace =
com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server
启动期间的线程时 main,项目启动后,使用异步线程执行注册了。
解决这个问题的方法是,添加下面的配置:
eureka.client.service-url.defaultZone=http://localhost:8769/eureka/
再次启动web-first项目,没有报错,注册成功。
注:在注册中心中,有一个 DS Replicas 项目,其下为 localhost 超链接,其仍然指向 8761端口:http://localhost:8761/eureka/
页面上还有其它现实 8761端口的地方。
因此,此时需要修改Eureka Server的配置,同 web-first的 新增配置:
eureka.client.service-url.defaultZone=http://localhost:8769/eureka/
再次启动 Eureka Server,服务启动成功:,上面的 DS Replicas 下 超链接正常了:
http://localhost:8769/eureka/
特别提醒:
配置中的 defaultZone 必须是 驼峰格式,不能写成 default-zone,否则,无效。
因为,eureka.client.service-url 是一个 Map类型。
更多细节还需 深挖。
服务器的 /eureka 端点 不能访问
虽然配置了 /eureka 端点,但是,其不能访问。
可以通过 /eureka/apps 来访问 注册到注册中心的 应用:
/eureka/apps/ + 服务名称 还可以访问 服务的信息:
更进一步:
/eureka/apps/ + 服务名称 + / + instance名称 还可以访问实例详情,略。
启动两个同名的服务
上面注册 WEB-FIRST 服务的一个实例,接下来使用 端口9090 启动一个 web-first服务。
-Dserver.port=9090
启动后的注册中心页面:
注:同一台主机,端口需要不同;如果是不同主机运行,则无妨。一般来说,同一个 application 的多个实例 是 运行在不同主机上。
更多配置,TODO