使用自定义端口和地址配置 Eureka Server 和客户端
Configure Eureka Server and client with custom port and address
我在默认本地主机和端口 8761 上有一个 Eureka 服务器 运行,所以我尝试以这种方式更改此默认配置:
server:
port: 6000
servlet:
context-path: /myeureka
eureka:
client:
register-with-eureka: false
fetch-registry: false
但是这样我无法访问尤里卡仪表板,只能使用默认配置:
server:
port: 8761
eureka:
client:
register-with-eureka: false
fetch-registry: false
在我的客户端中发生了同样的事情,我无法指向另一个不同于默认的 eureka 服务器 (localhost:8761),查看我的配置:
server:
port: 7000
servlet:
context-path: /client-eureka
spring:
application:
name: client-eureka
eureka:
instance:
prefer-ip-address: true
client:
eureka-server-port: 6000
eureka-server-u-r-l-context: /myeureka
查看客户端日志我得到以下信息:
2018-09-01 09:19:37.175 INFO 4931 --- [ main] c.n.eureka.cluster.PeerEurekaNodes : Replica node URL: http://localhost:8761/eureka/
无论我在客户端配置什么端口或主机,总是尝试达到默认值。
重要:
我在这个版本中使用尤里卡:https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-eureka-server/2.0.1.RELEASE
我使用了和你一样的依赖版本,找不到配置路径server.servlet.contextpath
相反,您可以使用 server.servlet-path 或 server.context-path
对于每个服务器配置,您还需要更新客户端 application.yml 文件。请记住,/eureka 是用于向 Eureka 服务器注册 Eureka 客户端的默认 REST 端点
情况一:使用server.servlet-path
尤里卡服务器:
server:
port: 7000
servlet-path: /myeureka
eureka:
client:
register-with-eureka: false
fetch-registry: false
尤里卡客户端:
spring:
application:
name: spring-cloud-eureka-client
server:
port: 0
eureka:
client:
service-url:
defaultZone: ${EUREKA_URI:http://localhost:7000/eureka}
instance:
preferIpAddress: true
情况二:使用server.context-路径
尤里卡服务器:
server:
port: 7000
context-path: /myeureka
eureka:
client:
register-with-eureka: false
fetch-registry: false
尤里卡客户端:
spring:
application:
name: spring-cloud-eureka-client
server:
port: 0
eureka:
client:
service-url:
defaultZone: ${EUREKA_URI:http://localhost:7000/myeureka/eureka}
instance:
preferIpAddress: true
更新后的答案:
因为 server.servlet-path 和 server.context-path 已弃用,eureka server 将配置如下:
server:
port: 7000
servlet:
context-path: /myeureka
eureka:
client:
register-with-eureka: false
fetch-registry: false
Eureka 客户端 application.yml 将保留在案例 2 中。
我在默认本地主机和端口 8761 上有一个 Eureka 服务器 运行,所以我尝试以这种方式更改此默认配置:
server:
port: 6000
servlet:
context-path: /myeureka
eureka:
client:
register-with-eureka: false
fetch-registry: false
但是这样我无法访问尤里卡仪表板,只能使用默认配置:
server:
port: 8761
eureka:
client:
register-with-eureka: false
fetch-registry: false
在我的客户端中发生了同样的事情,我无法指向另一个不同于默认的 eureka 服务器 (localhost:8761),查看我的配置:
server:
port: 7000
servlet:
context-path: /client-eureka
spring:
application:
name: client-eureka
eureka:
instance:
prefer-ip-address: true
client:
eureka-server-port: 6000
eureka-server-u-r-l-context: /myeureka
查看客户端日志我得到以下信息:
2018-09-01 09:19:37.175 INFO 4931 --- [ main] c.n.eureka.cluster.PeerEurekaNodes : Replica node URL: http://localhost:8761/eureka/
无论我在客户端配置什么端口或主机,总是尝试达到默认值。
重要: 我在这个版本中使用尤里卡:https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-eureka-server/2.0.1.RELEASE
我使用了和你一样的依赖版本,找不到配置路径server.servlet.contextpath
相反,您可以使用 server.servlet-path 或 server.context-path
对于每个服务器配置,您还需要更新客户端 application.yml 文件。请记住,/eureka 是用于向 Eureka 服务器注册 Eureka 客户端的默认 REST 端点
情况一:使用server.servlet-path
尤里卡服务器:
server:
port: 7000
servlet-path: /myeureka
eureka:
client:
register-with-eureka: false
fetch-registry: false
尤里卡客户端:
spring:
application:
name: spring-cloud-eureka-client
server:
port: 0
eureka:
client:
service-url:
defaultZone: ${EUREKA_URI:http://localhost:7000/eureka}
instance:
preferIpAddress: true
情况二:使用server.context-路径
尤里卡服务器:
server:
port: 7000
context-path: /myeureka
eureka:
client:
register-with-eureka: false
fetch-registry: false
尤里卡客户端:
spring:
application:
name: spring-cloud-eureka-client
server:
port: 0
eureka:
client:
service-url:
defaultZone: ${EUREKA_URI:http://localhost:7000/myeureka/eureka}
instance:
preferIpAddress: true
更新后的答案: 因为 server.servlet-path 和 server.context-path 已弃用,eureka server 将配置如下:
server:
port: 7000
servlet:
context-path: /myeureka
eureka:
client:
register-with-eureka: false
fetch-registry: false
Eureka 客户端 application.yml 将保留在案例 2 中。