我正在重写一个Spring MVC系统 .
系统就像这样简单: [Gateway<->Backend Services<->Databases] ,其中Gateway是一个控制器,仅用于身份验证并将请求转发给后端服务 .
后端服务将重构为微服务 . 我将使用Eureka服务为每个人进行注册 . 所以最终架构将是: [Gateway <-> Eureka <-> Backend micro-services <-> Databases] . Gateway将从Eureka服务器查找注册表并调用微服务 .
但是,Gateway不是Spring启动应用程序(并且不会被重写为Spring启动),因此我不相信Spring的eureka功能(@EnableEurekaClient,DiscoveryClient等)可以像示例那样轻松采用 . 实际上我尝试将eureka客户端注释添加到Gateway的控制器,这导致我的应用程序崩溃 .
此外,网关中需要 Ribbon 用于客户端负载 balancer . 但同样的担忧如上 .
//Update on 1st Nov. 我已经设置了一个Eureka服务器和一个客户端,两者都是用spring boot编写的 . 我正在使用这两个应用程序进行Spring MVC测试 . 服务器和客户端运行良好 .
Server's configuration application.properties 如下
server.port=8761
spring.application.name=service-itself
eureka.client.service-url.default-zone=http://localhost:8761/eureka/
eureka.client.register-with-eureka=true
eureka.client.fetch-registry=false
logging.level.com.netflix.eureka=OFF
logging.level.com.netflix.discovery=OFF
客户端如下:application.yml
spring:
application:
name: say-hello
server:
port: 8090
eureka:
client:
service-url:
defaultZone: http://${eureka.host:localhost}:${eureka.port:8761}/eureka/
访问Eureka仪表板localhost:8761时,我可以看到该客户端已注册 .
对于我的Spring MVC网关,因为它不是Spring引导项目,所以我将example复制到我的项目只是为了测试它是否可以连接到Eureka服务器并检索已注册的"say-hello"客户端实例 . 不幸的是,它不能这样说"Cannot get an instance of example service to talk to from eureka",它是在示例类的第76行打印的 .
以下是放置在网关类路径中的eureka-client.properties . 我可以确认Client类正在读取配置文件 .
eureka.name=gatewayEurekaClient
eureka.vipAddress=say-hello
eureka.port=8761
eureka.preferSameZone=true
eureka.preferSameZone=true
eureka.shouldUseDns=false
eureka.serviceUrl.default=http://localhost:8761/eureka
eureka.serviceUrl.defaultZone=http://localhost:8761/eureka
而且,这是 new DiscoveryClient(applicationInfoManager, clientConfig); 执行时的调试信息
instanceInfo InstanceInfo (id=234)
actionType null
appGroupName "UNKNOWN" (id=246)
appName "GATEWAYEUREKACLIENT" (id=247)
asgName null
countryId 1
dataCenterInfo PropertiesInstanceConfig$1 (id=248)
healthCheckExplicitUrl null
healthCheckRelativeUrl "/healthcheck" (id=253)
healthCheckSecureExplicitUrl null
healthCheckUrl "http://A156N7AB89AXNZQ:8761/healthcheck" (id=254)
homePageUrl "http://A156N7AB89AXNZQ:8761/" (id=255)
hostName "A156N7AB89AXNZQ" (id=256)
instanceId "A156N7AB89AXNZQ" (id=256)
ipAddr "10.209.66.64" (id=257)
isCoordinatingDiscoveryServer Boolean (id=258)
isInstanceInfoDirty false
isSecurePortEnabled false
isUnsecurePortEnabled true
lastDirtyTimestamp Long (id=260)
lastUpdatedTimestamp Long (id=263)
leaseInfo LeaseInfo (id=264)
metadata ConcurrentHashMap<K,V> (id=266)
overriddenstatus InstanceInfo$InstanceStatus (id=267)
port 8761
secureHealthCheckUrl null
securePort 443
secureVipAddress null
secureVipAddressUnresolved null
sid "na" (id=270)
status InstanceInfo$InstanceStatus (id=271)
statusPageExplicitUrl null
statusPageRelativeUrl "/Status" (id=272)
statusPageUrl "http://A156N7AB89AXNZQ:8761/Status" (id=273)
version "unknown" (id=274)
vipAddress "say-hello" (id=275)
vipAddressUnresolved "say-hello" (id=275)
我的想法已经不多了 . 任何人都可以帮忙解决这个问题吗?
2 回答
功能区(https://github.com/Netflix/ribbon)和Eureka(https://github.com/Netflix/eureka)可以在没有Spring的情况下工作(这是它们首先开发的方式),但您需要花费更多精力来配置满足您需求的所有内容 .
在Spring中支持Ribbon和Eureka是Spring Cloud项目(和mvn组ID)的一部分,而不是Spring Boot . 我不认为启动是强制性的 . Ribbon和Eureka本身由Netflix提供 .
对于功能区,无论如何都需要定义自己的@LoadBalanced RestTemplate @Bean . 只要依赖项具有spring cloud eureka并且您的类是@Configuration类,@ EnableDiscoveryClient就应该工作 .
简短的回答是 - 为什么不尝试快速测试? :) .